FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
JSAPIAuto.h
1 /**********************************************************\
2 Original Author: Georg Fritzsche
3 
4 Created: November 7, 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 Georg Fritzsche, Firebreath development team
13 \**********************************************************/
14 
15 #pragma once
16 #ifndef JSAPIAUTO_H
17 #define JSAPIAUTO_H
18 
19 #include <deque>
20 #include <vector>
21 #include <string>
22 #include <boost/thread/recursive_mutex.hpp>
23 #include <boost/tuple/tuple.hpp>
24 #include <boost/tuple/tuple_comparison.hpp>
25 #include "JSAPIImpl.h"
26 #include "MethodConverter.h"
27 #include "PropertyConverter.h"
28 #include "Util/typesafe_event.h"
29 
30 namespace FB {
31  FB_FORWARD_PTR(JSFunction);
32 
47 
94  class JSAPIAuto : public JSAPIImpl
95  {
96  protected:
97  struct Attribute {
98  bool operator<(const Attribute& rh) const
99  {
100  return value < rh.value;
101  }
102  FB::variant value;
103  bool readonly;
104  };
105  typedef std::map<boost::tuple<std::string, FB::SecurityZone>, FB::JSFunctionPtr> MethodObjectMap;
106  typedef std::map<std::string, FB::SecurityZone> ZoneMap;
107  typedef std::map<std::string, Attribute> AttributeMap;
108 
109  public:
111  JSAPIAuto(const std::string& description = "<JSAPI-Auto Javascript Object>");
112  JSAPIAuto(const SecurityZone& securityLevel, const std::string& description = "<JSAPI-Auto Secure Javascript Object>");
113  typedef std::deque<SecurityZone> ZoneStack;
114 
115  void init();
116 
117  virtual ~JSAPIAuto();
118 
132  virtual void registerAttribute(const std::string &name, const FB::variant& value, bool readonly = false);
133 
150  virtual void setReserved(const std::string &name)
151  {
152  m_reservedMembers.insert(name);
153  }
154 
168  bool isReserved( const std::string& propertyName ) const
169  {
170  return m_reservedMembers.find(propertyName) != m_reservedMembers.end();
171  }
172 
173  virtual void getMemberNames(std::vector<std::string> &nameVector) const;
174  virtual size_t getMemberCount() const;
175 
176  virtual variant Invoke(const std::string& methodName, const std::vector<variant>& args);
177  virtual variant Construct(const std::vector<variant>& args);
178  virtual JSAPIPtr GetMethodObject(const std::string& methodObjName);
179 
180  virtual void unregisterMethod(const std::wstring& name)
181  {
182  unregisterMethod(FB::wstring_to_utf8(name));
183  }
195  virtual void unregisterMethod(const std::string& name);
196 
197  virtual void registerMethod(const std::wstring& name, const CallMethodFunctor& func)
198  {
199  registerMethod(FB::wstring_to_utf8(name), func);
200  }
226  virtual void registerMethod(const std::string& name, const CallMethodFunctor& func);
227 
228  virtual bool HasMethod(const std::string& methodName) const;
229  virtual bool HasMethodObject(const std::string& methodObjName) const;
230  virtual bool HasProperty(const std::string& propertyName) const;
231  virtual bool HasProperty(int idx) const;
232 
233  virtual void registerProperty(const std::wstring& name, const PropertyFunctors& propFuncs);
234 
258  virtual void registerProperty(const std::string& name, const PropertyFunctors& propFuncs);
259 
260  virtual void unregisterProperty(const std::wstring& name);
272  virtual void unregisterProperty(const std::string& name);
273 
274  virtual variant GetProperty(const std::string& propertyName);
275  virtual void SetProperty(const std::string& propertyName, const variant& value);
276  virtual void RemoveProperty(const std::string& propertyName);
277  virtual variant GetProperty(int idx);
278  virtual void SetProperty(int idx, const variant& value);
279  virtual void RemoveProperty(int idx);
280 
281  virtual void FireJSEvent(const std::string& eventName, const FB::VariantMap &members, const FB::VariantList &arguments);
282  virtual void fireAsyncEvent( const std::string& eventName, const std::vector<variant>& args );
283 
293  virtual std::string ToString()
294  {
295  return m_description;
296  }
297 
306  virtual bool get_valid()
307  {
308  return true;
309  }
310 
325  virtual FB::variant getAttribute(const std::string& name);
339  virtual void setAttribute(const std::string& name, const FB::variant& value);
352  virtual void unregisterAttribute(const std::string& name);
353 
354  protected:
355  bool memberAccessible( ZoneMap::const_iterator it ) const
356  {
357  return (it != m_zoneMap.end()) && getZone() >= it->second;
358  }
359 
360  protected:
361  // Stores Method Objects -- JSAPI proxy objects for calling a method on this object
362  MethodObjectMap m_methodObjectMap;
363  // Stores the methods exposed to JS
364  MethodFunctorMap m_methodFunctorMap;
365  // Stores the properties exposed to JS
366  PropertyFunctorsMap m_propertyFunctorsMap;
367  // Keeps track of the security zone of each member
368  ZoneMap m_zoneMap;
369 
370  const std::string m_description;
371 
372  AttributeMap m_attributes;
373  FB::StringSet m_reservedMembers;
374  bool m_allowDynamicAttributes;
375  bool m_allowRemoveProperties;
376  bool m_allowMethodObjects;
377  public:
378  static bool s_allowDynamicAttributes;
379  static bool s_allowRemoveProperties;
380  static bool s_allowMethodObjects;
381  };
382 
383 };
384 
385 #endif // JSAPIAUTO_H
386 
bool isReserved(const std::string &propertyName) const
Returns true if the specified propertyName is a reserved attribute.
Definition: JSAPIAuto.h:168
boost::function< variant(const std::vector< variant > &)> CallMethodFunctor
Defines an alias representing a method functor used by FB::JSAPIAuto, created by FB::make_method().
Definition: APITypes.h:286
JavaScript API base class implementation – provides basic functionality for C++ JSAPI objects...
Definition: JSAPIImpl.h:49
virtual void RemoveProperty(const std::string &propertyName)
Removes a property.
Definition: JSAPIAuto.cpp:254
virtual std::string ToString()
Default method called when a string value is requested for the scriptable object. ...
Definition: JSAPIAuto.h:293
virtual void registerAttribute(const std::string &name, const FB::variant &value, bool readonly=false)
Registers an attribute name and sets the value to _value. Optionally read-only.
Definition: JSAPIAuto.cpp:383
Accepts any datatype, used in all interactions with javascript. Provides tools for getting back out t...
Definition: variant.h:198
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: JSAPIAuto.cpp:426
virtual bool HasMethodObject(const std::string &methodObjName) const
Query if 'methodObjName' is a valid methodObj.
Definition: JSAPIAuto.cpp:155
std::vector< variant > VariantList
Defines an alias representing list of variants.
Definition: APITypes.h:64
std::set< std::string > StringSet
Defines an alias representing a set of std::strings.
Definition: APITypes.h:79
int SecurityZone
Used to set a SecurityZone for a method or property – used by JSAPIAuto.
Definition: APITypes.h:275
virtual void getMemberNames(std::vector< std::string > &nameVector) const
Called by the browser to enumerate the members of this JSAPI object.
Definition: JSAPIAuto.cpp:125
virtual bool HasProperty(const std::string &propertyName) const
Query if 'propertyName' is a valid property.
Definition: JSAPIAuto.cpp:162
virtual bool HasMethod(const std::string &methodName) const
Query if the JSAPI object has the 'methodName' method.
Definition: JSAPIAuto.cpp:146
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
std::map< std::string, PropertyFunctors > PropertyFunctorsMap
Defines an alias representing a map of property functors used by FB::JSAPIAuto.
Definition: APITypes.h:327
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 SetProperty(const std::string &propertyName, const variant &value)
Sets the value of a property.
Definition: JSAPIAuto.cpp:224
virtual variant Invoke(const std::string &methodName, const std::vector< variant > &args)
Called by the browser to invoke a method on the JSAPI object.
Definition: JSAPIAuto.cpp:329
virtual JSAPIPtr GetMethodObject(const std::string &methodObjName)
Gets a method object (JSAPI object that has a default method)
Definition: JSAPIAuto.cpp:363
virtual FB::variant getAttribute(const std::string &name)
Returns the attribute with the given name, empty variant if none.
Definition: JSAPIAuto.cpp:406
virtual variant GetProperty(const std::string &propertyName)
Gets a property value.
Definition: JSAPIAuto.cpp:191
virtual bool get_valid()
Property exposed by default to javascript useful for checking to make sure that the JSAPI is working...
Definition: JSAPIAuto.h:306
virtual variant Construct(const std::vector< variant > &args)
Called by the browser to construct the JSAPI object.
Definition: JSAPIAuto.cpp:354
std::map< std::string, variant > VariantMap
Defines an alias representing a string -> variant map.
Definition: APITypes.h:72
std::map< std::string, MethodFunctors > MethodFunctorMap
Defines an alias representing a map of method functors used by FB::JSAPIAuto.
Definition: APITypes.h:302
JSAPIAuto(const std::string &description="<JSAPI-Auto Javascript Object>")
Description is used by ToString().
Definition: JSAPIAuto.cpp:29
virtual void setAttribute(const std::string &name, const FB::variant &value)
Assigns a value to the specified attribute, if it is not reserved or read-only.
Definition: JSAPIAuto.cpp:414
virtual size_t getMemberCount() const
Gets the member count.
Definition: JSAPIAuto.cpp:135
JSAPI class with automatic argument type enforcement.
Definition: JSAPIAuto.h:94
virtual void setReserved(const std::string &name)
Prevents attributes from being created from JavaScript with the specified name.
Definition: JSAPIAuto.h:150
virtual SecurityZone getZone() const
Gets the currently active zone.
Definition: JSAPIImpl.h:238