OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 "webkit/glue/plugins/pepper_plugin_module.h" |
| 6 |
| 7 #include <set> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/scoped_ptr.h" |
| 11 #include "third_party/ppapi/c/ppb_core.h" |
| 12 #include "third_party/ppapi/c/ppb_device_context_2d.h" |
| 13 #include "third_party/ppapi/c/ppb_image_data.h" |
| 14 #include "third_party/ppapi/c/ppb_instance.h" |
| 15 #include "third_party/ppapi/c/ppb_var.h" |
| 16 #include "third_party/ppapi/c/ppp.h" |
| 17 #include "third_party/ppapi/c/ppp_instance.h" |
| 18 #include "third_party/ppapi/c/pp_module.h" |
| 19 #include "third_party/ppapi/c/pp_resource.h" |
| 20 #include "third_party/ppapi/c/pp_var.h" |
| 21 #include "webkit/glue/plugins/pepper_device_context_2d.h" |
| 22 #include "webkit/glue/plugins/pepper_image_data.h" |
| 23 #include "webkit/glue/plugins/pepper_plugin_instance.h" |
| 24 #include "webkit/glue/plugins/pepper_resource_tracker.h" |
| 25 #include "webkit/glue/plugins/pepper_var.h" |
| 26 |
| 27 typedef bool (*PPP_InitializeModuleFunc)(PP_Module, PPB_GetInterface); |
| 28 typedef void (*PPP_ShutdownModuleFunc)(); |
| 29 |
| 30 namespace pepper { |
| 31 |
| 32 namespace { |
| 33 |
| 34 // Maintains all currently loaded plugin libs for validating PP_Module |
| 35 // identifiers. |
| 36 typedef std::set<PluginModule*> PluginModuleSet; |
| 37 |
| 38 PluginModuleSet* GetLivePluginSet() { |
| 39 static PluginModuleSet live_plugin_libs; |
| 40 return &live_plugin_libs; |
| 41 } |
| 42 |
| 43 // PPB_Core -------------------------------------------------------------------- |
| 44 |
| 45 void AddRefResource(PP_Resource resource) { |
| 46 Resource* res = ResourceTracker::Get()->GetResource(resource); |
| 47 if (!res) { |
| 48 DLOG(WARNING) << "AddRef()ing a nonexistent resource"; |
| 49 return; |
| 50 } |
| 51 res->AddRef(); |
| 52 } |
| 53 |
| 54 void ReleaseResource(PP_Resource resource) { |
| 55 Resource* res = ResourceTracker::Get()->GetResource(resource); |
| 56 if (!res) { |
| 57 DLOG(WARNING) << "Release()ing a nonexistent resource"; |
| 58 return; |
| 59 } |
| 60 res->Release(); |
| 61 } |
| 62 |
| 63 const PPB_Core core_interface = { |
| 64 &AddRefResource, |
| 65 &ReleaseResource, |
| 66 }; |
| 67 |
| 68 // GetInterface ---------------------------------------------------------------- |
| 69 |
| 70 const void* GetInterface(const char* name) { |
| 71 if (strcmp(name, PPB_CORE_INTERFACE) == 0) |
| 72 return &core_interface; |
| 73 if (strcmp(name, PPB_VAR_INTERFACE) == 0) |
| 74 return GetVarInterface(); |
| 75 if (strcmp(name, PPB_INSTANCE_INTERFACE) == 0) |
| 76 return PluginInstance::GetInterface(); |
| 77 if (strcmp(name, PPB_IMAGEDATA_INTERFACE) == 0) |
| 78 return ImageData::GetInterface(); |
| 79 if (strcmp(name, PPB_DEVICECONTEXT2D_INTERFACE) == 0) |
| 80 return DeviceContext2D::GetInterface(); |
| 81 return NULL; |
| 82 } |
| 83 |
| 84 } // namespace |
| 85 |
| 86 PluginModule::PluginModule(const FilePath& filename) |
| 87 : filename_(filename), |
| 88 initialized_(false), |
| 89 library_(0), |
| 90 ppp_get_interface_(NULL) { |
| 91 GetLivePluginSet()->insert(this); |
| 92 } |
| 93 |
| 94 PluginModule::~PluginModule() { |
| 95 // When the module is being deleted, there should be no more instances still |
| 96 // holding a reference to us. |
| 97 DCHECK(instances_.empty()); |
| 98 |
| 99 GetLivePluginSet()->erase(this); |
| 100 |
| 101 if (library_) { |
| 102 PPP_ShutdownModuleFunc shutdown_module = |
| 103 reinterpret_cast<PPP_ShutdownModuleFunc>( |
| 104 base::GetFunctionPointerFromNativeLibrary(library_, |
| 105 "PPP_ShutdownModule")); |
| 106 if (shutdown_module) |
| 107 shutdown_module(); |
| 108 base::UnloadNativeLibrary(library_); |
| 109 } |
| 110 } |
| 111 |
| 112 // static |
| 113 scoped_refptr<PluginModule> PluginModule::CreateModule( |
| 114 const FilePath& filename) { |
| 115 // FIXME(brettw) do uniquifying of the plugin here like the NPAPI one. |
| 116 |
| 117 scoped_refptr<PluginModule> lib(new PluginModule(filename)); |
| 118 if (!lib->Load()) |
| 119 lib = NULL; |
| 120 return lib; |
| 121 } |
| 122 |
| 123 // static |
| 124 PluginModule* PluginModule::FromPPModule(PP_Module module) { |
| 125 PluginModule* lib = reinterpret_cast<PluginModule*>(module.id); |
| 126 if (GetLivePluginSet()->find(lib) == GetLivePluginSet()->end()) |
| 127 return NULL; // Invalid plugin. |
| 128 return lib; |
| 129 } |
| 130 |
| 131 bool PluginModule::Load() { |
| 132 if (initialized_) |
| 133 return true; |
| 134 initialized_ = true; |
| 135 |
| 136 library_ = base::LoadNativeLibrary(filename_); |
| 137 if (!library_) |
| 138 return false; |
| 139 |
| 140 // Save the GetInterface function pointer for later. |
| 141 ppp_get_interface_ = |
| 142 reinterpret_cast<PPP_GetInterfaceFunc>( |
| 143 base::GetFunctionPointerFromNativeLibrary(library_, |
| 144 "PPP_GetInterface")); |
| 145 if (!ppp_get_interface_) { |
| 146 LOG(WARNING) << "No PPP_GetInterface in plugin library"; |
| 147 return false; |
| 148 } |
| 149 |
| 150 // Call the plugin initialize function. |
| 151 PPP_InitializeModuleFunc initialize_module = |
| 152 reinterpret_cast<PPP_InitializeModuleFunc>( |
| 153 base::GetFunctionPointerFromNativeLibrary(library_, |
| 154 "PPP_InitializeModule")); |
| 155 if (!initialize_module) { |
| 156 LOG(WARNING) << "No PPP_InitializeModule in plugin library"; |
| 157 return false; |
| 158 } |
| 159 int retval = initialize_module(GetPPModule(), &GetInterface); |
| 160 if (retval != 0) { |
| 161 LOG(WARNING) << "PPP_InitializeModule returned failure " << retval; |
| 162 return false; |
| 163 } |
| 164 |
| 165 return true; |
| 166 } |
| 167 |
| 168 PP_Module PluginModule::GetPPModule() const { |
| 169 PP_Module ret; |
| 170 ret.id = reinterpret_cast<intptr_t>(this); |
| 171 return ret; |
| 172 } |
| 173 |
| 174 PluginInstance* PluginModule::CreateInstance(PluginDelegate* delegate) { |
| 175 const PPP_Instance* plugin_instance_interface = |
| 176 reinterpret_cast<const PPP_Instance*>(GetPluginInterface( |
| 177 PPP_INSTANCE_INTERFACE)); |
| 178 if (!plugin_instance_interface) { |
| 179 LOG(WARNING) << "Plugin doesn't support instance interface, failing."; |
| 180 return NULL; |
| 181 } |
| 182 return new PluginInstance(delegate, this, plugin_instance_interface); |
| 183 } |
| 184 |
| 185 PluginInstance* PluginModule::GetSomeInstance() const { |
| 186 // This will generally crash later if there is not actually any instance to |
| 187 // return, so we force a crash now to make bugs easier to track down. |
| 188 CHECK(!instances_.empty()); |
| 189 return *instances_.begin(); |
| 190 } |
| 191 |
| 192 const void* PluginModule::GetPluginInterface(const char* name) const { |
| 193 if (!ppp_get_interface_) |
| 194 return NULL; |
| 195 return ppp_get_interface_(name); |
| 196 } |
| 197 |
| 198 void PluginModule::InstanceCreated(PluginInstance* instance) { |
| 199 instances_.insert(instance); |
| 200 } |
| 201 |
| 202 void PluginModule::InstanceDeleted(PluginInstance* instance) { |
| 203 instances_.erase(instance); |
| 204 } |
| 205 |
| 206 } // namespace pepper |
OLD | NEW |