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.
5 #ifndef WEBKIT_PLUGINS_PPAPI_PLUGIN_MODULE_H_
6 #define WEBKIT_PLUGINS_PPAPI_PLUGIN_MODULE_H_
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_nacl_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"
29 typedef void* NPIdentifier
;
36 class CallbackTracker
;
37 class WebKitForwarding
;
41 class WebPluginContainer
;
50 // Represents one plugin library loaded into one renderer. This library may
51 // have multiple instances.
53 // Note: to get from a PP_Instance to a PluginInstance*, use the
55 class WEBKIT_PLUGINS_EXPORT PluginModule
:
56 public base::RefCounted
<PluginModule
>,
57 public base::SupportsWeakPtr
<PluginModule
> {
59 typedef const void* (*GetInterfaceFunc
)(const char*);
60 typedef int (*PPP_InitializeModuleFunc
)(PP_Module
, PPB_GetInterface
);
61 typedef void (*PPP_ShutdownModuleFunc
)();
64 // This structure is POD, with the constructor initializing to NULL.
65 WEBKIT_PLUGINS_EXPORT
EntryPoints();
67 GetInterfaceFunc get_interface
;
68 PPP_InitializeModuleFunc initialize_module
;
69 PPP_ShutdownModuleFunc shutdown_module
; // Optional, may be NULL.
72 // Allows the embedder to associate a class with this module. This is opaque
73 // from the PluginModule's perspective (see Set/GetEmbedderState below) but
74 // the module is in charge of deleting the class.
77 virtual ~EmbedderState() {}
80 typedef std::set
<PluginInstance
*> PluginInstanceSet
;
82 // You must call one of the Init functions after the constructor to create a
83 // module of the type you desire.
85 // The module lifetime delegate is a non-owning pointer that must outlive
86 // all plugin modules. In practice it will be a global singleton that
87 // tracks which modules are alive.
88 PluginModule(const std::string
& name
,
89 const base::FilePath
& path
,
90 PluginDelegate::ModuleLifetime
* lifetime_delegate
,
91 const ::ppapi::PpapiPermissions
& perms
);
95 // Sets the given class as being associated with this module. It will be
96 // deleted when the module is destroyed. You can only set it once, subsequent
99 // See EmbedderState above for more.
100 void SetEmbedderState(scoped_ptr
<EmbedderState
> state
);
101 EmbedderState
* GetEmbedderState();
103 // Initializes this module as an internal plugin with the given entrypoints.
104 // This is used for "plugins" compiled into Chrome. Returns true on success.
105 // False means that the plugin can not be used.
106 bool InitAsInternalPlugin(const EntryPoints
& entry_points
);
108 // Initializes this module using the given library path as the plugin.
109 // Returns true on success. False means that the plugin can not be used.
110 bool InitAsLibrary(const base::FilePath
& path
);
112 // Initializes this module for the given out of process proxy. This takes
113 // ownership of the given pointer, even in the failure case.
114 void InitAsProxied(PluginDelegate::OutOfProcessProxy
* out_of_process_proxy
);
116 // Creates a new module for a NaCl instance that will be using the IPC proxy.
117 // We can't use the existing module, or new instances of the plugin can't
119 scoped_refptr
<PluginModule
> CreateModuleForNaClInstance();
121 // Initializes the NaCl module for the out of process proxy. InitAsProxied
122 // must be called before calling InitAsProxiedNaCl. Returns a NaCl result code
123 // indicating whether the proxy started successfully or there was an error.
124 PP_NaClResult
InitAsProxiedNaCl(PluginInstance
* instance
);
126 bool IsProxied() const;
128 // Returns the peer process ID if the plugin is running out of process;
129 // returns |base::kNullProcessId| otherwise.
130 base::ProcessId
GetPeerProcessId();
132 static const PPB_Core
* GetCore();
134 // Returns a pointer to the local GetInterface function for retrieving
136 static GetInterfaceFunc
GetLocalGetInterfaceFunc();
138 // Returns whether an interface is supported. This method can be called from
139 // the browser process and used for interface matching before plugin
141 // NOTE: those custom interfaces provided by PpapiInterfaceFactoryManager
142 // will not be considered when called on the browser process.
143 static bool SupportsInterface(const char* name
);
145 // Returns the module handle. This may be used before Init() is called (the
146 // proxy needs this information to set itself up properly).
147 PP_Module
pp_module() const { return pp_module_
; }
149 const std::string
& name() const { return name_
; }
150 const base::FilePath
& path() const { return path_
; }
151 const ::ppapi::PpapiPermissions
& permissions() const { return permissions_
; }
153 PluginInstance
* CreateInstance(PluginDelegate
* delegate
,
154 WebKit::WebPluginContainer
* container
,
155 const GURL
& plugin_url
);
157 // Returns "some" plugin instance associated with this module. This is not
158 // guaranteed to be any one in particular. This is normally used to execute
159 // callbacks up to the browser layer that are not inherently per-instance,
160 // but the delegate lives only on the plugin instance so we need one of them.
161 PluginInstance
* GetSomeInstance() const;
163 const PluginInstanceSet
& GetAllInstances() const { return instances_
; }
165 // Calls the plugin's GetInterface and returns the given interface pointer,
166 // which could be NULL.
167 const void* GetPluginInterface(const char* name
) const;
169 // This module is associated with a set of instances. The PluginInstance
170 // object declares its association with this module in its destructor and
171 // releases us in its destructor.
172 void InstanceCreated(PluginInstance
* instance
);
173 void InstanceDeleted(PluginInstance
* instance
);
175 scoped_refptr
< ::ppapi::CallbackTracker
> GetCallbackTracker();
177 // Called when running out of process and the plugin crashed. This will
178 // release relevant resources and update all affected instances.
179 void PluginCrashed();
181 bool is_in_destructor() const { return is_in_destructor_
; }
182 bool is_crashed() const { return is_crashed_
; }
184 // Reserves the given instance is unique within the plugin, checking for
185 // collisions. See PPB_Proxy_Private for more information.
187 // The setter will set the callback which is set up when the proxy
188 // initializes. The Reserve function will call the previously set callback if
189 // it exists to validate the ID. If the callback has not been set (such as
190 // for in-process plugins), the Reserve function will assume that the ID is
191 // usable and will return true.
192 void SetReserveInstanceIDCallback(
193 PP_Bool (*reserve
)(PP_Module
, PP_Instance
));
194 bool ReserveInstanceID(PP_Instance instance
);
196 // These should only be called from the main thread.
197 void SetBroker(PluginDelegate::Broker
* broker
);
198 PluginDelegate::Broker
* GetBroker();
201 // Calls the InitializeModule entrypoint. The entrypoint must have been
202 // set and the plugin must not be out of process (we don't maintain
203 // entrypoints in that case).
204 bool InitializeModule(const EntryPoints
& entry_points
);
206 // Note: This may be null.
207 PluginDelegate::ModuleLifetime
* lifetime_delegate_
;
209 // See EmbedderState above.
210 scoped_ptr
<EmbedderState
> embedder_state_
;
212 // Tracker for completion callbacks, used mainly to ensure that all callbacks
213 // are properly aborted on module shutdown.
214 scoped_refptr
< ::ppapi::CallbackTracker
> callback_tracker_
;
216 PP_Module pp_module_
;
218 // True when we're running in the destructor. This allows us to write some
220 bool is_in_destructor_
;
222 // True if the plugin is running out-of-process and has crashed.
225 // Manages the out of process proxy interface. The presence of this
226 // pointer indicates that the plugin is running out of process and that the
227 // entry_points_ aren't valid.
228 scoped_ptr
<PluginDelegate::OutOfProcessProxy
> out_of_process_proxy_
;
230 // Non-owning pointer to the broker for this plugin module, if one exists.
231 // It is populated and cleared in the main thread.
232 PluginDelegate::Broker
* broker_
;
234 // Holds a reference to the base::NativeLibrary handle if this PluginModule
235 // instance wraps functions loaded from a library. Can be NULL. If
236 // |library_| is non-NULL, PluginModule will attempt to unload the library
237 // during destruction.
238 base::NativeLibrary library_
;
240 // Contains pointers to the entry points of the actual plugin implementation.
241 // These will be NULL for out-of-process plugins, which is indicated by the
242 // presence of the out_of_process_proxy_ value.
243 EntryPoints entry_points_
;
245 // The name and file location of the module.
246 const std::string name_
;
247 const base::FilePath path_
;
249 ::ppapi::PpapiPermissions permissions_
;
251 // Non-owning pointers to all instances associated with this module. When
252 // there are no more instances, this object should be deleted.
253 PluginInstanceSet instances_
;
255 PP_Bool (*reserve_instance_id_
)(PP_Module
, PP_Instance
);
257 DISALLOW_COPY_AND_ASSIGN(PluginModule
);
261 } // namespace webkit
263 #endif // WEBKIT_PLUGINS_PPAPI_PLUGIN_MODULE_H_