FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
BrowserHost.h
1 /**********************************************************\
2 Original Author: Richard Bateman (taxilian)
3 
4 Created: Sept 24, 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 Richard Bateman, Firebreath development team
13 \**********************************************************/
14 
15 #pragma once
16 #ifndef H_FB_BROWSERHOSTWRAPPER
17 #define H_FB_BROWSERHOSTWRAPPER
18 
19 #include "APITypes.h"
20 #include <boost/enable_shared_from_this.hpp>
21 #include <boost/noncopyable.hpp>
22 #include <boost/thread.hpp>
23 #include <boost/thread/shared_mutex.hpp>
24 #include <boost/thread/recursive_mutex.hpp>
25 
26 namespace FB
27 {
28  FB_FORWARD_PTR(BrowserHost);
29  FB_FORWARD_PTR(BrowserStream);
30  FB_FORWARD_PTR(PluginEventSink);
31  FB_FORWARD_PTR(JSObject);
32  class BrowserStreamRequest;
33 
34  namespace DOM {
35  FB_FORWARD_PTR(Window);
36  FB_FORWARD_PTR(Document);
37  FB_FORWARD_PTR(Element);
38  FB_FORWARD_PTR(Node);
39  };
40 
47  {
48  AsyncLogRequest(const BrowserHostPtr& host, const std::string& message) : m_host(host), m_msg(message) { }
49 
50  const boost::shared_ptr<BrowserHost> m_host;
51  std::string m_msg;
52  };
53 
54  FB_FORWARD_PTR(AsyncCallManager);
55  FB_FORWARD_PTR(BrowserStreamManager);
56 
69  class BrowserHost : public boost::enable_shared_from_this<BrowserHost>, boost::noncopyable
70  {
71  public:
72 
78  BrowserHost();
79 
85  virtual ~BrowserHost();
86 
87  public:
112  bool ScheduleAsyncCall(void (*func)(void *), void *userData) const;
113 
138  template<class Functor>
139  typename Functor::result_type CallOnMainThread(Functor func) const;
140 
169  template<class C, class Functor>
170  void ScheduleOnMainThread(const boost::shared_ptr<C>& obj, Functor func) const;
171 
181  static void AsyncHtmlLog(void *data);
182 
192  virtual void *getContextID() const = 0;
193 
210  virtual BrowserStreamPtr createStream( const BrowserStreamRequest& req, const bool enable_async = true ) const;
211 
229  virtual BrowserStreamPtr createStream(const std::string& url, const PluginEventSinkPtr& callback,
230  bool cache = true, bool seekable = false,
231  size_t internalBufferSize = 128 * 1024 ) const;
232 
253  virtual BrowserStreamPtr createPostStream(const std::string& url, const PluginEventSinkPtr& callback,
254  const std::string& postdata,
255  bool cache = true, bool seekable = false,
256  size_t internalBufferSize = 128 * 1024 ) const;
257 
258 
272  virtual BrowserStreamPtr createUnsolicitedStream( const BrowserStreamRequest& req ) const;
273 
274  // Methods for accessing the DOM
275  public:
276 
283  void assertMainThread() const;
284 
293  bool isMainThread() const;
294 
303  virtual DOM::DocumentPtr getDOMDocument() = 0;
304 
313  virtual DOM::WindowPtr getDOMWindow() = 0;
314 
323  virtual DOM::ElementPtr getDOMElement() = 0;
324 
334  virtual void Navigate(const std::string& url, const std::string& target) {};
335 
344  virtual void evaluateJavaScript(const std::string &script) = 0;
348  virtual void evaluateJavaScript(const std::wstring &script);
349 
369  int delayedInvoke(const int delayms, const FB::JSObjectPtr& func,
370  const FB::VariantList& args, const std::string& fname = "");
371  FB::JSObjectPtr getDelayedInvokeDelegate();
372  virtual void initJS(const void* inst);
373 
381  virtual void htmlLog(const std::string& str);
382 
393  virtual void setEnableHtmlLog(const bool enabled = true) {
394  m_htmlLogEnabled = enabled;
395  }
396 
407  virtual void shutdown();
408 
417  inline bool isShutDown() const { return m_isShutDown; }
418 
425  void freeRetainedObjects() const;
426 
435  void retainJSAPIPtr(const FB::JSAPIPtr& obj) const;
443  void releaseJSAPIPtr(const FB::JSAPIPtr& obj) const;
444 
452  virtual void DoDeferredRelease() const = 0;
453 
472  virtual bool DetectProxySettings(std::map<std::string, std::string>& settingsMap, const std::string& url = "");
473 
474  public:
475  virtual FB::DOM::WindowPtr _createWindow(const FB::JSObjectPtr& obj) const;
476  virtual FB::DOM::DocumentPtr _createDocument(const FB::JSObjectPtr& obj) const;
477  virtual FB::DOM::ElementPtr _createElement(const FB::JSObjectPtr& obj) const;
478  virtual FB::DOM::NodePtr _createNode(const FB::JSObjectPtr& obj) const;
479 
480  private:
481  mutable AsyncCallManagerPtr _asyncManager;
482  // Yes, this is supposed to be both private and pure virtual.
483  virtual bool _scheduleAsyncCall(void (*func)(void *), void *userData) const = 0;
484  virtual BrowserStreamPtr _createStream(const BrowserStreamRequest& req) const = 0;
485  virtual BrowserStreamPtr _createUnsolicitedStream( const BrowserStreamRequest& req ) const = 0;
486  public:
487 
496  static int getInstanceCount() { return InstanceCount; }
497 
498  private:
499  // Stores the thread_id for the thread the plugin was started on
500  boost::thread::id m_threadId;
501  // if true then the plugin this belongs to has shut down (or is shutting down)
502  bool m_isShutDown;
503  // Used to prevent race conditions with scheduling cross-thread calls during shutdown
504  mutable boost::shared_mutex m_xtmutex;
505  // Used to prevent race conditions with scheduling cross-thread calls during shutdown
506  mutable boost::recursive_mutex m_jsapimutex;
507 
508  mutable std::list<FB::JSAPIPtr> m_retainedObjects;
509  static volatile int InstanceCount;
510  BrowserStreamManagerPtr m_streamMgr;
511 
512  // Indicates if html logging should be enabled (default true)
513  bool m_htmlLogEnabled;
514 
515  std::string unique_key;
516  std::string call_delegate;
517  };
518 
519 
520  // Due to interdependency issues, the implementation for ScheduleAsyncCall and CallOnMainThread are found in
521  // CrossThreadCall.h
522 };
523 
524 #include "CrossThreadCall.h"
525 
526 #endif
527 
DOM Node wrapper.
static void AsyncHtmlLog(void *data)
Don't call this; it is a helper function used by htmlLog.
This class is used by BrowserHost for the BrowserHost::AsyncHtmlLog method.
Definition: BrowserHost.h:46
virtual DOM::ElementPtr getDOMElement()=0
Gets a DOM::Element wrapper for the DOM/JS object tag that the plugin resides in. ...
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 retainJSAPIPtr(const FB::JSAPIPtr &obj) const
retains an instance of the JSAPI object until the plugin shuts down
void releaseJSAPIPtr(const FB::JSAPIPtr &obj) const
releases the specified JSAPI object to allow it to be invalidated and freed. This is done automatical...
boost::shared_ptr< Node > NodePtr
shared_ptr for a FB::DOM::Node
Browser host base class.
Definition: BrowserHost.h:69
std::vector< variant > VariantList
Defines an alias representing list of variants.
Definition: APITypes.h:64
boost::shared_ptr< Window > WindowPtr
shared_ptr for a FB::DOM::Window
boost::shared_ptr< Document > DocumentPtr
shared_ptr for a FB::DOM::Document
virtual DOM::DocumentPtr getDOMDocument()=0
Gets a DOM::Document wrapper for the document object that the plugin resides in.
virtual bool DetectProxySettings(std::map< std::string, std::string > &settingsMap, const std::string &url="")
Detects the proxy settings from the browser.
bool isMainThread() const
Query if this object is on the main thread.
Information about an HTTP request to be made.
bool isShutDown() const
returns true if the FB::BrowserHost::shutdown() method has been called on this object ...
Definition: BrowserHost.h:417
virtual void Navigate(const std::string &url, const std::string &target)
Instructs the browser to navigate to the specified url in the target window.
Definition: BrowserHost.h:334
boost::shared_ptr< FB::JSAPI > JSAPIPtr
Defines an alias for a JSAPI shared_ptr (you should never use a JSAPI* directly)
Definition: APITypes.h:94
virtual void * getContextID() const =0
Gets a unique identifier for this BrowserHost. There are cases where you may need multiple BrowserHos...
virtual BrowserStreamPtr createUnsolicitedStream(const BrowserStreamRequest &req) const
Used internally to create a BrowserStream to handle an unsolicited NPP_NewStream. ...
virtual void DoDeferredRelease() const =0
Releases any browser-specific objects that were destroyed on a thread other than the main thread...
DOM Window abstraction for manipulating and accessing the javascript window object that the plugin is...
virtual void htmlLog(const std::string &str)
Sends a log message to the containing web page using Console.log (firebug)
int delayedInvoke(const int delayms, const FB::JSObjectPtr &func, const FB::VariantList &args, const std::string &fname="")
Executes the provided method object after a delay using window.setTimeout.
void ScheduleOnMainThread(const boost::shared_ptr< C > &obj, Functor func) const
Schedule a call to be executed on the main thread.
boost::shared_ptr< FB::BrowserHost > BrowserHostPtr
Defines an alias representing a BrowserHost shared_ptr (you should never use a BrowserHost* directly)...
Definition: APITypes.h:117
DOM Element wrapper.
virtual void shutdown()
Notifies the browserhost object that the associated plugin object is shutting down.
Definition: BrowserHost.cpp:87
virtual ~BrowserHost()
Finaliser.
Definition: BrowserHost.cpp:82
void assertMainThread() const
When running in debug mode, asserts that the call is made on the main thread.
virtual void setEnableHtmlLog(const bool enabled=true)
Enables or disables the htmlLog method.
Definition: BrowserHost.h:393
static int getInstanceCount()
Returns the count of how many BrowserHost object instances are active.
Definition: BrowserHost.h:496
virtual void evaluateJavaScript(const std::string &script)=0
Evaluates arbitrary javascript; note that it does not return the result due to cross- browser compati...
Definition: BrowserHost.h:334
virtual BrowserStreamPtr createPostStream(const std::string &url, const PluginEventSinkPtr &callback, const std::string &postdata, bool cache=true, bool seekable=false, size_t internalBufferSize=128 *1024) const
Creates a BrowserStream.
BrowserHost()
Default constructor.
Definition: BrowserHost.cpp:75
virtual DOM::WindowPtr getDOMWindow()=0
Gets a DOM::Window wrapper for the DOM/JS window object that the plugin resides in.
void freeRetainedObjects() const
releases all JSAPI objects that have been passed to the browser
boost::shared_ptr< Element > ElementPtr
shared_ptr for a FB::DOM::Element
Functor::result_type CallOnMainThread(Functor func) const
Execute a call on the main thread, wait for the result, and return it.
bool ScheduleAsyncCall(void(*func)(void *), void *userData) const
Schedule asynchronous call to be executed on the main thread.
Abstraction for accessing and manipulating a DOM Document.