FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
JSAPIProxy.cpp
1 /**********************************************************\
2 Original Author: Richard Bateman (taxilian)
3 
4 Created: Nov 23, 2010
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 <boost/make_shared.hpp>
16 #include "precompiled_headers.h" // On windows, everything above this line in PCH
17 
18 #include "JSAPIProxy.h"
19 
20 FB::JSAPIProxyPtr FB::JSAPIProxy::create( const FB::JSAPIPtr &inner )
21 {
22  // This is necessary because you can't use shared_from_this in the constructor
23  FB::JSAPIProxyPtr ptr(new FB::JSAPIProxy(inner));
24  FB::JSAPIImplPtr impl(FB::ptr_cast<JSAPIImpl>(ptr));
25  if (impl)
26  impl->registerProxy(ptr);
27 
28  return ptr;
29 }
30 
31 FB::JSAPIProxyPtr FB::JSAPIProxy::create( const FB::JSAPIWeakPtr &inner )
32 {
33  // This is necessary because you can't use shared_from_this in the constructor
34  FB::JSAPIProxyPtr ptr(new FB::JSAPIProxy(inner));
35  FB::JSAPIImplPtr impl(FB::ptr_cast<JSAPIImpl>(inner.lock()));
36  if (impl)
37  impl->registerProxy(ptr);
38 
39  return ptr;
40 }
41 
42 FB::JSAPIProxyPtr FB::JSAPIProxy::create( const SecurityZone& securityLevel, const FB::JSAPIPtr &inner )
43 {
44  // This is necessary because you can't use shared_from_this in the constructor
45  FB::JSAPIProxyPtr ptr(new FB::JSAPIProxy(securityLevel, inner));
46  FB::JSAPIImplPtr impl(FB::ptr_cast<JSAPIImpl>(ptr));
47  if (impl)
48  impl->registerProxy(ptr);
49 
50  return ptr;
51 }
52 
53 FB::JSAPIProxyPtr FB::JSAPIProxy::create( const SecurityZone& securityLevel, const FB::JSAPIWeakPtr &inner )
54 {
55  // This is necessary because you can't use shared_from_this in the constructor
56  FB::JSAPIProxyPtr ptr(new FB::JSAPIProxy(securityLevel, inner));
57  FB::JSAPIImplPtr impl(FB::ptr_cast<JSAPIImpl>(inner.lock()));
58  if (impl)
59  impl->registerProxy(ptr);
60 
61  return ptr;
62 }
63 
64 FB::JSAPIProxy::JSAPIProxy( const FB::JSAPIPtr &inner )
65  : m_api(inner), m_apiWeak(inner)
66 {
67  if (!inner) {
68  throw FB::script_error("Invalid inner JSAPI object passed to proxy");
69  }
70 }
71 
72 FB::JSAPIProxy::JSAPIProxy( const FB::JSAPIWeakPtr &inner )
73  : m_apiWeak(inner)
74 {
75 
76 }
77 
78 FB::JSAPIProxy::JSAPIProxy( const SecurityZone& securityLevel, const FB::JSAPIPtr &inner )
79  : FB::JSAPIImpl(securityLevel), m_api(inner), m_apiWeak(inner)
80 {
81  if (!inner) {
82  throw FB::script_error("Invalid inner JSAPI object passed to proxy");
83  }
84 }
85 
86 FB::JSAPIProxy::JSAPIProxy( const SecurityZone& securityLevel, const FB::JSAPIWeakPtr &inner )
87  : FB::JSAPIImpl(securityLevel), m_apiWeak(inner)
88 {
89 
90 }
91 
92 FB::JSAPIProxy::~JSAPIProxy( void )
93 {
94 }
95 
97 {
98  if (FB::JSAPIImplPtr ptr = FB::ptr_cast<FB::JSAPIImpl>(m_apiWeak.lock())) {
99  ptr->unregisterProxy(shared_from_this());
100  }
101  this->m_api.reset();
102  this->m_apiWeak = inner;
103  FB::JSAPIImplPtr ptr = FB::ptr_cast<FB::JSAPIImpl>(inner.lock());
104  if (ptr)
105  ptr->registerProxy(shared_from_this());
106 }
107 
109 {
110  if (FB::JSAPIImplPtr ptr = FB::ptr_cast<FB::JSAPIImpl>(m_apiWeak.lock())) {
111  ptr->unregisterProxy(shared_from_this());
112  }
113  this->m_api = inner;
114  this->m_apiWeak = inner;
115  FB::JSAPIImplPtr ptr = FB::ptr_cast<FB::JSAPIImpl>(inner);
116  if (ptr)
117  ptr->registerProxy(shared_from_this());
118 }
119 
120 void FB::JSAPIProxy::reset()
121 {
122  if (FB::JSAPIImplPtr ptr = FB::ptr_cast<FB::JSAPIImpl>(m_apiWeak.lock())) {
123  ptr->unregisterProxy(shared_from_this());
124  }
125 
126 }
127 
129 {
130  try {
131  getAPI()->invalidate();
132  } catch (...) {
133  // If that didn't work it's already invalid
134  }
135 }
136 
137 void FB::JSAPIProxy::getMemberNames( std::vector<std::string> &nameVector ) const
138 {
139  FB::scoped_zonelock _l(getAPI(), getZone());
140  getAPI()->getMemberNames(nameVector);
141 }
142 
144 {
145  FB::scoped_zonelock _l(getAPI(), getZone());
146  return getAPI()->getMemberCount();
147 }
148 
149 bool FB::JSAPIProxy::HasMethod( const std::wstring& methodName ) const
150 {
151  FB::scoped_zonelock _l(getAPI(), getZone());
152  return getAPI()->HasMethod(methodName);
153 }
154 
155 bool FB::JSAPIProxy::HasMethod( const std::string& methodName ) const
156 {
157  FB::scoped_zonelock _l(getAPI(), getZone());
158  return getAPI()->HasMethod(methodName);
159 }
160 
161 bool FB::JSAPIProxy::HasProperty( const std::wstring& propertyName ) const
162 {
163  if (propertyName == L"expired")
164  return true;
165  FB::scoped_zonelock _l(getAPI(), getZone());
166  return getAPI()->HasProperty(propertyName);
167 }
168 
169 bool FB::JSAPIProxy::HasProperty( const std::string& propertyName ) const
170 {
171  if (propertyName == "expired")
172  return true;
173  FB::scoped_zonelock _l(getAPI(), getZone());
174  return getAPI()->HasProperty(propertyName);
175 }
176 
177 bool FB::JSAPIProxy::HasProperty( int idx ) const
178 {
179  FB::scoped_zonelock _l(getAPI(), getZone());
180  return getAPI()->HasProperty(idx);
181 }
182 
183 FB::variant FB::JSAPIProxy::GetProperty( const std::wstring& propertyName )
184 {
185  if (propertyName == L"expired")
186  return isExpired();
187  FB::scoped_zonelock _l(getAPI(), getZone());
188  return getAPI()->GetProperty(propertyName);
189 }
190 
191 FB::variant FB::JSAPIProxy::GetProperty( const std::string& propertyName )
192 {
193  if (propertyName == "expired")
194  return isExpired();
195  FB::scoped_zonelock _l(getAPI(), getZone());
196  return getAPI()->GetProperty(propertyName);
197 }
198 
200 {
201  FB::scoped_zonelock _l(getAPI(), getZone());
202  return getAPI()->GetProperty(idx);
203 }
204 
205 void FB::JSAPIProxy::SetProperty( const std::wstring& propertyName, const variant& value )
206 {
207  FB::scoped_zonelock _l(getAPI(), getZone());
208  getAPI()->SetProperty(propertyName, value);
209 }
210 
211 void FB::JSAPIProxy::SetProperty( const std::string& propertyName, const variant& value )
212 {
213  FB::scoped_zonelock _l(getAPI(), getZone());
214  getAPI()->SetProperty(propertyName, value);
215 }
216 
217 void FB::JSAPIProxy::SetProperty( int idx, const variant& value )
218 {
219  FB::scoped_zonelock _l(getAPI(), getZone());
220  getAPI()->SetProperty(idx, value);
221 }
222 
223 void FB::JSAPIProxy::RemoveProperty( const std::wstring& propertyName )
224 {
225  FB::scoped_zonelock _l(getAPI(), getZone());
226  getAPI()->RemoveProperty(propertyName);
227 }
228 
229 void FB::JSAPIProxy::RemoveProperty( const std::string& propertyName )
230 {
231  FB::scoped_zonelock _l(getAPI(), getZone());
232  getAPI()->RemoveProperty(propertyName);
233 }
234 
236 {
237  FB::scoped_zonelock _l(getAPI(), getZone());
238  getAPI()->RemoveProperty(idx);
239 }
240 
241 FB::variant FB::JSAPIProxy::Invoke( const std::wstring& methodName, const std::vector<variant>& args )
242 {
243  FB::scoped_zonelock _l(getAPI(), getZone());
244  return getAPI()->Invoke(methodName, args);
245 }
246 
247 FB::variant FB::JSAPIProxy::Invoke( const std::string& methodName, const std::vector<variant>& args )
248 {
249  FB::scoped_zonelock _l(getAPI(), getZone());
250  return getAPI()->Invoke(methodName, args);
251 }
252 
253 FB::variant FB::JSAPIProxy::Construct( const std::vector<variant>& args )
254 {
255  FB::scoped_zonelock _l(getAPI(), getZone());
256  return getAPI()->Construct(args);
257 }
258 
JavaScript API base class implementation – provides basic functionality for C++ JSAPI objects...
Definition: JSAPIImpl.h:49
static JSAPIProxyPtr create(const FB::JSAPIPtr &inner)
Creates a JSAPIProxy that holds a shared_ptr reference to the JSAPI object provided.
Definition: JSAPIProxy.cpp:20
virtual variant Invoke(const std::wstring &methodName, const std::vector< variant > &args)
Definition: JSAPIProxy.cpp:241
Accepts any datatype, used in all interactions with javascript. Provides tools for getting back out t...
Definition: variant.h:198
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
JavaScript API Wrapper – this can wrap any type of JSAPI object and be passed back to any browser tha...
Definition: JSAPIProxy.h:45
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
virtual void RemoveProperty(const std::wstring &propertyName)
Definition: JSAPIProxy.cpp:223
virtual size_t getMemberCount() const
Gets the member count.
Definition: JSAPIProxy.cpp:143
int SecurityZone
Used to set a SecurityZone for a method or property – used by JSAPIAuto.
Definition: APITypes.h:275
virtual bool HasProperty(const std::wstring &propertyName) const
Definition: JSAPIProxy.cpp:161
virtual void SetProperty(const std::wstring &propertyName, const variant &value)
Definition: JSAPIProxy.cpp:205
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
virtual bool HasMethod(const std::wstring &methodName) const
Definition: JSAPIProxy.cpp:149
virtual variant Construct(const std::vector< variant > &args)
Called by the browser to construct the JSAPI object.
Definition: JSAPIProxy.cpp:253
virtual variant GetProperty(const std::wstring &propertyName)
Definition: JSAPIProxy.cpp:183
void changeObject(const FB::JSAPIPtr &inner)
Changes the wrapped JSAPIPtr to the given one.
Definition: JSAPIProxy.cpp:108
void invalidate()
Invalidates this object.
Definition: JSAPIProxy.cpp:128
virtual void getMemberNames(std::vector< std::string > &nameVector) const
Called by the browser to enumerate the members of this JSAPI object.
Definition: JSAPIProxy.cpp:137