FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
NPJavascriptObject.h
1 /**********************************************************\
2 Original Author: Richard Bateman (taxilian)
3 
4 Created: Oct 16, 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_NPJAVASCRIPTOBJECT
17 #define H_NPJAVASCRIPTOBJECT
18 
19 #include "APITypes.h"
20 #include "JSAPI.h"
21 #include "NpapiTypes.h"
22 #include "NpapiBrowserHost.h"
23 #include "JSFunction.h"
24 
25 namespace FB { namespace Npapi {
26  class NPJavascriptObject :
27  public NPObject
28  {
29  public:
30  static bool isNPJavaScriptObject(const NPObject* const npo);
31 
32  protected:
33  NpapiBrowserHostPtr getHost() {
34  NpapiBrowserHostPtr ptr(m_browser.lock());
35  if (!ptr) {
36  Invalidate();
37  throw std::bad_cast();
38  }
39  return ptr;
40  }
41  static NPClass NPJavascriptObjectClass;
42  FB::JSAPIWeakPtr m_api;
43  NpapiBrowserHostWeakPtr m_browser;
44  bool m_valid;
45  bool m_autoRelease;
46  boost::shared_ptr<FB::ShareableReference< NPJavascriptObject > > m_sharedRef;
47 
48  public:
49  static NPJavascriptObject *NewObject(const NpapiBrowserHostPtr& host, const FB::JSAPIWeakPtr& api, bool auto_release = false);
50  void setAPI(const FB::JSAPIWeakPtr& api, const NpapiBrowserHostPtr& host);
51  FB::JSAPIPtr getAPI() const
52  {
53  FB::JSAPIPtr ptr(m_api.lock());
54  if (!ptr)
55  throw std::bad_cast();
56  return ptr;
57  }
58  const boost::shared_ptr<FB::ShareableReference< NPJavascriptObject > > getWeakReference() { return m_sharedRef; }
59 
60  virtual ~NPJavascriptObject(void);
61 
62  bool isValid() {
63  return m_valid && !m_api.expired() && !m_browser.expired();
64  }
65 
66  private:
67  NPJavascriptObject(NPP npp);
68 
69  protected:
70  void Invalidate();
71  bool HasMethod(NPIdentifier name);
72  bool Invoke(NPIdentifier name, const NPVariant *args, uint32_t argCount, NPVariant *result);
73  bool InvokeDefault(const NPVariant *args, uint32_t argCount, NPVariant *result);
74  bool HasProperty(NPIdentifier name);
75  bool GetProperty(NPIdentifier name, NPVariant *result);
76  bool SetProperty(NPIdentifier name, const NPVariant *value);
77  bool RemoveProperty(NPIdentifier name);
78  bool Enumeration(NPIdentifier **value, uint32_t *count);
79  bool Construct(const NPVariant *args, uint32_t argCount, NPVariant *result);
80 
81  public:
82  // Static methods referenced in the NPClass
83  static NPObject *_Allocate(NPP npp, NPClass *aClass);
84  static void _Deallocate(NPObject *npobj);
85  static void _Invalidate(NPObject *npobj);
86  static bool _HasMethod(NPObject *npobj, NPIdentifier name);
87  static bool _Invoke(NPObject *npobj, NPIdentifier name, const NPVariant *args, uint32_t argCount, NPVariant *result);
88  static bool _InvokeDefault(NPObject *npobj, const NPVariant *args, uint32_t argCount, NPVariant *result);
89  static bool _HasProperty(NPObject *npobj, NPIdentifier name);
90  static bool _GetProperty(NPObject *npobj, NPIdentifier name, NPVariant *result);
91  static bool _SetProperty(NPObject *npobj, NPIdentifier name, const NPVariant *value);
92  static bool _RemoveProperty(NPObject *npobj, NPIdentifier name);
93  static bool _Enumeration(NPObject *npobj, NPIdentifier **value, uint32_t *count);
94  static bool _Construct(NPObject *npobj, const NPVariant *args, uint32_t argCount, NPVariant *result);
95 
96  public:
97  FB_FORWARD_PTR(NPO_addEventListener);
98  FB_FORWARD_PTR(NPO_removeEventListener);
99  FB_FORWARD_PTR(NPO_getLastException);
100  friend class NPO_addEventListener;
101  friend class NPO_removeEventListener;
102  NPO_addEventListenerPtr m_addEventFunc;
103  NPO_removeEventListenerPtr m_removeEventFunc;
104  NPO_getLastExceptionPtr m_getLastExceptionFunc;
105 
106  class NPO_addEventListener : public FB::JSFunction
107  {
108  public:
109  NPO_addEventListener(NPJavascriptObject* ptr)
110  : FB::JSFunction(FB::JSAPIPtr(), "attachEvent", SecurityScope_Public), obj(ptr) { }
111  FB::variant exec(const std::vector<variant>& args);
112  private:
113  NPJavascriptObject* obj;
114  };
115  class NPO_removeEventListener : public FB::JSFunction
116  {
117  public:
118  NPO_removeEventListener(NPJavascriptObject* ptr)
119  : FB::JSFunction(FB::JSAPIPtr(), "detachEvent", SecurityScope_Public), obj(ptr) { }
120  FB::variant exec(const std::vector<variant>& args);
121  private:
122  NPJavascriptObject* obj;
123  };
124  class NPO_getLastException : public FB::JSFunction
125  {
126  public:
127  NPO_getLastException(NPJavascriptObject* ptr)
128  : FB::JSFunction(FB::JSAPIPtr(), "getLastException", SecurityScope_Public), obj(ptr) { }
129  FB::variant exec(const FB::VariantList& args) { return m_msg; }
130  void setMessage(const FB::variant& msg) { m_msg = msg; }
131  private:
132  NPJavascriptObject* obj;
133  static FB::variant m_msg;
134  };
135  };
136 
137 }; };
138 
139 #endif
140 
Accepts any datatype, used in all interactions with javascript. Provides tools for getting back out t...
Definition: variant.h:198
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
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