FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
JSAPISimple.cpp
1 /**********************************************************\
2 Original Author: Richard Bateman (taxilian)
3 
4 Created: Oct 29, 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 #include "precompiled_headers.h" // On windows, everything above this line in PCH
16 #include "JSAPISimple.h"
17 
18 using namespace FB;
19 JSAPISimple::JSAPISimple(void) : m_allowRemoveProperty(false)
20 {
21  registerMethod( "toString", (CallMethodPtr)&JSAPISimple::callToString );
22  registerMethod( "testEvent", (CallMethodPtr)&JSAPISimple::callFireEvent );
23 
24  registerProperty( "valid", (GetPropPtr)&JSAPISimple::getValid, NULL );
25 
26 
27 }
28 
29 JSAPISimple::~JSAPISimple(void)
30 {
31 }
32 
33 
34 variant JSAPISimple::callFireEvent(const std::vector<FB::variant>& args_in)
35 {
36  std::vector<FB::variant> args(args_in);
37 
38  try {
39  std::string event = args[0].convert_cast<std::string>();
40  args.erase(args.begin());
41  this->FireEvent(event, args);
42  return event;
43  } catch (...) {
44  throw invalid_arguments();
45  }
46 }
47 
48 // Example function call and read-only property; override these if desired in derived classes
49 variant JSAPISimple::callToString(const std::vector<FB::variant>& args)
50 {
51  return "JSAPI Javascript Object";
52 }
53 
55 {
56  return m_valid;
57 }
58 
59 void JSAPISimple::getMemberNames(std::vector<std::string> &nameVector) const
60 {
61  nameVector.clear();
62 
63  for (FB::MethodMap::const_iterator it = m_methodMap.begin();
64  it != m_methodMap.end(); ++it) {
65  nameVector.push_back(it->first);
66  }
67  for (FB::PropertyMap::const_iterator it = m_propertyMap.begin();
68  it != m_propertyMap.end(); ++it) {
69  nameVector.push_back(it->first);
70  }
71 }
72 
74 {
75  return m_methodMap.size()
76  + m_propertyMap.size();
77 }
78 
79 // Methods for registering properties and functions to the auto-table
80 void JSAPISimple::registerMethod(const std::string& name, CallMethodPtr func)
81 {
82  m_methodMap[name].callFunc = func;
83 }
84 
85 void JSAPISimple::registerProperty(const std::string& name, GetPropPtr getFunc, SetPropPtr setFunc)
86 {
87  m_propertyMap[name].getFunc = getFunc;
88  m_propertyMap[name].setFunc = setFunc;
89 }
90 
91 // Methods to query existance of members on the API
92 bool JSAPISimple::HasMethod(const std::string& methodName) const
93 {
94  if (!m_valid)
95  return false;
96 
97  MethodMap::const_iterator fnd = m_methodMap.find(methodName);
98  if (fnd != m_methodMap.end()) {
99  return true;
100  } else {
101  return false;
102  }
103 }
104 
105 bool JSAPISimple::HasProperty(const std::string& propertyName) const
106 {
107  if (!m_valid)
108  return false;
109 
110  PropertyMap::const_iterator fnd = m_propertyMap.find(propertyName);
111  if (fnd != m_propertyMap.end()) {
112  return true;
113  } else {
114  return false;
115  }
116  return false;
117 }
118 
119 
120 // Methods to manage properties on the API
121 variant JSAPISimple::GetProperty(const std::string& propertyName)
122 {
123  if (!m_valid)
124  throw object_invalidated();
125 
126  PropertyMap::const_iterator fnd = m_propertyMap.find(propertyName);
127  if (fnd != m_propertyMap.end() && fnd->second.getFunc != NULL) {
128  return (this->*fnd->second.getFunc)();
129  } else {
130  throw invalid_member(propertyName);
131  }
132 }
133 
134 void JSAPISimple::SetProperty(const std::string& propertyName, const variant& value)
135 {
136  if (!m_valid)
137  throw object_invalidated();
138 
139  PropertyMap::const_iterator fnd = m_propertyMap.find(propertyName);
140  if (fnd->second.setFunc != NULL) {
141  (this->*fnd->second.setFunc)(value);
142  } else {
143  throw invalid_member(propertyName);
144  }
145 }
146 
147 void JSAPISimple::RemoveProperty(const std::string& propertyName)
148 {
149  if (!m_valid)
150  throw object_invalidated();
151 
152  m_propertyMap.erase(propertyName);
153 }
154 
155 bool JSAPISimple::HasProperty(int idx) const
156 {
157  if (!m_valid)
158  throw object_invalidated();
159 
160  // By default do not support indexing
161  // To use array style access, override this method in your API object
162  return false;
163 }
164 
166 {
167  if (!m_valid)
168  throw object_invalidated();
169 
170  // By default do not support indexing
171  // To use array style access, override this method in your API object
172  throw invalid_member("Array index: " + variant(idx).convert_cast<std::string>());
173 }
174 
175 void JSAPISimple::SetProperty(int idx, const variant& value)
176 {
177  if (!m_valid)
178  throw object_invalidated();
179 
180  // By default do not support indexing
181  // To use array style access, override this method in your API object
182  throw invalid_member("Array index: " + variant(idx).convert_cast<std::string>());
183 }
184 
186 {
187  if (!m_valid)
188  throw object_invalidated();
189 
190  // By default do not support indexing
191  // To use array style access, override this method in your API object
192  throw invalid_member("Array index: " + variant(idx).convert_cast<std::string>());
193 }
194 
195 
196 // Methods to manage methods on the API
197 variant JSAPISimple::Invoke(const std::string& methodName, const std::vector<FB::variant>& args)
198 {
199  if (!m_valid)
200  throw object_invalidated();
201 
202  MethodMap::iterator fnd = m_methodMap.find(methodName);
203  if (fnd != m_methodMap.end() && fnd->second.callFunc != NULL) {
204  return (this->*fnd->second.callFunc)(args);
205  } else {
206  throw invalid_member(methodName);
207  }
208 }
209 
210 variant JSAPISimple::Construct(const std::vector<FB::variant>& args)
211 {
212  if (!m_valid)
213  throw object_invalidated();
214 
215  throw invalid_member("constructor");
216 }
217 
virtual void registerMethod(const std::string &name, CallMethodPtr func)
Register a method to be exposed to javascript.
Definition: JSAPISimple.cpp:80
Accepts any datatype, used in all interactions with javascript. Provides tools for getting back out t...
Definition: variant.h:198
Thrown by a JSAPI object when the argument(s) provided to a SetProperty or Invoke call are found to b...
Definition: JSExceptions.h:47
Thrown by a JSAPI object when a call is made on it after the object has been invalidated.
Definition: JSExceptions.h:69
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
size_t getMemberCount() const
Gets the member count.
Definition: JSAPISimple.cpp:73
virtual variant Construct(const std::vector< variant > &args)
Called by the browser to construct the JSAPI object.
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
virtual variant callToString(const std::vector< variant > &args)
Called when a string representation of the object requested. Always returns "JSAPI Javascript Object"...
Definition: JSAPISimple.cpp:49
virtual void RemoveProperty(const std::string &propertyName)
Removes a property.
variant(JSAPI::* GetPropPtr)()
Defines an alias representing a function pointer for a property getter on a FB::JSAPISimple object...
Definition: APITypes.h:258
virtual void registerProperty(const std::string &name, GetPropPtr getFunc, SetPropPtr setFunc)
Register a property to be exposed to javascript.
Definition: JSAPISimple.cpp:85
virtual variant getValid()
Returns true to indicate that the JSAPI interface is working.
Definition: JSAPISimple.cpp:54
virtual variant GetProperty(const std::string &propertyName)
Gets a property value.
virtual void SetProperty(const std::string &propertyName, const variant &value)
Sets the value of a property.
Thrown when an Invoke, SetProperty, or GetProperty call is made for a member that is invalid (does no...
Definition: JSExceptions.h:83
virtual bool HasProperty(const std::string &propertyName) const
Query if 'propertyName' is a valid property.
void getMemberNames(std::vector< std::string > &nameVector) const
Called by the browser to enumerate the members of this JSAPI object.
Definition: JSAPISimple.cpp:59
virtual variant callFireEvent(const std::vector< variant > &args)
Example function for testing fireEvent.
Definition: JSAPISimple.cpp:34
virtual void FireEvent(const std::wstring &eventName, const std::vector< variant > &args)
Definition: JSAPIImpl.h:92
virtual variant Invoke(const std::string &methodName, const std::vector< variant > &args)
Called by the browser to invoke a method on the JSAPI object.
virtual bool HasMethod(const std::string &methodName) const
Query if the JSAPI object has the 'methodName' method.
Definition: JSAPISimple.cpp:92