FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
APITypes.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_APITYPES
17 #define H_FB_APITYPES
18 
19 #include <string>
20 #include <vector>
21 #include <map>
22 #include <set>
23 #include <boost/function.hpp>
24 #include <boost/shared_ptr.hpp>
25 #include <boost/variant/variant_fwd.hpp>
26 #include "fb_stdint.h"
27 #include "FBPointers.h"
28 
41 namespace FB
42 {
43  FB_FORWARD_PTR(BrowserHost);
44  FB_FORWARD_PTR(JSAPI);
45  FB_FORWARD_PTR(JSObject);
46  class variant;
47  namespace variant_detail {
48  // Note that null translates to returning NULL
49  struct null;
50  // Note that empty translates into return VOID (undefined)
51  struct empty;
52  }
53 
54  // Variant list
55 
64  typedef std::vector<variant> VariantList;
65 
72  typedef std::map<std::string, variant> VariantMap;
73 
79  typedef std::set<std::string> StringSet;
80 
81  // FB pointer types
82 
88  typedef boost::weak_ptr<FB::JSAPI> JSAPIWeakPtr;
94  typedef boost::shared_ptr<FB::JSAPI> JSAPIPtr;
95 
102  typedef boost::weak_ptr<FB::JSObject> JSObjectWeakPtr;
109  typedef boost::shared_ptr<FB::JSObject> JSObjectPtr;
110 
117  typedef boost::shared_ptr<FB::BrowserHost> BrowserHostPtr;
118 
119  // backwards compability typedefs
120 
128 
136 
144 
145  // deprecation warnings
146 
147 #if defined(_MSC_VER)
148 # pragma deprecated(BrowserHostWrapper, BrowserObjectAPI, JSOutObject)
149 #elif defined(__GNUC__)
150  typedef BrowserHost BrowserHostWrapper __attribute__((deprecated));
151  typedef JSObject BrowserObjectAPI __attribute__((deprecated));
152  typedef JSAPIPtr JSOutObject __attribute__((deprecated));
153 #endif
154 
155  // dynamically cast a FB pointer
156 
168  template<class T, class U>
169  boost::shared_ptr<T> ptr_cast(boost::shared_ptr<U> const & r);
170 
172  typedef variant (JSAPI::*InvokeType)(const std::string&, const std::vector<variant>&);
174  typedef variant (JSAPI::*ConstructType)(const std::vector<variant>&);
176  typedef void (JSAPI::*SetPropertyType)(const std::string&, const variant&);
178  typedef variant (JSAPI::*GetPropertyType)(const std::string&);
180  typedef void (JSAPI::*RemovePropertyType)(const std::string&);
181 
208  struct CatchAll {
209  typedef FB::VariantList value_type;
210  FB::VariantList value;
211  };
212 
213  // Special Variant types
214  typedef FB::variant_detail::empty FBVoid;
215  typedef FB::variant_detail::null FBNull;
216  struct FBDateString {
217  public:
218  FBDateString() { }
219  FBDateString(const FBDateString& rhs) : date(rhs.date) { }
220  FBDateString(const std::string &dstr) : date(dstr) { }
221 
222  FBDateString& operator=(const std::string& dstr) { date = dstr; return *this; }
223  std::string getValue() { return date; }
224  void setValue(std::string value) { date = value; }
225  bool operator<(const std::string& rh) const
226  {
227  return date < rh;
228  }
229  bool operator<(const FBDateString& rh) const
230  {
231  return date < rh.date;
232  }
233 
234  protected:
235  std::string date;
236  };
237 
238  // JSAPI methods
239 
240  class JSAPI;
242  typedef variant (JSAPI::*CallMethodPtr)(const std::vector<variant>&);
244  struct MethodInfo {
245  MethodInfo() : callFunc(NULL) { }
246  MethodInfo(CallMethodPtr callFunc) : callFunc(callFunc) { }
247  MethodInfo(const MethodInfo &rh) : callFunc(rh.callFunc) { }
248  CallMethodPtr callFunc;
249  };
250 
252  typedef std::map<std::string, MethodInfo> MethodMap;
253 
254  // JSAPI properties
255 
256 
258  typedef variant (JSAPI::*GetPropPtr)();
260  typedef void (JSAPI::*SetPropPtr)(const variant& value);
262  struct PropertyInfo {
263  PropertyInfo() : getFunc(NULL), setFunc(NULL) { }
264  PropertyInfo(GetPropPtr getFunc, SetPropPtr setFunc) : getFunc(getFunc), setFunc(setFunc) { }
265  PropertyInfo(const PropertyInfo &rh) : getFunc(rh.getFunc), setFunc(rh.setFunc) { }
266  GetPropPtr getFunc;
267  SetPropPtr setFunc;
268  };
269 
271  typedef std::map<std::string, PropertyInfo> PropertyMap;
272 
273  // new style JSAPI methods
275  typedef int SecurityZone;
276 
279  SecurityScope_Public = 0,
280  SecurityScope_Protected = 2,
281  SecurityScope_Private = 4,
282  SecurityScope_Local = 6
283  };
284 
286  typedef boost::function<variant (const std::vector<variant>&)> CallMethodFunctor;
287  struct MethodFunctors
288  {
289  CallMethodFunctor call;
290  SecurityZone zone;
291  MethodFunctors() : call() {}
292  MethodFunctors(const CallMethodFunctor& call) : call(call) {}
293  MethodFunctors(const SecurityZone& zone, const CallMethodFunctor& call) : call(call), zone(zone) {}
294  MethodFunctors(const MethodFunctors& m) : call(m.call) {}
295  MethodFunctors& operator=(const MethodFunctors& rhs) {
296  call = rhs.call;
297  zone = rhs.zone;
298  return *this;
299  }
300  };
302  typedef std::map<std::string, MethodFunctors> MethodFunctorMap;
303 
304  // new style JSAPI properties
305 
307  typedef boost::function<FB::variant ()> GetPropFunctor;
309  typedef boost::function<void (const FB::variant&)> SetPropFunctor;
312  {
313  GetPropFunctor get;
314  SetPropFunctor set;
315  PropertyFunctors() : get(), set() {}
316  PropertyFunctors(const GetPropFunctor& get, const SetPropFunctor& set)
317  : get(get), set(set) {}
319  : get(p.get), set(p.set) {}
320  PropertyFunctors& operator=(const PropertyFunctors& rhs) {
321  get = rhs.get;
322  set = rhs.set;
323  return *this;
324  }
325  };
327  typedef std::map<std::string, PropertyFunctors> PropertyFunctorsMap;
328 
329  // JSAPI event handlers
330 
331  typedef std::pair<std::string, FB::JSObjectPtr> EventPair;
332  typedef std::multimap<std::string, FB::JSObjectPtr> EventMultiMap;
333  typedef std::map<void*, FB::JSObjectPtr> EventIFaceMap;
334  typedef std::map<std::string, FB::JSObjectPtr> EventSingleMap;
335 
336  // implementation details
337 
338  template<class T, class U>
339  boost::shared_ptr<T> ptr_cast(boost::shared_ptr<U> const & r)
340  {
341  return boost::dynamic_pointer_cast<T>(r);
342  }
343 
344  namespace boost_variant {
345  typedef boost::variant<long, int, double, std::string, FB::JSAPIPtr, FB::JSObjectPtr, FB::FBNull, FB::FBVoid> fb_compat;
346  typedef boost::variant<long, int, double, float, std::string, FB::FBNull, FB::FBVoid> primitives;
347  typedef boost::variant<std::string, FB::StringSet> strings;
348  }
349 
350  struct Rect {
351  int32_t top;
352  int32_t left;
353  int32_t bottom;
354  int32_t right;
355  };
356 }
357 
358 // This needs to be included after all our classes are defined because it relies on types defined in this file
359 // TODO: can this be done better?
360 #include "variant.h"
361 
362 #endif
363 
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
variant(JSAPI::* InvokeType)(const std::string &, const std::vector< variant > &)
Defines an alias representing a function pointer to JSAPI::Invoke.
Definition: APITypes.h:172
boost::function< FB::variant()> GetPropFunctor
Defines an alias representing a property getter functor used by FB::JSAPIAuto.
Definition: APITypes.h:307
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
boost::function< void(const FB::variant &)> SetPropFunctor
Defines an alias representing a property setter functor used by FB::JSAPIAuto.
Definition: APITypes.h:309
Used by FB::JSAPISimple to store information about a property.
Definition: APITypes.h:262
Used by FB::JSAPISimple to store information about a method.
Definition: APITypes.h:244
Wraps a Javascript Object.
Definition: JSObject.h:46
JSAPIPtr JSOutObject
Defines an alias for JSOutObject -> JSAPIPtr.
Definition: APITypes.h:143
JSObject BrowserObjectAPI
Defines a alias for backwards compatibility.
Definition: APITypes.h:135
Accepts any datatype, used in all interactions with javascript. Provides tools for getting back out t...
Definition: variant.h:198
Browser host base class.
Definition: BrowserHost.h:69
variant(JSAPI::* GetPropertyType)(const std::string &)
Defines an alias representing a function pointer to JSAPI::GetProperty.
Definition: APITypes.h:178
variant(JSAPI::* CallMethodPtr)(const std::vector< variant > &)
Defines an alias representing a function ptr for a method on a FB::JSAPISimple object.
Definition: APITypes.h:242
boost::shared_ptr< T > ptr_cast(boost::shared_ptr< U > const &r)
Convenience function for doing a dynamic cast of one boost::shared_ptr to another.
Definition: APITypes.h:339
std::vector< variant > VariantList
Defines an alias representing list of variants.
Definition: APITypes.h:64
boost::weak_ptr< FB::JSAPI > JSAPIWeakPtr
Defines an alias for a JSAPI weak_ptr (you should never use a JSAPI* directly)
Definition: APITypes.h:88
variant(JSAPI::* ConstructType)(const std::vector< variant > &)
Defines an alias representing a function pointer to JSAPI::Invoke.
Definition: APITypes.h:174
std::set< std::string > StringSet
Defines an alias representing a set of std::strings.
Definition: APITypes.h:79
boost::weak_ptr< FB::JSObject > JSObjectWeakPtr
Defines an alias representing a JSObject weak_ptr (you should never use a JSObject* directly) ...
Definition: APITypes.h:102
int SecurityZone
Used to set a SecurityZone for a method or property – used by JSAPIAuto.
Definition: APITypes.h:275
void(JSAPI::* SetPropPtr)(const variant &value)
Defines an alias representing a function pointer for a property setter on a FB::JSAPISimple object...
Definition: APITypes.h:260
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
void(JSAPI::* RemovePropertyType)(const std::string &)
Defines an alias representing a function pointer to JSAPI::GetProperty.
Definition: APITypes.h:180
BrowserHost BrowserHostWrapper
Defines a alias for backwards compatibility.
Definition: APITypes.h:127
variant(JSAPI::* GetPropPtr)()
Defines an alias representing a function pointer for a property getter on a FB::JSAPISimple object...
Definition: APITypes.h:258
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
SecurityLevel
Default SecurityZone values; you can use these or provide your own.
Definition: APITypes.h:278
When used as a parameter on a JSAPIAuto function this matches 0 or more variants – in other words...
Definition: APITypes.h:208
std::map< std::string, MethodInfo > MethodMap
Defines an alias representing a map of methods used by FB::JSAPISimple.
Definition: APITypes.h:252
used by FB::JSAPIAuto to store property implementation details, created by FB::make_property().
Definition: APITypes.h:311
std::map< std::string, PropertyInfo > PropertyMap
Defines an alias representing a map of properties used by FB::JSAPISimple.
Definition: APITypes.h:271
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
void(JSAPI::* SetPropertyType)(const std::string &, const variant &)
Defines an alias representing a function pointer to JSAPI::SetProperty.
Definition: APITypes.h:176
JavaScript API class – provides a javascript interface that can be exposed to the browser...
Definition: JSAPI.h:56