Home | History | Annotate | Download | only in pepper
      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 #include "content/renderer/pepper/plugin_module.h"
      6 
      7 #include <set>
      8 
      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/common.h"
     20 #include "content/renderer/pepper/host_dispatcher_wrapper.h"
     21 #include "content/renderer/pepper/host_globals.h"
     22 #include "content/renderer/pepper/pepper_hung_plugin_filter.h"
     23 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
     24 #include "content/renderer/pepper/pepper_plugin_registry.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_mapping.h"
     67 #include "ppapi/c/ppb_file_ref.h"
     68 #include "ppapi/c/ppb_file_system.h"
     69 #include "ppapi/c/ppb_fullscreen.h"
     70 #include "ppapi/c/ppb_graphics_2d.h"
     71 #include "ppapi/c/ppb_graphics_3d.h"
     72 #include "ppapi/c/ppb_host_resolver.h"
     73 #include "ppapi/c/ppb_image_data.h"
     74 #include "ppapi/c/ppb_instance.h"
     75 #include "ppapi/c/ppb_media_stream_audio_track.h"
     76 #include "ppapi/c/ppb_media_stream_video_track.h"
     77 #include "ppapi/c/ppb_messaging.h"
     78 #include "ppapi/c/ppb_mouse_cursor.h"
     79 #include "ppapi/c/ppb_mouse_lock.h"
     80 #include "ppapi/c/ppb_net_address.h"
     81 #include "ppapi/c/ppb_network_list.h"
     82 #include "ppapi/c/ppb_network_monitor.h"
     83 #include "ppapi/c/ppb_network_proxy.h"
     84 #include "ppapi/c/ppb_opengles2.h"
     85 #include "ppapi/c/ppb_tcp_socket.h"
     86 #include "ppapi/c/ppb_text_input_controller.h"
     87 #include "ppapi/c/ppb_udp_socket.h"
     88 #include "ppapi/c/ppb_url_loader.h"
     89 #include "ppapi/c/ppb_url_request_info.h"
     90 #include "ppapi/c/ppb_url_response_info.h"
     91 #include "ppapi/c/ppb_var.h"
     92 #include "ppapi/c/ppb_var_array.h"
     93 #include "ppapi/c/ppb_var_array_buffer.h"
     94 #include "ppapi/c/ppb_var_dictionary.h"
     95 #include "ppapi/c/ppb_video_decoder.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_ext_crx_file_system_private.h"
    101 #include "ppapi/c/private/ppb_file_io_private.h"
    102 #include "ppapi/c/private/ppb_file_ref_private.h"
    103 #include "ppapi/c/private/ppb_find_private.h"
    104 #include "ppapi/c/private/ppb_flash.h"
    105 #include "ppapi/c/private/ppb_flash_clipboard.h"
    106 #include "ppapi/c/private/ppb_flash_device_id.h"
    107 #include "ppapi/c/private/ppb_flash_drm.h"
    108 #include "ppapi/c/private/ppb_flash_file.h"
    109 #include "ppapi/c/private/ppb_flash_font_file.h"
    110 #include "ppapi/c/private/ppb_flash_fullscreen.h"
    111 #include "ppapi/c/private/ppb_flash_menu.h"
    112 #include "ppapi/c/private/ppb_flash_message_loop.h"
    113 #include "ppapi/c/private/ppb_flash_print.h"
    114 #include "ppapi/c/private/ppb_host_resolver_private.h"
    115 #include "ppapi/c/private/ppb_input_event_private.h"
    116 #include "ppapi/c/private/ppb_instance_private.h"
    117 #include "ppapi/c/private/ppb_isolated_file_system_private.h"
    118 #include "ppapi/c/private/ppb_output_protection_private.h"
    119 #include "ppapi/c/private/ppb_pdf.h"
    120 #include "ppapi/c/private/ppb_proxy_private.h"
    121 #include "ppapi/c/private/ppb_talk_private.h"
    122 #include "ppapi/c/private/ppb_tcp_server_socket_private.h"
    123 #include "ppapi/c/private/ppb_tcp_socket_private.h"
    124 #include "ppapi/c/private/ppb_testing_private.h"
    125 #include "ppapi/c/private/ppb_udp_socket_private.h"
    126 #include "ppapi/c/private/ppb_uma_private.h"
    127 #include "ppapi/c/private/ppb_video_destination_private.h"
    128 #include "ppapi/c/private/ppb_video_source_private.h"
    129 #include "ppapi/c/private/ppb_x509_certificate_private.h"
    130 #include "ppapi/c/trusted/ppb_broker_trusted.h"
    131 #include "ppapi/c/trusted/ppb_browser_font_trusted.h"
    132 #include "ppapi/c/trusted/ppb_char_set_trusted.h"
    133 #include "ppapi/c/trusted/ppb_file_chooser_trusted.h"
    134 #include "ppapi/c/trusted/ppb_url_loader_trusted.h"
    135 #include "ppapi/shared_impl/callback_tracker.h"
    136 #include "ppapi/shared_impl/ppapi_preferences.h"
    137 #include "ppapi/shared_impl/ppapi_switches.h"
    138 #include "ppapi/shared_impl/ppb_input_event_shared.h"
    139 #include "ppapi/shared_impl/ppb_opengles2_shared.h"
    140 #include "ppapi/shared_impl/ppb_var_shared.h"
    141 #include "ppapi/shared_impl/time_conversion.h"
    142 #include "ppapi/thunk/enter.h"
    143 #include "ppapi/thunk/ppb_graphics_2d_api.h"
    144 #include "ppapi/thunk/thunk.h"
    145 
    146 #if defined(OS_CHROMEOS)
    147 #include "ppapi/c/private/ppb_platform_verification_private.h"
    148 #endif
    149 
    150 using ppapi::InputEventData;
    151 using ppapi::PpapiGlobals;
    152 using ppapi::TimeTicksToPPTimeTicks;
    153 using ppapi::TimeToPPTime;
    154 using ppapi::thunk::EnterResource;
    155 using ppapi::thunk::PPB_Graphics2D_API;
    156 using ppapi::thunk::PPB_InputEvent_API;
    157 
    158 namespace content {
    159 
    160 namespace {
    161 
    162 // Global tracking info for PPAPI plugins. This is lazily created before the
    163 // first plugin is allocated, and leaked on shutdown.
    164 //
    165 // Note that we don't want a Singleton here since destroying this object will
    166 // try to free some stuff that requires WebKit, and Singletons are destroyed
    167 // after WebKit.
    168 // TODO(raymes): I'm not sure if it is completely necessary to leak the
    169 // HostGlobals. Figure out the shutdown sequence and find a way to do this
    170 // more elegantly.
    171 HostGlobals* host_globals = NULL;
    172 
    173 // Maintains all currently loaded plugin libs for validating PP_Module
    174 // identifiers.
    175 typedef std::set<PluginModule*> PluginModuleSet;
    176 
    177 PluginModuleSet* GetLivePluginSet() {
    178   CR_DEFINE_STATIC_LOCAL(PluginModuleSet, live_plugin_libs, ());
    179   return &live_plugin_libs;
    180 }
    181 
    182 // PPB_Core --------------------------------------------------------------------
    183 
    184 void AddRefResource(PP_Resource resource) {
    185   PpapiGlobals::Get()->GetResourceTracker()->AddRefResource(resource);
    186 }
    187 
    188 void ReleaseResource(PP_Resource resource) {
    189   PpapiGlobals::Get()->GetResourceTracker()->ReleaseResource(resource);
    190 }
    191 
    192 PP_Time GetTime() { return TimeToPPTime(base::Time::Now()); }
    193 
    194 PP_TimeTicks GetTickTime() {
    195   return TimeTicksToPPTimeTicks(base::TimeTicks::Now());
    196 }
    197 
    198 void CallOnMainThread(int delay_in_msec,
    199                       PP_CompletionCallback callback,
    200                       int32_t result) {
    201   if (callback.func) {
    202     PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostDelayedTask(
    203         FROM_HERE,
    204         base::Bind(callback.func, callback.user_data, result),
    205         base::TimeDelta::FromMilliseconds(delay_in_msec));
    206   }
    207 }
    208 
    209 PP_Bool IsMainThread() {
    210   return BoolToPPBool(PpapiGlobals::Get()
    211                           ->GetMainThreadMessageLoop()
    212                           ->BelongsToCurrentThread());
    213 }
    214 
    215 const PPB_Core core_interface = {&AddRefResource,   &ReleaseResource,
    216                                  &GetTime,          &GetTickTime,
    217                                  &CallOnMainThread, &IsMainThread};
    218 
    219 // PPB_Testing -----------------------------------------------------------------
    220 
    221 PP_Bool ReadImageData(PP_Resource device_context_2d,
    222                       PP_Resource image,
    223                       const PP_Point* top_left) {
    224   EnterResource<PPB_Graphics2D_API> enter(device_context_2d, true);
    225   if (enter.failed())
    226     return PP_FALSE;
    227   return BoolToPPBool(enter.object()->ReadImageData(image, top_left));
    228 }
    229 
    230 void RunMessageLoop(PP_Instance instance) {
    231   base::MessageLoop::ScopedNestableTaskAllower allow(
    232       base::MessageLoop::current());
    233   base::MessageLoop::current()->Run();
    234 }
    235 
    236 void QuitMessageLoop(PP_Instance instance) {
    237   base::MessageLoop::current()->QuitNow();
    238 }
    239 
    240 uint32_t GetLiveObjectsForInstance(PP_Instance instance_id) {
    241   return HostGlobals::Get()->GetResourceTracker()->GetLiveObjectsForInstance(
    242       instance_id);
    243 }
    244 
    245 PP_Bool IsOutOfProcess() { return PP_FALSE; }
    246 
    247 void SimulateInputEvent(PP_Instance instance, PP_Resource input_event) {
    248   PepperPluginInstanceImpl* plugin_instance =
    249       host_globals->GetInstance(instance);
    250   if (!plugin_instance)
    251     return;
    252 
    253   EnterResource<PPB_InputEvent_API> enter(input_event, false);
    254   if (enter.failed())
    255     return;
    256 
    257   const InputEventData& input_event_data = enter.object()->GetInputEventData();
    258   plugin_instance->SimulateInputEvent(input_event_data);
    259 }
    260 
    261 PP_Var GetDocumentURL(PP_Instance instance, PP_URLComponents_Dev* components) {
    262   PepperPluginInstanceImpl* plugin_instance =
    263       host_globals->GetInstance(instance);
    264   if (!plugin_instance)
    265     return PP_MakeUndefined();
    266   return plugin_instance->GetDocumentURL(instance, components);
    267 }
    268 
    269 uint32_t GetLiveVars(PP_Var live_vars[], uint32_t array_size) {
    270   std::vector<PP_Var> vars =
    271       PpapiGlobals::Get()->GetVarTracker()->GetLiveVars();
    272   for (size_t i = 0u;
    273        i < std::min(static_cast<size_t>(array_size), vars.size());
    274        ++i)
    275     live_vars[i] = vars[i];
    276   return vars.size();
    277 }
    278 
    279 void SetMinimumArrayBufferSizeForShmem(PP_Instance /*instance*/,
    280                                        uint32_t /*threshold*/) {
    281   // Does nothing. Not needed in-process.
    282 }
    283 
    284 const PPB_Testing_Private testing_interface = {
    285     &ReadImageData,                    &RunMessageLoop,
    286     &QuitMessageLoop,                  &GetLiveObjectsForInstance,
    287     &IsOutOfProcess,                   &SimulateInputEvent,
    288     &GetDocumentURL,                   &GetLiveVars,
    289     &SetMinimumArrayBufferSizeForShmem};
    290 
    291 // GetInterface ----------------------------------------------------------------
    292 
    293 const void* InternalGetInterface(const char* name) {
    294   // Allow custom interface factories first stab at the GetInterface call.
    295   const void* custom_interface =
    296       GetContentClient()->renderer()->CreatePPAPIInterface(name);
    297   if (custom_interface)
    298     return custom_interface;
    299 
    300 // TODO(brettw) put these in a hash map for better performance.
    301 #define PROXIED_IFACE(iface_str, iface_struct) \
    302   if (strcmp(name, iface_str) == 0)            \
    303     return ppapi::thunk::Get##iface_struct##_Thunk();
    304 
    305 #include "ppapi/thunk/interfaces_ppb_private.h"
    306 #include "ppapi/thunk/interfaces_ppb_private_flash.h"
    307 #include "ppapi/thunk/interfaces_ppb_private_no_permissions.h"
    308 #include "ppapi/thunk/interfaces_ppb_public_dev.h"
    309 #include "ppapi/thunk/interfaces_ppb_public_dev_channel.h"
    310 #include "ppapi/thunk/interfaces_ppb_public_stable.h"
    311 
    312 #undef PROXIED_IFACE
    313 
    314 #define LEGACY_IFACE(iface_str, function_name) \
    315   if (strcmp(name, iface_str) == 0)            \
    316     return function_name;
    317 
    318 #include "ppapi/thunk/interfaces_legacy.h"
    319 
    320 #undef LEGACY_IFACE
    321 
    322   // Only support the testing interface when the command line switch is
    323   // specified. This allows us to prevent people from (ab)using this interface
    324   // in production code.
    325   if (CommandLine::ForCurrentProcess()->HasSwitch(
    326           switches::kEnablePepperTesting)) {
    327     if (strcmp(name, PPB_TESTING_PRIVATE_INTERFACE) == 0)
    328       return &testing_interface;
    329   }
    330   return NULL;
    331 }
    332 
    333 const void* GetInterface(const char* name) {
    334   // All interfaces should be used on the main thread.
    335   CHECK(IsMainThread());
    336 
    337   return InternalGetInterface(name);
    338 }
    339 
    340 // Gets the PPAPI entry points from the given library and places them into the
    341 // given structure. Returns true on success.
    342 bool LoadEntryPointsFromLibrary(const base::NativeLibrary& library,
    343                                 PepperPluginInfo::EntryPoints* entry_points) {
    344   entry_points->get_interface =
    345       reinterpret_cast<PepperPluginInfo::GetInterfaceFunc>(
    346           base::GetFunctionPointerFromNativeLibrary(library,
    347                                                     "PPP_GetInterface"));
    348   if (!entry_points->get_interface) {
    349     LOG(WARNING) << "No PPP_GetInterface in plugin library";
    350     return false;
    351   }
    352 
    353   entry_points->initialize_module =
    354       reinterpret_cast<PepperPluginInfo::PPP_InitializeModuleFunc>(
    355           base::GetFunctionPointerFromNativeLibrary(library,
    356                                                     "PPP_InitializeModule"));
    357   if (!entry_points->initialize_module) {
    358     LOG(WARNING) << "No PPP_InitializeModule in plugin library";
    359     return false;
    360   }
    361 
    362   // It's okay for PPP_ShutdownModule to not be defined and shutdown_module to
    363   // be NULL.
    364   entry_points->shutdown_module =
    365       reinterpret_cast<PepperPluginInfo::PPP_ShutdownModuleFunc>(
    366           base::GetFunctionPointerFromNativeLibrary(library,
    367                                                     "PPP_ShutdownModule"));
    368 
    369   return true;
    370 }
    371 
    372 void CreateHostForInProcessModule(RenderFrameImpl* render_frame,
    373                                   PluginModule* module,
    374                                   const WebPluginInfo& webplugin_info) {
    375   // First time an in-process plugin was used, make a host for it.
    376   const PepperPluginInfo* info =
    377       PepperPluginRegistry::GetInstance()->GetInfoForPlugin(webplugin_info);
    378   DCHECK(!info->is_out_of_process);
    379 
    380   ppapi::PpapiPermissions perms(PepperPluginRegistry::GetInstance()
    381                                     ->GetInfoForPlugin(webplugin_info)
    382                                     ->permissions);
    383   RendererPpapiHostImpl* host_impl =
    384       RendererPpapiHostImpl::CreateOnModuleForInProcess(module, perms);
    385   render_frame->PepperPluginCreated(host_impl);
    386 }
    387 
    388 }  // namespace
    389 
    390 // PluginModule ----------------------------------------------------------------
    391 
    392 PluginModule::PluginModule(const std::string& name,
    393                            const base::FilePath& path,
    394                            const ppapi::PpapiPermissions& perms)
    395     : callback_tracker_(new ppapi::CallbackTracker),
    396       is_in_destructor_(false),
    397       is_crashed_(false),
    398       broker_(NULL),
    399       library_(NULL),
    400       name_(name),
    401       path_(path),
    402       permissions_(ppapi::PpapiPermissions::GetForCommandLine(perms.GetBits())),
    403       reserve_instance_id_(NULL) {
    404   // Ensure the globals object is created.
    405   if (!host_globals)
    406     host_globals = new HostGlobals;
    407 
    408   memset(&entry_points_, 0, sizeof(entry_points_));
    409   pp_module_ = HostGlobals::Get()->AddModule(this);
    410   GetLivePluginSet()->insert(this);
    411 }
    412 
    413 PluginModule::~PluginModule() {
    414   // In the past there have been crashes reentering the plugin module
    415   // destructor. Catch if that happens again earlier.
    416   CHECK(!is_in_destructor_);
    417   is_in_destructor_ = true;
    418 
    419   // When the module is being deleted, there should be no more instances still
    420   // holding a reference to us.
    421   DCHECK(instances_.empty());
    422 
    423   // Some resources and other stuff are hung off of the embedder state, which
    424   // should be torn down before the routing stuff below.
    425   renderer_ppapi_host_.reset();
    426 
    427   GetLivePluginSet()->erase(this);
    428 
    429   callback_tracker_->AbortAll();
    430 
    431   if (entry_points_.shutdown_module)
    432     entry_points_.shutdown_module();
    433 
    434   if (library_)
    435     base::UnloadNativeLibrary(library_);
    436 
    437   // Notifications that we've been deleted should be last.
    438   HostGlobals::Get()->ModuleDeleted(pp_module_);
    439   if (!is_crashed_) {
    440     // When the plugin crashes, we immediately tell the lifetime delegate that
    441     // we're gone, so we don't want to tell it again.
    442     PepperPluginRegistry::GetInstance()->PluginModuleDead(this);
    443   }
    444 
    445   // Don't add stuff here, the two notifications that the module object has
    446   // been deleted should be last. This allows, for example,
    447   // PPB_Proxy.IsInModuleDestructor to map PP_Module to this class during the
    448   // previous parts of the destructor.
    449 }
    450 
    451 void PluginModule::SetRendererPpapiHost(
    452     scoped_ptr<RendererPpapiHostImpl> host) {
    453   renderer_ppapi_host_ = host.Pass();
    454 }
    455 
    456 bool PluginModule::InitAsInternalPlugin(
    457     const PepperPluginInfo::EntryPoints& entry_points) {
    458   if (InitializeModule(entry_points)) {
    459     entry_points_ = entry_points;
    460     return true;
    461   }
    462   return false;
    463 }
    464 
    465 bool PluginModule::InitAsLibrary(const base::FilePath& path) {
    466   base::NativeLibrary library = base::LoadNativeLibrary(path, NULL);
    467   if (!library)
    468     return false;
    469 
    470   PepperPluginInfo::EntryPoints entry_points;
    471 
    472   if (!LoadEntryPointsFromLibrary(library, &entry_points) ||
    473       !InitializeModule(entry_points)) {
    474     base::UnloadNativeLibrary(library);
    475     return false;
    476   }
    477   entry_points_ = entry_points;
    478   library_ = library;
    479   return true;
    480 }
    481 
    482 void PluginModule::InitAsProxied(
    483     HostDispatcherWrapper* host_dispatcher_wrapper) {
    484   DCHECK(!host_dispatcher_wrapper_.get());
    485   host_dispatcher_wrapper_.reset(host_dispatcher_wrapper);
    486 }
    487 
    488 scoped_refptr<PluginModule>
    489 PluginModule::CreateModuleForExternalPluginInstance() {
    490   // Create a new module, but don't set the lifetime delegate. This isn't a
    491   // plugin in the usual sense, so it isn't tracked by the browser.
    492   scoped_refptr<PluginModule> external_plugin_module(
    493       new PluginModule(name_, path_, permissions_));
    494   return external_plugin_module;
    495 }
    496 
    497 PP_ExternalPluginResult PluginModule::InitAsProxiedExternalPlugin(
    498     PepperPluginInstanceImpl* instance) {
    499   DCHECK(host_dispatcher_wrapper_.get());
    500   // InitAsProxied (for the trusted/out-of-process case) initializes only the
    501   // module, and one or more instances are added later. In this case, the
    502   // PluginInstance was already created as in-process, so we missed the proxy
    503   // AddInstance step and must do it now.
    504   host_dispatcher_wrapper_->AddInstance(instance->pp_instance());
    505   // For external plugins, we need to tell the instance to reset itself as
    506   // proxied. This will clear cached interface pointers and send DidCreate (etc)
    507   // to the plugin side of the proxy.
    508   return instance->ResetAsProxied(this);
    509 }
    510 
    511 bool PluginModule::IsProxied() const { return !!host_dispatcher_wrapper_; }
    512 
    513 base::ProcessId PluginModule::GetPeerProcessId() {
    514   if (host_dispatcher_wrapper_)
    515     return host_dispatcher_wrapper_->peer_pid();
    516   return base::kNullProcessId;
    517 }
    518 
    519 int PluginModule::GetPluginChildId() {
    520   if (host_dispatcher_wrapper_)
    521     return host_dispatcher_wrapper_->plugin_child_id();
    522   return 0;
    523 }
    524 
    525 // static
    526 const PPB_Core* PluginModule::GetCore() { return &core_interface; }
    527 
    528 // static
    529 bool PluginModule::SupportsInterface(const char* name) {
    530   return !!InternalGetInterface(name);
    531 }
    532 
    533 PepperPluginInstanceImpl* PluginModule::CreateInstance(
    534     RenderFrameImpl* render_frame,
    535     blink::WebPluginContainer* container,
    536     const GURL& plugin_url) {
    537   PepperPluginInstanceImpl* instance = PepperPluginInstanceImpl::Create(
    538       render_frame, this, container, plugin_url);
    539   if (!instance) {
    540     LOG(WARNING) << "Plugin doesn't support instance interface, failing.";
    541     return NULL;
    542   }
    543   if (host_dispatcher_wrapper_)
    544     host_dispatcher_wrapper_->AddInstance(instance->pp_instance());
    545   return instance;
    546 }
    547 
    548 PepperPluginInstanceImpl* PluginModule::GetSomeInstance() const {
    549   // This will generally crash later if there is not actually any instance to
    550   // return, so we force a crash now to make bugs easier to track down.
    551   CHECK(!instances_.empty());
    552   return *instances_.begin();
    553 }
    554 
    555 const void* PluginModule::GetPluginInterface(const char* name) const {
    556   if (host_dispatcher_wrapper_)
    557     return host_dispatcher_wrapper_->GetProxiedInterface(name);
    558 
    559   // In-process plugins.
    560   if (!entry_points_.get_interface)
    561     return NULL;
    562   return entry_points_.get_interface(name);
    563 }
    564 
    565 void PluginModule::InstanceCreated(PepperPluginInstanceImpl* instance) {
    566   instances_.insert(instance);
    567 }
    568 
    569 void PluginModule::InstanceDeleted(PepperPluginInstanceImpl* instance) {
    570   if (host_dispatcher_wrapper_)
    571     host_dispatcher_wrapper_->RemoveInstance(instance->pp_instance());
    572   instances_.erase(instance);
    573 }
    574 
    575 scoped_refptr<ppapi::CallbackTracker> PluginModule::GetCallbackTracker() {
    576   return callback_tracker_;
    577 }
    578 
    579 void PluginModule::PluginCrashed() {
    580   DCHECK(!is_crashed_);  // Should only get one notification.
    581   is_crashed_ = true;
    582 
    583   // Notify all instances that they crashed.
    584   for (PluginInstanceSet::iterator i = instances_.begin();
    585        i != instances_.end();
    586        ++i)
    587     (*i)->InstanceCrashed();
    588 
    589   PepperPluginRegistry::GetInstance()->PluginModuleDead(this);
    590 }
    591 
    592 void PluginModule::SetReserveInstanceIDCallback(
    593     PP_Bool (*reserve)(PP_Module, PP_Instance)) {
    594   DCHECK(!reserve_instance_id_) << "Only expect one set.";
    595   reserve_instance_id_ = reserve;
    596 }
    597 
    598 bool PluginModule::ReserveInstanceID(PP_Instance instance) {
    599   if (reserve_instance_id_)
    600     return PPBoolToBool(reserve_instance_id_(pp_module_, instance));
    601   return true;  // Instance ID is usable.
    602 }
    603 
    604 void PluginModule::SetBroker(PepperBroker* broker) {
    605   DCHECK(!broker_ || !broker);
    606   broker_ = broker;
    607 }
    608 
    609 PepperBroker* PluginModule::GetBroker() { return broker_; }
    610 
    611 RendererPpapiHostImpl* PluginModule::CreateOutOfProcessModule(
    612     RenderFrameImpl* render_frame,
    613     const base::FilePath& path,
    614     ppapi::PpapiPermissions permissions,
    615     const IPC::ChannelHandle& channel_handle,
    616     base::ProcessId peer_pid,
    617     int plugin_child_id,
    618     bool is_external) {
    619   scoped_refptr<PepperHungPluginFilter> hung_filter(new PepperHungPluginFilter(
    620       path, render_frame->GetRoutingID(), plugin_child_id));
    621   scoped_ptr<HostDispatcherWrapper> dispatcher(new HostDispatcherWrapper(
    622       this, peer_pid, plugin_child_id, permissions, is_external));
    623   if (!dispatcher->Init(
    624           channel_handle,
    625           &GetInterface,
    626           ppapi::Preferences(render_frame->render_view()->webkit_preferences()),
    627           hung_filter.get()))
    628     return NULL;
    629 
    630   RendererPpapiHostImpl* host_impl =
    631       RendererPpapiHostImpl::CreateOnModuleForOutOfProcess(
    632           this, dispatcher->dispatcher(), permissions);
    633   render_frame->PepperPluginCreated(host_impl);
    634 
    635   InitAsProxied(dispatcher.release());
    636   return host_impl;
    637 }
    638 
    639 // static
    640 void PluginModule::ResetHostGlobalsForTest() {
    641   delete host_globals;
    642   host_globals = NULL;
    643 }
    644 
    645 bool PluginModule::InitializeModule(
    646     const PepperPluginInfo::EntryPoints& entry_points) {
    647   DCHECK(!host_dispatcher_wrapper_.get()) << "Don't call for proxied modules.";
    648   DCHECK(entry_points.initialize_module != NULL);
    649   int retval = entry_points.initialize_module(pp_module(), &GetInterface);
    650   if (retval != 0) {
    651 #if !defined(DISABLE_NACL)
    652     LOG(WARNING) << "PPP_InitializeModule returned failure " << retval;
    653 #endif  // !defined(DISABLE_NACL)
    654     return false;
    655   }
    656   return true;
    657 }
    658 
    659 scoped_refptr<PluginModule> PluginModule::Create(
    660     RenderFrameImpl* render_frame,
    661     const WebPluginInfo& webplugin_info,
    662     bool* pepper_plugin_was_registered) {
    663   *pepper_plugin_was_registered = true;
    664 
    665   // See if a module has already been loaded for this plugin.
    666   base::FilePath path(webplugin_info.path);
    667   scoped_refptr<PluginModule> module =
    668       PepperPluginRegistry::GetInstance()->GetLiveModule(path);
    669   if (module.get()) {
    670     if (!module->renderer_ppapi_host()) {
    671       // If the module exists and no embedder state was associated with it,
    672       // then the module was one of the ones preloaded and is an in-process
    673       // plugin. We need to associate our host state with it.
    674       CreateHostForInProcessModule(render_frame, module.get(), webplugin_info);
    675     }
    676     return module;
    677   }
    678 
    679   // In-process plugins will have always been created up-front to avoid the
    680   // sandbox restrictions. So getting here implies it doesn't exist or should
    681   // be out of process.
    682   const PepperPluginInfo* info =
    683       PepperPluginRegistry::GetInstance()->GetInfoForPlugin(webplugin_info);
    684   if (!info) {
    685     *pepper_plugin_was_registered = false;
    686     return scoped_refptr<PluginModule>();
    687   } else if (!info->is_out_of_process) {
    688     // In-process plugin not preloaded, it probably couldn't be initialized.
    689     return scoped_refptr<PluginModule>();
    690   }
    691 
    692   // Out of process: have the browser start the plugin process for us.
    693   IPC::ChannelHandle channel_handle;
    694   base::ProcessId peer_pid;
    695   int plugin_child_id = 0;
    696   render_frame->Send(new ViewHostMsg_OpenChannelToPepperPlugin(
    697       path, &channel_handle, &peer_pid, &plugin_child_id));
    698   if (channel_handle.name.empty()) {
    699     // Couldn't be initialized.
    700     return scoped_refptr<PluginModule>();
    701   }
    702 
    703   ppapi::PpapiPermissions permissions(info->permissions);
    704 
    705   // AddLiveModule must be called before any early returns since the
    706   // module's destructor will remove itself.
    707   module = new PluginModule(info->name, path, permissions);
    708   PepperPluginRegistry::GetInstance()->AddLiveModule(path, module.get());
    709 
    710   if (!module->CreateOutOfProcessModule(render_frame,
    711                                         path,
    712                                         permissions,
    713                                         channel_handle,
    714                                         peer_pid,
    715                                         plugin_child_id,
    716                                         false))  // is_external = false
    717     return scoped_refptr<PluginModule>();
    718 
    719   return module;
    720 }
    721 
    722 }  // namespace content
    723