FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
JSAPI.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_JSAPI
17 #define H_FB_JSAPI
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 "boost/thread/recursive_mutex.hpp"
26 #include "boost/thread/mutex.hpp"
27 
28 namespace FB
29 {
30  class JSObject;
31  class BrowserHost;
32 
56  class JSAPI : public boost::enable_shared_from_this<JSAPI>, boost::noncopyable
57  {
58  public:
59 
65  JSAPI(void) {}
66 
72  virtual ~JSAPI(void) {}
73 
74  public:
75 
81  virtual void invalidate() = 0;
82 
88  virtual void shutdown() {}
89 
90  public:
118  virtual void pushZone(const SecurityZone& securityLevel) { }
119 
131  virtual void popZone() { }
132 
145  virtual void setDefaultZone(const SecurityZone& securityLevel) { }
146 
159  virtual SecurityZone getDefaultZone() const { return SecurityScope_Public; }
160 
172  virtual SecurityZone getZone() const { return SecurityScope_Public; }
173 
182  virtual void registerEventMethod(const std::string& name, JSObjectPtr& event)
183  {
184  // TODO: add support for this in IDispatchAPI and NPObjectAPI
185  throw FB::script_error("Not implemented");
186  }
190  virtual void registerEventMethod(const std::wstring& name, JSObjectPtr& event)
191  {
192  registerEventMethod(wstring_to_utf8(name), event);
193  }
194 
203  virtual void unregisterEventMethod(const std::string& name, JSObjectPtr& event)
204  {
205  // TODO: add support for this in IDispatchAPI and NPObjectAPI
206  throw FB::script_error("Not implemented");
207  }
211  virtual void unregisterEventMethod(const std::wstring& name, JSObjectPtr& event)
212  {
214  }
215 
225  virtual void registerEventInterface(const JSObjectPtr& event)
226  {
227  // TODO: add support for this in IDispatchAPI and NPObjectAPI
228  throw FB::script_error("Not implemented");
229  }
237  virtual void unregisterEventInterface(const JSObjectPtr& event)
238  {
239  // TODO: add support for this in IDispatchAPI and NPObjectAPI
240  throw FB::script_error("Not implemented");
241  }
242 
243  virtual void getMemberNames(std::vector<std::wstring> &nameVector) const
244  {
245  nameVector.clear();
246  std::vector<std::string> utf8Vector;
247  getMemberNames(utf8Vector);
248  for (std::vector<std::string>::const_iterator it = utf8Vector.begin();
249  it != utf8Vector.end(); ++it) {
250  std::wstring wStrVal(utf8_to_wstring(*it));
251  nameVector.push_back(wStrVal);
252  }
253  }
254 
255  virtual void getMemberNames(std::vector<std::wstring> *nameVector) const
256  {
257  getMemberNames(*nameVector);
258  }
269  virtual void getMemberNames(std::vector<std::string> &nameVector) const = 0;
270  virtual void getMemberNames(std::vector<std::string> *nameVector) const
271  {
272  getMemberNames(*nameVector);
273  }
274 
282  virtual size_t getMemberCount() const = 0;
283 
287  virtual bool HasMethod(const std::wstring& methodName) const
288  {
289  return HasMethod(wstring_to_utf8(methodName));
290  }
291 
301  virtual bool HasMethod(const std::string& methodName) const = 0;
302 
306  virtual bool HasMethodObject(const std::wstring& methodObjName) const
307  {
308  return HasMethodObject(wstring_to_utf8(methodObjName));
309  }
310 
323  virtual bool HasMethodObject(const std::string& methodObjName) const { return false; }
324 
328  virtual bool HasProperty(const std::wstring& propertyName) const
329  {
330  return HasProperty(wstring_to_utf8(propertyName));
331  }
341  virtual bool HasProperty(const std::string& propertyName) const = 0;
357  virtual bool HasProperty(int idx) const = 0;
358 
362  virtual JSAPIPtr GetMethodObject(const std::wstring& methodObjName)
363  {
364  return GetMethodObject(FB::wstring_to_utf8(methodObjName));
365  }
381  virtual JSAPIPtr GetMethodObject(const std::string& methodObjName) { return FB::JSAPIPtr(); }
382 
386  virtual variant GetProperty(const std::wstring& propertyName)
387  {
388  return GetProperty(wstring_to_utf8(propertyName));
389  }
390 
400  virtual variant GetProperty(const std::string& propertyName) = 0;
401 
405  virtual void SetProperty(const std::wstring& propertyName, const variant& value)
406  {
407  SetProperty(wstring_to_utf8(propertyName), value);
408  }
417  virtual void SetProperty(const std::string& propertyName, const variant& value) = 0;
418 
434  virtual variant GetProperty(int idx) = 0;
435 
450  virtual void SetProperty(int idx, const variant& value) = 0;
451 
455  virtual void RemoveProperty(const std::wstring& propertyName)
456  {
457  return RemoveProperty(wstring_to_utf8(propertyName));
458  }
459 
468  virtual void RemoveProperty(const std::string& propertyName) = 0;
469 
484  virtual void RemoveProperty(int idx) = 0;
485 
489  virtual variant Invoke(const std::wstring& methodName, const std::vector<variant>& args)
490  {
491  return Invoke(wstring_to_utf8(methodName), args);
492  }
493 
505  virtual variant Invoke(const std::string& methodName, const std::vector<variant>& args) = 0;
506 
520  virtual variant Construct(const std::vector<variant>& args) = 0;
521  };
522 
548  class scoped_zonelock : boost::noncopyable
549  {
550  public:
561  scoped_zonelock(const JSAPIPtr &api, const SecurityZone& zone)
562  : m_api(api.get()), ref(api) {
563  lock(zone);
564  }
574  scoped_zonelock(JSAPI* api, const SecurityZone& zone) : m_api(api) {
575  lock(zone);
576  }
584  if (m_api)
585  m_api->popZone();
586  }
587  private:
588  void lock(const SecurityZone& zone) const {
589  if (m_api)
590  m_api->pushZone(zone);
591  }
592  JSAPI* m_api;
593  const FB::JSAPIPtr ref;
594  };
595 };
596 
597 // There are important conversion routines that require JSObject and JSAPI to both be loaded
598 #include "JSObject.h"
599 #endif
600 
virtual SecurityZone getZone() const
Gets the currently active zone.
Definition: JSAPI.h:172
virtual void SetProperty(const std::wstring &propertyName, const variant &value)
Definition: JSAPI.h:405
virtual variant GetProperty(const std::wstring &propertyName)
Definition: JSAPI.h:386
virtual bool HasMethod(const std::wstring &methodName) const
Definition: JSAPI.h:287
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 bool HasProperty(const std::wstring &propertyName) const
Definition: JSAPI.h:328
virtual void invalidate()=0
Invalidates this object.
virtual JSAPIPtr GetMethodObject(const std::wstring &methodObjName)
Definition: JSAPI.h:362
virtual void shutdown()
Called to notify the object that the plugin is shutting down.
Definition: JSAPI.h:88
virtual variant Invoke(const std::wstring &methodName, const std::vector< variant > &args)
Definition: JSAPI.h:489
virtual void pushZone(const SecurityZone &securityLevel)
Pushes a new security level and locks a mutex (for every Push there must be a Pop!) ...
Definition: JSAPI.h:118
virtual SecurityZone getDefaultZone() const
Gets the default zone (the zone the class operates on before a push)
Definition: JSAPI.h:159
virtual void unregisterEventInterface(const JSObjectPtr &event)
Called by the browser to unregister a JSObject interface that handles events.
Definition: JSAPI.h:237
virtual void RemoveProperty(const std::wstring &propertyName)
Definition: JSAPI.h:455
virtual variant Construct(const std::vector< variant > &args)=0
Called by the browser to construct the JSAPI object.
Accepts any datatype, used in all interactions with javascript. Provides tools for getting back out t...
Definition: variant.h:198
virtual void popZone()
Pops off a security level and unlocks the mutex (for every Push there must be a Pop!) ...
Definition: JSAPI.h:131
virtual void setDefaultZone(const SecurityZone &securityLevel)
Sets the default zone (the zone the class operates on before a push)
Definition: JSAPI.h:145
virtual void registerEventInterface(const JSObjectPtr &event)
Called by the browser to register a JSObject interface that handles events. This is primarily used by...
Definition: JSAPI.h:225
virtual bool HasMethodObject(const std::string &methodObjName) const
Query if 'methodObjName' is a valid methodObj.
Definition: JSAPI.h:323
int SecurityZone
Used to set a SecurityZone for a method or property – used by JSAPIAuto.
Definition: APITypes.h:275
JSAPI(void)
Default constructor.
Definition: JSAPI.h:65
virtual void registerEventMethod(const std::string &name, JSObjectPtr &event)
Called by the browser to register an event handler method.
Definition: JSAPI.h:182
scoped_zonelock(const JSAPIPtr &api, const SecurityZone &zone)
Accepts a FB::JSAPIPtr and pushes the specified security zone to be used until this object goes out o...
Definition: JSAPI.h:561
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
Provides a helper class for locking.
Definition: JSAPI.h:548
Exception type; when thrown in a JSAPI method, a javascript exception will be thrown.
Definition: JSExceptions.h:28
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 void registerEventMethod(const std::wstring &name, JSObjectPtr &event)
Definition: JSAPI.h:190
virtual bool HasMethodObject(const std::wstring &methodObjName) const
Definition: JSAPI.h:306
~scoped_zonelock()
Unlocks/pops the zone.
Definition: JSAPI.h:583
virtual ~JSAPI(void)
Finaliser.
Definition: JSAPI.h:72
scoped_zonelock(JSAPI *api, const SecurityZone &zone)
Definition: JSAPI.h:574
virtual size_t getMemberCount() const =0
Gets the member count.
JavaScript API class – provides a javascript interface that can be exposed to the browser...
Definition: JSAPI.h:56
virtual JSAPIPtr GetMethodObject(const std::string &methodObjName)
Gets a method object (JSAPI object that has a default method)
Definition: JSAPI.h:381
virtual void unregisterEventMethod(const std::wstring &name, JSObjectPtr &event)
Definition: JSAPI.h:211
virtual void unregisterEventMethod(const std::string &name, JSObjectPtr &event)
Called by the browser to unregister an event handler method.
Definition: JSAPI.h:203
std::wstring utf8_to_wstring(const std::string &src)
Accepts a UTF8-encoded std::string and returns a std::wstring.
Definition: utf8_tools.cpp:50