Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / pepper / plugin_module.cc
blobd07c3a8a3bb03741b72063112c64c3a323b8c013
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 #include "content/renderer/pepper/plugin_module.h"
7 #include <set>
9 #include "base/bind.h"
10 #include "base/command_line.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/message_loop/message_loop_proxy.h"
15 #include "base/time/time.h"
16 #include "build/build_config.h"
17 #include "content/common/view_messages.h"
18 #include "content/public/renderer/content_renderer_client.h"
19 #include "content/renderer/pepper/host_dispatcher_wrapper.h"
20 #include "content/renderer/pepper/host_globals.h"
21 #include "content/renderer/pepper/pepper_hung_plugin_filter.h"
22 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
23 #include "content/renderer/pepper/pepper_plugin_registry.h"
24 #include "content/renderer/pepper/ppapi_preferences_builder.h"
25 #include "content/renderer/pepper/ppb_image_data_impl.h"
26 #include "content/renderer/pepper/ppb_proxy_impl.h"
27 #include "content/renderer/pepper/ppb_scrollbar_impl.h"
28 #include "content/renderer/pepper/ppb_var_deprecated_impl.h"
29 #include "content/renderer/pepper/ppb_video_decoder_impl.h"
30 #include "content/renderer/pepper/renderer_ppapi_host_impl.h"
31 #include "content/renderer/render_view_impl.h"
32 #include "ppapi/c/dev/ppb_audio_input_dev.h"
33 #include "ppapi/c/dev/ppb_buffer_dev.h"
34 #include "ppapi/c/dev/ppb_char_set_dev.h"
35 #include "ppapi/c/dev/ppb_crypto_dev.h"
36 #include "ppapi/c/dev/ppb_cursor_control_dev.h"
37 #include "ppapi/c/dev/ppb_device_ref_dev.h"
38 #include "ppapi/c/dev/ppb_file_chooser_dev.h"
39 #include "ppapi/c/dev/ppb_font_dev.h"
40 #include "ppapi/c/dev/ppb_gles_chromium_texture_mapping_dev.h"
41 #include "ppapi/c/dev/ppb_memory_dev.h"
42 #include "ppapi/c/dev/ppb_opengles2ext_dev.h"
43 #include "ppapi/c/dev/ppb_printing_dev.h"
44 #include "ppapi/c/dev/ppb_scrollbar_dev.h"
45 #include "ppapi/c/dev/ppb_text_input_dev.h"
46 #include "ppapi/c/dev/ppb_trace_event_dev.h"
47 #include "ppapi/c/dev/ppb_truetype_font_dev.h"
48 #include "ppapi/c/dev/ppb_url_util_dev.h"
49 #include "ppapi/c/dev/ppb_var_deprecated.h"
50 #include "ppapi/c/dev/ppb_video_capture_dev.h"
51 #include "ppapi/c/dev/ppb_video_decoder_dev.h"
52 #include "ppapi/c/dev/ppb_view_dev.h"
53 #include "ppapi/c/dev/ppb_widget_dev.h"
54 #include "ppapi/c/dev/ppb_zoom_dev.h"
55 #include "ppapi/c/pp_module.h"
56 #include "ppapi/c/pp_resource.h"
57 #include "ppapi/c/pp_var.h"
58 #include "ppapi/c/ppb_audio.h"
59 #include "ppapi/c/ppb_audio_buffer.h"
60 #include "ppapi/c/ppb_audio_config.h"
61 #include "ppapi/c/ppb_compositor.h"
62 #include "ppapi/c/ppb_compositor_layer.h"
63 #include "ppapi/c/ppb_console.h"
64 #include "ppapi/c/ppb_core.h"
65 #include "ppapi/c/ppb_file_io.h"
66 #include "ppapi/c/ppb_file_ref.h"
67 #include "ppapi/c/ppb_file_system.h"
68 #include "ppapi/c/ppb_fullscreen.h"
69 #include "ppapi/c/ppb_graphics_2d.h"
70 #include "ppapi/c/ppb_graphics_3d.h"
71 #include "ppapi/c/ppb_host_resolver.h"
72 #include "ppapi/c/ppb_image_data.h"
73 #include "ppapi/c/ppb_instance.h"
74 #include "ppapi/c/ppb_media_stream_audio_track.h"
75 #include "ppapi/c/ppb_media_stream_video_track.h"
76 #include "ppapi/c/ppb_messaging.h"
77 #include "ppapi/c/ppb_mouse_cursor.h"
78 #include "ppapi/c/ppb_mouse_lock.h"
79 #include "ppapi/c/ppb_net_address.h"
80 #include "ppapi/c/ppb_network_list.h"
81 #include "ppapi/c/ppb_network_monitor.h"
82 #include "ppapi/c/ppb_network_proxy.h"
83 #include "ppapi/c/ppb_opengles2.h"
84 #include "ppapi/c/ppb_tcp_socket.h"
85 #include "ppapi/c/ppb_text_input_controller.h"
86 #include "ppapi/c/ppb_udp_socket.h"
87 #include "ppapi/c/ppb_url_loader.h"
88 #include "ppapi/c/ppb_url_request_info.h"
89 #include "ppapi/c/ppb_url_response_info.h"
90 #include "ppapi/c/ppb_var.h"
91 #include "ppapi/c/ppb_var_array.h"
92 #include "ppapi/c/ppb_var_array_buffer.h"
93 #include "ppapi/c/ppb_var_dictionary.h"
94 #include "ppapi/c/ppb_video_decoder.h"
95 #include "ppapi/c/ppb_video_encoder.h"
96 #include "ppapi/c/ppb_video_frame.h"
97 #include "ppapi/c/ppb_view.h"
98 #include "ppapi/c/ppp.h"
99 #include "ppapi/c/ppp_instance.h"
100 #include "ppapi/c/private/ppb_camera_capabilities_private.h"
101 #include "ppapi/c/private/ppb_camera_device_private.h"
102 #include "ppapi/c/private/ppb_ext_crx_file_system_private.h"
103 #include "ppapi/c/private/ppb_file_io_private.h"
104 #include "ppapi/c/private/ppb_file_ref_private.h"
105 #include "ppapi/c/private/ppb_find_private.h"
106 #include "ppapi/c/private/ppb_flash.h"
107 #include "ppapi/c/private/ppb_flash_clipboard.h"
108 #include "ppapi/c/private/ppb_flash_device_id.h"
109 #include "ppapi/c/private/ppb_flash_drm.h"
110 #include "ppapi/c/private/ppb_flash_file.h"
111 #include "ppapi/c/private/ppb_flash_font_file.h"
112 #include "ppapi/c/private/ppb_flash_fullscreen.h"
113 #include "ppapi/c/private/ppb_flash_menu.h"
114 #include "ppapi/c/private/ppb_flash_message_loop.h"
115 #include "ppapi/c/private/ppb_flash_print.h"
116 #include "ppapi/c/private/ppb_host_resolver_private.h"
117 #include "ppapi/c/private/ppb_input_event_private.h"
118 #include "ppapi/c/private/ppb_instance_private.h"
119 #include "ppapi/c/private/ppb_isolated_file_system_private.h"
120 #include "ppapi/c/private/ppb_output_protection_private.h"
121 #include "ppapi/c/private/ppb_pdf.h"
122 #include "ppapi/c/private/ppb_proxy_private.h"
123 #include "ppapi/c/private/ppb_talk_private.h"
124 #include "ppapi/c/private/ppb_tcp_server_socket_private.h"
125 #include "ppapi/c/private/ppb_tcp_socket_private.h"
126 #include "ppapi/c/private/ppb_testing_private.h"
127 #include "ppapi/c/private/ppb_udp_socket_private.h"
128 #include "ppapi/c/private/ppb_uma_private.h"
129 #include "ppapi/c/private/ppb_video_destination_private.h"
130 #include "ppapi/c/private/ppb_video_source_private.h"
131 #include "ppapi/c/private/ppb_x509_certificate_private.h"
132 #include "ppapi/c/trusted/ppb_broker_trusted.h"
133 #include "ppapi/c/trusted/ppb_browser_font_trusted.h"
134 #include "ppapi/c/trusted/ppb_char_set_trusted.h"
135 #include "ppapi/c/trusted/ppb_file_chooser_trusted.h"
136 #include "ppapi/c/trusted/ppb_url_loader_trusted.h"
137 #include "ppapi/shared_impl/callback_tracker.h"
138 #include "ppapi/shared_impl/ppapi_preferences.h"
139 #include "ppapi/shared_impl/ppapi_switches.h"
140 #include "ppapi/shared_impl/ppb_input_event_shared.h"
141 #include "ppapi/shared_impl/ppb_opengles2_shared.h"
142 #include "ppapi/shared_impl/ppb_var_shared.h"
143 #include "ppapi/shared_impl/time_conversion.h"
144 #include "ppapi/thunk/enter.h"
145 #include "ppapi/thunk/ppb_graphics_2d_api.h"
146 #include "ppapi/thunk/thunk.h"
148 #if defined(OS_CHROMEOS)
149 #include "ppapi/c/private/ppb_platform_verification_private.h"
150 #endif
152 using ppapi::InputEventData;
153 using ppapi::PpapiGlobals;
154 using ppapi::TimeTicksToPPTimeTicks;
155 using ppapi::TimeToPPTime;
156 using ppapi::thunk::EnterResource;
157 using ppapi::thunk::PPB_Graphics2D_API;
158 using ppapi::thunk::PPB_InputEvent_API;
160 namespace content {
162 namespace {
164 // Global tracking info for PPAPI plugins. This is lazily created before the
165 // first plugin is allocated, and leaked on shutdown.
167 // Note that we don't want a Singleton here since destroying this object will
168 // try to free some stuff that requires WebKit, and Singletons are destroyed
169 // after WebKit.
170 // TODO(raymes): I'm not sure if it is completely necessary to leak the
171 // HostGlobals. Figure out the shutdown sequence and find a way to do this
172 // more elegantly.
173 HostGlobals* host_globals = NULL;
175 // Maintains all currently loaded plugin libs for validating PP_Module
176 // identifiers.
177 typedef std::set<PluginModule*> PluginModuleSet;
179 PluginModuleSet* GetLivePluginSet() {
180 CR_DEFINE_STATIC_LOCAL(PluginModuleSet, live_plugin_libs, ());
181 return &live_plugin_libs;
184 // PPB_Core --------------------------------------------------------------------
186 void AddRefResource(PP_Resource resource) {
187 PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(resource);
190 void ReleaseResource(PP_Resource resource) {
191 PpapiGlobals::Get()->GetResourceTracker()->ReleaseResource(resource);
194 PP_Time GetTime() { return TimeToPPTime(base::Time::Now()); }
196 PP_TimeTicks GetTickTime() {
197 return TimeTicksToPPTimeTicks(base::TimeTicks::Now());
200 void CallOnMainThread(int delay_in_msec,
201 PP_CompletionCallback callback,
202 int32_t result) {
203 if (callback.func) {
204 PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostDelayedTask(
205 FROM_HERE,
206 base::Bind(callback.func, callback.user_data, result),
207 base::TimeDelta::FromMilliseconds(delay_in_msec));
211 PP_Bool IsMainThread() {
212 return PP_FromBool(PpapiGlobals::Get()
213 ->GetMainThreadMessageLoop()
214 ->BelongsToCurrentThread());
217 const PPB_Core core_interface = {&AddRefResource, &ReleaseResource,
218 &GetTime, &GetTickTime,
219 &CallOnMainThread, &IsMainThread};
221 // PPB_Testing -----------------------------------------------------------------
223 PP_Bool ReadImageData(PP_Resource device_context_2d,
224 PP_Resource image,
225 const PP_Point* top_left) {
226 EnterResource<PPB_Graphics2D_API> enter(device_context_2d, true);
227 if (enter.failed())
228 return PP_FALSE;
229 return PP_FromBool(enter.object()->ReadImageData(image, top_left));
232 void RunMessageLoop(PP_Instance instance) {
233 base::MessageLoop::ScopedNestableTaskAllower allow(
234 base::MessageLoop::current());
235 base::MessageLoop::current()->Run();
238 void QuitMessageLoop(PP_Instance instance) {
239 base::MessageLoop::current()->QuitNow();
242 uint32_t GetLiveObjectsForInstance(PP_Instance instance_id) {
243 return HostGlobals::Get()->GetResourceTracker()->GetLiveObjectsForInstance(
244 instance_id);
247 PP_Bool IsOutOfProcess() { return PP_FALSE; }
249 void SimulateInputEvent(PP_Instance instance, PP_Resource input_event) {
250 PepperPluginInstanceImpl* plugin_instance =
251 host_globals->GetInstance(instance);
252 if (!plugin_instance)
253 return;
255 EnterResource<PPB_InputEvent_API> enter(input_event, false);
256 if (enter.failed())
257 return;
259 const InputEventData& input_event_data = enter.object()->GetInputEventData();
260 plugin_instance->SimulateInputEvent(input_event_data);
263 PP_Var GetDocumentURL(PP_Instance instance, PP_URLComponents_Dev* components) {
264 PepperPluginInstanceImpl* plugin_instance =
265 host_globals->GetInstance(instance);
266 if (!plugin_instance)
267 return PP_MakeUndefined();
268 return plugin_instance->GetDocumentURL(instance, components);
271 uint32_t GetLiveVars(PP_Var live_vars[], uint32_t array_size) {
272 std::vector<PP_Var> vars =
273 PpapiGlobals::Get()->GetVarTracker()->GetLiveVars();
274 for (size_t i = 0u;
275 i < std::min(static_cast<size_t>(array_size), vars.size());
276 ++i)
277 live_vars[i] = vars[i];
278 return vars.size();
281 void SetMinimumArrayBufferSizeForShmem(PP_Instance /*instance*/,
282 uint32_t /*threshold*/) {
283 // Does nothing. Not needed in-process.
286 void RunV8GC(PP_Instance instance) {
287 content::PepperPluginInstance::Get(instance)->GetIsolate()->
288 RequestGarbageCollectionForTesting(v8::Isolate::kFullGarbageCollection);
291 const PPB_Testing_Private testing_interface = {
292 &ReadImageData, &RunMessageLoop,
293 &QuitMessageLoop, &GetLiveObjectsForInstance,
294 &IsOutOfProcess, &SimulateInputEvent,
295 &GetDocumentURL, &GetLiveVars,
296 &SetMinimumArrayBufferSizeForShmem,&RunV8GC};
298 // GetInterface ----------------------------------------------------------------
300 const void* InternalGetInterface(const char* name) {
301 // Allow custom interface factories first stab at the GetInterface call.
302 const void* custom_interface =
303 GetContentClient()->renderer()->CreatePPAPIInterface(name);
304 if (custom_interface)
305 return custom_interface;
307 // TODO(brettw) put these in a hash map for better performance.
308 #define PROXIED_IFACE(iface_str, iface_struct) \
309 if (strcmp(name, iface_str) == 0) \
310 return ppapi::thunk::Get##iface_struct##_Thunk();
312 #include "ppapi/thunk/interfaces_ppb_private.h"
313 #include "ppapi/thunk/interfaces_ppb_private_flash.h"
314 #include "ppapi/thunk/interfaces_ppb_private_no_permissions.h"
315 #include "ppapi/thunk/interfaces_ppb_public_dev.h"
316 #include "ppapi/thunk/interfaces_ppb_public_dev_channel.h"
317 #include "ppapi/thunk/interfaces_ppb_public_stable.h"
319 #undef PROXIED_IFACE
321 #define LEGACY_IFACE(iface_str, function_name) \
322 if (strcmp(name, iface_str) == 0) \
323 return function_name;
325 #include "ppapi/thunk/interfaces_legacy.h"
327 #undef LEGACY_IFACE
329 // Only support the testing interface when the command line switch is
330 // specified. This allows us to prevent people from (ab)using this interface
331 // in production code.
332 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
333 switches::kEnablePepperTesting)) {
334 if (strcmp(name, PPB_TESTING_PRIVATE_INTERFACE) == 0)
335 return &testing_interface;
337 return NULL;
340 const void* GetInterface(const char* name) {
341 // All interfaces should be used on the main thread.
342 CHECK(IsMainThread());
344 return InternalGetInterface(name);
347 // Gets the PPAPI entry points from the given library and places them into the
348 // given structure. Returns true on success.
349 bool LoadEntryPointsFromLibrary(const base::NativeLibrary& library,
350 PepperPluginInfo::EntryPoints* entry_points) {
351 entry_points->get_interface =
352 reinterpret_cast<PepperPluginInfo::GetInterfaceFunc>(
353 base::GetFunctionPointerFromNativeLibrary(library,
354 "PPP_GetInterface"));
355 if (!entry_points->get_interface) {
356 LOG(WARNING) << "No PPP_GetInterface in plugin library";
357 return false;
360 entry_points->initialize_module =
361 reinterpret_cast<PepperPluginInfo::PPP_InitializeModuleFunc>(
362 base::GetFunctionPointerFromNativeLibrary(library,
363 "PPP_InitializeModule"));
364 if (!entry_points->initialize_module) {
365 LOG(WARNING) << "No PPP_InitializeModule in plugin library";
366 return false;
369 // It's okay for PPP_ShutdownModule to not be defined and shutdown_module to
370 // be NULL.
371 entry_points->shutdown_module =
372 reinterpret_cast<PepperPluginInfo::PPP_ShutdownModuleFunc>(
373 base::GetFunctionPointerFromNativeLibrary(library,
374 "PPP_ShutdownModule"));
376 return true;
379 void CreateHostForInProcessModule(RenderFrameImpl* render_frame,
380 PluginModule* module,
381 const WebPluginInfo& webplugin_info) {
382 // First time an in-process plugin was used, make a host for it.
383 const PepperPluginInfo* info =
384 PepperPluginRegistry::GetInstance()->GetInfoForPlugin(webplugin_info);
385 DCHECK(!info->is_out_of_process);
387 ppapi::PpapiPermissions perms(PepperPluginRegistry::GetInstance()
388 ->GetInfoForPlugin(webplugin_info)
389 ->permissions);
390 RendererPpapiHostImpl* host_impl =
391 RendererPpapiHostImpl::CreateOnModuleForInProcess(module, perms);
392 render_frame->PepperPluginCreated(host_impl);
395 } // namespace
397 // PluginModule ----------------------------------------------------------------
399 PluginModule::PluginModule(const std::string& name,
400 const std::string& version,
401 const base::FilePath& path,
402 const ppapi::PpapiPermissions& perms)
403 : callback_tracker_(new ppapi::CallbackTracker),
404 is_in_destructor_(false),
405 is_crashed_(false),
406 broker_(NULL),
407 library_(NULL),
408 name_(name),
409 version_(version),
410 path_(path),
411 permissions_(ppapi::PpapiPermissions::GetForCommandLine(perms.GetBits())),
412 reserve_instance_id_(NULL) {
413 // Ensure the globals object is created.
414 if (!host_globals)
415 host_globals = new HostGlobals;
417 memset(&entry_points_, 0, sizeof(entry_points_));
418 pp_module_ = HostGlobals::Get()->AddModule(this);
419 GetLivePluginSet()->insert(this);
422 PluginModule::~PluginModule() {
423 // In the past there have been crashes reentering the plugin module
424 // destructor. Catch if that happens again earlier.
425 CHECK(!is_in_destructor_);
426 is_in_destructor_ = true;
428 // When the module is being deleted, there should be no more instances still
429 // holding a reference to us.
430 DCHECK(instances_.empty());
432 // Some resources and other stuff are hung off of the embedder state, which
433 // should be torn down before the routing stuff below.
434 renderer_ppapi_host_.reset();
436 GetLivePluginSet()->erase(this);
438 callback_tracker_->AbortAll();
440 if (entry_points_.shutdown_module)
441 entry_points_.shutdown_module();
443 if (library_)
444 base::UnloadNativeLibrary(library_);
446 // Notifications that we've been deleted should be last.
447 HostGlobals::Get()->ModuleDeleted(pp_module_);
448 if (!is_crashed_) {
449 // When the plugin crashes, we immediately tell the lifetime delegate that
450 // we're gone, so we don't want to tell it again.
451 PepperPluginRegistry::GetInstance()->PluginModuleDead(this);
454 // Don't add stuff here, the two notifications that the module object has
455 // been deleted should be last. This allows, for example,
456 // PPB_Proxy.IsInModuleDestructor to map PP_Module to this class during the
457 // previous parts of the destructor.
460 void PluginModule::SetRendererPpapiHost(
461 scoped_ptr<RendererPpapiHostImpl> host) {
462 renderer_ppapi_host_ = host.Pass();
465 bool PluginModule::InitAsInternalPlugin(
466 const PepperPluginInfo::EntryPoints& entry_points) {
467 if (InitializeModule(entry_points)) {
468 entry_points_ = entry_points;
469 return true;
471 return false;
474 bool PluginModule::InitAsLibrary(const base::FilePath& path) {
475 base::NativeLibrary library = base::LoadNativeLibrary(path, NULL);
476 if (!library)
477 return false;
479 PepperPluginInfo::EntryPoints entry_points;
481 if (!LoadEntryPointsFromLibrary(library, &entry_points) ||
482 !InitializeModule(entry_points)) {
483 base::UnloadNativeLibrary(library);
484 return false;
486 entry_points_ = entry_points;
487 library_ = library;
488 return true;
491 void PluginModule::InitAsProxied(
492 HostDispatcherWrapper* host_dispatcher_wrapper) {
493 DCHECK(!host_dispatcher_wrapper_.get());
494 host_dispatcher_wrapper_.reset(host_dispatcher_wrapper);
497 scoped_refptr<PluginModule>
498 PluginModule::CreateModuleForExternalPluginInstance() {
499 // Create a new module, but don't set the lifetime delegate. This isn't a
500 // plugin in the usual sense, so it isn't tracked by the browser.
501 scoped_refptr<PluginModule> external_plugin_module(
502 new PluginModule(name_, version_, path_, permissions_));
503 return external_plugin_module;
506 PP_ExternalPluginResult PluginModule::InitAsProxiedExternalPlugin(
507 PepperPluginInstanceImpl* instance) {
508 DCHECK(host_dispatcher_wrapper_.get());
509 // InitAsProxied (for the trusted/out-of-process case) initializes only the
510 // module, and one or more instances are added later. In this case, the
511 // PluginInstance was already created as in-process, so we missed the proxy
512 // AddInstance step and must do it now.
513 host_dispatcher_wrapper_->AddInstance(instance->pp_instance());
514 // For external plugins, we need to tell the instance to reset itself as
515 // proxied. This will clear cached interface pointers and send DidCreate (etc)
516 // to the plugin side of the proxy.
517 return instance->ResetAsProxied(this);
520 bool PluginModule::IsProxied() const { return !!host_dispatcher_wrapper_; }
522 base::ProcessId PluginModule::GetPeerProcessId() {
523 if (host_dispatcher_wrapper_)
524 return host_dispatcher_wrapper_->peer_pid();
525 return base::kNullProcessId;
528 int PluginModule::GetPluginChildId() {
529 if (host_dispatcher_wrapper_)
530 return host_dispatcher_wrapper_->plugin_child_id();
531 return 0;
534 // static
535 const PPB_Core* PluginModule::GetCore() { return &core_interface; }
537 // static
538 bool PluginModule::SupportsInterface(const char* name) {
539 return !!InternalGetInterface(name);
542 PepperPluginInstanceImpl* PluginModule::CreateInstance(
543 RenderFrameImpl* render_frame,
544 blink::WebPluginContainer* container,
545 const GURL& plugin_url) {
546 PepperPluginInstanceImpl* instance = PepperPluginInstanceImpl::Create(
547 render_frame, this, container, plugin_url);
548 if (!instance) {
549 LOG(WARNING) << "Plugin doesn't support instance interface, failing.";
550 return NULL;
552 if (host_dispatcher_wrapper_)
553 host_dispatcher_wrapper_->AddInstance(instance->pp_instance());
554 return instance;
557 PepperPluginInstanceImpl* PluginModule::GetSomeInstance() const {
558 // This will generally crash later if there is not actually any instance to
559 // return, so we force a crash now to make bugs easier to track down.
560 CHECK(!instances_.empty());
561 return *instances_.begin();
564 const void* PluginModule::GetPluginInterface(const char* name) const {
565 if (host_dispatcher_wrapper_)
566 return host_dispatcher_wrapper_->GetProxiedInterface(name);
568 // In-process plugins.
569 if (!entry_points_.get_interface)
570 return NULL;
571 return entry_points_.get_interface(name);
574 void PluginModule::InstanceCreated(PepperPluginInstanceImpl* instance) {
575 instances_.insert(instance);
578 void PluginModule::InstanceDeleted(PepperPluginInstanceImpl* instance) {
579 if (host_dispatcher_wrapper_)
580 host_dispatcher_wrapper_->RemoveInstance(instance->pp_instance());
581 instances_.erase(instance);
584 scoped_refptr<ppapi::CallbackTracker> PluginModule::GetCallbackTracker() {
585 return callback_tracker_;
588 void PluginModule::PluginCrashed() {
589 DCHECK(!is_crashed_); // Should only get one notification.
590 is_crashed_ = true;
592 // Notify all instances that they crashed.
593 for (PluginInstanceSet::iterator i = instances_.begin();
594 i != instances_.end();
595 ++i)
596 (*i)->InstanceCrashed();
598 PepperPluginRegistry::GetInstance()->PluginModuleDead(this);
601 void PluginModule::SetReserveInstanceIDCallback(
602 PP_Bool (*reserve)(PP_Module, PP_Instance)) {
603 DCHECK(!reserve_instance_id_) << "Only expect one set.";
604 reserve_instance_id_ = reserve;
607 bool PluginModule::ReserveInstanceID(PP_Instance instance) {
608 if (reserve_instance_id_)
609 return PP_ToBool(reserve_instance_id_(pp_module_, instance));
610 return true; // Instance ID is usable.
613 void PluginModule::SetBroker(PepperBroker* broker) {
614 DCHECK(!broker_ || !broker);
615 broker_ = broker;
618 PepperBroker* PluginModule::GetBroker() { return broker_; }
620 RendererPpapiHostImpl* PluginModule::CreateOutOfProcessModule(
621 RenderFrameImpl* render_frame,
622 const base::FilePath& path,
623 ppapi::PpapiPermissions permissions,
624 const IPC::ChannelHandle& channel_handle,
625 base::ProcessId peer_pid,
626 int plugin_child_id,
627 bool is_external) {
628 scoped_refptr<PepperHungPluginFilter> hung_filter(new PepperHungPluginFilter(
629 path, render_frame->GetRoutingID(), plugin_child_id));
630 scoped_ptr<HostDispatcherWrapper> dispatcher(new HostDispatcherWrapper(
631 this, peer_pid, plugin_child_id, permissions, is_external));
632 if (!dispatcher->Init(channel_handle,
633 &GetInterface,
634 ppapi::Preferences(PpapiPreferencesBuilder::Build(
635 render_frame->render_view()->webkit_preferences())),
636 hung_filter.get()))
637 return NULL;
639 RendererPpapiHostImpl* host_impl =
640 RendererPpapiHostImpl::CreateOnModuleForOutOfProcess(
641 this, dispatcher->dispatcher(), permissions);
642 render_frame->PepperPluginCreated(host_impl);
644 InitAsProxied(dispatcher.release());
645 return host_impl;
648 // static
649 void PluginModule::ResetHostGlobalsForTest() {
650 delete host_globals;
651 host_globals = NULL;
654 bool PluginModule::InitializeModule(
655 const PepperPluginInfo::EntryPoints& entry_points) {
656 DCHECK(!host_dispatcher_wrapper_.get()) << "Don't call for proxied modules.";
657 DCHECK(entry_points.initialize_module != NULL);
658 int retval = entry_points.initialize_module(pp_module(), &GetInterface);
659 if (retval != 0) {
660 #if !defined(DISABLE_NACL)
661 LOG(WARNING) << "PPP_InitializeModule returned failure " << retval;
662 #endif // !defined(DISABLE_NACL)
663 return false;
665 return true;
668 scoped_refptr<PluginModule> PluginModule::Create(
669 RenderFrameImpl* render_frame,
670 const WebPluginInfo& webplugin_info,
671 bool* pepper_plugin_was_registered) {
672 *pepper_plugin_was_registered = true;
674 // See if a module has already been loaded for this plugin.
675 base::FilePath path(webplugin_info.path);
676 scoped_refptr<PluginModule> module =
677 PepperPluginRegistry::GetInstance()->GetLiveModule(path);
678 if (module.get()) {
679 if (!module->renderer_ppapi_host()) {
680 // If the module exists and no embedder state was associated with it,
681 // then the module was one of the ones preloaded and is an in-process
682 // plugin. We need to associate our host state with it.
683 CreateHostForInProcessModule(render_frame, module.get(), webplugin_info);
685 return module;
688 // In-process plugins will have always been created up-front to avoid the
689 // sandbox restrictions. So getting here implies it doesn't exist or should
690 // be out of process.
691 const PepperPluginInfo* info =
692 PepperPluginRegistry::GetInstance()->GetInfoForPlugin(webplugin_info);
693 if (!info) {
694 *pepper_plugin_was_registered = false;
695 return scoped_refptr<PluginModule>();
696 } else if (!info->is_out_of_process) {
697 // In-process plugin not preloaded, it probably couldn't be initialized.
698 return scoped_refptr<PluginModule>();
701 // Out of process: have the browser start the plugin process for us.
702 IPC::ChannelHandle channel_handle;
703 base::ProcessId peer_pid = 0;
704 int plugin_child_id = 0;
705 render_frame->Send(new ViewHostMsg_OpenChannelToPepperPlugin(
706 path, &channel_handle, &peer_pid, &plugin_child_id));
707 if (channel_handle.name.empty()) {
708 // Couldn't be initialized.
709 return scoped_refptr<PluginModule>();
712 ppapi::PpapiPermissions permissions(info->permissions);
714 // AddLiveModule must be called before any early returns since the
715 // module's destructor will remove itself.
716 module = new PluginModule(info->name, info->version, path, permissions);
717 PepperPluginRegistry::GetInstance()->AddLiveModule(path, module.get());
719 if (!module->CreateOutOfProcessModule(render_frame,
720 path,
721 permissions,
722 channel_handle,
723 peer_pid,
724 plugin_child_id,
725 false)) // is_external = false
726 return scoped_refptr<PluginModule>();
728 return module;
731 } // namespace content