- Page restrictions apply
- Added by Automation System, last edited by Automation System on Oct 11, 2012 (view change)
JSAPIImpl.h
00001 /**********************************************************\ 00002 Original Author: Richard Bateman (taxilian) 00003 00004 Created: April 8, 2011 00005 License: Dual license model; choose one of two: 00006 New BSD License 00007 http://www.opensource.org/licenses/bsd-license.php 00008 - or - 00009 GNU Lesser General Public License, version 2.1 00010 http://www.gnu.org/licenses/lgpl-2.1.html 00011 00012 Copyright 2009 Richard Bateman, Firebreath development team 00013 \**********************************************************/ 00014 00015 #pragma once 00016 #ifndef H_FB_JSAPIImpl 00017 #define H_FB_JSAPIImpl 00018 00019 #include "APITypes.h" 00020 #include <list> 00021 #include <deque> 00022 #include <boost/enable_shared_from_this.hpp> 00023 #include <boost/noncopyable.hpp> 00024 #include "JSExceptions.h" 00025 #include "JSAPI.h" 00026 #include "boost/thread/recursive_mutex.hpp" 00027 #include "boost/thread/mutex.hpp" 00028 00029 namespace FB 00030 { 00031 FB_FORWARD_PTR(JSAPIImpl); 00032 00049 class JSAPIImpl : public JSAPI 00050 { 00051 public: 00052 00058 JSAPIImpl(void); 00059 JSAPIImpl( const SecurityZone& securityLevel ); 00060 00066 virtual ~JSAPIImpl(void); 00067 00068 public: 00069 JSAPIImplPtr shared_from_this() { return boost::static_pointer_cast<JSAPIImpl>(JSAPI::shared_from_this()); } 00070 boost::shared_ptr<const JSAPIImpl> shared_from_this() const { return boost::static_pointer_cast<const JSAPIImpl>(JSAPI::shared_from_this()); } 00071 00077 void invalidate(); 00083 virtual void shutdown() {}; 00084 00085 protected: 00086 virtual void fireAsyncEvent( const std::string& eventName, const std::vector<variant>& args ); 00087 00088 protected: 00092 virtual void FireEvent(const std::wstring& eventName, const std::vector<variant> &args) 00093 { 00094 FireEvent(wstring_to_utf8(eventName), args); 00095 } 00096 00129 virtual void FireEvent(const std::string& eventName, const std::vector<variant> &args); 00130 00163 virtual void FireJSEvent(const std::string& eventName, const FB::VariantMap &members, const FB::VariantList &arguments); 00165 virtual void FireJSEvent(const std::string& eventName, const FB::VariantMap ¶ms) 00166 { 00167 FireJSEvent(eventName, params, FB::VariantList()); 00168 } 00170 virtual void FireJSEvent(const std::string& eventName, const FB::VariantList &arguments) 00171 { 00172 FireJSEvent(eventName, FB::VariantMap(), arguments); 00173 } 00174 00175 public: 00176 virtual void pushZone(const SecurityZone& securityLevel) 00177 { 00178 m_zoneMutex.lock(); 00179 m_zoneStack.push_back(securityLevel); 00180 } 00181 00182 virtual void popZone() 00183 { 00184 m_zoneStack.pop_back(); 00185 m_zoneMutex.unlock(); 00186 } 00187 00200 virtual void setDefaultZone(const SecurityZone& securityLevel) 00201 { 00202 boost::recursive_mutex::scoped_lock lock(m_zoneMutex); 00203 assert(!m_zoneStack.empty()); 00204 m_zoneStack.pop_front(); 00205 m_zoneStack.push_front(securityLevel); 00206 } 00207 00220 virtual SecurityZone getDefaultZone() const 00221 { 00222 boost::recursive_mutex::scoped_lock lock(m_zoneMutex); 00223 assert(!m_zoneStack.empty()); 00224 return m_zoneStack.front(); 00225 } 00226 00238 virtual SecurityZone getZone() const 00239 { 00240 assert(!m_zoneStack.empty()); 00241 boost::recursive_mutex::scoped_lock lock(m_zoneMutex); 00242 return m_zoneStack.back(); 00243 } 00244 00253 virtual void registerEvent(const std::string& name) {} 00257 virtual void registerEvent(const std::wstring& name) 00258 { 00259 registerEvent(wstring_to_utf8(name)); 00260 } 00261 00262 00271 virtual void registerEventMethod(const std::string& name, JSObjectPtr& event); 00275 virtual void registerEventMethod(const std::wstring& name, JSObjectPtr& event) 00276 { 00277 registerEventMethod(wstring_to_utf8(name), event); 00278 } 00279 00288 virtual void unregisterEventMethod(const std::string& name, JSObjectPtr& event); 00292 virtual void unregisterEventMethod(const std::wstring& name, JSObjectPtr& event) 00293 { 00294 unregisterEventMethod(wstring_to_utf8(name), event); 00295 } 00296 00306 virtual void registerEventInterface(const JSObjectPtr& event) 00307 { 00308 boost::recursive_mutex::scoped_lock _l(m_eventMutex); 00309 m_evtIfaces[event->getEventContext()][static_cast<void*>(event.get())] = event; 00310 } 00318 virtual void unregisterEventInterface(const JSObjectPtr& event) 00319 { 00320 boost::recursive_mutex::scoped_lock _l(m_eventMutex); 00321 EventIFaceMap::iterator fnd = m_evtIfaces[event->getEventContext()].find(static_cast<void*>(event.get())); 00322 m_evtIfaces[event->getEventContext()].erase(fnd); 00323 } 00324 00325 public: 00326 virtual void registerProxy(const JSAPIImplWeakPtr &ptr) const; 00327 virtual void unregisterProxy( const FB::JSAPIImplPtr& ptr ) const; 00328 00329 protected: 00330 typedef std::deque<SecurityZone> ZoneStack; 00331 typedef std::map<void*, EventMultiMap> EventContextMap; 00332 // Stores event handlers 00333 EventContextMap m_eventMap; 00334 typedef std::map<void*, EventIFaceMap> EventIfaceContextMap; 00335 // Stores event interfaces 00336 EventIfaceContextMap m_evtIfaces; 00337 00338 typedef std::vector<JSAPIImplWeakPtr> ProxyList; 00339 mutable ProxyList m_proxies; 00340 00341 mutable boost::recursive_mutex m_eventMutex; 00342 mutable boost::recursive_mutex m_proxyMutex; 00343 mutable boost::recursive_mutex m_zoneMutex; 00344 ZoneStack m_zoneStack; 00345 00346 bool m_valid; // Tracks if this object has been invalidated 00347 }; 00348 }; 00349 00350 // There are important conversion routines that require JSObject and JSAPI to both be loaded 00351 #include "JSObject.h" 00352 #endif 00353
Generated on 25 May 2013 for FireBreath by
1.6.1
Labels
