| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/client/plugin/pepper_entrypoints.h" | 5 #include "remoting/client/plugin/pepper_entrypoints.h" |
| 6 | 6 |
| 7 #include "base/message_loop.h" |
| 7 #include "remoting/client/plugin/chromoting_plugin.h" | 8 #include "remoting/client/plugin/chromoting_plugin.h" |
| 9 #include "third_party/ppapi/c/pp_errors.h" |
| 8 #include "third_party/ppapi/c/pp_instance.h" | 10 #include "third_party/ppapi/c/pp_instance.h" |
| 9 #include "third_party/ppapi/c/pp_module.h" | 11 #include "third_party/ppapi/c/pp_module.h" |
| 10 #include "third_party/ppapi/c/ppp_instance.h" | 12 #include "third_party/ppapi/c/ppb_instance.h" |
| 11 #include "third_party/ppapi/cpp/instance.h" | 13 #include "third_party/ppapi/cpp/instance.h" |
| 12 #include "third_party/ppapi/cpp/module.h" | 14 #include "third_party/ppapi/cpp/module.h" |
| 13 | 15 |
| 14 static const int kModuleInitSuccess = 0; | 16 static pp::Module* g_module_singleton = NULL; |
| 15 static const int kModuleInitFailure = 1; | 17 |
| 18 namespace pp { |
| 19 |
| 20 Module* Module::Get() { |
| 21 return g_module_singleton; |
| 22 } |
| 23 |
| 24 } // namespace pp |
| 16 | 25 |
| 17 namespace remoting { | 26 namespace remoting { |
| 18 | 27 |
| 19 // Fork of ppapi::Module | 28 class ChromotingModule : public pp::Module { |
| 20 // | |
| 21 // TODO(ajwong): Generalize this into something that other internal plugins can | |
| 22 // use. Either that, or attempt to refactor the external ppapi C++ wrapper to | |
| 23 // make it friendly for multiple Modules in one process. I think we can do this | |
| 24 // by: | |
| 25 // 1) Moving the singleton Module instance + C-bindings into another class | |
| 26 // (eg., ModuleExporter) under a different gyp targe. | |
| 27 // 2) Extracting the idea of a "Browser" out of the module that returns | |
| 28 // PPB_Core, etc. This can be a singleton per process regardless of | |
| 29 // module. | |
| 30 // 3) Migrate all PPB related objects to get data out of Browser interface | |
| 31 // instead of Module::Get(). | |
| 32 class ChromotingModule { | |
| 33 public: | |
| 34 ChromotingModule() {} | |
| 35 | |
| 36 // This function will be automatically called after the object is created. | |
| 37 // This is where you can put functions that rely on other parts of the API, | |
| 38 // now that the module has been created. | |
| 39 virtual bool Init() { return true; } | |
| 40 | |
| 41 PP_Module pp_module() const { return pp_module_; } | |
| 42 const PPB_Core& core() const { return *core_; } | |
| 43 | |
| 44 // Implements GetInterface for the browser to get plugin interfaces. Override | |
| 45 // if you need to implement your own interface types that this wrapper | |
| 46 // doesn't support. | |
| 47 virtual const void* GetInstanceInterface(const char* interface_name) { | |
| 48 if (strcmp(interface_name, PPP_INSTANCE_INTERFACE) == 0) | |
| 49 return &instance_interface_; | |
| 50 | |
| 51 return NULL; | |
| 52 } | |
| 53 | |
| 54 // Returns an interface in the browser. | |
| 55 const void* GetBrowserInterface(const char* interface_name) { | |
| 56 return get_browser_interface_(interface_name); | |
| 57 } | |
| 58 | |
| 59 // Returns the object associated with this PP_Instance, or NULL if one is | |
| 60 // not found. | |
| 61 ChromotingPlugin* InstanceForPPInstance(PP_Instance instance) { | |
| 62 InstanceMap::iterator found = current_instances_.find(instance); | |
| 63 if (found == current_instances_.end()) | |
| 64 return NULL; | |
| 65 return found->second; | |
| 66 } | |
| 67 | |
| 68 // Sets the browser interface and calls the regular init function that | |
| 69 // can be overridden by the base classes. | |
| 70 // | |
| 71 // TODO(brettw) make this private when I can figure out how to make the | |
| 72 // initialize function a friend. | |
| 73 bool InternalInit(PP_Module mod, | |
| 74 PPB_GetInterface get_browser_interface) { | |
| 75 pp_module_ = mod; | |
| 76 get_browser_interface_ = get_browser_interface; | |
| 77 core_ = reinterpret_cast<const PPB_Core*>(GetBrowserInterface( | |
| 78 PPB_CORE_INTERFACE)); | |
| 79 if (!core_) | |
| 80 return false; // Can't run without the core interface. | |
| 81 | |
| 82 return Init(); | |
| 83 } | |
| 84 | |
| 85 // Implementation of Global PPP functions --------------------------------- | |
| 86 static int PPP_InitializeModule(PP_Module module_id, | |
| 87 PPB_GetInterface get_browser_interface) { | |
| 88 ChromotingModule* module = new ChromotingModule(); | |
| 89 if (!module) | |
| 90 return kModuleInitFailure; | |
| 91 | |
| 92 if (!module->InternalInit(module_id, get_browser_interface)) { | |
| 93 delete module; | |
| 94 return kModuleInitFailure; | |
| 95 } | |
| 96 | |
| 97 module_singleton_ = module; | |
| 98 return kModuleInitSuccess; | |
| 99 } | |
| 100 | |
| 101 static void PPP_ShutdownModule() { | |
| 102 delete module_singleton_; | |
| 103 module_singleton_ = NULL; | |
| 104 } | |
| 105 | |
| 106 static const void* PPP_GetInterface(const char* interface_name) { | |
| 107 if (!module_singleton_) | |
| 108 return NULL; | |
| 109 return module_singleton_->GetInstanceInterface(interface_name); | |
| 110 } | |
| 111 | |
| 112 protected: | 29 protected: |
| 113 virtual ChromotingPlugin* CreateInstance(PP_Instance instance) { | 30 virtual ChromotingPlugin* CreateInstance(PP_Instance instance) { |
| 114 const PPB_Instance* ppb_instance_funcs = | 31 return new ChromotingPlugin(instance); |
| 115 reinterpret_cast<const PPB_Instance *>( | |
| 116 module_singleton_->GetBrowserInterface(PPB_INSTANCE_INTERFACE)); | |
| 117 return new ChromotingPlugin(instance, ppb_instance_funcs); | |
| 118 } | 32 } |
| 119 | |
| 120 private: | |
| 121 static bool Instance_New(PP_Instance instance) { | |
| 122 if (!module_singleton_) | |
| 123 return false; | |
| 124 ChromotingPlugin* obj = module_singleton_->CreateInstance(instance); | |
| 125 if (obj) { | |
| 126 module_singleton_->current_instances_[instance] = obj; | |
| 127 return true; | |
| 128 } | |
| 129 return false; | |
| 130 } | |
| 131 | |
| 132 static void Instance_Delete(PP_Instance instance) { | |
| 133 if (!module_singleton_) | |
| 134 return; | |
| 135 ChromotingModule::InstanceMap::iterator found = | |
| 136 module_singleton_->current_instances_.find(instance); | |
| 137 if (found == module_singleton_->current_instances_.end()) | |
| 138 return; | |
| 139 | |
| 140 // Remove it from the map before deleting to try to catch reentrancy. | |
| 141 ChromotingPlugin* obj = found->second; | |
| 142 module_singleton_->current_instances_.erase(found); | |
| 143 delete obj; | |
| 144 } | |
| 145 | |
| 146 static bool Instance_Initialize(PP_Instance pp_instance, | |
| 147 uint32_t argc, | |
| 148 const char* argn[], | |
| 149 const char* argv[]) { | |
| 150 if (!module_singleton_) | |
| 151 return false; | |
| 152 ChromotingPlugin* instance = | |
| 153 module_singleton_->InstanceForPPInstance(pp_instance); | |
| 154 if (!instance) | |
| 155 return false; | |
| 156 return instance->Init(argc, argn, argv); | |
| 157 } | |
| 158 | |
| 159 static bool Instance_HandleDocumentLoad(PP_Instance pp_instance, | |
| 160 PP_Resource url_loader) { | |
| 161 return false; | |
| 162 } | |
| 163 | |
| 164 static bool Instance_HandleEvent(PP_Instance pp_instance, | |
| 165 const PP_Event* event) { | |
| 166 if (!module_singleton_) | |
| 167 return false; | |
| 168 ChromotingPlugin* instance = | |
| 169 module_singleton_->InstanceForPPInstance(pp_instance); | |
| 170 if (!instance) | |
| 171 return false; | |
| 172 return instance->HandleEvent(*event); | |
| 173 } | |
| 174 | |
| 175 static PP_Var Instance_GetInstanceObject(PP_Instance pp_instance) { | |
| 176 PP_Var var; | |
| 177 var.type = PP_VarType_Void; | |
| 178 return var; | |
| 179 } | |
| 180 | |
| 181 static void Instance_ViewChanged(PP_Instance pp_instance, | |
| 182 const PP_Rect* position, | |
| 183 const PP_Rect* clip) { | |
| 184 if (!module_singleton_) | |
| 185 return; | |
| 186 ChromotingPlugin* instance = | |
| 187 module_singleton_->InstanceForPPInstance(pp_instance); | |
| 188 if (!instance) | |
| 189 return; | |
| 190 instance->ViewChanged(*position, *clip); | |
| 191 } | |
| 192 | |
| 193 // Bindings and identifiers to and from the browser. | |
| 194 PP_Module pp_module_; | |
| 195 PPB_GetInterface get_browser_interface_; | |
| 196 PPB_Core const* core_; | |
| 197 | |
| 198 // Instance tracking. | |
| 199 typedef std::map<PP_Instance, ChromotingPlugin*> InstanceMap; | |
| 200 InstanceMap current_instances_; | |
| 201 | |
| 202 // Static members for the ppapi C-bridge. | |
| 203 static PPP_Instance instance_interface_; | |
| 204 static ChromotingModule* module_singleton_; | |
| 205 | |
| 206 DISALLOW_COPY_AND_ASSIGN(ChromotingModule); | |
| 207 }; | |
| 208 | |
| 209 ChromotingModule* ChromotingModule::module_singleton_ = NULL; | |
| 210 | |
| 211 PPP_Instance ChromotingModule::instance_interface_ = { | |
| 212 &ChromotingModule::Instance_New, | |
| 213 &ChromotingModule::Instance_Delete, | |
| 214 &ChromotingModule::Instance_Initialize, | |
| 215 &ChromotingModule::Instance_HandleDocumentLoad, | |
| 216 &ChromotingModule::Instance_HandleEvent, | |
| 217 &ChromotingModule::Instance_GetInstanceObject, | |
| 218 &ChromotingModule::Instance_ViewChanged, | |
| 219 }; | 33 }; |
| 220 | 34 |
| 221 // Implementation of Global PPP functions --------------------------------- | 35 // Implementation of Global PPP functions --------------------------------- |
| 222 // | 36 int32_t PPP_InitializeModule(PP_Module module_id, |
| 223 // TODO(ajwong): This is to get around friending issues. Fix it after we decide | 37 PPB_GetInterface get_browser_interface) { |
| 224 // whether or not ChromotingModule should be generalized. | 38 ChromotingModule* module = new ChromotingModule(); |
| 225 int PPP_InitializeModule(PP_Module module_id, | 39 if (!module) |
| 226 PPB_GetInterface get_browser_interface) { | 40 return PP_Error_Failed; |
| 227 return ChromotingModule::PPP_InitializeModule(module_id, | 41 |
| 228 get_browser_interface); | 42 if (!module->InternalInit(module_id, get_browser_interface)) { |
| 43 delete module; |
| 44 return PP_Error_Failed; |
| 45 } |
| 46 |
| 47 g_module_singleton = module; |
| 48 return PP_OK; |
| 229 } | 49 } |
| 230 | 50 |
| 231 void PPP_ShutdownModule() { | 51 void PPP_ShutdownModule() { |
| 232 return ChromotingModule::PPP_ShutdownModule(); | 52 delete pp::Module::Get(); |
| 53 g_module_singleton = NULL; |
| 233 } | 54 } |
| 234 | 55 |
| 235 const void* PPP_GetInterface(const char* interface_name) { | 56 const void* PPP_GetInterface(const char* interface_name) { |
| 236 return ChromotingModule::PPP_GetInterface(interface_name); | 57 if (!pp::Module::Get()) |
| 58 return NULL; |
| 59 return pp::Module::Get()->GetInstanceInterface(interface_name); |
| 237 } | 60 } |
| 238 | 61 |
| 239 } // namespace remoting | 62 } // namespace remoting |
| OLD | NEW |