OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef WEBKIT_PLUGINS_PPAPI_PLUGIN_MODULE_H_ | |
6 #define WEBKIT_PLUGINS_PPAPI_PLUGIN_MODULE_H_ | |
7 | |
8 #include <map> | |
9 #include <set> | |
10 #include <string> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/files/file_path.h" | |
14 #include "base/memory/ref_counted.h" | |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "base/memory/weak_ptr.h" | |
17 #include "base/native_library.h" | |
18 #include "base/process.h" | |
19 #include "ppapi/c/pp_bool.h" | |
20 #include "ppapi/c/pp_instance.h" | |
21 #include "ppapi/c/pp_module.h" | |
22 #include "ppapi/c/ppb.h" | |
23 #include "ppapi/c/ppb_core.h" | |
24 #include "ppapi/c/private/ppb_instance_private.h" | |
25 #include "ppapi/shared_impl/ppapi_permissions.h" | |
26 #include "webkit/plugins/ppapi/plugin_delegate.h" | |
27 #include "webkit/plugins/webkit_plugins_export.h" | |
28 | |
29 typedef void* NPIdentifier; | |
30 | |
31 namespace base { | |
32 class FilePath; | |
33 } | |
34 | |
35 namespace content { | |
36 class RenderView; | |
37 } | |
38 | |
39 namespace ppapi { | |
40 class CallbackTracker; | |
41 class WebKitForwarding; | |
42 } // namespace ppapi | |
43 | |
44 namespace WebKit { | |
45 class WebPluginContainer; | |
46 } // namespace WebKit | |
47 | |
48 namespace webkit { | |
49 namespace ppapi { | |
50 | |
51 class PluginDelegate; | |
52 class PluginInstanceImpl; | |
53 | |
54 // Represents one plugin library loaded into one renderer. This library may | |
55 // have multiple instances. | |
56 // | |
57 // Note: to get from a PP_Instance to a PluginInstance*, use the | |
58 // ResourceTracker. | |
59 class WEBKIT_PLUGINS_EXPORT PluginModule : | |
60 public base::RefCounted<PluginModule>, | |
61 public base::SupportsWeakPtr<PluginModule> { | |
62 public: | |
63 typedef const void* (*GetInterfaceFunc)(const char*); | |
64 typedef int (*PPP_InitializeModuleFunc)(PP_Module, PPB_GetInterface); | |
65 typedef void (*PPP_ShutdownModuleFunc)(); | |
66 | |
67 struct EntryPoints { | |
68 // This structure is POD, with the constructor initializing to NULL. | |
69 WEBKIT_PLUGINS_EXPORT EntryPoints(); | |
70 | |
71 GetInterfaceFunc get_interface; | |
72 PPP_InitializeModuleFunc initialize_module; | |
73 PPP_ShutdownModuleFunc shutdown_module; // Optional, may be NULL. | |
74 }; | |
75 | |
76 // Allows the embedder to associate a class with this module. This is opaque | |
77 // from the PluginModule's perspective (see Set/GetEmbedderState below) but | |
78 // the module is in charge of deleting the class. | |
79 class EmbedderState { | |
80 public: | |
81 virtual ~EmbedderState() {} | |
82 }; | |
83 | |
84 typedef std::set<PluginInstanceImpl*> PluginInstanceSet; | |
85 | |
86 // You must call one of the Init functions after the constructor to create a | |
87 // module of the type you desire. | |
88 // | |
89 // The module lifetime delegate is a non-owning pointer that must outlive | |
90 // all plugin modules. In practice it will be a global singleton that | |
91 // tracks which modules are alive. | |
92 PluginModule(const std::string& name, | |
93 const base::FilePath& path, | |
94 PluginDelegate::ModuleLifetime* lifetime_delegate, | |
95 const ::ppapi::PpapiPermissions& perms); | |
96 | |
97 ~PluginModule(); | |
98 | |
99 // Sets the given class as being associated with this module. It will be | |
100 // deleted when the module is destroyed. You can only set it once, subsequent | |
101 // sets will assert. | |
102 // | |
103 // See EmbedderState above for more. | |
104 void SetEmbedderState(scoped_ptr<EmbedderState> state); | |
105 EmbedderState* GetEmbedderState(); | |
106 | |
107 // Initializes this module as an internal plugin with the given entrypoints. | |
108 // This is used for "plugins" compiled into Chrome. Returns true on success. | |
109 // False means that the plugin can not be used. | |
110 bool InitAsInternalPlugin(const EntryPoints& entry_points); | |
111 | |
112 // Initializes this module using the given library path as the plugin. | |
113 // Returns true on success. False means that the plugin can not be used. | |
114 bool InitAsLibrary(const base::FilePath& path); | |
115 | |
116 // Initializes this module for the given out of process proxy. This takes | |
117 // ownership of the given pointer, even in the failure case. | |
118 void InitAsProxied(PluginDelegate::OutOfProcessProxy* out_of_process_proxy); | |
119 | |
120 // Creates a new module for an external plugin instance that will be using the | |
121 // IPC proxy. We can't use the existing module, or new instances of the plugin | |
122 // can't be created. | |
123 scoped_refptr<PluginModule> CreateModuleForExternalPluginInstance(); | |
124 | |
125 // Initializes the external plugin module for the out of process proxy. | |
126 // InitAsProxied must be called before calling InitAsProxiedExternalPlugin. | |
127 // Returns a result code indicating whether the proxy started successfully or | |
128 // there was an error. | |
129 PP_ExternalPluginResult InitAsProxiedExternalPlugin( | |
130 PluginInstanceImpl* instance); | |
131 | |
132 bool IsProxied() const; | |
133 | |
134 // Returns the peer process ID if the plugin is running out of process; | |
135 // returns |base::kNullProcessId| otherwise. | |
136 base::ProcessId GetPeerProcessId(); | |
137 | |
138 // Returns the plugin child process ID if the plugin is running out of | |
139 // process. Returns 0 otherwise. This is the ID that the browser process uses | |
140 // to idetify the child process for the plugin. This isn't directly useful | |
141 // from our process (the renderer) except in messages to the browser to | |
142 // disambiguate plugins. | |
143 int GetPluginChildId(); | |
144 | |
145 static const PPB_Core* GetCore(); | |
146 | |
147 // Returns a pointer to the local GetInterface function for retrieving | |
148 // PPB interfaces. | |
149 static GetInterfaceFunc GetLocalGetInterfaceFunc(); | |
150 | |
151 // Returns whether an interface is supported. This method can be called from | |
152 // the browser process and used for interface matching before plugin | |
153 // registration. | |
154 // NOTE: those custom interfaces provided by PpapiInterfaceFactoryManager | |
155 // will not be considered when called on the browser process. | |
156 static bool SupportsInterface(const char* name); | |
157 | |
158 // Returns the module handle. This may be used before Init() is called (the | |
159 // proxy needs this information to set itself up properly). | |
160 PP_Module pp_module() const { return pp_module_; } | |
161 | |
162 const std::string& name() const { return name_; } | |
163 const base::FilePath& path() const { return path_; } | |
164 const ::ppapi::PpapiPermissions& permissions() const { return permissions_; } | |
165 | |
166 PluginInstanceImpl* CreateInstance(PluginDelegate* delegate, | |
167 content::RenderView* render_view, | |
168 WebKit::WebPluginContainer* container, | |
169 const GURL& plugin_url); | |
170 | |
171 // Returns "some" plugin instance associated with this module. This is not | |
172 // guaranteed to be any one in particular. This is normally used to execute | |
173 // callbacks up to the browser layer that are not inherently per-instance, | |
174 // but the delegate lives only on the plugin instance so we need one of them. | |
175 PluginInstanceImpl* GetSomeInstance() const; | |
176 | |
177 const PluginInstanceSet& GetAllInstances() const { return instances_; } | |
178 | |
179 // Calls the plugin's GetInterface and returns the given interface pointer, | |
180 // which could be NULL. | |
181 const void* GetPluginInterface(const char* name) const; | |
182 | |
183 // This module is associated with a set of instances. The PluginInstance | |
184 // object declares its association with this module in its destructor and | |
185 // releases us in its destructor. | |
186 void InstanceCreated(PluginInstanceImpl* instance); | |
187 void InstanceDeleted(PluginInstanceImpl* instance); | |
188 | |
189 scoped_refptr< ::ppapi::CallbackTracker> GetCallbackTracker(); | |
190 | |
191 // Called when running out of process and the plugin crashed. This will | |
192 // release relevant resources and update all affected instances. | |
193 void PluginCrashed(); | |
194 | |
195 bool is_in_destructor() const { return is_in_destructor_; } | |
196 bool is_crashed() const { return is_crashed_; } | |
197 | |
198 // Reserves the given instance is unique within the plugin, checking for | |
199 // collisions. See PPB_Proxy_Private for more information. | |
200 // | |
201 // The setter will set the callback which is set up when the proxy | |
202 // initializes. The Reserve function will call the previously set callback if | |
203 // it exists to validate the ID. If the callback has not been set (such as | |
204 // for in-process plugins), the Reserve function will assume that the ID is | |
205 // usable and will return true. | |
206 void SetReserveInstanceIDCallback( | |
207 PP_Bool (*reserve)(PP_Module, PP_Instance)); | |
208 bool ReserveInstanceID(PP_Instance instance); | |
209 | |
210 // These should only be called from the main thread. | |
211 void SetBroker(PluginDelegate::Broker* broker); | |
212 PluginDelegate::Broker* GetBroker(); | |
213 | |
214 // In production we purposely leak the HostGlobals object but in unittest | |
215 // code, this can interfere with subsequent tests. This deletes the | |
216 // existing HostGlobals. A new one will be constructed when a PluginModule is | |
217 // instantiated. | |
218 static void ResetHostGlobalsForTest(); | |
219 | |
220 private: | |
221 // Calls the InitializeModule entrypoint. The entrypoint must have been | |
222 // set and the plugin must not be out of process (we don't maintain | |
223 // entrypoints in that case). | |
224 bool InitializeModule(const EntryPoints& entry_points); | |
225 | |
226 // Note: This may be null. | |
227 PluginDelegate::ModuleLifetime* lifetime_delegate_; | |
228 | |
229 // See EmbedderState above. | |
230 scoped_ptr<EmbedderState> embedder_state_; | |
231 | |
232 // Tracker for completion callbacks, used mainly to ensure that all callbacks | |
233 // are properly aborted on module shutdown. | |
234 scoped_refptr< ::ppapi::CallbackTracker> callback_tracker_; | |
235 | |
236 PP_Module pp_module_; | |
237 | |
238 // True when we're running in the destructor. This allows us to write some | |
239 // assertions. | |
240 bool is_in_destructor_; | |
241 | |
242 // True if the plugin is running out-of-process and has crashed. | |
243 bool is_crashed_; | |
244 | |
245 // Manages the out of process proxy interface. The presence of this | |
246 // pointer indicates that the plugin is running out of process and that the | |
247 // entry_points_ aren't valid. | |
248 scoped_ptr<PluginDelegate::OutOfProcessProxy> out_of_process_proxy_; | |
249 | |
250 // Non-owning pointer to the broker for this plugin module, if one exists. | |
251 // It is populated and cleared in the main thread. | |
252 PluginDelegate::Broker* broker_; | |
253 | |
254 // Holds a reference to the base::NativeLibrary handle if this PluginModule | |
255 // instance wraps functions loaded from a library. Can be NULL. If | |
256 // |library_| is non-NULL, PluginModule will attempt to unload the library | |
257 // during destruction. | |
258 base::NativeLibrary library_; | |
259 | |
260 // Contains pointers to the entry points of the actual plugin implementation. | |
261 // These will be NULL for out-of-process plugins, which is indicated by the | |
262 // presence of the out_of_process_proxy_ value. | |
263 EntryPoints entry_points_; | |
264 | |
265 // The name and file location of the module. | |
266 const std::string name_; | |
267 const base::FilePath path_; | |
268 | |
269 ::ppapi::PpapiPermissions permissions_; | |
270 | |
271 // Non-owning pointers to all instances associated with this module. When | |
272 // there are no more instances, this object should be deleted. | |
273 PluginInstanceSet instances_; | |
274 | |
275 PP_Bool (*reserve_instance_id_)(PP_Module, PP_Instance); | |
276 | |
277 DISALLOW_COPY_AND_ASSIGN(PluginModule); | |
278 }; | |
279 | |
280 } // namespace ppapi | |
281 } // namespace webkit | |
282 | |
283 #endif // WEBKIT_PLUGINS_PPAPI_PLUGIN_MODULE_H_ | |
OLD | NEW |