OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2011 (c) The Native Client Authors. All rights reserved. | |
3 * Use of this source code is governed by a BSD-style license that can be | |
4 * found in the LICENSE file. | |
5 */ | |
6 | |
7 | |
8 // The browser scriptable container class. The methods on this class | |
9 // are defined in the specific API directories. | |
10 | |
11 #ifndef NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_SCRIPTABLE_HANDLE_H_ | |
12 #define NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_SCRIPTABLE_HANDLE_H_ | |
13 | |
14 #include "native_client/src/trusted/plugin/scriptable_handle.h" | |
15 | |
16 #include <stdio.h> | |
17 #include <string.h> | |
18 | |
19 #include <set> | |
20 #include <string> | |
21 #include <vector> | |
22 | |
23 #include "native_client/src/include/checked_cast.h" | |
24 #include "native_client/src/include/nacl_macros.h" | |
25 #include "native_client/src/include/nacl_string.h" | |
26 #include "native_client/src/include/portability.h" | |
27 #include "native_client/src/trusted/plugin/utility.h" | |
28 #include "ppapi/cpp/dev/scriptable_object_deprecated.h" | |
29 #include "ppapi/cpp/private/var_private.h" | |
30 | |
31 struct NaClDesc; | |
32 | |
33 namespace plugin { | |
34 | |
35 // Forward declarations for externals. | |
36 class DescBasedHandle; | |
37 class Plugin; | |
38 | |
39 // ScriptableHandle encapsulates objects that are scriptable from the browser. | |
40 class ScriptableHandle : public pp::deprecated::ScriptableObject { | |
41 public: | |
42 // Factory methods for creation. | |
43 static ScriptableHandle* NewPlugin(Plugin* plugin); | |
44 static ScriptableHandle* NewDescHandle(DescBasedHandle* desc_handle); | |
45 | |
46 // If not NULL, this var should be reused to pass this object to the browser. | |
47 pp::VarPrivate* var() { return var_; } | |
48 | |
49 // Check that a pointer is to a validly created ScriptableHandle. | |
50 static bool is_valid(const ScriptableHandle* handle); | |
51 static void Unref(ScriptableHandle** handle); | |
52 | |
53 // Get the contained plugin object. NULL if this contains a descriptor. | |
54 Plugin* plugin() const { return plugin_; } | |
55 | |
56 // Get the contained descriptor object. NULL if this contains a plugin. | |
57 // OBSOLETE -- this support is only needed for SRPC descriptor passing. | |
58 // TODO(polina): Remove this support when SRPC descriptor passing is removed. | |
59 DescBasedHandle* desc_handle() const { return desc_handle_; } | |
60 | |
61 // This function is called when we are about to share the object owned by the | |
62 // plugin with the browser. Since reference counting on the browser side is | |
63 // handled via pp::Var's, we create the var() here if not created already. | |
64 ScriptableHandle* AddRef(); | |
65 // Remove a browser reference to this object. | |
66 // Delete the object when the ref count becomes 0. | |
67 // If var() is set, we delete it. Otherwise, we delete the object itself. | |
68 // Therefore, this cannot be called more than once. | |
69 void Unref(); | |
70 | |
71 // ------ Methods inherited from pp::deprecated::ScriptableObject: | |
72 | |
73 // Returns true for preloaded NaCl Plugin properties. | |
74 // Does not set |exception|. | |
75 virtual bool HasProperty(const pp::Var& name, pp::Var* exception); | |
76 // Returns true for preloaded NaCl Plugin methods and SRPC methods exported | |
77 // from a NaCl module. Does not set |exception|. | |
78 virtual bool HasMethod(const pp::Var& name, pp::Var* exception); | |
79 | |
80 // Gets the value of a preloaded NaCl Plugin property. | |
81 // Sets |exception| on failure. | |
82 virtual pp::Var GetProperty(const pp::Var& name, pp::Var* exception); | |
83 // Sets the value of a preloaded NaCl Plugin property. | |
84 // Does not add new properties. Sets |exception| of failure. | |
85 virtual void SetProperty(const pp::Var& name, const pp::Var& value, | |
86 pp::Var* exception); | |
87 // Set |exception| to indicate that property removal is not supported. | |
88 virtual void RemoveProperty(const pp::Var& name, pp::Var* exception); | |
89 // Returns a list of all preloaded NaCl Plugin |properties|. | |
90 // Does not set |exception|. | |
91 virtual void GetAllPropertyNames(std::vector<pp::Var>* properties, | |
92 pp::Var* exception); | |
93 | |
94 // Calls preloaded NaCl Plugin methods or SRPC methods exported from | |
95 // a NaCl module. Sets |exception| on failure. | |
96 virtual pp::Var Call(const pp::Var& name, const std::vector<pp::Var>& args, | |
97 pp::Var* exception); | |
98 | |
99 // Sets |exception| to indicate that constructor is not supported. | |
100 virtual pp::Var Construct(const std::vector<pp::Var>& args, | |
101 pp::Var* exception); | |
102 | |
103 private: | |
104 NACL_DISALLOW_COPY_AND_ASSIGN(ScriptableHandle); | |
105 // Prevent construction from outside the class: must use factory New() | |
106 // method instead. | |
107 explicit ScriptableHandle(Plugin* plugin); | |
108 explicit ScriptableHandle(DescBasedHandle* desc_handle); | |
109 // This will be called when both the plugin and the browser clear all | |
110 // references to this object. | |
111 virtual ~ScriptableHandle(); | |
112 | |
113 // When we pass the object owned by the plugin to the browser, we need to wrap | |
114 // it in a pp::VarPrivate, which also registers the object with the browser | |
115 // for refcounting. It must be registered only once with all other var | |
116 // references being copies of the original one. Thus, we record the | |
117 // pp::VarPrivate here and reuse it when satisfiying additional browser | |
118 // requests. This way we also ensure that when the browser clears its | |
119 // references, this object does not get deallocated while we still hold ours. | |
120 // This is never set for objects that are not shared with the browser nor for | |
121 // objects created during SRPC calls as they are taken over by the browser on | |
122 // return. | |
123 pp::VarPrivate* var_; | |
124 | |
125 // We should have no more than one internal plugin owner for this object, | |
126 // and only that owner should call Unref(). To CHECK for that keep a counter. | |
127 int num_unref_calls_; | |
128 | |
129 // The contained plugin object. | |
130 Plugin* plugin_; | |
131 // OBSOLETE -- this support is only needed for SRPC descriptor passing. | |
132 // TODO(polina): Remove this support when SRPC descriptor passing is removed. | |
133 // The contained descriptor handle object. | |
134 DescBasedHandle* desc_handle_; | |
135 }; | |
136 | |
137 } // namespace plugin | |
138 | |
139 #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_SCRIPTABLE_HANDLE_H_ | |
OLD | NEW |