| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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/plugins/ppapi/plugin_object.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/memory/ref_counted.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/strings/string_number_conversions.h" | |
| 11 #include "base/strings/string_util.h" | |
| 12 #include "ppapi/c/dev/ppb_var_deprecated.h" | |
| 13 #include "ppapi/c/dev/ppp_class_deprecated.h" | |
| 14 #include "ppapi/c/pp_resource.h" | |
| 15 #include "ppapi/c/pp_var.h" | |
| 16 #include "ppapi/shared_impl/ppapi_globals.h" | |
| 17 #include "ppapi/shared_impl/resource_tracker.h" | |
| 18 #include "ppapi/shared_impl/var.h" | |
| 19 #include "ppapi/shared_impl/var_tracker.h" | |
| 20 #include "third_party/WebKit/public/web/WebBindings.h" | |
| 21 #include "third_party/npapi/bindings/npapi.h" | |
| 22 #include "third_party/npapi/bindings/npruntime.h" | |
| 23 #include "webkit/plugins/ppapi/npapi_glue.h" | |
| 24 #include "webkit/plugins/ppapi/plugin_module.h" | |
| 25 #include "webkit/plugins/ppapi/ppapi_plugin_instance_impl.h" | |
| 26 | |
| 27 using ppapi::PpapiGlobals; | |
| 28 using ppapi::StringVar; | |
| 29 using ppapi::Var; | |
| 30 using WebKit::WebBindings; | |
| 31 | |
| 32 namespace webkit { | |
| 33 namespace ppapi { | |
| 34 | |
| 35 namespace { | |
| 36 | |
| 37 const char kInvalidValueException[] = "Error: Invalid value"; | |
| 38 | |
| 39 // NPObject implementation in terms of PPP_Class_Deprecated -------------------- | |
| 40 | |
| 41 NPObject* WrapperClass_Allocate(NPP npp, NPClass* unused) { | |
| 42 return PluginObject::AllocateObjectWrapper(); | |
| 43 } | |
| 44 | |
| 45 void WrapperClass_Deallocate(NPObject* np_object) { | |
| 46 PluginObject* plugin_object = PluginObject::FromNPObject(np_object); | |
| 47 if (!plugin_object) | |
| 48 return; | |
| 49 plugin_object->ppp_class()->Deallocate(plugin_object->ppp_class_data()); | |
| 50 delete plugin_object; | |
| 51 } | |
| 52 | |
| 53 void WrapperClass_Invalidate(NPObject* object) { | |
| 54 } | |
| 55 | |
| 56 bool WrapperClass_HasMethod(NPObject* object, NPIdentifier method_name) { | |
| 57 NPObjectAccessorWithIdentifier accessor(object, method_name, false); | |
| 58 if (!accessor.is_valid()) | |
| 59 return false; | |
| 60 | |
| 61 PPResultAndExceptionToNPResult result_converter( | |
| 62 accessor.object()->GetNPObject(), NULL); | |
| 63 bool rv = accessor.object()->ppp_class()->HasMethod( | |
| 64 accessor.object()->ppp_class_data(), accessor.identifier(), | |
| 65 result_converter.exception()); | |
| 66 result_converter.CheckExceptionForNoResult(); | |
| 67 return rv; | |
| 68 } | |
| 69 | |
| 70 bool WrapperClass_Invoke(NPObject* object, NPIdentifier method_name, | |
| 71 const NPVariant* argv, uint32_t argc, | |
| 72 NPVariant* result) { | |
| 73 NPObjectAccessorWithIdentifier accessor(object, method_name, false); | |
| 74 if (!accessor.is_valid()) | |
| 75 return false; | |
| 76 | |
| 77 PPResultAndExceptionToNPResult result_converter( | |
| 78 accessor.object()->GetNPObject(), result); | |
| 79 PPVarArrayFromNPVariantArray args(accessor.object()->instance(), | |
| 80 argc, argv); | |
| 81 | |
| 82 // For the OOP plugin case we need to grab a reference on the plugin module | |
| 83 // object to ensure that it is not destroyed courtsey an incoming | |
| 84 // ExecuteScript call which destroys the plugin module and in turn the | |
| 85 // dispatcher. | |
| 86 scoped_refptr<webkit::ppapi::PluginModule> ref( | |
| 87 accessor.object()->instance()->module()); | |
| 88 | |
| 89 return result_converter.SetResult(accessor.object()->ppp_class()->Call( | |
| 90 accessor.object()->ppp_class_data(), accessor.identifier(), | |
| 91 argc, args.array(), result_converter.exception())); | |
| 92 } | |
| 93 | |
| 94 bool WrapperClass_InvokeDefault(NPObject* np_object, const NPVariant* argv, | |
| 95 uint32_t argc, NPVariant* result) { | |
| 96 PluginObject* obj = PluginObject::FromNPObject(np_object); | |
| 97 if (!obj) | |
| 98 return false; | |
| 99 | |
| 100 PPVarArrayFromNPVariantArray args(obj->instance(), argc, argv); | |
| 101 PPResultAndExceptionToNPResult result_converter(obj->GetNPObject(), result); | |
| 102 | |
| 103 // For the OOP plugin case we need to grab a reference on the plugin module | |
| 104 // object to ensure that it is not destroyed courtsey an incoming | |
| 105 // ExecuteScript call which destroys the plugin module and in turn the | |
| 106 // dispatcher. | |
| 107 scoped_refptr<webkit::ppapi::PluginModule> ref( | |
| 108 obj->instance()->module()); | |
| 109 | |
| 110 result_converter.SetResult(obj->ppp_class()->Call( | |
| 111 obj->ppp_class_data(), PP_MakeUndefined(), argc, args.array(), | |
| 112 result_converter.exception())); | |
| 113 return result_converter.success(); | |
| 114 } | |
| 115 | |
| 116 bool WrapperClass_HasProperty(NPObject* object, NPIdentifier property_name) { | |
| 117 NPObjectAccessorWithIdentifier accessor(object, property_name, true); | |
| 118 if (!accessor.is_valid()) | |
| 119 return false; | |
| 120 | |
| 121 PPResultAndExceptionToNPResult result_converter( | |
| 122 accessor.object()->GetNPObject(), NULL); | |
| 123 bool rv = accessor.object()->ppp_class()->HasProperty( | |
| 124 accessor.object()->ppp_class_data(), accessor.identifier(), | |
| 125 result_converter.exception()); | |
| 126 result_converter.CheckExceptionForNoResult(); | |
| 127 return rv; | |
| 128 } | |
| 129 | |
| 130 bool WrapperClass_GetProperty(NPObject* object, NPIdentifier property_name, | |
| 131 NPVariant* result) { | |
| 132 NPObjectAccessorWithIdentifier accessor(object, property_name, true); | |
| 133 if (!accessor.is_valid()) | |
| 134 return false; | |
| 135 | |
| 136 PPResultAndExceptionToNPResult result_converter( | |
| 137 accessor.object()->GetNPObject(), result); | |
| 138 return result_converter.SetResult(accessor.object()->ppp_class()->GetProperty( | |
| 139 accessor.object()->ppp_class_data(), accessor.identifier(), | |
| 140 result_converter.exception())); | |
| 141 } | |
| 142 | |
| 143 bool WrapperClass_SetProperty(NPObject* object, NPIdentifier property_name, | |
| 144 const NPVariant* value) { | |
| 145 NPObjectAccessorWithIdentifier accessor(object, property_name, true); | |
| 146 if (!accessor.is_valid()) | |
| 147 return false; | |
| 148 | |
| 149 PPResultAndExceptionToNPResult result_converter( | |
| 150 accessor.object()->GetNPObject(), NULL); | |
| 151 PP_Var value_var = NPVariantToPPVar(accessor.object()->instance(), value); | |
| 152 accessor.object()->ppp_class()->SetProperty( | |
| 153 accessor.object()->ppp_class_data(), accessor.identifier(), value_var, | |
| 154 result_converter.exception()); | |
| 155 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(value_var); | |
| 156 return result_converter.CheckExceptionForNoResult(); | |
| 157 } | |
| 158 | |
| 159 bool WrapperClass_RemoveProperty(NPObject* object, NPIdentifier property_name) { | |
| 160 NPObjectAccessorWithIdentifier accessor(object, property_name, true); | |
| 161 if (!accessor.is_valid()) | |
| 162 return false; | |
| 163 | |
| 164 PPResultAndExceptionToNPResult result_converter( | |
| 165 accessor.object()->GetNPObject(), NULL); | |
| 166 accessor.object()->ppp_class()->RemoveProperty( | |
| 167 accessor.object()->ppp_class_data(), accessor.identifier(), | |
| 168 result_converter.exception()); | |
| 169 return result_converter.CheckExceptionForNoResult(); | |
| 170 } | |
| 171 | |
| 172 bool WrapperClass_Enumerate(NPObject* object, NPIdentifier** values, | |
| 173 uint32_t* count) { | |
| 174 *values = NULL; | |
| 175 *count = 0; | |
| 176 PluginObject* obj = PluginObject::FromNPObject(object); | |
| 177 if (!obj) | |
| 178 return false; | |
| 179 | |
| 180 uint32_t property_count = 0; | |
| 181 PP_Var* properties = NULL; // Must be freed! | |
| 182 PPResultAndExceptionToNPResult result_converter(obj->GetNPObject(), NULL); | |
| 183 obj->ppp_class()->GetAllPropertyNames(obj->ppp_class_data(), | |
| 184 &property_count, &properties, | |
| 185 result_converter.exception()); | |
| 186 | |
| 187 // Convert the array of PP_Var to an array of NPIdentifiers. If any | |
| 188 // conversions fail, we will set the exception. | |
| 189 if (!result_converter.has_exception()) { | |
| 190 if (property_count > 0) { | |
| 191 *values = static_cast<NPIdentifier*>( | |
| 192 malloc(sizeof(NPIdentifier) * property_count)); | |
| 193 *count = 0; // Will be the number of items successfully converted. | |
| 194 for (uint32_t i = 0; i < property_count; ++i) { | |
| 195 if (!((*values)[i] = PPVarToNPIdentifier(properties[i]))) { | |
| 196 // Throw an exception for the failed convertion. | |
| 197 *result_converter.exception() = | |
| 198 StringVar::StringToPPVar(kInvalidValueException); | |
| 199 break; | |
| 200 } | |
| 201 (*count)++; | |
| 202 } | |
| 203 | |
| 204 if (result_converter.has_exception()) { | |
| 205 // We don't actually have to free the identifiers we converted since | |
| 206 // all identifiers leak anyway :( . | |
| 207 free(*values); | |
| 208 *values = NULL; | |
| 209 *count = 0; | |
| 210 } | |
| 211 } | |
| 212 } | |
| 213 | |
| 214 // This will actually throw the exception, either from GetAllPropertyNames, | |
| 215 // or if anything was set during the conversion process. | |
| 216 result_converter.CheckExceptionForNoResult(); | |
| 217 | |
| 218 // Release the PP_Var that the plugin allocated. On success, they will all | |
| 219 // be converted to NPVariants, and on failure, we want them to just go away. | |
| 220 ::ppapi::VarTracker* var_tracker = PpapiGlobals::Get()->GetVarTracker(); | |
| 221 for (uint32_t i = 0; i < property_count; ++i) | |
| 222 var_tracker->ReleaseVar(properties[i]); | |
| 223 free(properties); | |
| 224 return result_converter.success(); | |
| 225 } | |
| 226 | |
| 227 bool WrapperClass_Construct(NPObject* object, const NPVariant* argv, | |
| 228 uint32_t argc, NPVariant* result) { | |
| 229 PluginObject* obj = PluginObject::FromNPObject(object); | |
| 230 if (!obj) | |
| 231 return false; | |
| 232 | |
| 233 PPVarArrayFromNPVariantArray args(obj->instance(), argc, argv); | |
| 234 PPResultAndExceptionToNPResult result_converter(obj->GetNPObject(), result); | |
| 235 return result_converter.SetResult(obj->ppp_class()->Construct( | |
| 236 obj->ppp_class_data(), argc, args.array(), | |
| 237 result_converter.exception())); | |
| 238 } | |
| 239 | |
| 240 const NPClass wrapper_class = { | |
| 241 NP_CLASS_STRUCT_VERSION, | |
| 242 WrapperClass_Allocate, | |
| 243 WrapperClass_Deallocate, | |
| 244 WrapperClass_Invalidate, | |
| 245 WrapperClass_HasMethod, | |
| 246 WrapperClass_Invoke, | |
| 247 WrapperClass_InvokeDefault, | |
| 248 WrapperClass_HasProperty, | |
| 249 WrapperClass_GetProperty, | |
| 250 WrapperClass_SetProperty, | |
| 251 WrapperClass_RemoveProperty, | |
| 252 WrapperClass_Enumerate, | |
| 253 WrapperClass_Construct | |
| 254 }; | |
| 255 | |
| 256 } // namespace | |
| 257 | |
| 258 // PluginObject ---------------------------------------------------------------- | |
| 259 | |
| 260 struct PluginObject::NPObjectWrapper : public NPObject { | |
| 261 // Points to the var object that owns this wrapper. This value may be NULL | |
| 262 // if there is no var owning this wrapper. This can happen if the plugin | |
| 263 // releases all references to the var, but a reference to the underlying | |
| 264 // NPObject is still held by script on the page. | |
| 265 PluginObject* obj; | |
| 266 }; | |
| 267 | |
| 268 PluginObject::PluginObject(PluginInstanceImpl* instance, | |
| 269 NPObjectWrapper* object_wrapper, | |
| 270 const PPP_Class_Deprecated* ppp_class, | |
| 271 void* ppp_class_data) | |
| 272 : instance_(instance), | |
| 273 object_wrapper_(object_wrapper), | |
| 274 ppp_class_(ppp_class), | |
| 275 ppp_class_data_(ppp_class_data) { | |
| 276 // Make the object wrapper refer back to this class so our NPObject | |
| 277 // implementation can call back into the Pepper layer. | |
| 278 object_wrapper_->obj = this; | |
| 279 instance_->AddPluginObject(this); | |
| 280 } | |
| 281 | |
| 282 PluginObject::~PluginObject() { | |
| 283 // The wrapper we made for this NPObject may still have a reference to it | |
| 284 // from JavaScript, so we clear out its ObjectVar back pointer which will | |
| 285 // cause all calls "up" to the plugin to become NOPs. Our ObjectVar base | |
| 286 // class will release our reference to the object, which may or may not | |
| 287 // delete the NPObject. | |
| 288 DCHECK(object_wrapper_->obj == this); | |
| 289 object_wrapper_->obj = NULL; | |
| 290 instance_->RemovePluginObject(this); | |
| 291 } | |
| 292 | |
| 293 PP_Var PluginObject::Create(PluginInstanceImpl* instance, | |
| 294 const PPP_Class_Deprecated* ppp_class, | |
| 295 void* ppp_class_data) { | |
| 296 // This will internally end up calling our AllocateObjectWrapper via the | |
| 297 // WrapperClass_Allocated function which will have created an object wrapper | |
| 298 // appropriate for this class (derived from NPObject). | |
| 299 NPObjectWrapper* wrapper = static_cast<NPObjectWrapper*>( | |
| 300 WebBindings::createObject(instance->instanceNPP(), | |
| 301 const_cast<NPClass*>(&wrapper_class))); | |
| 302 | |
| 303 // This object will register itself both with the NPObject and with the | |
| 304 // PluginModule. The NPObject will normally handle its lifetime, and it | |
| 305 // will get deleted in the destroy method. It may also get deleted when the | |
| 306 // plugin module is deallocated. | |
| 307 new PluginObject(instance, wrapper, ppp_class, ppp_class_data); | |
| 308 | |
| 309 // We can just use a normal ObjectVar to refer to this object from the | |
| 310 // plugin. It will hold a ref to the underlying NPObject which will in turn | |
| 311 // hold our pluginObject. | |
| 312 PP_Var obj_var(NPObjectToPPVar(instance, wrapper)); | |
| 313 | |
| 314 // Note that the ObjectVar constructor incremented the reference count, and so | |
| 315 // did WebBindings::createObject above. Now that the PP_Var has taken | |
| 316 // ownership, we need to release to balance out the createObject reference | |
| 317 // count bump. | |
| 318 WebBindings::releaseObject(wrapper); | |
| 319 return obj_var; | |
| 320 } | |
| 321 | |
| 322 NPObject* PluginObject::GetNPObject() const { | |
| 323 return object_wrapper_; | |
| 324 } | |
| 325 | |
| 326 // static | |
| 327 bool PluginObject::IsInstanceOf(NPObject* np_object, | |
| 328 const PPP_Class_Deprecated* ppp_class, | |
| 329 void** ppp_class_data) { | |
| 330 // Validate that this object is implemented by our wrapper class before | |
| 331 // trying to get the PluginObject. | |
| 332 if (np_object->_class != &wrapper_class) | |
| 333 return false; | |
| 334 | |
| 335 PluginObject* plugin_object = FromNPObject(np_object); | |
| 336 if (!plugin_object) | |
| 337 return false; // Object is no longer alive. | |
| 338 | |
| 339 if (plugin_object->ppp_class() != ppp_class) | |
| 340 return false; | |
| 341 if (ppp_class_data) | |
| 342 *ppp_class_data = plugin_object->ppp_class_data(); | |
| 343 return true; | |
| 344 } | |
| 345 | |
| 346 // static | |
| 347 PluginObject* PluginObject::FromNPObject(NPObject* object) { | |
| 348 return static_cast<NPObjectWrapper*>(object)->obj; | |
| 349 } | |
| 350 | |
| 351 // static | |
| 352 NPObject* PluginObject::AllocateObjectWrapper() { | |
| 353 NPObjectWrapper* wrapper = new NPObjectWrapper; | |
| 354 memset(wrapper, 0, sizeof(NPObjectWrapper)); | |
| 355 return wrapper; | |
| 356 } | |
| 357 | |
| 358 } // namespace ppapi | |
| 359 } // namespace webkit | |
| 360 | |
| OLD | NEW |