OLD | NEW |
| (Empty) |
1 // Copyright 2010 The Native Client Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can | |
3 // be found in the LICENSE file. | |
4 | |
5 #ifndef C_SALT_NPAPI_BROWSER_BINDING_H_ | |
6 #define C_SALT_NPAPI_BROWSER_BINDING_H_ | |
7 | |
8 #include <nacl/nacl_npapi.h> | |
9 #include <nacl/npruntime.h> | |
10 | |
11 #include <string> | |
12 | |
13 #include "boost/shared_ptr.hpp" | |
14 #include "c_salt/instance.h" | |
15 #include "c_salt/npapi/scoped_npid_to_string_converter.h" | |
16 #include "c_salt/npapi/variant_converter.h" | |
17 #include "c_salt/scripting_bridge_ptrs.h" | |
18 #include "c_salt/scripting_bridge.h" | |
19 | |
20 namespace c_salt { | |
21 | |
22 namespace npapi { | |
23 | |
24 // A thin wrapper that owns the ScriptingBridge class. This is necessary | |
25 // because the NPObject layout has to be preserved, and it cannot have things | |
26 // like a vtable inserted into it. | |
27 class BrowserBinding : public NPObject { | |
28 public: | |
29 explicit BrowserBinding(NPP npp) | |
30 : npp_(npp), | |
31 scripting_bridge_(new ScriptingBridge(this)), | |
32 variant_converter_(npp) {} | |
33 // The dtor *cannot* be virtual because this object must preserve NPObject's | |
34 // POD memory layout. | |
35 ~BrowserBinding() {} | |
36 | |
37 // Factory method to create a browser binding. This asks the browser to | |
38 // create the proxy object via NPN_CreateObject() that represents this | |
39 // instance. Calling this causes the browser to call the NPAPI Allocate() | |
40 // function, which then calls the ctor for this class. | |
41 // This is a synchronous call to the browser. Memory has been allocated | |
42 // and ctors called by the time it returns. | |
43 static BrowserBinding* CreateBrowserBinding(const c_salt::Instance& instance); | |
44 | |
45 // Bump the retain count of the proxy object in the browser. | |
46 void Retain(); | |
47 | |
48 const NPP& npp() const { | |
49 return npp_; | |
50 } | |
51 | |
52 SharedScriptingBridge scripting_bridge() { | |
53 return scripting_bridge_; | |
54 } | |
55 | |
56 private: | |
57 // NPAPI support methods; the browser calls these on scriptable objects. | |
58 bool HasMethod(NPIdentifier name) const; | |
59 void Invalidate(); | |
60 bool Invoke(NPIdentifier name, | |
61 const NPVariant* args, | |
62 uint32_t arg_count, | |
63 NPVariant* return_value); | |
64 bool HasProperty(NPIdentifier name) const; | |
65 bool GetProperty(NPIdentifier name, NPVariant* return_value) const; | |
66 bool SetProperty(NPIdentifier name, const NPVariant& np_value); | |
67 bool RemoveProperty(NPIdentifier name); | |
68 | |
69 // These are the free functions that the browser actually calls. They are | |
70 // all simple wrappers to the above NPAPI support methods. | |
71 friend void Invalidate(NPObject* object); | |
72 friend bool Invoke(NPObject* object, NPIdentifier name, | |
73 const NPVariant* args, | |
74 uint32_t arg_count, | |
75 NPVariant* return_value); | |
76 friend bool HasMethod(NPObject* object, NPIdentifier name); | |
77 friend bool HasProperty(NPObject* object, NPIdentifier name); | |
78 friend bool GetProperty(NPObject* object, | |
79 NPIdentifier name, | |
80 NPVariant* result); | |
81 friend bool SetProperty(NPObject* object, | |
82 NPIdentifier name, | |
83 const NPVariant* value); | |
84 friend bool RemoveProperty(NPObject* object, NPIdentifier name); | |
85 | |
86 NPP npp_; | |
87 SharedScriptingBridge scripting_bridge_; | |
88 VariantConverter variant_converter_; | |
89 }; | |
90 | |
91 } // namespace npapi | |
92 } // namespace c_salt | |
93 | |
94 #endif // C_SALT_NPAPI_BROWSER_BINDING_H_ | |
OLD | NEW |