FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
PluginCore.cpp
1 /**********************************************************\
2 Original Author: Richard Bateman (taxilian)
3 
4 Created: Oct 19, 2009
5 License: Dual license model; choose one of two:
6  New BSD License
7  http://www.opensource.org/licenses/bsd-license.php
8  - or -
9  GNU Lesser General Public License, version 2.1
10  http://www.gnu.org/licenses/lgpl-2.1.html
11 
12 Copyright 2009 PacketPass, Inc and the Firebreath development team
13 \**********************************************************/
14 
15 #include "PluginWindow.h"
16 #include "JSAPI.h"
17 #include "variant_list.h"
18 #include "FactoryBase.h"
19 #include "BrowserHost.h"
20 #include "DOM/Window.h"
21 #include "logging.h"
22 #include "precompiled_headers.h" // On windows, everything above this line in PCH
23 
24 #include "PluginCore.h"
25 
26 using namespace FB;
27 
28 /***************************\
29  Static initialization stuff
30 \***************************/
31 
32 volatile int PluginCore::ActivePluginCount = 0;
33 
34 std::string PluginCore::OS;
35 std::string PluginCore::Browser;
36 void PluginCore::setPlatform(const std::string& opsys, const std::string& browser)
37 {
38  PluginCore::OS = opsys;
39  PluginCore::Browser = browser;
40  FBLOG_INFO("PluginCore", "os: " << opsys << "; browser: " << browser);
41 }
42 
43 /***************************\
44  Regular Class Stuff
45 \***************************/
46 
47 PluginCore::PluginCore() : m_paramsSet(false), m_Window(NULL),
48  m_windowLessParam(boost::indeterminate), m_scriptingOnly(false)
49 {
50  FB::Log::initLogging();
51  // This class is only created on the main UI thread,
52  // so there is no need for mutexes here
53  ++PluginCore::ActivePluginCount;
54 }
55 
57 {
58  // Tell the host that the plugin is shutting down
59  if(m_host) {
60  m_host->shutdown();
61  }
62 
63  // This class is only destroyed on the main UI thread,
64  // so there is no need for mutexes here
65  --PluginCore::ActivePluginCount;
66 }
67 
69 {
70  for (FB::VariantMap::const_iterator it = inParams.begin(); it != inParams.end(); ++it)
71  {
72  std::string key(it->first);
73  try {
74  std::string value(it->second.convert_cast<std::string>());
75  if (key.substr(0, 2) == "on") {
76  FB::JSObjectPtr tmp;
77  tmp = m_host->getDOMWindow()
78  ->getProperty<FB::JSObjectPtr>(value);
79 
80  FBLOG_TRACE("PluginCore", "Found <param> event handler: " << key);
81 
82  m_params[key] = tmp;
83  } else {
84  m_params[key] = it->second;
85  }
86  } catch (const std::exception &ex) {
87  FBLOG_WARN("PluginCore", "Exception processing <param> " << key << ": " << ex.what());
88  m_params[it->first] = it->second;
89  }
90  }
91 }
92 
93 boost::optional<std::string> PluginCore::getParam(const std::string& key) {
94  boost::optional<std::string> rval;
95  FB::VariantMap::const_iterator fnd = m_params.find(key.c_str());
96  if (fnd != m_params.end())
97  rval.reset(fnd->second.convert_cast<std::string>());
98  return rval;
99 }
100 
101 FB::variant FB::PluginCore::getParamVariant( const std::string& key )
102 {
103  FB::VariantMap::const_iterator fnd = m_params.find(key.c_str());
104  if (fnd != m_params.end())
105  return fnd->second;
106  return FB::variant();
107 }
108 
109 // If you override this, you probably want to call it again, since this is what calls back into the page
110 // to indicate that we're done.
112 {
113  bool rval = false;
114  FBLOG_TRACE("PluginCore", "Plugin Ready");
115  try {
116  FB::VariantMap::iterator fnd = m_params.find("onload");
117  if (fnd != m_params.end()) {
118  m_host->initJS(this);
119  FB::JSObjectPtr method = fnd->second.convert_cast<FB::JSObjectPtr>();
120  if (method) {
121  FBLOG_TRACE("PluginCore", "InvokeDelayed(onload)");
122  m_host->delayedInvoke(250, method, FB::variant_list_of(getRootJSAPI()));
123  rval = true;
124  }
125  }
126  } catch(...) {
127  // Usually this would be if it isn't a JSObjectPtr or the object can't be called
128  }
129  onPluginReady();
130  return rval;
131 }
132 
134 {
135  if (boost::indeterminate(m_windowLessParam)) {
136  // initialise m_windowLessParam (defaulting to not windowless)
137  m_windowLessParam = false;
138  FB::VariantMap::iterator itr = m_params.find("windowless");
139  if (itr != m_params.end()) {
140  try {
141  m_windowLessParam = itr->second.convert_cast<bool>();
142  } catch (const FB::bad_variant_cast& ex) {
143  FB_UNUSED_VARIABLE(ex);
144  }
145  }
146  }
147  return m_windowLessParam;
148 }
149 
151 {
152  FBLOG_TRACE("PluginCore", "Window Set");
153  if (m_Window && m_Window != win) {
154  ClearWindow();
155  }
156  m_Window = win;
157  win->AttachObserver(this);
158 }
159 
161 {
162  FBLOG_TRACE("PluginCore", "Window Cleared");
163  if (m_Window) {
164  m_Window->DetachObserver(this);
165  m_Window = NULL;
166  }
167 }
168 
170 {
171  return "";
172 }
virtual JSAPIPtr getRootJSAPI()
Gets the root JSAPI object. It is not recommended to call this from the constructor or before setHost...
Definition: PluginCore.h:263
boost::shared_ptr< FB::JSObject > JSObjectPtr
Defines an alias representing a JSObject shared_ptr (you should never use a JSObject* directly) ...
Definition: APITypes.h:109
virtual ~PluginCore()
Finaliser.
Definition: PluginCore.cpp:56
virtual std::string negotiateDrawingModel()
Called at plugin creation to query the plugin's preferred drawing model.
Definition: PluginCore.cpp:169
FB::detail::VariantListInserter variant_list_of(FB::variant v)
Allows convenient creation of an FB::VariantList.
Definition: variant_list.h:122
Accepts any datatype, used in all interactions with javascript. Provides tools for getting back out t...
Definition: variant.h:198
FB::VariantMap m_params
Definition: PluginCore.h:361
virtual void AttachObserver(PluginEventSink *sink)
Attach a PluginEventSink to receive events.
BrowserHostPtr m_host
The BrowserHost object for the current session; deprecated, use getHost()
Definition: PluginCore.h:354
Thrown when variant::cast<type> or variant::convert_cast<type> fails because the type of the value st...
Definition: variant.h:133
virtual void onPluginReady()
This is called when the plugin is ready.
Definition: PluginCore.h:210
virtual bool setReady()
Called by the browser to indicate that the basic initialization is complete and the plugin is now rea...
Definition: PluginCore.cpp:111
virtual bool isWindowless()
Called by the browser to query if this plugin should be windowless.
Definition: PluginCore.cpp:133
PluginCore()
Default Constructor.
Definition: PluginCore.cpp:47
virtual void SetWindow(PluginWindow *win)
Called by the browser to set the window. The default implementation fires the WindowAttached event in...
Definition: PluginCore.cpp:150
PluginWindow is the base class for all PluginWindow objects.
Definition: PluginWindow.h:33
static void setPlatform(const std::string &os, const std::string &browser)
Called by the browser when the Operating System and Browser are known.
Definition: PluginCore.cpp:36
virtual void ClearWindow()
Called by the browser to indicate that the window is no longer valid. The default implementation fire...
Definition: PluginCore.cpp:160
std::map< std::string, variant > VariantMap
Defines an alias representing a string -> variant map.
Definition: APITypes.h:72
virtual void setParams(const FB::VariantMap &inParams)
Called by the browser to store the values provided in param tags in m_params.
Definition: PluginCore.cpp:68