FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
JSAPIImpl.h
1 /**********************************************************\
2 Original Author: Richard Bateman (taxilian)
3 
4 Created: April 8, 2011
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 Richard Bateman, Firebreath development team
13 \**********************************************************/
14 
15 #pragma once
16 #ifndef H_FB_JSAPIImpl
17 #define H_FB_JSAPIImpl
18 
19 #include "APITypes.h"
20 #include <list>
21 #include <deque>
22 #include <boost/enable_shared_from_this.hpp>
23 #include <boost/noncopyable.hpp>
24 #include "JSExceptions.h"
25 #include "JSAPI.h"
26 #include "boost/thread/recursive_mutex.hpp"
27 #include "boost/thread/mutex.hpp"
28 
29 namespace FB
30 {
31  FB_FORWARD_PTR(JSAPIImpl);
32 
49  class JSAPIImpl : public JSAPI
50  {
51  public:
52 
58  JSAPIImpl(void);
59  JSAPIImpl( const SecurityZone& securityLevel );
60 
66  virtual ~JSAPIImpl(void);
67 
68  public:
69  JSAPIImplPtr shared_from_this() { return boost::static_pointer_cast<JSAPIImpl>(JSAPI::shared_from_this()); }
70  boost::shared_ptr<const JSAPIImpl> shared_from_this() const { return boost::static_pointer_cast<const JSAPIImpl>(JSAPI::shared_from_this()); }
71 
77  void invalidate();
83  virtual void shutdown() {};
84 
85  protected:
86  virtual void fireAsyncEvent( const std::string& eventName, const std::vector<variant>& args );
87 
88  protected:
92  virtual void FireEvent(const std::wstring& eventName, const std::vector<variant> &args)
93  {
94  FireEvent(wstring_to_utf8(eventName), args);
95  }
96 
129  virtual void FireEvent(const std::string& eventName, const std::vector<variant> &args);
130 
163  virtual void FireJSEvent(const std::string& eventName, const FB::VariantMap &members, const FB::VariantList &arguments);
165  virtual void FireJSEvent(const std::string& eventName, const FB::VariantMap &params)
166  {
167  FireJSEvent(eventName, params, FB::VariantList());
168  }
170  virtual void FireJSEvent(const std::string& eventName, const FB::VariantList &arguments)
171  {
172  FireJSEvent(eventName, FB::VariantMap(), arguments);
173  }
174 
175  public:
176  virtual void pushZone(const SecurityZone& securityLevel)
177  {
178  m_zoneMutex.lock();
179  m_zoneStack.push_back(securityLevel);
180  }
181 
182  virtual void popZone()
183  {
184  m_zoneStack.pop_back();
185  m_zoneMutex.unlock();
186  }
187 
200  virtual void setDefaultZone(const SecurityZone& securityLevel)
201  {
202  boost::recursive_mutex::scoped_lock lock(m_zoneMutex);
203  assert(!m_zoneStack.empty());
204  m_zoneStack.pop_front();
205  m_zoneStack.push_front(securityLevel);
206  }
207 
220  virtual SecurityZone getDefaultZone() const
221  {
222  boost::recursive_mutex::scoped_lock lock(m_zoneMutex);
223  assert(!m_zoneStack.empty());
224  return m_zoneStack.front();
225  }
226 
238  virtual SecurityZone getZone() const
239  {
240  assert(!m_zoneStack.empty());
241  boost::recursive_mutex::scoped_lock lock(m_zoneMutex);
242  return m_zoneStack.back();
243  }
244 
253  virtual void registerEvent(const std::string& name) {}
257  virtual void registerEvent(const std::wstring& name)
258  {
260  }
261 
262 
271  virtual void registerEventMethod(const std::string& name, JSObjectPtr& event);
275  virtual void registerEventMethod(const std::wstring& name, JSObjectPtr& event)
276  {
277  registerEventMethod(wstring_to_utf8(name), event);
278  }
279 
288  virtual void unregisterEventMethod(const std::string& name, JSObjectPtr& event);
292  virtual void unregisterEventMethod(const std::wstring& name, JSObjectPtr& event)
293  {
295  }
296 
306  virtual void registerEventInterface(const JSObjectPtr& event)
307  {
308  boost::recursive_mutex::scoped_lock _l(m_eventMutex);
309  m_evtIfaces[event->getEventContext()][static_cast<void*>(event.get())] = event;
310  }
318  virtual void unregisterEventInterface(const JSObjectPtr& event)
319  {
320  boost::recursive_mutex::scoped_lock _l(m_eventMutex);
321  EventIFaceMap::iterator fnd = m_evtIfaces[event->getEventContext()].find(static_cast<void*>(event.get()));
322  m_evtIfaces[event->getEventContext()].erase(fnd);
323  }
324 
325  public:
326  virtual void registerProxy(const JSAPIImplWeakPtr &ptr) const;
327  virtual void unregisterProxy( const FB::JSAPIImplPtr& ptr ) const;
328 
329  protected:
330  typedef std::deque<SecurityZone> ZoneStack;
331  typedef std::map<void*, EventMultiMap> EventContextMap;
332  // Stores event handlers
333  EventContextMap m_eventMap;
334  typedef std::map<void*, EventIFaceMap> EventIfaceContextMap;
335  // Stores event interfaces
336  EventIfaceContextMap m_evtIfaces;
337 
338  typedef std::vector<JSAPIImplWeakPtr> ProxyList;
339  mutable ProxyList m_proxies;
340 
341  mutable boost::recursive_mutex m_eventMutex;
342  mutable boost::recursive_mutex m_proxyMutex;
343  mutable boost::recursive_mutex m_zoneMutex;
344  ZoneStack m_zoneStack;
345 
346  bool m_valid; // Tracks if this object has been invalidated
347  };
348 };
349 
350 // There are important conversion routines that require JSObject and JSAPI to both be loaded
351 #include "JSObject.h"
352 #endif
353 
virtual void unregisterEventMethod(const std::string &name, JSObjectPtr &event)
Called by the browser to unregister an event handler method.
Definition: JSAPIImpl.cpp:266
JavaScript API base class implementation – provides basic functionality for C++ JSAPI objects...
Definition: JSAPIImpl.h:49
virtual void registerEvent(const std::wstring &name)
Definition: JSAPIImpl.h:257
JSAPIImpl(void)
Default constructor.
Definition: JSAPIImpl.cpp:26
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
void invalidate()
Invalidates this object.
Definition: JSAPIImpl.cpp:42
virtual void registerEventInterface(const JSObjectPtr &event)
Called by the browser to register a JSObject interface that handles events. This is primarily used by...
Definition: JSAPIImpl.h:306
virtual void registerEventMethod(const std::wstring &name, JSObjectPtr &event)
Definition: JSAPIImpl.h:275
virtual void FireJSEvent(const std::string &eventName, const FB::VariantMap &members, const FB::VariantList &arguments)
Fires an event into javascript asynchronously using a W3C-compliant event parameter.
Definition: JSAPIImpl.cpp:185
virtual void FireJSEvent(const std::string &eventName, const FB::VariantList &arguments)
Definition: JSAPIImpl.h:170
std::vector< variant > VariantList
Defines an alias representing list of variants.
Definition: APITypes.h:64
virtual void unregisterEventInterface(const JSObjectPtr &event)
Called by the browser to unregister a JSObject interface that handles events.
Definition: JSAPIImpl.h:318
virtual void registerEvent(const std::string &name)
Register event so that event listeners can be added/attached from javascript.
Definition: JSAPIImpl.h:253
virtual void shutdown()
Called to notify the object that the plugin is shutting down.
Definition: JSAPIImpl.h:83
int SecurityZone
Used to set a SecurityZone for a method or property – used by JSAPIAuto.
Definition: APITypes.h:275
virtual void FireJSEvent(const std::string &eventName, const FB::VariantMap &params)
Definition: JSAPIImpl.h:165
virtual void unregisterEventMethod(const std::wstring &name, JSObjectPtr &event)
Definition: JSAPIImpl.h:292
std::string wstring_to_utf8(const std::wstring &src)
Accepts a std::wstring and returns a UTF8-encoded std::string.
Definition: utf8_tools.cpp:37
virtual ~JSAPIImpl(void)
Finaliser.
Definition: JSAPIImpl.cpp:38
virtual void pushZone(const SecurityZone &securityLevel)
Pushes a new security level and locks a mutex (for every Push there must be a Pop!) ...
Definition: JSAPIImpl.h:176
virtual void popZone()
Pops off a security level and unlocks the mutex (for every Push there must be a Pop!) ...
Definition: JSAPIImpl.h:182
std::map< std::string, variant > VariantMap
Defines an alias representing a string -> variant map.
Definition: APITypes.h:72
virtual void FireEvent(const std::wstring &eventName, const std::vector< variant > &args)
Definition: JSAPIImpl.h:92
JavaScript API class – provides a javascript interface that can be exposed to the browser...
Definition: JSAPI.h:56
virtual SecurityZone getDefaultZone() const
Gets the default zone (the zone the class operates on before a push)
Definition: JSAPIImpl.h:220
virtual void setDefaultZone(const SecurityZone &securityLevel)
Sets the default zone (the zone the class operates on before a push)
Definition: JSAPIImpl.h:200
virtual void registerEventMethod(const std::string &name, JSObjectPtr &event)
Called by the browser to register an event handler method.
Definition: JSAPIImpl.cpp:249
virtual SecurityZone getZone() const
Gets the currently active zone.
Definition: JSAPIImpl.h:238