| 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 "remoting/client/plugin/pepper_entrypoints.h" | |
| 6 | |
| 7 #include "remoting/client/plugin/chromoting_plugin.h" | |
| 8 #include "third_party/ppapi/c/pp_instance.h" | |
| 9 #include "third_party/ppapi/c/pp_module.h" | |
| 10 #include "third_party/ppapi/c/ppp_instance.h" | |
| 11 #include "third_party/ppapi/cpp/instance.h" | |
| 12 #include "third_party/ppapi/cpp/module.h" | |
| 13 | |
| 14 static const int kModuleInitSuccess = 0; | |
| 15 static const int kModuleInitFailure = 1; | |
| 16 | |
| 17 namespace remoting { | |
| 18 | |
| 19 // Fork of ppapi::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: | |
| 113 virtual ChromotingPlugin* CreateInstance(PP_Instance instance) { | |
| 114 const PPB_Instance* ppb_instance_funcs = | |
| 115 reinterpret_cast<const PPB_Instance *>( | |
| 116 module_singleton_->GetBrowserInterface(PPB_INSTANCE_INTERFACE)); | |
| 117 return new ChromotingPlugin(instance, ppb_instance_funcs); | |
| 118 } | |
| 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_HandleEvent(PP_Instance pp_instance, | |
| 160 const PP_Event* event) { | |
| 161 if (!module_singleton_) | |
| 162 return false; | |
| 163 ChromotingPlugin* instance = | |
| 164 module_singleton_->InstanceForPPInstance(pp_instance); | |
| 165 if (!instance) | |
| 166 return false; | |
| 167 return instance->HandleEvent(*event); | |
| 168 } | |
| 169 | |
| 170 static PP_Var Instance_GetInstanceObject(PP_Instance pp_instance) { | |
| 171 PP_Var var; | |
| 172 var.type = PP_VarType_Void; | |
| 173 return var; | |
| 174 } | |
| 175 | |
| 176 static void Instance_ViewChanged(PP_Instance pp_instance, | |
| 177 const PP_Rect* position, | |
| 178 const PP_Rect* clip) { | |
| 179 if (!module_singleton_) | |
| 180 return; | |
| 181 ChromotingPlugin* instance = | |
| 182 module_singleton_->InstanceForPPInstance(pp_instance); | |
| 183 if (!instance) | |
| 184 return; | |
| 185 instance->ViewChanged(*position, *clip); | |
| 186 } | |
| 187 | |
| 188 // Bindings and identifiers to and from the browser. | |
| 189 PP_Module pp_module_; | |
| 190 PPB_GetInterface get_browser_interface_; | |
| 191 PPB_Core const* core_; | |
| 192 | |
| 193 // Instance tracking. | |
| 194 typedef std::map<PP_Instance, ChromotingPlugin*> InstanceMap; | |
| 195 InstanceMap current_instances_; | |
| 196 | |
| 197 // Static members for the ppapi C-bridge. | |
| 198 static PPP_Instance instance_interface_; | |
| 199 static ChromotingModule* module_singleton_; | |
| 200 | |
| 201 DISALLOW_COPY_AND_ASSIGN(ChromotingModule); | |
| 202 }; | |
| 203 | |
| 204 ChromotingModule* ChromotingModule::module_singleton_ = NULL; | |
| 205 | |
| 206 PPP_Instance ChromotingModule::instance_interface_ = { | |
| 207 &ChromotingModule::Instance_New, | |
| 208 &ChromotingModule::Instance_Delete, | |
| 209 &ChromotingModule::Instance_Initialize, | |
| 210 &ChromotingModule::Instance_HandleEvent, | |
| 211 &ChromotingModule::Instance_GetInstanceObject, | |
| 212 &ChromotingModule::Instance_ViewChanged, | |
| 213 }; | |
| 214 | |
| 215 // Implementation of Global PPP functions --------------------------------- | |
| 216 // | |
| 217 // TODO(ajwong): This is to get around friending issues. Fix it after we decide | |
| 218 // whether or not ChromotingModule should be generalized. | |
| 219 int PPP_InitializeModule(PP_Module module_id, | |
| 220 PPB_GetInterface get_browser_interface) { | |
| 221 return ChromotingModule::PPP_InitializeModule(module_id, | |
| 222 get_browser_interface); | |
| 223 } | |
| 224 | |
| 225 void PPP_ShutdownModule() { | |
| 226 return ChromotingModule::PPP_ShutdownModule(); | |
| 227 } | |
| 228 | |
| 229 const void* PPP_GetInterface(const char* interface_name) { | |
| 230 return ChromotingModule::PPP_GetInterface(interface_name); | |
| 231 } | |
| 232 | |
| 233 } // namespace remoting | |
| OLD | NEW |