From 859a7f3a9264b3a2174e9625d6cfa20412ac6081 Mon Sep 17 00:00:00 2001 From: "brettw@chromium.org" Date: Sat, 15 Jan 2011 03:44:13 +0000 Subject: Make PP_Resources associated with the Instance rather than the module. This adds PP_Instance to the necessary places in the API to make this possible. String and Object vars used to be PP_Resources. But it is not practical to assocaited strings with an instance since then we can't have implicit var constructors and have to litter every string with an instance. So this changes vars to use their own tracking system associated with the module (i.e. keeping the current semantics) and making it no longer a resource. I made the internal Var IDs 32 bits since Neb is about to land his 64->32 change. Now it force-deletes resources associated with an instance when that instance goes away. I added some additional code and tracking in ResourceTracker to do this. I could then remove the Instance::Observer class since the resource can use the (now renamed) StoppedTracking to know that it's being deleted in response to the instance being destroyed. TEST=ppapi ui tests BUG=none git-svn-id: svn://svn.chromium.org/chrome/trunk/src@71544 0039d316-1c4b-4281-b951-d872f2087c98 --- webkit/plugins/ppapi/callbacks_unittest.cc | 21 +++-- webkit/plugins/ppapi/mock_resource.h | 2 +- webkit/plugins/ppapi/npapi_glue.cc | 13 ++- webkit/plugins/ppapi/npapi_glue.h | 5 +- webkit/plugins/ppapi/plugin_module.cc | 52 ---------- webkit/plugins/ppapi/plugin_module.h | 27 ------ webkit/plugins/ppapi/plugin_object.cc | 27 +++--- webkit/plugins/ppapi/plugin_object.h | 10 +- webkit/plugins/ppapi/ppapi_plugin_instance.cc | 66 ++++++++++--- webkit/plugins/ppapi/ppapi_plugin_instance.h | 45 +++++---- webkit/plugins/ppapi/ppapi_unittest.h | 1 + webkit/plugins/ppapi/ppb_audio_impl.cc | 24 ++--- webkit/plugins/ppapi/ppb_audio_impl.h | 13 +-- webkit/plugins/ppapi/ppb_buffer_impl.cc | 14 ++- webkit/plugins/ppapi/ppb_buffer_impl.h | 2 +- webkit/plugins/ppapi/ppb_char_set_impl.cc | 19 ++-- webkit/plugins/ppapi/ppb_context_3d_impl.cc | 3 +- webkit/plugins/ppapi/ppb_directory_reader_impl.cc | 7 +- webkit/plugins/ppapi/ppb_file_chooser_impl.cc | 12 +-- webkit/plugins/ppapi/ppb_file_chooser_impl.h | 3 +- webkit/plugins/ppapi/ppb_file_io_impl.cc | 31 +++--- webkit/plugins/ppapi/ppb_file_io_impl.h | 3 +- webkit/plugins/ppapi/ppb_file_ref_impl.cc | 28 +++--- webkit/plugins/ppapi/ppb_file_ref_impl.h | 4 +- webkit/plugins/ppapi/ppb_file_system_impl.cc | 2 +- webkit/plugins/ppapi/ppb_flash_impl.cc | 41 ++++---- webkit/plugins/ppapi/ppb_flash_impl.h | 8 +- webkit/plugins/ppapi/ppb_flash_impl_linux.cc | 3 +- webkit/plugins/ppapi/ppb_font_impl.cc | 16 ++-- webkit/plugins/ppapi/ppb_font_impl.h | 2 +- webkit/plugins/ppapi/ppb_graphics_2d_impl.cc | 12 +-- webkit/plugins/ppapi/ppb_graphics_2d_impl.h | 2 +- webkit/plugins/ppapi/ppb_image_data_impl.cc | 11 +-- webkit/plugins/ppapi/ppb_image_data_impl.h | 2 +- webkit/plugins/ppapi/ppb_pdf_impl.cc | 33 ++++--- webkit/plugins/ppapi/ppb_scrollbar_impl.cc | 4 +- webkit/plugins/ppapi/ppb_surface_3d_impl.cc | 3 +- webkit/plugins/ppapi/ppb_surface_3d_impl.h | 5 - webkit/plugins/ppapi/ppb_transport_impl.cc | 6 +- webkit/plugins/ppapi/ppb_transport_impl.h | 2 +- webkit/plugins/ppapi/ppb_url_loader_impl.cc | 63 ++++--------- webkit/plugins/ppapi/ppb_url_loader_impl.h | 15 +-- webkit/plugins/ppapi/ppb_url_request_info_impl.cc | 12 +-- webkit/plugins/ppapi/ppb_url_request_info_impl.h | 2 +- webkit/plugins/ppapi/ppb_url_response_info_impl.cc | 17 ++-- webkit/plugins/ppapi/ppb_url_response_info_impl.h | 2 +- webkit/plugins/ppapi/ppb_url_util_impl.cc | 1 + webkit/plugins/ppapi/ppb_video_decoder_impl.cc | 3 +- webkit/plugins/ppapi/ppb_video_decoder_impl.h | 3 - webkit/plugins/ppapi/ppb_widget_impl.cc | 5 +- webkit/plugins/ppapi/resource.cc | 13 ++- webkit/plugins/ppapi/resource.h | 38 +++++--- webkit/plugins/ppapi/resource_tracker.cc | 92 ++++++++++++++++-- webkit/plugins/ppapi/resource_tracker.h | 34 ++++++- webkit/plugins/ppapi/resource_tracker_unittest.cc | 36 ++++--- webkit/plugins/ppapi/url_request_info_unittest.cc | 2 +- webkit/plugins/ppapi/var.cc | 105 +++++++++++++-------- webkit/plugins/ppapi/var.h | 46 ++++++--- webkit/plugins/ppapi/var_object_class.cc | 56 +++++------ webkit/plugins/ppapi/var_object_class.h | 4 +- 60 files changed, 605 insertions(+), 528 deletions(-) (limited to 'webkit/plugins') diff --git a/webkit/plugins/ppapi/callbacks_unittest.cc b/webkit/plugins/ppapi/callbacks_unittest.cc index 3fd92b9..9426cd3 100644 --- a/webkit/plugins/ppapi/callbacks_unittest.cc +++ b/webkit/plugins/ppapi/callbacks_unittest.cc @@ -11,6 +11,7 @@ #include "webkit/plugins/ppapi/callbacks.h" #include "webkit/plugins/ppapi/mock_resource.h" #include "webkit/plugins/ppapi/plugin_module.h" +#include "webkit/plugins/ppapi/ppapi_plugin_instance.h" #include "webkit/plugins/ppapi/resource_tracker.h" namespace webkit { @@ -60,7 +61,7 @@ TEST_F(CallbackShutdownTest, AbortOnShutdown) { EXPECT_EQ(0U, info_did_run().run_count); scoped_refptr callback_did_run = new TrackedCompletionCallback( - module()->GetCallbackTracker(), + instance()->module()->GetCallbackTracker(), 0, PP_MakeCompletionCallback(&TestCallback, &info_did_run())); EXPECT_EQ(0U, info_did_run().run_count); @@ -72,7 +73,7 @@ TEST_F(CallbackShutdownTest, AbortOnShutdown) { EXPECT_EQ(0U, info_did_abort().run_count); scoped_refptr callback_did_abort = new TrackedCompletionCallback( - module()->GetCallbackTracker(), + instance()->module()->GetCallbackTracker(), 0, PP_MakeCompletionCallback(&TestCallback, &info_did_abort())); EXPECT_EQ(0U, info_did_abort().run_count); @@ -85,7 +86,7 @@ TEST_F(CallbackShutdownTest, AbortOnShutdown) { EXPECT_EQ(0U, info_didnt_run().run_count); scoped_refptr callback_didnt_run = new TrackedCompletionCallback( - module()->GetCallbackTracker(), + instance()->module()->GetCallbackTracker(), 0, PP_MakeCompletionCallback(&TestCallback, &info_didnt_run())); EXPECT_EQ(0U, info_didnt_run().run_count); @@ -109,7 +110,7 @@ namespace { class CallbackMockResource : public MockResource { public: - CallbackMockResource(PluginModule* module) : MockResource(module) {} + CallbackMockResource(PluginInstance* instance) : MockResource(instance) {} ~CallbackMockResource() {} PP_Resource SetupForTest() { @@ -117,19 +118,19 @@ class CallbackMockResource : public MockResource { EXPECT_NE(0, resource_id); callback_did_run_ = new TrackedCompletionCallback( - module()->GetCallbackTracker(), + instance()->module()->GetCallbackTracker(), resource_id, PP_MakeCompletionCallback(&TestCallback, &info_did_run_)); EXPECT_EQ(0U, info_did_run_.run_count); callback_did_abort_ = new TrackedCompletionCallback( - module()->GetCallbackTracker(), + instance()->module()->GetCallbackTracker(), resource_id, PP_MakeCompletionCallback(&TestCallback, &info_did_abort_)); EXPECT_EQ(0U, info_did_abort_.run_count); callback_didnt_run_ = new TrackedCompletionCallback( - module()->GetCallbackTracker(), + instance()->module()->GetCallbackTracker(), resource_id, PP_MakeCompletionCallback(&TestCallback, &info_didnt_run_)); EXPECT_EQ(0U, info_didnt_run_.run_count); @@ -187,13 +188,13 @@ TEST_F(CallbackResourceTest, AbortOnNoRef) { // Check that the uncompleted one gets aborted, and that the others don't get // called again. scoped_refptr resource_1( - new CallbackMockResource(module())); + new CallbackMockResource(instance())); PP_Resource resource_1_id = resource_1->SetupForTest(); // Also do the same for a second resource, and make sure that unref-ing the // first resource doesn't much up the second resource. scoped_refptr resource_2( - new CallbackMockResource(module())); + new CallbackMockResource(instance())); PP_Resource resource_2_id = resource_2->SetupForTest(); // Double-check that resource #1 is still okay. @@ -222,7 +223,7 @@ TEST_F(CallbackResourceTest, Resurrection) { ResourceTracker* resource_tracker = ResourceTracker::Get(); scoped_refptr resource( - new CallbackMockResource(module())); + new CallbackMockResource(instance())); PP_Resource resource_id = resource->SetupForTest(); // Unref it, spin the message loop to run posted calls, and check that things diff --git a/webkit/plugins/ppapi/mock_resource.h b/webkit/plugins/ppapi/mock_resource.h index 081c990..d8034c9 100644 --- a/webkit/plugins/ppapi/mock_resource.h +++ b/webkit/plugins/ppapi/mock_resource.h @@ -15,7 +15,7 @@ namespace ppapi { // static_cast to get its own implementation. class MockResource : public Resource { public: - MockResource(PluginModule* module) : Resource(module) {} + MockResource(PluginInstance* instance) : Resource(instance) {} ~MockResource() {} virtual MockResource* AsMockResource() { return this; } diff --git a/webkit/plugins/ppapi/npapi_glue.cc b/webkit/plugins/ppapi/npapi_glue.cc index d22c13f..478d524 100644 --- a/webkit/plugins/ppapi/npapi_glue.cc +++ b/webkit/plugins/ppapi/npapi_glue.cc @@ -7,6 +7,7 @@ #include "base/logging.h" #include "base/ref_counted.h" #include "base/string_util.h" +#include "webkit/plugins/ppapi/ppapi_plugin_instance.h" #include "webkit/plugins/ppapi/plugin_object.h" #include "webkit/plugins/ppapi/var.h" #include "third_party/npapi/bindings/npapi.h" @@ -150,14 +151,15 @@ void PPResultAndExceptionToNPResult::ThrowException() { // PPVarArrayFromNPVariantArray ------------------------------------------------ -PPVarArrayFromNPVariantArray::PPVarArrayFromNPVariantArray(PluginModule* module, +PPVarArrayFromNPVariantArray::PPVarArrayFromNPVariantArray( + PluginInstance* instance, size_t size, const NPVariant* variants) : size_(size) { if (size_ > 0) { array_.reset(new PP_Var[size_]); for (size_t i = 0; i < size_; i++) - array_[i] = Var::NPVariantToPPVar(module, &variants[i]); + array_[i] = Var::NPVariantToPPVar(instance, &variants[i]); } } @@ -168,8 +170,8 @@ PPVarArrayFromNPVariantArray::~PPVarArrayFromNPVariantArray() { // PPVarFromNPObject ----------------------------------------------------------- -PPVarFromNPObject::PPVarFromNPObject(PluginModule* module, NPObject* object) - : var_(ObjectVar::NPObjectToPPVar(module, object)) { +PPVarFromNPObject::PPVarFromNPObject(PluginInstance* instance, NPObject* object) + : var_(ObjectVar::NPObjectToPPVar(instance, object)) { } PPVarFromNPObject::~PPVarFromNPObject() { @@ -185,7 +187,8 @@ NPObjectAccessorWithIdentifier::NPObjectAccessorWithIdentifier( : object_(PluginObject::FromNPObject(object)), identifier_(PP_MakeUndefined()) { if (object_) { - identifier_ = Var::NPIdentifierToPPVar(object_->module(), identifier); + identifier_ = Var::NPIdentifierToPPVar(object_->instance()->module(), + identifier); if (identifier_.type == PP_VARTYPE_INT32 && !allow_integer_identifier) identifier_.type = PP_VARTYPE_UNDEFINED; // Mark it invalid. } diff --git a/webkit/plugins/ppapi/npapi_glue.h b/webkit/plugins/ppapi/npapi_glue.h index 11fd82a..4a887f1 100644 --- a/webkit/plugins/ppapi/npapi_glue.h +++ b/webkit/plugins/ppapi/npapi_glue.h @@ -16,6 +16,7 @@ typedef void* NPIdentifier; namespace webkit { namespace ppapi { +class PluginInstance; class PluginModule; class PluginObject; @@ -108,7 +109,7 @@ class PPResultAndExceptionToNPResult { // WebKit to the plugin. class PPVarArrayFromNPVariantArray { public: - PPVarArrayFromNPVariantArray(PluginModule* module, + PPVarArrayFromNPVariantArray(PluginInstance* instance, size_t size, const NPVariant* variants); ~PPVarArrayFromNPVariantArray(); @@ -128,7 +129,7 @@ class PPVarArrayFromNPVariantArray { // is used when converting 'this' pointer from WebKit to the plugin. class PPVarFromNPObject { public: - PPVarFromNPObject(PluginModule* module, NPObject* object); + PPVarFromNPObject(PluginInstance* instance, NPObject* object); ~PPVarFromNPObject(); PP_Var var() const { return var_; } diff --git a/webkit/plugins/ppapi/plugin_module.cc b/webkit/plugins/ppapi/plugin_module.cc index e1c5ce0..ea0188d 100644 --- a/webkit/plugins/ppapi/plugin_module.cc +++ b/webkit/plugins/ppapi/plugin_module.cc @@ -55,7 +55,6 @@ #include "ppapi/c/trusted/ppb_url_loader_trusted.h" #include "webkit/plugins/ppapi/callbacks.h" #include "webkit/plugins/ppapi/common.h" -#include "webkit/plugins/ppapi/plugin_object.h" #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" #include "webkit/plugins/ppapi/ppb_audio_impl.h" #include "webkit/plugins/ppapi/ppb_buffer_impl.h" @@ -367,17 +366,6 @@ PluginModule::PluginModule(PluginDelegate::ModuleLifetime* lifetime_delegate) } PluginModule::~PluginModule() { - // Free all the plugin objects. This will automatically clear the back- - // pointer from the NPObject so WebKit can't call into the plugin any more. - // - // Swap out the set so we can delete from it (the objects will try to - // unregister themselves inside the delete call). - PluginObjectSet plugin_object_copy; - live_plugin_objects_.swap(plugin_object_copy); - for (PluginObjectSet::iterator i = live_plugin_objects_.begin(); - i != live_plugin_objects_.end(); ++i) - delete *i; - // When the module is being deleted, there should be no more instances still // holding a reference to us. DCHECK(instances_.empty()); @@ -474,46 +462,6 @@ void PluginModule::InstanceDeleted(PluginInstance* instance) { instances_.erase(instance); } -void PluginModule::AddNPObjectVar(ObjectVar* object_var) { - DCHECK(np_object_to_object_var_.find(object_var->np_object()) == - np_object_to_object_var_.end()) << "ObjectVar already in map"; - np_object_to_object_var_[object_var->np_object()] = object_var; -} - -void PluginModule::RemoveNPObjectVar(ObjectVar* object_var) { - NPObjectToObjectVarMap::iterator found = - np_object_to_object_var_.find(object_var->np_object()); - if (found == np_object_to_object_var_.end()) { - NOTREACHED() << "ObjectVar not registered."; - return; - } - if (found->second != object_var) { - NOTREACHED() << "ObjectVar doesn't match."; - return; - } - np_object_to_object_var_.erase(found); -} - -ObjectVar* PluginModule::ObjectVarForNPObject(NPObject* np_object) const { - NPObjectToObjectVarMap::const_iterator found = - np_object_to_object_var_.find(np_object); - if (found == np_object_to_object_var_.end()) - return NULL; - return found->second; -} - -void PluginModule::AddPluginObject(PluginObject* plugin_object) { - DCHECK(live_plugin_objects_.find(plugin_object) == - live_plugin_objects_.end()); - live_plugin_objects_.insert(plugin_object); -} - -void PluginModule::RemovePluginObject(PluginObject* plugin_object) { - // Don't actually verify that the object is in the set since during module - // deletion we'll be in the process of freeing them. - live_plugin_objects_.erase(plugin_object); -} - scoped_refptr PluginModule::GetCallbackTracker() { return callback_tracker_; } diff --git a/webkit/plugins/ppapi/plugin_module.h b/webkit/plugins/ppapi/plugin_module.h index 913b003..cfdfb6af 100644 --- a/webkit/plugins/ppapi/plugin_module.h +++ b/webkit/plugins/ppapi/plugin_module.h @@ -20,7 +20,6 @@ class FilePath; class MessageLoop; -typedef struct NPObject NPObject; struct PPB_Core; typedef void* NPIdentifier; @@ -42,10 +41,8 @@ namespace webkit { namespace ppapi { class CallbackTracker; -class ObjectVar; class PluginDelegate; class PluginInstance; -class PluginObject; // Represents one plugin library loaded into one renderer. This library may // have multiple instances. @@ -122,22 +119,6 @@ class PluginModule : public base::RefCounted, void InstanceCreated(PluginInstance* instance); void InstanceDeleted(PluginInstance* instance); - // Tracks all live ObjectVar. This is so we can map between PluginModule + - // NPObject and get the ObjectVar corresponding to it. This Add/Remove - // function should be called by the ObjectVar when it is created and - // destroyed. - void AddNPObjectVar(ObjectVar* object_var); - void RemoveNPObjectVar(ObjectVar* object_var); - - // Looks up a previously registered ObjectVar for the given NPObject and - // module. Returns NULL if there is no ObjectVar corresponding to the given - // NPObject for the given module. See AddNPObjectVar above. - ObjectVar* ObjectVarForNPObject(NPObject* np_object) const; - - // Tracks all live PluginObjects. - void AddPluginObject(PluginObject* plugin_object); - void RemovePluginObject(PluginObject* plugin_object); - scoped_refptr GetCallbackTracker(); private: @@ -178,14 +159,6 @@ class PluginModule : public base::RefCounted, typedef std::set PluginInstanceSet; PluginInstanceSet instances_; - // Tracks all live ObjectVars used by this module so we can map NPObjects to - // the corresponding object. These are non-owning references. - typedef std::map NPObjectToObjectVarMap; - NPObjectToObjectVarMap np_object_to_object_var_; - - typedef std::set PluginObjectSet; - PluginObjectSet live_plugin_objects_; - DISALLOW_COPY_AND_ASSIGN(PluginModule); }; diff --git a/webkit/plugins/ppapi/plugin_object.cc b/webkit/plugins/ppapi/plugin_object.cc index 516f255..e17d42a 100644 --- a/webkit/plugins/ppapi/plugin_object.cc +++ b/webkit/plugins/ppapi/plugin_object.cc @@ -19,6 +19,7 @@ #include "third_party/WebKit/WebKit/chromium/public/WebBindings.h" #include "webkit/plugins/ppapi/npapi_glue.h" #include "webkit/plugins/ppapi/plugin_module.h" +#include "webkit/plugins/ppapi/ppapi_plugin_instance.h" #include "webkit/plugins/ppapi/resource.h" #include "webkit/plugins/ppapi/string.h" #include "webkit/plugins/ppapi/var.h" @@ -73,7 +74,8 @@ bool WrapperClass_Invoke(NPObject* object, NPIdentifier method_name, PPResultAndExceptionToNPResult result_converter( accessor.object()->GetNPObject(), result); - PPVarArrayFromNPVariantArray args(accessor.object()->module(), argc, argv); + PPVarArrayFromNPVariantArray args(accessor.object()->instance(), + argc, argv); return result_converter.SetResult(accessor.object()->ppp_class()->Call( accessor.object()->ppp_class_data(), accessor.identifier(), @@ -86,7 +88,7 @@ bool WrapperClass_InvokeDefault(NPObject* np_object, const NPVariant* argv, if (!obj) return false; - PPVarArrayFromNPVariantArray args(obj->module(), argc, argv); + PPVarArrayFromNPVariantArray args(obj->instance(), argc, argv); PPResultAndExceptionToNPResult result_converter(obj->GetNPObject(), result); result_converter.SetResult(obj->ppp_class()->Call( @@ -130,7 +132,8 @@ bool WrapperClass_SetProperty(NPObject* object, NPIdentifier property_name, PPResultAndExceptionToNPResult result_converter( accessor.object()->GetNPObject(), NULL); - PP_Var value_var = Var::NPVariantToPPVar(accessor.object()->module(), value); + PP_Var value_var = Var::NPVariantToPPVar(accessor.object()->instance(), + value); accessor.object()->ppp_class()->SetProperty( accessor.object()->ppp_class_data(), accessor.identifier(), value_var, result_converter.exception()); @@ -177,7 +180,7 @@ bool WrapperClass_Enumerate(NPObject* object, NPIdentifier** values, if (!((*values)[i] = Var::PPVarToNPIdentifier(properties[i]))) { // Throw an exception for the failed convertion. *result_converter.exception() = StringVar::StringToPPVar( - obj->module(), kInvalidValueException); + obj->instance()->module(), kInvalidValueException); break; } (*count)++; @@ -211,7 +214,7 @@ bool WrapperClass_Construct(NPObject* object, const NPVariant* argv, if (!obj) return false; - PPVarArrayFromNPVariantArray args(obj->module(), argc, argv); + PPVarArrayFromNPVariantArray args(obj->instance(), argc, argv); PPResultAndExceptionToNPResult result_converter(obj->GetNPObject(), result); return result_converter.SetResult(obj->ppp_class()->Construct( obj->ppp_class_data(), argc, args.array(), @@ -246,18 +249,18 @@ struct PluginObject::NPObjectWrapper : public NPObject { PluginObject* obj; }; -PluginObject::PluginObject(PluginModule* module, +PluginObject::PluginObject(PluginInstance* instance, NPObjectWrapper* object_wrapper, const PPP_Class_Deprecated* ppp_class, void* ppp_class_data) - : module_(module), + : instance_(instance), object_wrapper_(object_wrapper), ppp_class_(ppp_class), ppp_class_data_(ppp_class_data) { // Make the object wrapper refer back to this class so our NPObject // implementation can call back into the Pepper layer. object_wrapper_->obj = this; - module_->AddPluginObject(this); + instance_->AddPluginObject(this); } PluginObject::~PluginObject() { @@ -268,10 +271,10 @@ PluginObject::~PluginObject() { // delete the NPObject. DCHECK(object_wrapper_->obj == this); object_wrapper_->obj = NULL; - module_->RemovePluginObject(this); + instance_->RemovePluginObject(this); } -PP_Var PluginObject::Create(PluginModule* module, +PP_Var PluginObject::Create(PluginInstance* instance, const PPP_Class_Deprecated* ppp_class, void* ppp_class_data) { // This will internally end up calling our AllocateObjectWrapper via the @@ -284,12 +287,12 @@ PP_Var PluginObject::Create(PluginModule* module, // PluginModule. The NPObject will normally handle its lifetime, and it // will get deleted in the destroy method. It may also get deleted when the // plugin module is deallocated. - new PluginObject(module, wrapper, ppp_class, ppp_class_data); + new PluginObject(instance, wrapper, ppp_class, ppp_class_data); // We can just use a normal ObjectVar to refer to this object from the // plugin. It will hold a ref to the underlying NPObject which will in turn // hold our pluginObject. - return ObjectVar::NPObjectToPPVar(module, wrapper); + return ObjectVar::NPObjectToPPVar(instance, wrapper); } NPObject* PluginObject::GetNPObject() const { diff --git a/webkit/plugins/ppapi/plugin_object.h b/webkit/plugins/ppapi/plugin_object.h index 01637e5..9f4b607 100644 --- a/webkit/plugins/ppapi/plugin_object.h +++ b/webkit/plugins/ppapi/plugin_object.h @@ -17,7 +17,7 @@ typedef struct _NPVariant NPVariant; namespace webkit { namespace ppapi { -class PluginModule; +class PluginInstance; class PluginObject { public: @@ -25,11 +25,11 @@ class PluginObject { // Allocates a new PluginObject and returns it as a PP_Var with a // refcount of 1. - static PP_Var Create(PluginModule* module, + static PP_Var Create(PluginInstance* instance, const PPP_Class_Deprecated* ppp_class, void* ppp_class_data); - PluginModule* module() const { return module_; } + PluginInstance* instance() const { return instance_; } const PPP_Class_Deprecated* ppp_class() { return ppp_class_; } void* ppp_class_data() { return ppp_class_data_; }; @@ -64,12 +64,12 @@ class PluginObject { // // The NPObjectWrapper (an NPObject) should already have the reference // incremented on it, and this class will take ownership of that reference. - PluginObject(PluginModule* module, + PluginObject(PluginInstance* instance, NPObjectWrapper* object_wrapper, const PPP_Class_Deprecated* ppp_class, void* ppp_class_data); - PluginModule* module_; + PluginInstance* instance_; // Holds a pointer to the NPObject wrapper backing the var. This class // derives from NPObject and we hold a reference to it, so it must be diff --git a/webkit/plugins/ppapi/ppapi_plugin_instance.cc b/webkit/plugins/ppapi/ppapi_plugin_instance.cc index 7533a79..5179497 100644 --- a/webkit/plugins/ppapi/ppapi_plugin_instance.cc +++ b/webkit/plugins/ppapi/ppapi_plugin_instance.cc @@ -44,6 +44,7 @@ #include "webkit/plugins/ppapi/fullscreen_container.h" #include "webkit/plugins/ppapi/plugin_delegate.h" #include "webkit/plugins/ppapi/plugin_module.h" +#include "webkit/plugins/ppapi/plugin_object.h" #include "webkit/plugins/ppapi/ppb_buffer_impl.h" #include "webkit/plugins/ppapi/ppb_graphics_2d_impl.h" #include "webkit/plugins/ppapi/ppb_image_data_impl.h" @@ -320,7 +321,16 @@ PluginInstance::PluginInstance(PluginDelegate* delegate, } PluginInstance::~PluginInstance() { - FOR_EACH_OBSERVER(Observer, observers_, InstanceDestroyed(this)); + // Free all the plugin objects. This will automatically clear the back- + // pointer from the NPObject so WebKit can't call into the plugin any more. + // + // Swap out the set so we can delete from it (the objects will try to + // unregister themselves inside the delete call). + PluginObjectSet plugin_object_copy; + live_plugin_objects_.swap(plugin_object_copy); + for (PluginObjectSet::iterator i = live_plugin_objects_.begin(); + i != live_plugin_objects_.end(); ++i) + delete *i; delegate_->InstanceDeleted(this); module_->InstanceDeleted(this); @@ -351,14 +361,6 @@ const PPB_Zoom_Dev* PluginInstance::GetZoomInterface() { return &ppb_zoom; } -void PluginInstance::AddObserver(Observer* observer) { - observers_.AddObserver(observer); -} - -void PluginInstance::RemoveObserver(Observer* observer) { - observers_.RemoveObserver(observer); -} - void PluginInstance::Paint(WebCanvas* canvas, const gfx::Rect& plugin_rect, const gfx::Rect& paint_rect) { @@ -418,13 +420,13 @@ PP_Var PluginInstance::GetWindowObject() { if (!frame) return PP_MakeUndefined(); - return ObjectVar::NPObjectToPPVar(module(), frame->windowObject()); + return ObjectVar::NPObjectToPPVar(this, frame->windowObject()); } PP_Var PluginInstance::GetOwnerElementObject() { if (!container_) return PP_MakeUndefined(); - return ObjectVar::NPObjectToPPVar(module(), + return ObjectVar::NPObjectToPPVar(this, container_->scriptableObjectForElement()); } @@ -525,7 +527,7 @@ PP_Var PluginInstance::ExecuteScript(PP_Var script, PP_Var* exception) { return PP_MakeUndefined(); } - PP_Var ret = Var::NPVariantToPPVar(module_, &result); + PP_Var ret = Var::NPVariantToPPVar(this, &result); WebBindings::releaseVariantValue(&result); return ret; } @@ -1189,5 +1191,45 @@ PPB_Surface3D_Impl* PluginInstance::bound_graphics_3d() const { return bound_graphics_->Cast(); } +void PluginInstance::AddPluginObject(PluginObject* plugin_object) { + DCHECK(live_plugin_objects_.find(plugin_object) == + live_plugin_objects_.end()); + live_plugin_objects_.insert(plugin_object); +} + +void PluginInstance::RemovePluginObject(PluginObject* plugin_object) { + // Don't actually verify that the object is in the set since during module + // deletion we'll be in the process of freeing them. + live_plugin_objects_.erase(plugin_object); +} + +void PluginInstance::AddNPObjectVar(ObjectVar* object_var) { + DCHECK(np_object_to_object_var_.find(object_var->np_object()) == + np_object_to_object_var_.end()) << "ObjectVar already in map"; + np_object_to_object_var_[object_var->np_object()] = object_var; +} + +void PluginInstance::RemoveNPObjectVar(ObjectVar* object_var) { + NPObjectToObjectVarMap::iterator found = + np_object_to_object_var_.find(object_var->np_object()); + if (found == np_object_to_object_var_.end()) { + NOTREACHED() << "ObjectVar not registered."; + return; + } + if (found->second != object_var) { + NOTREACHED() << "ObjectVar doesn't match."; + return; + } + np_object_to_object_var_.erase(found); +} + +ObjectVar* PluginInstance::ObjectVarForNPObject(NPObject* np_object) const { + NPObjectToObjectVarMap::const_iterator found = + np_object_to_object_var_.find(np_object); + if (found == np_object_to_object_var_.end()) + return NULL; + return found->second; +} + } // namespace ppapi } // namespace webkit diff --git a/webkit/plugins/ppapi/ppapi_plugin_instance.h b/webkit/plugins/ppapi/ppapi_plugin_instance.h index a4624e6..8a220c6 100644 --- a/webkit/plugins/ppapi/ppapi_plugin_instance.h +++ b/webkit/plugins/ppapi/ppapi_plugin_instance.h @@ -5,11 +5,12 @@ #ifndef WEBKIT_PLUGINS_PPAPI_PPAPI_PLUGIN_INSTANCE_H_ #define WEBKIT_PLUGINS_PPAPI_PPAPI_PLUGIN_INSTANCE_H_ +#include +#include #include #include #include "base/basictypes.h" -#include "base/observer_list.h" #include "base/ref_counted.h" #include "base/scoped_ptr.h" #include "base/string16.h" @@ -23,6 +24,7 @@ #include "third_party/WebKit/WebKit/chromium/public/WebCanvas.h" #include "webkit/plugins/ppapi/plugin_delegate.h" +typedef struct NPObject NPObject; struct PP_Var; struct PPB_Instance; struct PPB_Find_Dev; @@ -51,8 +53,10 @@ namespace webkit { namespace ppapi { class FullscreenContainer; +class ObjectVar; class PluginDelegate; class PluginModule; +class PluginObject; class PPB_Graphics2D_Impl; class PPB_ImageData_Impl; class PPB_Surface3D_Impl; @@ -65,14 +69,6 @@ class Resource; // ResourceTracker. class PluginInstance : public base::RefCounted { public: - class Observer { - public: - // Indicates that the instance is being destroyed. This will be called from - // the instance's destructor so don't do anything in this callback that - // uses the instance. - virtual void InstanceDestroyed(PluginInstance* instance) = 0; - }; - PluginInstance(PluginDelegate* delegate, PluginModule* module, const PPP_Instance* instance_interface); @@ -102,12 +98,6 @@ class PluginInstance : public base::RefCounted { // nonzero. PP_Instance pp_instance() const { return pp_instance_; } - // Other classes can register an observer for instance events. These pointers - // are NOT owned by the Instance. If the object implementing the observer - // goes away, it must take care to unregister itself. - void AddObserver(Observer* observer); - void RemoveObserver(Observer* observer); - // Paints the current backing store to the web page. void Paint(WebKit::WebCanvas* canvas, const gfx::Rect& plugin_rect, @@ -172,6 +162,10 @@ class PluginInstance : public base::RefCounted { gfx::Rect* dib_bounds, gfx::Rect* clip); + // Tracks all live PluginObjects. + void AddPluginObject(PluginObject* plugin_object); + void RemovePluginObject(PluginObject* plugin_object); + string16 GetSelectedText(bool html); string16 GetLinkAtPosition(const gfx::Point& point); void Zoom(double factor, bool text_only); @@ -197,6 +191,18 @@ class PluginInstance : public base::RefCounted { PluginDelegate::PlatformContext3D* CreateContext3D(); + // Tracks all live ObjectVar. This is so we can map between PluginModule + + // NPObject and get the ObjectVar corresponding to it. This Add/Remove + // function should be called by the ObjectVar when it is created and + // destroyed. + void AddNPObjectVar(ObjectVar* object_var); + void RemoveNPObjectVar(ObjectVar* object_var); + + // Looks up a previously registered ObjectVar for the given NPObject and + // module. Returns NULL if there is no ObjectVar corresponding to the given + // NPObject for the given module. See AddNPObjectVar above. + ObjectVar* ObjectVarForNPObject(NPObject* np_object) const; + private: bool LoadFindInterface(); bool LoadPdfInterface(); @@ -318,8 +324,13 @@ class PluginInstance : public base::RefCounted { // Plugin container for fullscreen mode. NULL if not in fullscreen mode. FullscreenContainer* fullscreen_container_; - // Non-owning pointers to all active observers. - ObserverList observers_; + typedef std::set PluginObjectSet; + PluginObjectSet live_plugin_objects_; + + // Tracks all live ObjectVars used by this module so we can map NPObjects to + // the corresponding object. These are non-owning references. + typedef std::map NPObjectToObjectVarMap; + NPObjectToObjectVarMap np_object_to_object_var_; DISALLOW_COPY_AND_ASSIGN(PluginInstance); }; diff --git a/webkit/plugins/ppapi/ppapi_unittest.h b/webkit/plugins/ppapi/ppapi_unittest.h index c18af87..a48e620 100644 --- a/webkit/plugins/ppapi/ppapi_unittest.h +++ b/webkit/plugins/ppapi/ppapi_unittest.h @@ -27,6 +27,7 @@ class PpapiUnittest : public testing::Test, virtual void SetUp(); virtual void TearDown(); + MockPluginDelegate* delegate() { return delegate_.get(); } PluginModule* module() const { return module_.get(); } PluginInstance* instance() const { return instance_.get(); } diff --git a/webkit/plugins/ppapi/ppb_audio_impl.cc b/webkit/plugins/ppapi/ppb_audio_impl.cc index cd8dca7..93561f8 100644 --- a/webkit/plugins/ppapi/ppb_audio_impl.cc +++ b/webkit/plugins/ppapi/ppb_audio_impl.cc @@ -41,8 +41,7 @@ PP_Resource CreateStereo16bit(PP_Instance instance_id, return 0; scoped_refptr config( - new PPB_AudioConfig_Impl(instance->module(), sample_rate, - sample_frame_count)); + new PPB_AudioConfig_Impl(instance, sample_rate, sample_frame_count)); return config->GetReference(); } @@ -92,8 +91,7 @@ PP_Resource Create(PP_Instance instance_id, PP_Resource config_id, return 0; if (!user_callback) return 0; - scoped_refptr audio( - new PPB_Audio_Impl(instance->module(), instance_id)); + scoped_refptr audio(new PPB_Audio_Impl(instance)); if (!audio->Init(instance->delegate(), config_id, user_callback, user_data)) return 0; @@ -138,8 +136,7 @@ PP_Resource CreateTrusted(PP_Instance instance_id) { PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); if (!instance) return 0; - scoped_refptr audio( - new PPB_Audio_Impl(instance->module(), instance_id)); + scoped_refptr audio(new PPB_Audio_Impl(instance)); return audio->GetReference(); } @@ -152,11 +149,7 @@ int32_t Open(PP_Resource audio_id, return PP_ERROR_BADRESOURCE; if (!created.func) return PP_ERROR_BADARGUMENT; - PP_Instance instance_id = audio->pp_instance(); - PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); - if (!instance) - return PP_ERROR_FAILED; - return audio->Open(instance->delegate(), config_id, created); + return audio->Open(audio->instance()->delegate(), config_id, created); } int32_t GetSyncSocket(PP_Resource audio_id, int* sync_socket) { @@ -189,10 +182,10 @@ const PPB_AudioTrusted ppb_audiotrusted = { // PPB_AudioConfig_Impl -------------------------------------------------------- PPB_AudioConfig_Impl::PPB_AudioConfig_Impl( - PluginModule* module, + PluginInstance* instance, PP_AudioSampleRate sample_rate, uint32_t sample_frame_count) - : Resource(module), + : Resource(instance), sample_rate_(sample_rate), sample_frame_count_(sample_frame_count) { } @@ -217,9 +210,8 @@ PPB_AudioConfig_Impl* PPB_AudioConfig_Impl::AsPPB_AudioConfig_Impl() { // PPB_Audio_Impl -------------------------------------------------------------- -PPB_Audio_Impl::PPB_Audio_Impl(PluginModule* module, PP_Instance instance_id) - : Resource(module), - pp_instance_(instance_id), +PPB_Audio_Impl::PPB_Audio_Impl(PluginInstance* instance) + : Resource(instance), audio_(NULL), create_callback_pending_(false) { create_callback_ = PP_MakeCompletionCallback(NULL, NULL); diff --git a/webkit/plugins/ppapi/ppb_audio_impl.h b/webkit/plugins/ppapi/ppb_audio_impl.h index 04bc04d..cc0c112 100644 --- a/webkit/plugins/ppapi/ppb_audio_impl.h +++ b/webkit/plugins/ppapi/ppb_audio_impl.h @@ -15,7 +15,6 @@ #include "ppapi/c/trusted/ppb_audio_trusted.h" #include "ppapi/shared_impl/audio_impl.h" #include "webkit/plugins/ppapi/plugin_delegate.h" -#include "webkit/plugins/ppapi/plugin_module.h" #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" #include "webkit/plugins/ppapi/resource.h" @@ -23,11 +22,10 @@ namespace webkit { namespace ppapi { class PluginInstance; -class PluginModule; class PPB_AudioConfig_Impl : public Resource { public: - PPB_AudioConfig_Impl(PluginModule* module, + PPB_AudioConfig_Impl(PluginInstance* instance, PP_AudioSampleRate sample_rate, uint32_t sample_frame_count); size_t BufferSize(); @@ -50,16 +48,12 @@ class PPB_Audio_Impl : public Resource, public pp::shared_impl::AudioImpl, public PluginDelegate::PlatformAudio::Client { public: - explicit PPB_Audio_Impl(PluginModule* module, PP_Instance instance_id); + explicit PPB_Audio_Impl(PluginInstance* instance); virtual ~PPB_Audio_Impl(); static const PPB_Audio* GetInterface(); static const PPB_AudioTrusted* GetTrustedInterface(); - PP_Instance pp_instance() { - return pp_instance_; - } - // PPB_Audio implementation. bool Init(PluginDelegate* plugin_delegate, PP_Resource config_id, @@ -87,9 +81,6 @@ class PPB_Audio_Impl : public Resource, // AudioConfig used for creating this Audio object. scoped_refptr config_; - // Plugin instance that owns this audio object. - PP_Instance pp_instance_; - // PluginDelegate audio object that we delegate audio IPC through. PluginDelegate::PlatformAudio* audio_; diff --git a/webkit/plugins/ppapi/ppb_buffer_impl.cc b/webkit/plugins/ppapi/ppb_buffer_impl.cc index 1c67ec3..7a18338 100644 --- a/webkit/plugins/ppapi/ppb_buffer_impl.cc +++ b/webkit/plugins/ppapi/ppb_buffer_impl.cc @@ -10,10 +10,8 @@ #include "base/scoped_ptr.h" #include "ppapi/c/dev/ppb_buffer_dev.h" #include "ppapi/c/pp_instance.h" -#include "ppapi/c/pp_module.h" #include "ppapi/c/pp_resource.h" #include "webkit/plugins/ppapi/common.h" -#include "webkit/plugins/ppapi/plugin_module.h" #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" namespace webkit { @@ -21,12 +19,12 @@ namespace ppapi { namespace { -PP_Resource Create(PP_Module module_id, uint32_t size) { - PluginModule* module = ResourceTracker::Get()->GetModule(module_id); - if (!module) +PP_Resource Create(PP_Instance instance_id, uint32_t size) { + PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); + if (!instance) return 0; - scoped_refptr buffer(new PPB_Buffer_Impl(module)); + scoped_refptr buffer(new PPB_Buffer_Impl(instance)); if (!buffer->Init(size)) return 0; @@ -72,8 +70,8 @@ const PPB_Buffer_Dev ppb_buffer = { } // namespace -PPB_Buffer_Impl::PPB_Buffer_Impl(PluginModule* module) - : Resource(module), +PPB_Buffer_Impl::PPB_Buffer_Impl(PluginInstance* instance) + : Resource(instance), size_(0) { } diff --git a/webkit/plugins/ppapi/ppb_buffer_impl.h b/webkit/plugins/ppapi/ppb_buffer_impl.h index 9642e51..d562b0b 100644 --- a/webkit/plugins/ppapi/ppb_buffer_impl.h +++ b/webkit/plugins/ppapi/ppb_buffer_impl.h @@ -18,7 +18,7 @@ class PluginInstance; class PPB_Buffer_Impl : public Resource { public: - explicit PPB_Buffer_Impl(PluginModule* module); + explicit PPB_Buffer_Impl(PluginInstance* instance); virtual ~PPB_Buffer_Impl(); uint32_t size() const { return size_; } diff --git a/webkit/plugins/ppapi/ppb_char_set_impl.cc b/webkit/plugins/ppapi/ppb_char_set_impl.cc index edab32e..74144ed 100644 --- a/webkit/plugins/ppapi/ppb_char_set_impl.cc +++ b/webkit/plugins/ppapi/ppb_char_set_impl.cc @@ -13,8 +13,8 @@ #include "unicode/ucnv_err.h" #include "unicode/ustring.h" #include "webkit/plugins/ppapi/plugin_delegate.h" -#include "webkit/plugins/ppapi/plugin_module.h" #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" +#include "webkit/plugins/ppapi/resource_tracker.h" #include "webkit/plugins/ppapi/var.h" namespace webkit { @@ -46,7 +46,8 @@ bool PPToBaseConversionError(PP_CharSet_ConversionError on_error, // implementation in base, so we partially duplicate the code from // icu_string_conversions.cc with the correct error handling setup required // by this PPAPI interface. -char* UTF16ToCharSet(const uint16_t* utf16, uint32_t utf16_len, +char* UTF16ToCharSet(PP_Instance /* instance */, + const uint16_t* utf16, uint32_t utf16_len, const char* output_char_set, PP_CharSet_ConversionError on_error, uint32_t* output_length) { @@ -114,7 +115,8 @@ char* UTF16ToCharSet(const uint16_t* utf16, uint32_t utf16_len, return encoded; } -uint16_t* CharSetToUTF16(const char* input, uint32_t input_len, +uint16_t* CharSetToUTF16(PP_Instance /* instance */, + const char* input, uint32_t input_len, const char* input_char_set, PP_CharSet_ConversionError on_error, uint32_t* output_length) { @@ -141,14 +143,13 @@ uint16_t* CharSetToUTF16(const char* input, uint32_t input_len, return ret_buf; } -PP_Var GetDefaultCharSet(PP_Module pp_module) { - PluginModule* module = ResourceTracker::Get()->GetModule(pp_module); - if (!module) +PP_Var GetDefaultCharSet(PP_Instance instance_id) { + PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); + if (!instance) return PP_MakeUndefined(); - std::string encoding = - module->GetSomeInstance()->delegate()->GetDefaultEncoding(); - return StringVar::StringToPPVar(module, encoding); + std::string encoding = instance->delegate()->GetDefaultEncoding(); + return StringVar::StringToPPVar(instance->module(), encoding); } const PPB_CharSet_Dev ppb_charset = { diff --git a/webkit/plugins/ppapi/ppb_context_3d_impl.cc b/webkit/plugins/ppapi/ppb_context_3d_impl.cc index 369cbc9..034b3a9 100644 --- a/webkit/plugins/ppapi/ppb_context_3d_impl.cc +++ b/webkit/plugins/ppapi/ppb_context_3d_impl.cc @@ -4,6 +4,7 @@ #include "webkit/plugins/ppapi/ppb_context_3d_impl.h" +#include "base/logging.h" #include "gpu/command_buffer/common/command_buffer.h" #include "webkit/plugins/ppapi/common.h" #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" @@ -88,7 +89,7 @@ const PPB_Context3D_Dev ppb_context3d = { } // namespace PPB_Context3D_Impl::PPB_Context3D_Impl(PluginInstance* instance) - : Resource(instance->module()), + : Resource(instance), instance_(instance), gles2_impl_(NULL), draw_surface_(NULL), diff --git a/webkit/plugins/ppapi/ppb_directory_reader_impl.cc b/webkit/plugins/ppapi/ppb_directory_reader_impl.cc index c650be5..36e43bf 100644 --- a/webkit/plugins/ppapi/ppb_directory_reader_impl.cc +++ b/webkit/plugins/ppapi/ppb_directory_reader_impl.cc @@ -49,7 +49,8 @@ PP_Resource Create(PP_Resource directory_ref_id) { if (!directory_ref) return 0; - PPB_DirectoryReader_Impl* reader = new PPB_DirectoryReader_Impl(directory_ref); + PPB_DirectoryReader_Impl* reader = + new PPB_DirectoryReader_Impl(directory_ref); return reader->GetReference(); } @@ -78,7 +79,7 @@ const PPB_DirectoryReader_Dev ppb_directoryreader = { PPB_DirectoryReader_Impl::PPB_DirectoryReader_Impl( PPB_FileRef_Impl* directory_ref) - : Resource(directory_ref->module()), + : Resource(directory_ref->instance()), directory_ref_(directory_ref), has_more_(true), entry_(NULL) { @@ -149,7 +150,7 @@ bool PPB_DirectoryReader_Impl::FillUpEntry() { if (entry_->file_ref) ResourceTracker::Get()->UnrefResource(entry_->file_ref); PPB_FileRef_Impl* file_ref = - new PPB_FileRef_Impl(module(), directory_ref_->GetFileSystem(), + new PPB_FileRef_Impl(instance(), directory_ref_->GetFileSystem(), FilePathStringToUTF8String(dir_entry.name)); entry_->file_ref = file_ref->GetReference(); entry_->file_type = diff --git a/webkit/plugins/ppapi/ppb_file_chooser_impl.cc b/webkit/plugins/ppapi/ppb_file_chooser_impl.cc index b8e4a79..7d65e65 100644 --- a/webkit/plugins/ppapi/ppb_file_chooser_impl.cc +++ b/webkit/plugins/ppapi/ppb_file_chooser_impl.cc @@ -103,10 +103,10 @@ class FileChooserCompletionImpl : public WebFileChooserCompletion { } // namespace -PPB_FileChooser_Impl::PPB_FileChooser_Impl(PluginInstance* instance, - const PP_FileChooserOptions_Dev* options) - : Resource(instance->module()), - delegate_(instance->delegate()), +PPB_FileChooser_Impl::PPB_FileChooser_Impl( + PluginInstance* instance, + const PP_FileChooserOptions_Dev* options) + : Resource(instance), mode_(options->mode), accept_mime_types_(options->accept_mime_types), completion_callback_() { @@ -131,7 +131,7 @@ void PPB_FileChooser_Impl::StoreChosenFiles( for (std::vector::const_iterator it = files.begin(); it != end_it; it++) { chosen_files_.push_back(make_scoped_refptr( - new PPB_FileRef_Impl(module(), FilePath().AppendASCII(*it)))); + new PPB_FileRef_Impl(instance(), FilePath().AppendASCII(*it)))); } if (!completion_callback_.func) @@ -153,7 +153,7 @@ int32_t PPB_FileChooser_Impl::Show(PP_CompletionCallback callback) { params.acceptTypes = WebString::fromUTF8(accept_mime_types_); params.directory = false; - return delegate_->RunFileChooser( + return instance()->delegate()->RunFileChooser( params, new FileChooserCompletionImpl(this)); } diff --git a/webkit/plugins/ppapi/ppb_file_chooser_impl.h b/webkit/plugins/ppapi/ppb_file_chooser_impl.h index d60b503..1d4c7de 100644 --- a/webkit/plugins/ppapi/ppb_file_chooser_impl.h +++ b/webkit/plugins/ppapi/ppb_file_chooser_impl.h @@ -23,7 +23,7 @@ class PPB_FileRef_Impl; class PPB_FileChooser_Impl : public Resource { public: PPB_FileChooser_Impl(PluginInstance* instance, - const PP_FileChooserOptions_Dev* options); + const PP_FileChooserOptions_Dev* options); virtual ~PPB_FileChooser_Impl(); // Returns a pointer to the interface implementing PPB_FileChooser that is @@ -41,7 +41,6 @@ class PPB_FileChooser_Impl : public Resource { scoped_refptr GetNextChosenFile(); private: - PluginDelegate* delegate_; PP_FileChooserMode_Dev mode_; std::string accept_mime_types_; PP_CompletionCallback completion_callback_; diff --git a/webkit/plugins/ppapi/ppb_file_io_impl.cc b/webkit/plugins/ppapi/ppb_file_io_impl.cc index 6440d16..ac8004b 100644 --- a/webkit/plugins/ppapi/ppb_file_io_impl.cc +++ b/webkit/plugins/ppapi/ppb_file_io_impl.cc @@ -26,12 +26,12 @@ namespace ppapi { namespace { -PP_Resource Create(PP_Module module_id) { - PluginModule* module = ResourceTracker::Get()->GetModule(module_id); - if (!module) +PP_Resource Create(PP_Instance instance_id) { + PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); + if (!instance) return 0; - PPB_FileIO_Impl* file_io = new PPB_FileIO_Impl(module); + PPB_FileIO_Impl* file_io = new PPB_FileIO_Impl(instance); return file_io->GetReference(); } @@ -200,9 +200,8 @@ int PlatformFileErrorToPepperError(base::PlatformFileError error_code) { } // namespace -PPB_FileIO_Impl::PPB_FileIO_Impl(PluginModule* module) - : Resource(module), - delegate_(module->GetSomeInstance()->delegate()), +PPB_FileIO_Impl::PPB_FileIO_Impl(PluginInstance* instance) + : Resource(instance), ALLOW_THIS_IN_INITIALIZER_LIST(callback_factory_(this)), file_(base::kInvalidPlatformFileValue), callback_(), @@ -255,7 +254,7 @@ int32_t PPB_FileIO_Impl::Open(PPB_FileRef_Impl* file_ref, } file_system_type_ = file_ref->GetFileSystemType(); - if (!delegate_->AsyncOpenFile( + if (!instance()->delegate()->AsyncOpenFile( file_ref->GetSystemPath(), flags, callback_factory_.NewCallback( &PPB_FileIO_Impl::AsyncOpenFileCallback))) @@ -278,7 +277,7 @@ int32_t PPB_FileIO_Impl::Query(PP_FileInfo_Dev* info, info_ = info; if (!base::FileUtilProxy::GetFileInfoFromPlatformFile( - delegate_->GetFileThreadMessageLoopProxy(), file_, + instance()->delegate()->GetFileThreadMessageLoopProxy(), file_, callback_factory_.NewCallback(&PPB_FileIO_Impl::QueryInfoCallback))) return PP_ERROR_FAILED; @@ -294,7 +293,7 @@ int32_t PPB_FileIO_Impl::Touch(PP_Time last_access_time, return rv; if (!base::FileUtilProxy::Touch( - delegate_->GetFileThreadMessageLoopProxy(), + instance()->delegate()->GetFileThreadMessageLoopProxy(), file_, base::Time::FromDoubleT(last_access_time), base::Time::FromDoubleT(last_modified_time), callback_factory_.NewCallback(&PPB_FileIO_Impl::StatusCallback))) @@ -313,7 +312,7 @@ int32_t PPB_FileIO_Impl::Read(int64_t offset, return rv; if (!base::FileUtilProxy::Read( - delegate_->GetFileThreadMessageLoopProxy(), + instance()->delegate()->GetFileThreadMessageLoopProxy(), file_, offset, buffer, bytes_to_read, callback_factory_.NewCallback(&PPB_FileIO_Impl::ReadWriteCallback))) return PP_ERROR_FAILED; @@ -331,7 +330,7 @@ int32_t PPB_FileIO_Impl::Write(int64_t offset, return rv; if (!base::FileUtilProxy::Write( - delegate_->GetFileThreadMessageLoopProxy(), + instance()->delegate()->GetFileThreadMessageLoopProxy(), file_, offset, buffer, bytes_to_write, callback_factory_.NewCallback(&PPB_FileIO_Impl::ReadWriteCallback))) return PP_ERROR_FAILED; @@ -347,7 +346,7 @@ int32_t PPB_FileIO_Impl::SetLength(int64_t length, return rv; if (!base::FileUtilProxy::Truncate( - delegate_->GetFileThreadMessageLoopProxy(), + instance()->delegate()->GetFileThreadMessageLoopProxy(), file_, length, callback_factory_.NewCallback(&PPB_FileIO_Impl::StatusCallback))) return PP_ERROR_FAILED; @@ -362,7 +361,7 @@ int32_t PPB_FileIO_Impl::Flush(PP_CompletionCallback callback) { return rv; if (!base::FileUtilProxy::Flush( - delegate_->GetFileThreadMessageLoopProxy(), file_, + instance()->delegate()->GetFileThreadMessageLoopProxy(), file_, callback_factory_.NewCallback(&PPB_FileIO_Impl::StatusCallback))) return PP_ERROR_FAILED; @@ -373,7 +372,7 @@ int32_t PPB_FileIO_Impl::Flush(PP_CompletionCallback callback) { void PPB_FileIO_Impl::Close() { if (file_ != base::kInvalidPlatformFileValue) { base::FileUtilProxy::Close( - delegate_->GetFileThreadMessageLoopProxy(), file_, NULL); + instance()->delegate()->GetFileThreadMessageLoopProxy(), file_, NULL); file_ = base::kInvalidPlatformFileValue; } } @@ -430,7 +429,7 @@ void PPB_FileIO_Impl::RegisterCallback(PP_CompletionCallback callback) { PP_Resource resource_id = GetReferenceNoAddRef(); CHECK(resource_id); callback_ = new TrackedCompletionCallback( - module()->GetCallbackTracker(), resource_id, callback); + instance()->module()->GetCallbackTracker(), resource_id, callback); } void PPB_FileIO_Impl::RunPendingCallback(int result) { diff --git a/webkit/plugins/ppapi/ppb_file_io_impl.h b/webkit/plugins/ppapi/ppb_file_io_impl.h index 0743fdb..3786540 100644 --- a/webkit/plugins/ppapi/ppb_file_io_impl.h +++ b/webkit/plugins/ppapi/ppb_file_io_impl.h @@ -30,7 +30,7 @@ class PPB_FileRef_Impl; class PPB_FileIO_Impl : public Resource { public: - explicit PPB_FileIO_Impl(PluginModule* module); + explicit PPB_FileIO_Impl(PluginInstance* instance); virtual ~PPB_FileIO_Impl(); // Returns a pointer to the interface implementing PPB_FileIO that is exposed @@ -98,7 +98,6 @@ class PPB_FileIO_Impl : public Resource { void ReadWriteCallback(base::PlatformFileError error_code, int bytes_read_or_written); - PluginDelegate* delegate_; base::ScopedCallbackFactory callback_factory_; base::PlatformFile file_; diff --git a/webkit/plugins/ppapi/ppb_file_ref_impl.cc b/webkit/plugins/ppapi/ppb_file_ref_impl.cc index f400ce4..0665d2f 100644 --- a/webkit/plugins/ppapi/ppb_file_ref_impl.cc +++ b/webkit/plugins/ppapi/ppb_file_ref_impl.cc @@ -55,7 +55,7 @@ PP_Resource Create(PP_Resource file_system_id, const char* path) { TrimTrailingSlash(&validated_path); PPB_FileRef_Impl* file_ref = - new PPB_FileRef_Impl(file_system->instance()->module(), + new PPB_FileRef_Impl(file_system->instance(), file_system, validated_path); return file_ref->GetReference(); } @@ -77,7 +77,8 @@ PP_Var GetName(PP_Resource file_ref_id) { Resource::GetAs(file_ref_id)); if (!file_ref) return PP_MakeUndefined(); - return StringVar::StringToPPVar(file_ref->module(), file_ref->GetName()); + return StringVar::StringToPPVar(file_ref->instance()->module(), + file_ref->GetName()); } PP_Var GetPath(PP_Resource file_ref_id) { @@ -89,7 +90,8 @@ PP_Var GetPath(PP_Resource file_ref_id) { if (file_ref->GetFileSystemType() == PP_FILESYSTEMTYPE_EXTERNAL) return PP_MakeUndefined(); - return StringVar::StringToPPVar(file_ref->module(), file_ref->GetPath()); + return StringVar::StringToPPVar(file_ref->instance()->module(), + file_ref->GetPath()); } PP_Resource GetParent(PP_Resource file_ref_id) { @@ -116,7 +118,8 @@ int32_t MakeDirectory(PP_Resource directory_ref_id, if (!directory_ref) return PP_ERROR_BADRESOURCE; - scoped_refptr file_system = directory_ref->GetFileSystem(); + scoped_refptr file_system = + directory_ref->GetFileSystem(); if (!file_system || !file_system->opened() || (file_system->type() == PP_FILESYSTEMTYPE_EXTERNAL)) return PP_ERROR_NOACCESS; @@ -253,17 +256,18 @@ PPB_FileRef_Impl::PPB_FileRef_Impl() file_system_(NULL) { } -PPB_FileRef_Impl::PPB_FileRef_Impl(PluginModule* module, - scoped_refptr file_system, - const std::string& validated_path) - : Resource(module), +PPB_FileRef_Impl::PPB_FileRef_Impl( + PluginInstance* instance, + scoped_refptr file_system, + const std::string& validated_path) + : Resource(instance), file_system_(file_system), virtual_path_(validated_path) { } -PPB_FileRef_Impl::PPB_FileRef_Impl(PluginModule* module, - const FilePath& external_file_path) - : Resource(module), +PPB_FileRef_Impl::PPB_FileRef_Impl(PluginInstance* instance, + const FilePath& external_file_path) + : Resource(instance), file_system_(NULL), system_path_(external_file_path) { } @@ -317,7 +321,7 @@ scoped_refptr PPB_FileRef_Impl::GetParent() { pos++; std::string parent_path = virtual_path_.substr(0, pos); - PPB_FileRef_Impl* parent_ref = new PPB_FileRef_Impl(module(), file_system_, + PPB_FileRef_Impl* parent_ref = new PPB_FileRef_Impl(instance(), file_system_, parent_path); return parent_ref; } diff --git a/webkit/plugins/ppapi/ppb_file_ref_impl.h b/webkit/plugins/ppapi/ppb_file_ref_impl.h index 40924e7..f006991 100644 --- a/webkit/plugins/ppapi/ppb_file_ref_impl.h +++ b/webkit/plugins/ppapi/ppb_file_ref_impl.h @@ -21,10 +21,10 @@ class PluginModule; class PPB_FileRef_Impl : public Resource { public: PPB_FileRef_Impl(); - PPB_FileRef_Impl(PluginModule* module, + PPB_FileRef_Impl(PluginInstance* instance, scoped_refptr file_system, const std::string& validated_path); - PPB_FileRef_Impl(PluginModule* module, + PPB_FileRef_Impl(PluginInstance* instance, const FilePath& external_file_path); virtual ~PPB_FileRef_Impl(); diff --git a/webkit/plugins/ppapi/ppb_file_system_impl.cc b/webkit/plugins/ppapi/ppb_file_system_impl.cc index cec5cec..5f61a7d 100644 --- a/webkit/plugins/ppapi/ppb_file_system_impl.cc +++ b/webkit/plugins/ppapi/ppb_file_system_impl.cc @@ -75,7 +75,7 @@ const PPB_FileSystem_Dev ppb_filesystem = { PPB_FileSystem_Impl::PPB_FileSystem_Impl(PluginInstance* instance, PP_FileSystemType_Dev type) - : Resource(instance->module()), + : Resource(instance), instance_(instance), type_(type), opened_(false) { diff --git a/webkit/plugins/ppapi/ppb_flash_impl.cc b/webkit/plugins/ppapi/ppb_flash_impl.cc index 8f20203..386b24f 100644 --- a/webkit/plugins/ppapi/ppb_flash_impl.cc +++ b/webkit/plugins/ppapi/ppb_flash_impl.cc @@ -28,14 +28,6 @@ namespace ppapi { namespace { -PluginInstance* GetSomeInstance(PP_Module pp_module) { - PluginModule* module = ResourceTracker::Get()->GetModule(pp_module); - if (!module) - return NULL; - - return module->GetSomeInstance(); -} - void SetInstanceAlwaysOnTop(PP_Instance pp_instance, bool on_top) { PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance); if (!instance) @@ -43,8 +35,8 @@ void SetInstanceAlwaysOnTop(PP_Instance pp_instance, bool on_top) { instance->set_always_on_top(on_top); } -PP_Var GetProxyForURL(PP_Module pp_module, const char* url) { - PluginInstance* instance = GetSomeInstance(pp_module); +PP_Var GetProxyForURL(PP_Instance pp_instance, const char* url) { + PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance); if (!instance) return PP_MakeUndefined(); @@ -66,11 +58,11 @@ FilePath GetFilePathFromUTF8(const char* path) { #endif } -int32_t OpenModuleLocalFile(PP_Module module, +int32_t OpenModuleLocalFile(PP_Instance pp_instance, const char* path, int32_t mode, PP_FileHandle* file) { - PluginInstance* instance = GetSomeInstance(module); + PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance); if (!instance) return PP_ERROR_FAILED; @@ -106,10 +98,10 @@ int32_t OpenModuleLocalFile(PP_Module module, } -int32_t RenameModuleLocalFile(PP_Module module, +int32_t RenameModuleLocalFile(PP_Instance pp_instance, const char* path_from, const char* path_to) { - PluginInstance* instance = GetSomeInstance(module); + PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance); if (!instance) return PP_ERROR_FAILED; @@ -120,10 +112,10 @@ int32_t RenameModuleLocalFile(PP_Module module, return PlatformFileErrorToPepperError(result); } -int32_t DeleteModuleLocalFileOrDir(PP_Module module, +int32_t DeleteModuleLocalFileOrDir(PP_Instance pp_instance, const char* path, bool recursive) { - PluginInstance* instance = GetSomeInstance(module); + PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance); if (!instance) return PP_ERROR_FAILED; @@ -133,8 +125,8 @@ int32_t DeleteModuleLocalFileOrDir(PP_Module module, return PlatformFileErrorToPepperError(result); } -int32_t CreateModuleLocalDir(PP_Module module, const char* path) { - PluginInstance* instance = GetSomeInstance(module); +int32_t CreateModuleLocalDir(PP_Instance pp_instance, const char* path) { + PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance); if (!instance) return PP_ERROR_FAILED; @@ -143,10 +135,10 @@ int32_t CreateModuleLocalDir(PP_Module module, const char* path) { return PlatformFileErrorToPepperError(result); } -int32_t QueryModuleLocalFile(PP_Module module, +int32_t QueryModuleLocalFile(PP_Instance pp_instance, const char* path, PP_FileInfo_Dev* info) { - PluginInstance* instance = GetSomeInstance(module); + PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance); if (!instance) return PP_ERROR_FAILED; @@ -167,10 +159,10 @@ int32_t QueryModuleLocalFile(PP_Module module, return PlatformFileErrorToPepperError(result); } -int32_t GetModuleLocalDirContents(PP_Module module, +int32_t GetModuleLocalDirContents(PP_Instance pp_instance, const char* path, PP_DirContents_Dev** contents) { - PluginInstance* instance = GetSomeInstance(module); + PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance); if (!instance) return PP_ERROR_FAILED; @@ -205,7 +197,7 @@ int32_t GetModuleLocalDirContents(PP_Module module, return PP_OK; } -void FreeModuleLocalDirContents(PP_Module module, +void FreeModuleLocalDirContents(PP_Instance instance, PP_DirContents_Dev* contents) { DCHECK(contents); for (int32_t i = 0; i < contents->count; ++i) { @@ -305,8 +297,7 @@ const PPB_Flash_NetConnector ppb_flash_netconnector = { PPB_Flash_NetConnector_Impl::PPB_Flash_NetConnector_Impl( PluginInstance* instance) - : Resource(instance->module()), - instance_(instance) { + : Resource(instance) { } PPB_Flash_NetConnector_Impl::~PPB_Flash_NetConnector_Impl() { diff --git a/webkit/plugins/ppapi/ppb_flash_impl.h b/webkit/plugins/ppapi/ppb_flash_impl.h index 25b20d0..7eb2e8b 100644 --- a/webkit/plugins/ppapi/ppb_flash_impl.h +++ b/webkit/plugins/ppapi/ppb_flash_impl.h @@ -23,7 +23,8 @@ class PPB_Flash_Impl { // exposed to the plugin. static const PPB_Flash* GetInterface(); - static bool DrawGlyphs(PP_Resource pp_image_data, + static bool DrawGlyphs(PP_Instance pp_instance, + PP_Resource pp_image_data, const PP_FontDescription_Dev* font_desc, uint32_t color, PP_Point position, @@ -52,8 +53,6 @@ class PPB_Flash_NetConnector_Impl : public Resource { // Resource override. virtual PPB_Flash_NetConnector_Impl* AsPPB_Flash_NetConnector_Impl(); - PluginInstance* instance() { return instance_; } - // PPB_Flash_NetConnector implementation. int32_t ConnectTcp(const char* host, uint16_t port, @@ -73,9 +72,6 @@ class PPB_Flash_NetConnector_Impl : public Resource { const PP_Flash_NetAddress& remote_addr); private: - // Plugin instance this connector with which is associated. - PluginInstance* instance_; - // Any pending callback (for |ConnectTcp()| or |ConnectTcpAddress()|). scoped_refptr callback_; diff --git a/webkit/plugins/ppapi/ppb_flash_impl_linux.cc b/webkit/plugins/ppapi/ppb_flash_impl_linux.cc index 23290eb..50346b4 100644 --- a/webkit/plugins/ppapi/ppb_flash_impl_linux.cc +++ b/webkit/plugins/ppapi/ppb_flash_impl_linux.cc @@ -20,7 +20,8 @@ namespace webkit { namespace ppapi { -bool PPB_Flash_Impl::DrawGlyphs(PP_Resource pp_image_data, +bool PPB_Flash_Impl::DrawGlyphs(PP_Instance, + PP_Resource pp_image_data, const PP_FontDescription_Dev* font_desc, uint32_t color, PP_Point position, diff --git a/webkit/plugins/ppapi/ppb_font_impl.cc b/webkit/plugins/ppapi/ppb_font_impl.cc index d0faee9..c63f02ea 100644 --- a/webkit/plugins/ppapi/ppb_font_impl.cc +++ b/webkit/plugins/ppapi/ppb_font_impl.cc @@ -15,7 +15,7 @@ #include "third_party/WebKit/WebKit/chromium/public/WebFloatRect.h" #include "third_party/WebKit/WebKit/chromium/public/WebTextRun.h" #include "webkit/plugins/ppapi/common.h" -#include "webkit/plugins/ppapi/plugin_module.h" +#include "webkit/plugins/ppapi/ppapi_plugin_instance.h" #include "webkit/plugins/ppapi/ppb_image_data_impl.h" #include "webkit/plugins/ppapi/string.h" #include "webkit/plugins/ppapi/var.h" @@ -109,16 +109,16 @@ bool PPTextRunToWebTextRun(const PP_TextRun_Dev* run, WebTextRun* output) { return true; } -PP_Resource Create(PP_Module module_id, +PP_Resource Create(PP_Instance instance_id, const PP_FontDescription_Dev* description) { - PluginModule* module = ResourceTracker::Get()->GetModule(module_id); - if (!module) + PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); + if (!instance) return 0; if (!IsPPFontDescriptionValid(*description)) return 0; - scoped_refptr font(new PPB_Font_Impl(module, *description)); + scoped_refptr font(new PPB_Font_Impl(instance, *description)); return font->GetReference(); } @@ -186,9 +186,9 @@ const PPB_Font_Dev ppb_font = { } // namespace -PPB_Font_Impl::PPB_Font_Impl(PluginModule* module, +PPB_Font_Impl::PPB_Font_Impl(PluginInstance* instance, const PP_FontDescription_Dev& desc) - : Resource(module) { + : Resource(instance) { WebFontDescription web_font_desc = PPFontDescToWebFontDesc(desc); font_.reset(WebFont::create(web_font_desc)); } @@ -214,7 +214,7 @@ bool PPB_Font_Impl::Describe(PP_FontDescription_Dev* description, // While converting the other way in PPFontDescToWebFontDesc we validated // that the enums can be casted. - description->face = StringVar::StringToPPVar(module(), + description->face = StringVar::StringToPPVar(instance()->module(), UTF16ToUTF8(web_desc.family)); description->family = static_cast(web_desc.genericFamily); description->size = static_cast(web_desc.size); diff --git a/webkit/plugins/ppapi/ppb_font_impl.h b/webkit/plugins/ppapi/ppb_font_impl.h index ff70330..7a51acf 100644 --- a/webkit/plugins/ppapi/ppb_font_impl.h +++ b/webkit/plugins/ppapi/ppb_font_impl.h @@ -21,7 +21,7 @@ class PluginInstance; class PPB_Font_Impl : public Resource { public: - PPB_Font_Impl(PluginModule* module, const PP_FontDescription_Dev& desc); + PPB_Font_Impl(PluginInstance* instance, const PP_FontDescription_Dev& desc); virtual ~PPB_Font_Impl(); // Returns a pointer to the interface implementing PPB_Font that is exposed to diff --git a/webkit/plugins/ppapi/ppb_graphics_2d_impl.cc b/webkit/plugins/ppapi/ppb_graphics_2d_impl.cc index 507a3a9..3105b61 100644 --- a/webkit/plugins/ppapi/ppb_graphics_2d_impl.cc +++ b/webkit/plugins/ppapi/ppb_graphics_2d_impl.cc @@ -14,13 +14,11 @@ #include "gfx/rect.h" #include "skia/ext/platform_canvas.h" #include "ppapi/c/pp_errors.h" -#include "ppapi/c/pp_module.h" #include "ppapi/c/pp_rect.h" #include "ppapi/c/pp_resource.h" #include "ppapi/c/ppb_graphics_2d.h" #include "third_party/skia/include/core/SkBitmap.h" #include "webkit/plugins/ppapi/common.h" -#include "webkit/plugins/ppapi/plugin_module.h" #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" #include "webkit/plugins/ppapi/ppb_image_data_impl.h" @@ -119,8 +117,8 @@ PP_Resource Create(PP_Instance instance_id, if (!instance) return 0; - scoped_refptr context(new PPB_Graphics2D_Impl( - instance->module())); + scoped_refptr context( + new PPB_Graphics2D_Impl(instance)); if (!context->Init(size->width, size->height, PPBoolToBool(is_always_opaque))) return 0; return context->GetReference(); @@ -217,8 +215,8 @@ struct PPB_Graphics2D_Impl::QueuedOperation { scoped_refptr replace_image; }; -PPB_Graphics2D_Impl::PPB_Graphics2D_Impl(PluginModule* module) - : Resource(module), +PPB_Graphics2D_Impl::PPB_Graphics2D_Impl(PluginInstance* instance) + : Resource(instance), bound_instance_(NULL), flushed_any_data_(false), offscreen_flush_pending_(false), @@ -235,7 +233,7 @@ const PPB_Graphics2D* PPB_Graphics2D_Impl::GetInterface() { bool PPB_Graphics2D_Impl::Init(int width, int height, bool is_always_opaque) { // The underlying PPB_ImageData_Impl will validate the dimensions. - image_data_ = new PPB_ImageData_Impl(module()); + image_data_ = new PPB_ImageData_Impl(instance()); if (!image_data_->Init(PPB_ImageData_Impl::GetNativeImageDataFormat(), width, height, true) || !image_data_->Map()) { diff --git a/webkit/plugins/ppapi/ppb_graphics_2d_impl.h b/webkit/plugins/ppapi/ppb_graphics_2d_impl.h index 40f2e5d..f2f60b4 100644 --- a/webkit/plugins/ppapi/ppb_graphics_2d_impl.h +++ b/webkit/plugins/ppapi/ppb_graphics_2d_impl.h @@ -28,7 +28,7 @@ class PluginModule; class PPB_Graphics2D_Impl : public Resource { public: - PPB_Graphics2D_Impl(PluginModule* module); + PPB_Graphics2D_Impl(PluginInstance* instance); virtual ~PPB_Graphics2D_Impl(); // Returns a pointer to the interface implementing PPB_ImageData that is diff --git a/webkit/plugins/ppapi/ppb_image_data_impl.cc b/webkit/plugins/ppapi/ppb_image_data_impl.cc index b961a66..042f38b 100644 --- a/webkit/plugins/ppapi/ppb_image_data_impl.cc +++ b/webkit/plugins/ppapi/ppb_image_data_impl.cc @@ -11,13 +11,11 @@ #include "base/scoped_ptr.h" #include "skia/ext/platform_canvas.h" #include "ppapi/c/pp_instance.h" -#include "ppapi/c/pp_module.h" #include "ppapi/c/pp_resource.h" #include "ppapi/c/ppb_image_data.h" #include "ppapi/c/trusted/ppb_image_data_trusted.h" #include "third_party/skia/include/core/SkColorPriv.h" #include "webkit/plugins/ppapi/common.h" -#include "webkit/plugins/ppapi/plugin_module.h" #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" namespace webkit { @@ -41,8 +39,7 @@ PP_Resource Create(PP_Instance instance_id, if (!instance) return 0; - scoped_refptr data( - new PPB_ImageData_Impl(instance->module())); + scoped_refptr data(new PPB_ImageData_Impl(instance)); if (!data->Init(format, size->width, size->height, @@ -112,8 +109,8 @@ const PPB_ImageDataTrusted ppb_imagedata_trusted = { } // namespace -PPB_ImageData_Impl::PPB_ImageData_Impl(PluginModule* module) - : Resource(module), +PPB_ImageData_Impl::PPB_ImageData_Impl(PluginInstance* instance) + : Resource(instance), format_(PP_IMAGEDATAFORMAT_BGRA_PREMUL), width_(0), height_(0) { @@ -167,7 +164,7 @@ bool PPB_ImageData_Impl::Init(PP_ImageDataFormat format, return false; // Prevent overflow of signed 32-bit ints. platform_image_.reset( - module()->GetSomeInstance()->delegate()->CreateImage2D(width, height)); + instance()->delegate()->CreateImage2D(width, height)); format_ = format; width_ = width; height_ = height; diff --git a/webkit/plugins/ppapi/ppb_image_data_impl.h b/webkit/plugins/ppapi/ppb_image_data_impl.h index e036d39..84b2bde 100644 --- a/webkit/plugins/ppapi/ppb_image_data_impl.h +++ b/webkit/plugins/ppapi/ppb_image_data_impl.h @@ -23,7 +23,7 @@ namespace ppapi { class PPB_ImageData_Impl : public Resource { public: - explicit PPB_ImageData_Impl(PluginModule* module); + explicit PPB_ImageData_Impl(PluginInstance* instance); virtual ~PPB_ImageData_Impl(); int width() const { return width_; } diff --git a/webkit/plugins/ppapi/ppb_pdf_impl.cc b/webkit/plugins/ppapi/ppb_pdf_impl.cc index ee58aef..364e9c3 100644 --- a/webkit/plugins/ppapi/ppb_pdf_impl.cc +++ b/webkit/plugins/ppapi/ppb_pdf_impl.cc @@ -28,8 +28,8 @@ namespace ppapi { #if defined(OS_LINUX) class PrivateFontFile : public Resource { public: - PrivateFontFile(PluginModule* module, int fd) - : Resource(module), + PrivateFontFile(PluginInstance* instance, int fd) + : Resource(instance), fd_(fd) { } virtual ~PrivateFontFile() { @@ -82,9 +82,10 @@ static const ResourceImageInfo kResourceImageMap[] = { IDR_PDF_THUMBNAIL_NUM_BACKGROUND }, }; -PP_Var GetLocalizedString(PP_Module module_id, PP_ResourceString string_id) { - PluginModule* module = ResourceTracker::Get()->GetModule(module_id); - if (!module) +PP_Var GetLocalizedString(PP_Instance instance_id, + PP_ResourceString string_id) { + PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); + if (!instance) return PP_MakeUndefined(); std::string rv; @@ -98,10 +99,11 @@ PP_Var GetLocalizedString(PP_Module module_id, PP_ResourceString string_id) { NOTREACHED(); } - return StringVar::StringToPPVar(module, rv); + return StringVar::StringToPPVar(instance->module(), rv); } -PP_Resource GetResourceImage(PP_Module module_id, PP_ResourceImage image_id) { +PP_Resource GetResourceImage(PP_Instance instance_id, + PP_ResourceImage image_id) { int res_id = 0; for (size_t i = 0; i < arraysize(kResourceImageMap); ++i) { if (kResourceImageMap[i].pp_id == image_id) { @@ -115,10 +117,11 @@ PP_Resource GetResourceImage(PP_Module module_id, PP_ResourceImage image_id) { SkBitmap* res_bitmap = ResourceBundle::GetSharedInstance().GetBitmapNamed(res_id); - PluginModule* module = ResourceTracker::Get()->GetModule(module_id); - if (!module) + PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); + if (!instance) return 0; - scoped_refptr image_data(new PPB_ImageData_Impl(module)); + scoped_refptr image_data( + new PPB_ImageData_Impl(instance)); if (!image_data->Init(PPB_ImageData_Impl::GetNativeImageDataFormat(), res_bitmap->width(), res_bitmap->height(), false)) { return 0; @@ -139,12 +142,12 @@ PP_Resource GetResourceImage(PP_Module module_id, PP_ResourceImage image_id) { } PP_Resource GetFontFileWithFallback( - PP_Module module_id, + PP_Instance instance_id, const PP_FontDescription_Dev* description, PP_PrivateFontCharset charset) { #if defined(OS_LINUX) - PluginModule* module = ResourceTracker::Get()->GetModule(module_id); - if (!module) + PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); + if (!instance) return 0; scoped_refptr face_name(StringVar::FromPPVar(description->face)); @@ -159,7 +162,7 @@ PP_Resource GetFontFileWithFallback( if (fd == -1) return 0; - scoped_refptr font(new PrivateFontFile(module, fd)); + scoped_refptr font(new PrivateFontFile(instance, fd)); return font->GetReference(); #else @@ -184,7 +187,7 @@ bool GetFontTableForPrivateFontFile(PP_Resource font_file, #endif } -void SearchString(PP_Module module, +void SearchString(PP_Instance instance, const unsigned short* input_string, const unsigned short* input_term, bool case_sensitive, diff --git a/webkit/plugins/ppapi/ppb_scrollbar_impl.cc b/webkit/plugins/ppapi/ppb_scrollbar_impl.cc index c436748..bb93951 100644 --- a/webkit/plugins/ppapi/ppb_scrollbar_impl.cc +++ b/webkit/plugins/ppapi/ppb_scrollbar_impl.cc @@ -203,8 +203,8 @@ void PPB_Scrollbar_Impl::SetLocationInternal(const PP_Rect* location) { void PPB_Scrollbar_Impl::valueChanged(WebKit::WebScrollbar* scrollbar) { const PPP_Scrollbar_Dev* ppp_scrollbar = - static_cast( - module()->GetPluginInterface(PPP_SCROLLBAR_DEV_INTERFACE)); + static_cast(instance()->module()-> + GetPluginInterface(PPP_SCROLLBAR_DEV_INTERFACE)); if (!ppp_scrollbar) return; ScopedResourceId resource(this); diff --git a/webkit/plugins/ppapi/ppb_surface_3d_impl.cc b/webkit/plugins/ppapi/ppb_surface_3d_impl.cc index 5b42cf9..ab37013 100644 --- a/webkit/plugins/ppapi/ppb_surface_3d_impl.cc +++ b/webkit/plugins/ppapi/ppb_surface_3d_impl.cc @@ -65,8 +65,7 @@ const PPB_Surface3D_Dev ppb_surface3d = { } // namespace PPB_Surface3D_Impl::PPB_Surface3D_Impl(PluginInstance* instance) - : Resource(instance->module()), - instance_(instance), + : Resource(instance), bound_to_instance_(false), swap_initiated_(false), swap_callback_(PP_BlockUntilComplete()), diff --git a/webkit/plugins/ppapi/ppb_surface_3d_impl.h b/webkit/plugins/ppapi/ppb_surface_3d_impl.h index 0cd0643..e3d4173 100644 --- a/webkit/plugins/ppapi/ppb_surface_3d_impl.h +++ b/webkit/plugins/ppapi/ppb_surface_3d_impl.h @@ -30,9 +30,6 @@ class PPB_Surface3D_Impl : public Resource { bool Init(PP_Config3D_Dev config, const int32_t* attrib_list); - PluginInstance* instance() const { - return instance_; - } PluginDelegate::PlatformContext3D* context() const { return context_; } @@ -58,8 +55,6 @@ class PPB_Surface3D_Impl : public Resource { // Called when SwapBuffers is complete. void OnSwapBuffers(); - // Plugin instance this surface is associated with. - PluginInstance* instance_; bool bound_to_instance_; // True when the page's SwapBuffers has been issued but not returned yet. diff --git a/webkit/plugins/ppapi/ppb_transport_impl.cc b/webkit/plugins/ppapi/ppb_transport_impl.cc index 568e9cb..69139e7 100644 --- a/webkit/plugins/ppapi/ppb_transport_impl.cc +++ b/webkit/plugins/ppapi/ppb_transport_impl.cc @@ -18,7 +18,7 @@ namespace { // Creates a new transport object with the specified name // using the specified protocol. -PP_Resource CreateTransport(PP_Module module, +PP_Resource CreateTransport(PP_Instance instance, const char* name, const char* proto) { // TODO(juberti): implement me @@ -118,8 +118,8 @@ const PPB_Transport_Dev ppb_transport = { } // namespace -PPB_Transport_Impl::PPB_Transport_Impl(PluginModule* module) - : Resource(module) { +PPB_Transport_Impl::PPB_Transport_Impl(PluginInstance* instance) + : Resource(instance) { // TODO(juberti): impl } diff --git a/webkit/plugins/ppapi/ppb_transport_impl.h b/webkit/plugins/ppapi/ppb_transport_impl.h index 7a98a37..96169c7 100644 --- a/webkit/plugins/ppapi/ppb_transport_impl.h +++ b/webkit/plugins/ppapi/ppb_transport_impl.h @@ -18,7 +18,7 @@ namespace ppapi { class PPB_Transport_Impl : public Resource { public: - explicit PPB_Transport_Impl(PluginModule* module); + explicit PPB_Transport_Impl(PluginInstance* instance); virtual ~PPB_Transport_Impl(); static const PPB_Transport_Dev* GetInterface(); diff --git a/webkit/plugins/ppapi/ppb_url_loader_impl.cc b/webkit/plugins/ppapi/ppb_url_loader_impl.cc index 27b7af6..83c5e27 100644 --- a/webkit/plugins/ppapi/ppb_url_loader_impl.cc +++ b/webkit/plugins/ppapi/ppb_url_loader_impl.cc @@ -196,8 +196,7 @@ WebKit::WebFrame* GetFrame(PluginInstance* instance) { PPB_URLLoader_Impl::PPB_URLLoader_Impl(PluginInstance* instance, bool main_document_loader) - : Resource(instance->module()), - instance_(instance), + : Resource(instance), main_document_loader_(main_document_loader), pending_callback_(), bytes_sent_(0), @@ -209,12 +208,9 @@ PPB_URLLoader_Impl::PPB_URLLoader_Impl(PluginInstance* instance, done_status_(PP_ERROR_WOULDBLOCK), has_universal_access_(false), status_callback_(NULL) { - instance->AddObserver(this); } PPB_URLLoader_Impl::~PPB_URLLoader_Impl() { - if (instance_) - instance_->RemoveObserver(this); } // static @@ -231,6 +227,15 @@ PPB_URLLoader_Impl* PPB_URLLoader_Impl::AsPPB_URLLoader_Impl() { return this; } +void PPB_URLLoader_Impl::LastPluginRefWasDeleted(bool instance_destroyed) { + Resource::LastPluginRefWasDeleted(instance_destroyed); + if (instance_destroyed) { + // Free the WebKit request when the instance has been destroyed to avoid + // using bandwidth just in case this object lives longer than the instance. + loader_.reset(); + } +} + int32_t PPB_URLLoader_Impl::Open(PPB_URLRequestInfo_Impl* request, PP_CompletionCallback callback) { int32_t rv = ValidateCallback(callback); @@ -240,7 +245,7 @@ int32_t PPB_URLLoader_Impl::Open(PPB_URLRequestInfo_Impl* request, if (loader_.get()) return PP_ERROR_INPROGRESS; - WebFrame* frame = GetFrame(instance_); + WebFrame* frame = GetFrame(instance()); if (!frame) return PP_ERROR_FAILED; WebURLRequest web_request(request->ToWebURLRequest(frame)); @@ -269,7 +274,7 @@ int32_t PPB_URLLoader_Impl::FollowRedirect(PP_CompletionCallback callback) { WebURL redirect_url = GURL(response_info_->redirect_url()); - rv = CanRequest(GetFrame(instance_), redirect_url); + rv = CanRequest(GetFrame(instance()), redirect_url); if (rv != PP_OK) return rv; @@ -352,7 +357,7 @@ void PPB_URLLoader_Impl::Close() { if (loader_.get()) { loader_->cancel(); } else if (main_document_loader_) { - WebFrame* frame = instance_->container()->element().document().frame(); + WebFrame* frame = instance()->container()->element().document().frame(); frame->stopLoading(); } // TODO(viettrungluu): Check what happens to the callback (probably the @@ -377,7 +382,7 @@ void PPB_URLLoader_Impl::willSendRequest( loader_->setDefersLoading(true); RunCallback(PP_OK); } else { - int32_t rv = CanRequest(GetFrame(instance_), new_request.url()); + int32_t rv = CanRequest(GetFrame(instance()), new_request.url()); if (rv != PP_OK) { loader_->setDefersLoading(true); RunCallback(rv); @@ -438,40 +443,6 @@ void PPB_URLLoader_Impl::didFail(WebURLLoader* loader, RunCallback(done_status_); } -void PPB_URLLoader_Impl::InstanceDestroyed(PluginInstance* instance) { - // When the instance is destroyed, we force delete any associated loads. - DCHECK(instance == instance_); - instance_ = NULL; - - // Normally the only ref to this class will be from the plugin which - // ForceDeletePluginResourceRefs will free. We don't want our object to be - // deleted out from under us until the function completes. - scoped_refptr death_grip(this); - - // Force delete any plugin refs to us. If the instance is being deleted, we - // don't want to allow the requests to continue to use bandwidth and send us - // callbacks (for which we might have no plugin). - ResourceTracker *tracker = ResourceTracker::Get(); - PP_Resource loader_resource = GetReferenceNoAddRef(); - if (loader_resource) - tracker->ForceDeletePluginResourceRefs(loader_resource); - - // Also force free the response from the plugin, both the plugin's ref(s) - // and ours. - if (response_info_.get()) { - PP_Resource response_info_resource = response_info_->GetReferenceNoAddRef(); - if (response_info_resource) - tracker->ForceDeletePluginResourceRefs(response_info_resource); - response_info_ = NULL; - } - - // Free the WebKit request. - loader_.reset(); - - // Often, |this| will be deleted at the end of this function when death_grip - // goes out of scope. -} - int32_t PPB_URLLoader_Impl::ValidateCallback(PP_CompletionCallback callback) { // We only support non-blocking calls. if (!callback.func) @@ -490,7 +461,7 @@ void PPB_URLLoader_Impl::RegisterCallback(PP_CompletionCallback callback) { PP_Resource resource_id = GetReferenceNoAddRef(); CHECK(resource_id); pending_callback_ = new TrackedCompletionCallback( - module()->GetCallbackTracker(), resource_id, callback); + instance()->module()->GetCallbackTracker(), resource_id, callback); } void PPB_URLLoader_Impl::RunCallback(int32_t result) { @@ -521,7 +492,7 @@ size_t PPB_URLLoader_Impl::FillUserBuffer() { void PPB_URLLoader_Impl::SaveResponse(const WebKit::WebURLResponse& response) { scoped_refptr response_info( - new PPB_URLResponseInfo_Impl(module())); + new PPB_URLResponseInfo_Impl(instance())); if (response_info->Initialize(response)) response_info_ = response_info; } @@ -551,7 +522,7 @@ void PPB_URLLoader_Impl::UpdateStatus() { // getting download progress when they happen to set the upload progress // flag. status_callback_( - instance_->pp_instance(), pp_resource, + instance()->pp_instance(), pp_resource, RecordUploadProgress() ? bytes_sent_ : -1, RecordUploadProgress() ? total_bytes_to_be_sent_ : -1, RecordDownloadProgress() ? bytes_received_ : -1, diff --git a/webkit/plugins/ppapi/ppb_url_loader_impl.h b/webkit/plugins/ppapi/ppb_url_loader_impl.h index 4a3d72e..aa3950b 100644 --- a/webkit/plugins/ppapi/ppb_url_loader_impl.h +++ b/webkit/plugins/ppapi/ppb_url_loader_impl.h @@ -31,9 +31,7 @@ class PluginInstance; class PPB_URLRequestInfo_Impl; class PPB_URLResponseInfo_Impl; -class PPB_URLLoader_Impl : public Resource, - public WebKit::WebURLLoaderClient, - public PluginInstance::Observer { +class PPB_URLLoader_Impl : public Resource, public WebKit::WebURLLoaderClient { public: PPB_URLLoader_Impl(PluginInstance* instance, bool main_document_loader); virtual ~PPB_URLLoader_Impl(); @@ -48,6 +46,7 @@ class PPB_URLLoader_Impl : public Resource, // Resource overrides. virtual PPB_URLLoader_Impl* AsPPB_URLLoader_Impl(); + virtual void LastPluginRefWasDeleted(bool instance_destroyed); // PPB_URLLoader implementation. int32_t Open(PPB_URLRequestInfo_Impl* request, @@ -85,9 +84,6 @@ class PPB_URLLoader_Impl : public Resource, virtual void didFail(WebKit::WebURLLoader* loader, const WebKit::WebURLError& error); - // PluginInstance::Observer implementation. - virtual void InstanceDestroyed(PluginInstance* instance); - PPB_URLResponseInfo_Impl* response_info() const { return response_info_; } private: @@ -122,13 +118,6 @@ class PPB_URLLoader_Impl : public Resource, bool RecordDownloadProgress() const; bool RecordUploadProgress() const; - // This will be NULL if the instance has been deleted but this - // PPB_URLLoader_Impl was somehow leaked. In general, you should not need to - // check this for NULL. However, if you see a NULL pointer crash, that means - // somebody is holding a reference to this object longer than the - // PluginInstance's lifetime. - PluginInstance* instance_; - // If true, then the plugin instance is a full-frame plugin and we're just // wrapping the main document's loader (i.e. loader_ is null). bool main_document_loader_; diff --git a/webkit/plugins/ppapi/ppb_url_request_info_impl.cc b/webkit/plugins/ppapi/ppb_url_request_info_impl.cc index cb1e545..5c59a67 100644 --- a/webkit/plugins/ppapi/ppb_url_request_info_impl.cc +++ b/webkit/plugins/ppapi/ppb_url_request_info_impl.cc @@ -81,12 +81,12 @@ bool AreValidHeaders(const std::string& headers) { return true; } -PP_Resource Create(PP_Module module_id) { - PluginModule* module = ResourceTracker::Get()->GetModule(module_id); - if (!module) +PP_Resource Create(PP_Instance instance_id) { + PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); + if (!instance) return 0; - PPB_URLRequestInfo_Impl* request = new PPB_URLRequestInfo_Impl(module); + PPB_URLRequestInfo_Impl* request = new PPB_URLRequestInfo_Impl(instance); return request->GetReference(); } @@ -187,8 +187,8 @@ struct PPB_URLRequestInfo_Impl::BodyItem { PP_Time expected_last_modified_time; }; -PPB_URLRequestInfo_Impl::PPB_URLRequestInfo_Impl(PluginModule* module) - : Resource(module), +PPB_URLRequestInfo_Impl::PPB_URLRequestInfo_Impl(PluginInstance* instance) + : Resource(instance), stream_to_file_(false), follow_redirects_(true), record_download_progress_(false), diff --git a/webkit/plugins/ppapi/ppb_url_request_info_impl.h b/webkit/plugins/ppapi/ppb_url_request_info_impl.h index a55edfe..64c27fe 100644 --- a/webkit/plugins/ppapi/ppb_url_request_info_impl.h +++ b/webkit/plugins/ppapi/ppb_url_request_info_impl.h @@ -24,7 +24,7 @@ class PPB_FileRef_Impl; class PPB_URLRequestInfo_Impl : public Resource { public: - explicit PPB_URLRequestInfo_Impl(PluginModule* module); + explicit PPB_URLRequestInfo_Impl(PluginInstance* instance); virtual ~PPB_URLRequestInfo_Impl(); // Returns a pointer to the interface implementing PPB_URLRequestInfo that is diff --git a/webkit/plugins/ppapi/ppb_url_response_info_impl.cc b/webkit/plugins/ppapi/ppb_url_response_info_impl.cc index 43e3d9e..5ca945a 100644 --- a/webkit/plugins/ppapi/ppb_url_response_info_impl.cc +++ b/webkit/plugins/ppapi/ppb_url_response_info_impl.cc @@ -11,6 +11,7 @@ #include "third_party/WebKit/WebKit/chromium/public/WebURL.h" #include "third_party/WebKit/WebKit/chromium/public/WebURLResponse.h" #include "webkit/plugins/ppapi/common.h" +#include "webkit/plugins/ppapi/ppapi_plugin_instance.h" #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" #include "webkit/plugins/ppapi/var.h" #include "webkit/glue/webkit_glue.h" @@ -80,8 +81,8 @@ bool IsRedirect(int32_t status) { } // namespace -PPB_URLResponseInfo_Impl::PPB_URLResponseInfo_Impl(PluginModule* module) - : Resource(module), +PPB_URLResponseInfo_Impl::PPB_URLResponseInfo_Impl(PluginInstance* instance) + : Resource(instance), status_code_(-1) { } @@ -101,21 +102,21 @@ PPB_URLResponseInfo_Impl::AsPPB_URLResponseInfo_Impl() { PP_Var PPB_URLResponseInfo_Impl::GetProperty(PP_URLResponseProperty property) { switch (property) { case PP_URLRESPONSEPROPERTY_URL: - return StringVar::StringToPPVar(module(), url_); + return StringVar::StringToPPVar(instance()->module(), url_); case PP_URLRESPONSEPROPERTY_REDIRECTURL: if (IsRedirect(status_code_)) - return StringVar::StringToPPVar(module(), redirect_url_); + return StringVar::StringToPPVar(instance()->module(), redirect_url_); break; case PP_URLRESPONSEPROPERTY_REDIRECTMETHOD: if (IsRedirect(status_code_)) - return StringVar::StringToPPVar(module(), status_text_); + return StringVar::StringToPPVar(instance()->module(), status_text_); break; case PP_URLRESPONSEPROPERTY_STATUSCODE: return PP_MakeInt32(status_code_); case PP_URLRESPONSEPROPERTY_STATUSLINE: - return StringVar::StringToPPVar(module(), status_text_); + return StringVar::StringToPPVar(instance()->module(), status_text_); case PP_URLRESPONSEPROPERTY_HEADERS: - return StringVar::StringToPPVar(module(), headers_); + return StringVar::StringToPPVar(instance()->module(), headers_); } // The default is to return an undefined PP_Var. return PP_MakeUndefined(); @@ -136,7 +137,7 @@ bool PPB_URLResponseInfo_Impl::Initialize(const WebURLResponse& response) { WebString file_path = response.downloadFilePath(); if (!file_path.isEmpty()) - body_ = new PPB_FileRef_Impl(module(), + body_ = new PPB_FileRef_Impl(instance(), webkit_glue::WebStringToFilePath(file_path)); return true; } diff --git a/webkit/plugins/ppapi/ppb_url_response_info_impl.h b/webkit/plugins/ppapi/ppb_url_response_info_impl.h index ee12d35c..fa916d9 100644 --- a/webkit/plugins/ppapi/ppb_url_response_info_impl.h +++ b/webkit/plugins/ppapi/ppb_url_response_info_impl.h @@ -22,7 +22,7 @@ class PPB_FileRef_Impl; class PPB_URLResponseInfo_Impl : public Resource { public: - explicit PPB_URLResponseInfo_Impl(PluginModule* module); + explicit PPB_URLResponseInfo_Impl(PluginInstance* instance); virtual ~PPB_URLResponseInfo_Impl(); // Returns a pointer to the interface implementing PPB_URLResponseInfo that diff --git a/webkit/plugins/ppapi/ppb_url_util_impl.cc b/webkit/plugins/ppapi/ppb_url_util_impl.cc index 47980e4..1465145 100644 --- a/webkit/plugins/ppapi/ppb_url_util_impl.cc +++ b/webkit/plugins/ppapi/ppb_url_util_impl.cc @@ -15,6 +15,7 @@ #include "third_party/WebKit/WebKit/chromium/public/WebURL.h" #include "webkit/plugins/ppapi/common.h" #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" +#include "webkit/plugins/ppapi/resource_tracker.h" #include "webkit/plugins/ppapi/string.h" #include "webkit/plugins/ppapi/var.h" diff --git a/webkit/plugins/ppapi/ppb_video_decoder_impl.cc b/webkit/plugins/ppapi/ppb_video_decoder_impl.cc index bd7f204..9d6add6 100644 --- a/webkit/plugins/ppapi/ppb_video_decoder_impl.cc +++ b/webkit/plugins/ppapi/ppb_video_decoder_impl.cc @@ -97,8 +97,7 @@ const PPB_VideoDecoder_Dev ppb_videodecoder = { } // namespace PPB_VideoDecoder_Impl::PPB_VideoDecoder_Impl(PluginInstance* instance) - : Resource(instance->module()), - instance_(instance) { + : Resource(instance) { } PPB_VideoDecoder_Impl::~PPB_VideoDecoder_Impl() { diff --git a/webkit/plugins/ppapi/ppb_video_decoder_impl.h b/webkit/plugins/ppapi/ppb_video_decoder_impl.h index 169ebed..cc4527e 100644 --- a/webkit/plugins/ppapi/ppb_video_decoder_impl.h +++ b/webkit/plugins/ppapi/ppb_video_decoder_impl.h @@ -32,8 +32,6 @@ class PPB_VideoDecoder_Impl : public Resource { // Resource overrides. virtual PPB_VideoDecoder_Impl* AsPPB_VideoDecoder_Impl(); - PluginInstance* instance() { return instance_.get(); } - // PPB_VideoDecoder implementation. bool Init(const PP_VideoDecoderConfig_Dev& decoder_config); bool Decode(PP_VideoCompressedDataBuffer_Dev& input_buffer); @@ -44,7 +42,6 @@ class PPB_VideoDecoder_Impl : public Resource { // This is NULL before initialization, and if this PPB_VideoDecoder_Impl is // swapped with another. scoped_ptr platform_video_decoder_; - scoped_refptr instance_; DISALLOW_COPY_AND_ASSIGN(PPB_VideoDecoder_Impl); }; diff --git a/webkit/plugins/ppapi/ppb_widget_impl.cc b/webkit/plugins/ppapi/ppb_widget_impl.cc index 57491b5..89a0c069 100644 --- a/webkit/plugins/ppapi/ppb_widget_impl.cc +++ b/webkit/plugins/ppapi/ppb_widget_impl.cc @@ -69,8 +69,7 @@ const PPB_Widget_Dev ppb_widget = { } // namespace PPB_Widget_Impl::PPB_Widget_Impl(PluginInstance* instance) - : Resource(instance->module()), - instance_(instance) { + : Resource(instance) { } PPB_Widget_Impl::~PPB_Widget_Impl() { @@ -97,7 +96,7 @@ void PPB_Widget_Impl::SetLocation(const PP_Rect* location) { void PPB_Widget_Impl::Invalidate(const PP_Rect* dirty) { const PPP_Widget_Dev* widget = static_cast( - module()->GetPluginInterface(PPP_WIDGET_DEV_INTERFACE)); + instance()->module()->GetPluginInterface(PPP_WIDGET_DEV_INTERFACE)); if (!widget) return; ScopedResourceId resource(this); diff --git a/webkit/plugins/ppapi/resource.cc b/webkit/plugins/ppapi/resource.cc index 4f77104..9619861 100644 --- a/webkit/plugins/ppapi/resource.cc +++ b/webkit/plugins/ppapi/resource.cc @@ -6,14 +6,15 @@ #include "base/logging.h" #include "webkit/plugins/ppapi/callbacks.h" +#include "webkit/plugins/ppapi/ppapi_plugin_instance.h" #include "webkit/plugins/ppapi/plugin_module.h" #include "webkit/plugins/ppapi/resource_tracker.h" namespace webkit { namespace ppapi { -Resource::Resource(PluginModule* module) - : resource_id_(0), module_(module) { +Resource::Resource(PluginInstance* instance) + : resource_id_(0), instance_(instance) { } Resource::~Resource() { @@ -32,10 +33,14 @@ PP_Resource Resource::GetReferenceNoAddRef() const { return resource_id_; } -void Resource::StoppedTracking() { +void Resource::LastPluginRefWasDeleted(bool instance_destroyed) { DCHECK(resource_id_ != 0); - module_->GetCallbackTracker()->PostAbortForResource(resource_id_); + instance()->module()->GetCallbackTracker()->PostAbortForResource( + resource_id_); resource_id_ = 0; + + if (instance_destroyed) + instance_ = NULL; } #define DEFINE_TYPE_GETTER(RESOURCE) \ diff --git a/webkit/plugins/ppapi/resource.h b/webkit/plugins/ppapi/resource.h index d8114d0..fb4f2ef 100644 --- a/webkit/plugins/ppapi/resource.h +++ b/webkit/plugins/ppapi/resource.h @@ -16,7 +16,6 @@ namespace ppapi { // If you inherit from resource, make sure you add the class name here. #define FOR_ALL_RESOURCES(F) \ F(MockResource) \ - F(ObjectVar) \ F(PPB_AudioConfig_Impl) \ F(PPB_Audio_Impl) \ F(PPB_Buffer_Impl) \ @@ -39,10 +38,7 @@ namespace ppapi { F(PPB_URLResponseInfo_Impl) \ F(PPB_VideoDecoder_Impl) \ F(PPB_Widget_Impl) \ - F(PrivateFontFile) \ - F(StringVar) \ - F(Var) \ - F(VarObjectClass) + F(PrivateFontFile) // Forward declaration of Resource classes. #define DECLARE_RESOURCE_CLASS(RESOURCE) class RESOURCE; @@ -51,7 +47,7 @@ FOR_ALL_RESOURCES(DECLARE_RESOURCE_CLASS) class Resource : public base::RefCountedThreadSafe { public: - explicit Resource(PluginModule* module); + explicit Resource(PluginInstance* instance); virtual ~Resource(); // Returns NULL if the resource is invalid or is a different type. @@ -61,7 +57,10 @@ class Resource : public base::RefCountedThreadSafe { return resource ? resource->Cast() : NULL; } - PluginModule* module() const { return module_; } + // Returns the instance owning this resource. This is generally to be + // non-NULL except if the instance is destroyed and some code internal to the + // PPAPI implementation is keeping a reference for some reason. + PluginInstance* instance() const { return instance_; } // Cast the resource into a specified type. This will return NULL if the // resource does not match the specified type. Specializations of this @@ -101,6 +100,22 @@ class Resource : public base::RefCountedThreadSafe { const PP_Resource id; }; + // Called by the resource tracker when the last reference from the plugin + // was released. For a few types of resources, the resource could still + // stay alive if there are other references held by the PPAPI implementation + // (possibly for callbacks and things). + // + // When the plugin instance is deleted, all resources associated with that + // plugin will have their plugin references force-deleted and this function + // will be called with instance_destroyed as true. If the plugin normally + // Release()s a reference before the instance is destroyed, + // instance_destroyed will be false. It's possible in some rare cases for the + // plugin to get a new reference to the object in this latter case, if it's + // stored internal to the PPAPI implementation and returned by some function. + // + // If you override this, be sure to call the base class' implementation. + virtual void LastPluginRefWasDeleted(bool instance_destroyed); + private: // Type-specific getters for individual resource types. These will return // NULL if the resource does not match the specified type. Used by the Cast() @@ -118,13 +133,8 @@ class Resource : public base::RefCountedThreadSafe { // refcount. PP_Resource resource_id_; - // Non-owning pointer to our module. - PluginModule* module_; - - // Called by the resource tracker when the last plugin reference has been - // dropped. - friend class ResourceTracker; - void StoppedTracking(); + // Non-owning pointer to our instance. See getter above. + PluginInstance* instance_; DISALLOW_COPY_AND_ASSIGN(Resource); }; diff --git a/webkit/plugins/ppapi/resource_tracker.cc b/webkit/plugins/ppapi/resource_tracker.cc index 7f66160..38d55a9 100644 --- a/webkit/plugins/ppapi/resource_tracker.cc +++ b/webkit/plugins/ppapi/resource_tracker.cc @@ -11,7 +11,9 @@ #include "base/logging.h" #include "base/rand_util.h" #include "ppapi/c/pp_resource.h" +#include "webkit/plugins/ppapi/ppapi_plugin_instance.h" #include "webkit/plugins/ppapi/resource.h" +#include "webkit/plugins/ppapi/var.h" namespace webkit { namespace ppapi { @@ -31,7 +33,8 @@ scoped_refptr ResourceTracker::GetResource(PP_Resource res) const { ResourceTracker* ResourceTracker::singleton_override_ = NULL; ResourceTracker::ResourceTracker() - : last_id_(0) { + : last_resource_id_(0), + last_var_id_(0) { } ResourceTracker::~ResourceTracker() { @@ -45,14 +48,27 @@ ResourceTracker* ResourceTracker::Get() { } PP_Resource ResourceTracker::AddResource(Resource* resource) { - // If the plugin manages to create 4B resources... - if (last_id_ == std::numeric_limits::max()) { + // If the plugin manages to create 4 billion resources, don't do crazy stuff. + if (last_resource_id_ == std::numeric_limits::max()) + return 0; + + // Add the resource with plugin use-count 1. + PP_Resource new_id = ++last_resource_id_; + live_resources_.insert(std::make_pair(new_id, std::make_pair(resource, 1))); + instance_to_resources_[resource->instance()->pp_instance()].insert(new_id); + return new_id; +} + +int32 ResourceTracker::AddVar(Var* var) { + // If the plugin manages to create 4B strings... + if (last_var_id_ == std::numeric_limits::max()) { return 0; } // Add the resource with plugin use-count 1. - ++last_id_; - live_resources_.insert(std::make_pair(last_id_, std::make_pair(resource, 1))); - return last_id_; + ++last_var_id_; + live_vars_.insert(std::make_pair(last_var_id_, + std::make_pair(var, 1))); + return last_var_id_; } bool ResourceTracker::AddRefResource(PP_Resource res) { @@ -72,7 +88,14 @@ bool ResourceTracker::UnrefResource(PP_Resource res) { ResourceMap::iterator i = live_resources_.find(res); if (i != live_resources_.end()) { if (!--i->second.second) { - i->second.first->StoppedTracking(); + Resource* to_release = i->second.first; + to_release->LastPluginRefWasDeleted(false); + + ResourceSet& instance_resource_set = + instance_to_resources_[to_release->instance()->pp_instance()]; + DCHECK(instance_resource_set.find(res) != instance_resource_set.end()); + instance_resource_set.erase(res); + live_resources_.erase(i); } return true; @@ -87,7 +110,16 @@ void ResourceTracker::ForceDeletePluginResourceRefs(PP_Resource res) { return; // Nothing to do. i->second.second = 0; - i->second.first->StoppedTracking(); + Resource* resource = i->second.first; + + // Must delete from the resource set first since the resource's instance + // pointer will get zeroed out in LastPluginRefWasDeleted. + ResourceSet& resource_set = instance_to_resources_[ + resource->instance()->pp_instance()]; + DCHECK(resource_set.find(res) != resource_set.end()); + resource_set.erase(res); + + resource->LastPluginRefWasDeleted(true); live_resources_.erase(i); } @@ -105,6 +137,36 @@ uint32 ResourceTracker::GetLiveObjectsForModule(PluginModule* module) const { return count; } +scoped_refptr ResourceTracker::GetVar(int32 var_id) const { + VarMap::const_iterator result = live_vars_.find(var_id); + if (result == live_vars_.end()) { + return scoped_refptr(); + } + return result->second.first; +} + +bool ResourceTracker::AddRefVar(int32 var_id) { + VarMap::iterator i = live_vars_.find(var_id); + if (i != live_vars_.end()) { + // We don't protect against overflow, since a plugin as malicious as to ref + // once per every byte in the address space could have just as well unrefed + // one time too many. + ++i->second.second; + return true; + } + return false; +} + +bool ResourceTracker::UnrefVar(int32 var_id) { + VarMap::iterator i = live_vars_.find(var_id); + if (i != live_vars_.end()) { + if (!--i->second.second) + live_vars_.erase(i); + return true; + } + return false; +} + PP_Instance ResourceTracker::AddInstance(PluginInstance* instance) { #ifndef NDEBUG // Make sure we're not adding one more than once. @@ -130,6 +192,20 @@ PP_Instance ResourceTracker::AddInstance(PluginInstance* instance) { } void ResourceTracker::InstanceDeleted(PP_Instance instance) { + // Force release all plugin references to resources associated with the + // deleted instance. + ResourceSet& resource_set = instance_to_resources_[instance]; + ResourceSet::iterator i = resource_set.begin(); + while (i != resource_set.end()) { + // Iterators to a set are stable so we can iterate the set while the items + // are being deleted as long as we're careful not to delete the item we're + // holding an iterator to. + ResourceSet::iterator current = i++; + ForceDeletePluginResourceRefs(*current); + } + DCHECK(resource_set.empty()); + instance_to_resources_.erase(instance); + InstanceMap::iterator found = instance_map_.find(instance); if (found == instance_map_.end()) { NOTREACHED(); diff --git a/webkit/plugins/ppapi/resource_tracker.h b/webkit/plugins/ppapi/resource_tracker.h index 7a5e002..0b49ad8 100644 --- a/webkit/plugins/ppapi/resource_tracker.h +++ b/webkit/plugins/ppapi/resource_tracker.h @@ -6,6 +6,7 @@ #define WEBKIT_PLUGINS_PPAPI_RESOURCE_TRACKER_H_ #include +#include #include #include "base/basictypes.h" @@ -27,6 +28,7 @@ class PluginInstance; class PluginModule; class Resource; class ResourceTrackerTest; +class Var; // This class maintains a global list of all live pepper resources. It allows // us to check resource ID validity and to map them to a specific module. @@ -51,9 +53,8 @@ class ResourceTracker { bool AddRefResource(PP_Resource res); bool UnrefResource(PP_Resource res); - // Forces the plugin refcount of the given resource to 0. This can be used to - // delete an object the plugin has leaked or whose lifetime is otherwise - // exceeded. + // Forces the plugin refcount of the given resource to 0. This is used when + // the instance is destroyed and we want to free all resources. // // Note that this may not necessarily delete the resource object since the // regular refcount is maintained separately from the plugin refcount and @@ -66,6 +67,13 @@ class ResourceTracker { // This is slow, use only for testing. uint32 GetLiveObjectsForModule(PluginModule* module) const; + // PP_Vars ------------------------------------------------------------------- + + scoped_refptr GetVar(int32 var_id) const; + + bool AddRefVar(int32 var_id); + bool UnrefVar(int32 var_id); + // PP_Modules ---------------------------------------------------------------- // Adds a new plugin module to the list of tracked module, and returns a new @@ -98,6 +106,7 @@ class ResourceTracker { friend struct base::DefaultLazyInstanceTraits; friend class Resource; friend class ResourceTrackerTest; + friend class Var; // Prohibit creation other then by the Singleton class. ResourceTracker(); @@ -108,6 +117,9 @@ class ResourceTracker { // Resource class. PP_Resource AddResource(Resource* resource); + // The same as AddResource but for Var, and returns the new Var ID. + int32 AddVar(Var* var); + // Overrides the singleton object. This is used for tests which want to // specify their own tracker (otherwise, you can get cross-talk between // tests since the data will live into the subsequent tests). @@ -117,8 +129,9 @@ class ResourceTracker { // See SetSingletonOverride above. static ResourceTracker* singleton_override_; - // Last assigned resource ID. - PP_Resource last_id_; + // Last assigned resource & var ID. + PP_Resource last_resource_id_; + int32 last_var_id_; // For each PP_Resource, keep the Resource* (as refptr) and plugin use count. // This use count is different then Resource's RefCount, and is manipulated @@ -130,6 +143,17 @@ class ResourceTracker { typedef base::hash_map ResourceMap; ResourceMap live_resources_; + // Like ResourceAndRefCount but for vars, which are associated with modules. + typedef std::pair, size_t> VarAndRefCount; + typedef base::hash_map VarMap; + VarMap live_vars_; + + // Tracks all resources associated with each instance. This is used to + // delete resources when the instance has been destroyed to avoid leaks. + typedef std::set ResourceSet; + typedef std::map InstanceToResourceMap; + InstanceToResourceMap instance_to_resources_; + // Tracks all live instances. The pointers are non-owning, the PluginInstance // destructor will notify us when the instance is deleted. typedef std::map InstanceMap; diff --git a/webkit/plugins/ppapi/resource_tracker_unittest.cc b/webkit/plugins/ppapi/resource_tracker_unittest.cc index 4170460..bbcff1f 100644 --- a/webkit/plugins/ppapi/resource_tracker_unittest.cc +++ b/webkit/plugins/ppapi/resource_tracker_unittest.cc @@ -4,7 +4,10 @@ #include "webkit/plugins/ppapi/ppapi_unittest.h" +#include "ppapi/c/ppp_instance.h" +#include "webkit/plugins/ppapi/mock_plugin_delegate.h" #include "webkit/plugins/ppapi/mock_resource.h" +#include "webkit/plugins/ppapi/ppapi_plugin_instance.h" #include "webkit/plugins/ppapi/resource_tracker.h" namespace webkit { @@ -16,7 +19,7 @@ class TrackedMockResource : public MockResource { public: static int tracked_objects_alive; - TrackedMockResource(PluginModule* module) : MockResource(module) { + TrackedMockResource(PluginInstance* instance) : MockResource(instance) { tracked_objects_alive++; } ~TrackedMockResource() { @@ -53,7 +56,7 @@ TEST_F(ResourceTrackerTest, Ref) { EXPECT_EQ(0u, tracker().GetLiveObjectsForModule(module())); { scoped_refptr new_resource( - new TrackedMockResource(module())); + new TrackedMockResource(instance())); ASSERT_EQ(1, TrackedMockResource::tracked_objects_alive); // Since we haven't gotten a PP_Resource, it's not associated with the @@ -66,7 +69,7 @@ TEST_F(ResourceTrackerTest, Ref) { PP_Resource resource_id = 0; { scoped_refptr new_resource( - new TrackedMockResource(module())); + new TrackedMockResource(instance())); ASSERT_EQ(1, TrackedMockResource::tracked_objects_alive); resource_id = new_resource->GetReference(); EXPECT_EQ(1u, tracker().GetLiveObjectsForModule(module())); @@ -87,14 +90,20 @@ TEST_F(ResourceTrackerTest, Ref) { ASSERT_EQ(0, TrackedMockResource::tracked_objects_alive); } -TEST_F(ResourceTrackerTest, ForceDelete) { - // Make two resources. +TEST_F(ResourceTrackerTest, ForceDeleteWithInstance) { + // Make a second instance (the test harness already creates & manages one). + scoped_refptr instance2( + new PluginInstance(delegate(), module(), + static_cast( + GetMockInterface(PPP_INSTANCE_INTERFACE)))); + + // Make two resources and take refs on behalf of the "plugin" for each. scoped_refptr resource1( - new TrackedMockResource(module())); - PP_Resource pp_resource1 = resource1->GetReference(); + new TrackedMockResource(instance2)); + resource1->GetReference(); scoped_refptr resource2( - new TrackedMockResource(module())); - PP_Resource pp_resource2 = resource2->GetReference(); + new TrackedMockResource(instance2)); + resource2->GetReference(); // Keep an "internal" ref to only the first (the PP_Resource also holds a // ref to each resource on behalf of the plugin). @@ -103,12 +112,13 @@ TEST_F(ResourceTrackerTest, ForceDelete) { ASSERT_EQ(2, TrackedMockResource::tracked_objects_alive); EXPECT_EQ(2u, tracker().GetLiveObjectsForModule(module())); - // Force delete both refs. - tracker().ForceDeletePluginResourceRefs(pp_resource1); - tracker().ForceDeletePluginResourceRefs(pp_resource2); + // Free the instance, this should release both plugin refs. + instance2 = NULL; EXPECT_EQ(0u, tracker().GetLiveObjectsForModule(module())); - // The resource we have a scoped_refptr to should still be alive. + // The resource we have a scoped_refptr to should still be alive, but it + // should have a NULL instance. + ASSERT_FALSE(resource1->instance()); ASSERT_EQ(1, TrackedMockResource::tracked_objects_alive); resource1 = NULL; ASSERT_EQ(0, TrackedMockResource::tracked_objects_alive); diff --git a/webkit/plugins/ppapi/url_request_info_unittest.cc b/webkit/plugins/ppapi/url_request_info_unittest.cc index 769d760..ba66058 100644 --- a/webkit/plugins/ppapi/url_request_info_unittest.cc +++ b/webkit/plugins/ppapi/url_request_info_unittest.cc @@ -45,7 +45,7 @@ namespace ppapi { class URLRequestInfoTest : public PpapiUnittest { public: - URLRequestInfoTest() : info_(new PPB_URLRequestInfo_Impl(module())) { + URLRequestInfoTest() : info_(new PPB_URLRequestInfo_Impl(instance())) { } static void SetUpTestCase() { diff --git a/webkit/plugins/ppapi/var.cc b/webkit/plugins/ppapi/var.cc index 4ed0e10..6710c8e 100644 --- a/webkit/plugins/ppapi/var.cc +++ b/webkit/plugins/ppapi/var.cc @@ -17,6 +17,7 @@ #include "webkit/plugins/ppapi/plugin_module.h" #include "webkit/plugins/ppapi/plugin_object.h" #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" +#include "webkit/plugins/ppapi/resource_tracker.h" #include "v8/include/v8.h" using WebKit::WebBindings; @@ -206,7 +207,7 @@ PP_Var RunJSFunction(PP_Var scope_var, if (WebBindings::invokeDefault(NULL, NPVARIANT_TO_OBJECT(function_var), args.get(), argc, &result_var)) { - result = Var::NPVariantToPPVar(obj->module(), &result_var); + result = Var::NPVariantToPPVar(obj->instance(), &result_var); } else { DCHECK(try_catch.has_exception()); result = PP_MakeUndefined(); @@ -377,7 +378,7 @@ PP_Var GetProperty(PP_Var var, return PP_MakeUndefined(); } - PP_Var ret = Var::NPVariantToPPVar(accessor.object()->module(), &result); + PP_Var ret = Var::NPVariantToPPVar(accessor.object()->instance(), &result); WebBindings::releaseVariantValue(&result); return ret; } @@ -407,8 +408,9 @@ void EnumerateProperties(PP_Var var, *property_count = count; *properties = static_cast(malloc(sizeof(PP_Var) * count)); for (uint32_t i = 0; i < count; ++i) { - (*properties)[i] = Var::NPIdentifierToPPVar(accessor.object()->module(), - identifiers[i]); + (*properties)[i] = Var::NPIdentifierToPPVar( + accessor.object()->instance()->module(), + identifiers[i]); } free(identifiers); } @@ -493,7 +495,7 @@ struct PP_Var Call(struct PP_Var object, return PP_MakeUndefined(); } - PP_Var ret = Var::NPVariantToPPVar(accessor.module(), &result); + PP_Var ret = Var::NPVariantToPPVar(accessor.object()->instance(), &result); WebBindings::releaseVariantValue(&result); return ret; } @@ -551,7 +553,7 @@ PP_Var CallDeprecated(PP_Var var, return PP_MakeUndefined(); } - PP_Var ret = Var::NPVariantToPPVar(accessor.module(), &result); + PP_Var ret = Var::NPVariantToPPVar(accessor.object()->instance(), &result); WebBindings::releaseVariantValue(&result); return ret; } @@ -584,7 +586,7 @@ PP_Var Construct(PP_Var var, return PP_MakeUndefined(); } - PP_Var ret = Var::NPVariantToPPVar(accessor.module(), &result); + PP_Var ret = Var::NPVariantToPPVar(accessor.object()->instance(), &result); WebBindings::releaseVariantValue(&result); return ret; } @@ -608,7 +610,7 @@ PP_Var CreateObjectDeprecated(PP_Instance instance_id, DLOG(ERROR) << "Create object passed an invalid instance."; return PP_MakeNull(); } - return PluginObject::Create(instance->module(), ppp_class, ppp_class_data); + return PluginObject::Create(instance, ppp_class, ppp_class_data); } PP_Var CreateObjectWithModuleDeprecated(PP_Module module_id, @@ -617,7 +619,8 @@ PP_Var CreateObjectWithModuleDeprecated(PP_Module module_id, PluginModule* module = ResourceTracker::Get()->GetModule(module_id); if (!module) return PP_MakeNull(); - return PluginObject::Create(module, ppp_class, ppp_class_data); + return PluginObject::Create(module->GetSomeInstance(), + ppp_class, ppp_class_data); } const PPB_Var_Deprecated var_deprecated_interface = { @@ -659,18 +662,15 @@ const PPB_Var var_interface = { // Var ------------------------------------------------------------------------- -Var::Var(PluginModule* module) : Resource(module) { +Var::Var(PluginModule* module) : module_(module), var_id_(0) { } Var::~Var() { } -Var* Var::AsVar() { - return this; -} - // static -PP_Var Var::NPVariantToPPVar(PluginModule* module, const NPVariant* variant) { +PP_Var Var::NPVariantToPPVar(PluginInstance* instance, + const NPVariant* variant) { switch (variant->type) { case NPVariantType_Void: return PP_MakeUndefined(); @@ -684,11 +684,12 @@ PP_Var Var::NPVariantToPPVar(PluginModule* module, const NPVariant* variant) { return PP_MakeDouble(NPVARIANT_TO_DOUBLE(*variant)); case NPVariantType_String: return StringVar::StringToPPVar( - module, + instance->module(), NPVARIANT_TO_STRING(*variant).UTF8Characters, NPVARIANT_TO_STRING(*variant).UTF8Length); case NPVariantType_Object: - return ObjectVar::NPObjectToPPVar(module, NPVARIANT_TO_OBJECT(*variant)); + return ObjectVar::NPObjectToPPVar(instance, + NPVARIANT_TO_OBJECT(*variant)); } NOTREACHED(); return PP_MakeUndefined(); @@ -725,22 +726,16 @@ PP_Var Var::NPIdentifierToPPVar(PluginModule* module, NPIdentifier id) { // static void Var::PluginAddRefPPVar(PP_Var var) { if (var.type == PP_VARTYPE_STRING || var.type == PP_VARTYPE_OBJECT) { - // TODO(brettw) consider checking that the ID is actually a var ID rather - // than some random other resource ID. - PP_Resource resource = static_cast(var.value.as_id); - if (!ResourceTracker::Get()->AddRefResource(resource)) - DLOG(WARNING) << "AddRefVar()ing a nonexistant string/object var."; + if (!ResourceTracker::Get()->AddRefVar(static_cast(var.value.as_id))) + DLOG(WARNING) << "AddRefVar()ing a nonexistent string/object var."; } } // static void Var::PluginReleasePPVar(PP_Var var) { if (var.type == PP_VARTYPE_STRING || var.type == PP_VARTYPE_OBJECT) { - // TODO(brettw) consider checking that the ID is actually a var ID rather - // than some random other resource ID. - PP_Resource resource = static_cast(var.value.as_id); - if (!ResourceTracker::Get()->UnrefResource(resource)) - DLOG(WARNING) << "ReleaseVar()ing a nonexistant string/object var."; + if (!ResourceTracker::Get()->UnrefVar(static_cast(var.value.as_id))) + DLOG(WARNING) << "ReleaseVar()ing a nonexistent string/object var."; } } @@ -753,6 +748,27 @@ const PPB_Var* Var::GetInterface() { return &var_interface; } +StringVar* Var::AsStringVar() { + return NULL; +} + +ObjectVar* Var::AsObjectVar() { + return NULL; +} + +int32 Var::GetID() { + // This should only be called for objects and strings. POD vars like integers + // have no identifiers. + DCHECK(AsStringVar() || AsObjectVar()); + + ResourceTracker *tracker = ResourceTracker::Get(); + if (var_id_) + tracker->AddRefVar(var_id_); + else + var_id_ = tracker->AddVar(this); + return var_id_; +} + // StringVar ------------------------------------------------------------------- StringVar::StringVar(PluginModule* module, const char* str, uint32 len) @@ -783,29 +799,33 @@ PP_Var StringVar::StringToPPVar(PluginModule* module, ret.type = PP_VARTYPE_STRING; // The caller takes ownership now. - ret.value.as_id = str->GetReference(); + ret.value.as_id = str->GetID(); return ret; } // static scoped_refptr StringVar::FromPPVar(PP_Var var) { if (var.type != PP_VARTYPE_STRING) - return scoped_refptr(NULL); - PP_Resource resource = static_cast(var.value.as_id); - return Resource::GetAs(resource); + return scoped_refptr(); + scoped_refptr var_object( + ResourceTracker::Get()->GetVar(static_cast(var.value.as_id))); + if (!var_object) + return scoped_refptr(); + return scoped_refptr(var_object->AsStringVar()); } // ObjectVar ------------------------------------------------------------- -ObjectVar::ObjectVar(PluginModule* module, NPObject* np_object) - : Var(module), +ObjectVar::ObjectVar(PluginInstance* instance, NPObject* np_object) + : Var(instance->module()), + instance_(instance), np_object_(np_object) { WebBindings::retainObject(np_object_); - module->AddNPObjectVar(this); + instance->AddNPObjectVar(this); } ObjectVar::~ObjectVar() { - module()->RemoveNPObjectVar(this); + instance_->RemoveNPObjectVar(this); WebBindings::releaseObject(np_object_); } @@ -814,10 +834,10 @@ ObjectVar* ObjectVar::AsObjectVar() { } // static -PP_Var ObjectVar::NPObjectToPPVar(PluginModule* module, NPObject* object) { - scoped_refptr object_var(module->ObjectVarForNPObject(object)); +PP_Var ObjectVar::NPObjectToPPVar(PluginInstance* instance, NPObject* object) { + scoped_refptr object_var(instance->ObjectVarForNPObject(object)); if (!object_var) // No object for this module yet, make a new one. - object_var = new ObjectVar(module, object); + object_var = new ObjectVar(instance, object); if (!object_var) return PP_MakeUndefined(); @@ -825,7 +845,7 @@ PP_Var ObjectVar::NPObjectToPPVar(PluginModule* module, NPObject* object) { // Convert to a PP_Var, GetReference will AddRef for us. PP_Var result; result.type = PP_VARTYPE_OBJECT; - result.value.as_id = object_var->GetReference(); + result.value.as_id = object_var->GetID(); return result; } @@ -833,8 +853,11 @@ PP_Var ObjectVar::NPObjectToPPVar(PluginModule* module, NPObject* object) { scoped_refptr ObjectVar::FromPPVar(PP_Var var) { if (var.type != PP_VARTYPE_OBJECT) return scoped_refptr(NULL); - PP_Resource resource = static_cast(var.value.as_id); - return Resource::GetAs(resource); + scoped_refptr var_object( + ResourceTracker::Get()->GetVar(static_cast(var.value.as_id))); + if (!var_object) + return scoped_refptr(); + return scoped_refptr(var_object->AsObjectVar()); } // TryCatch -------------------------------------------------------------------- diff --git a/webkit/plugins/ppapi/var.h b/webkit/plugins/ppapi/var.h index 8f74733..ef3613f 100644 --- a/webkit/plugins/ppapi/var.h +++ b/webkit/plugins/ppapi/var.h @@ -7,7 +7,8 @@ #include -#include "webkit/plugins/ppapi/resource.h" +#include "base/compiler_specific.h" +#include "base/ref_counted.h" struct PP_Var; struct PPB_Var; @@ -19,18 +20,20 @@ typedef void* NPIdentifier; namespace webkit { namespace ppapi { +class ObjectVar; +class PluginInstance; +class PluginModule; +class StringVar; + // Var ------------------------------------------------------------------------- // Represents a non-POD var. This is derived from a resource even though it // isn't a resource from the plugin's perspective. This allows us to re-use // the refcounting and the association with the module from the resource code. -class Var : public Resource { +class Var : public base::RefCounted { public: virtual ~Var(); - // Resource overrides. - virtual Var* AsVar(); - // Returns a PP_Var that corresponds to the given NPVariant. The contents of // the NPVariant will be copied unless the NPVariant corresponds to an // object. This will handle all Variant types including POD, strings, and @@ -38,7 +41,7 @@ class Var : public Resource { // // The returned PP_Var will have a refcount of 1, this passing ownership of // the reference to the caller. This is suitable for returning to a plugin. - static PP_Var NPVariantToPPVar(PluginModule* module, + static PP_Var NPVariantToPPVar(PluginInstance* instance, const NPVariant* variant); // Returns a NPIdentifier that corresponds to the given PP_Var. The contents @@ -87,11 +90,26 @@ class Var : public Resource { // Returns the PPB_Var interface for the plugin to use. static const PPB_Var* GetInterface(); + virtual StringVar* AsStringVar(); + virtual ObjectVar* AsObjectVar(); + + PluginModule* module() const { return module_; } + + // Returns the unique ID associated with this string or object. The object + // must be a string or an object var, and the return value is guaranteed + // nonzero. + int32 GetID(); + protected: // This can only be constructed as a StringVar or an ObjectVar. explicit Var(PluginModule* module); private: + PluginModule* module_; + + // This will be 0 if no ID has been assigned (this happens lazily). + int32 var_id_; + DISALLOW_COPY_AND_ASSIGN(Var); }; @@ -114,8 +132,8 @@ class StringVar : public Var { const std::string& value() const { return value_; } - // Resource overrides. - virtual StringVar* AsStringVar(); + // Var override. + virtual StringVar* AsStringVar() OVERRIDE; // Helper function to create a PP_Var of type string that contains a copy of // the given string. The input data must be valid UTF-8 encoded text, if it @@ -154,8 +172,8 @@ class ObjectVar : public Var { public: virtual ~ObjectVar(); - // Resource overrides. - virtual ObjectVar* AsObjectVar(); + // Var overrides. + virtual ObjectVar* AsObjectVar() OVERRIDE; // Returns the underlying NPObject corresponding to this ObjectVar. // Guaranteed non-NULL. @@ -172,19 +190,23 @@ class ObjectVar : public Var { // // If no ObjectVar currently exists corresponding to the NPObject, one is // created associated with the given module. - static PP_Var NPObjectToPPVar(PluginModule* module, NPObject* object); + static PP_Var NPObjectToPPVar(PluginInstance* instance, NPObject* object); // Helper function that converts a PP_Var to an object. This will return NULL // if the PP_Var is not of object type or the object is invalid. static scoped_refptr FromPPVar(PP_Var var); + PluginInstance* instance() const { return instance_; } + protected: // You should always use FromNPObject to create an ObjectVar. This function // guarantees that we maintain the 1:1 mapping between NPObject and // ObjectVar. - ObjectVar(PluginModule* module, NPObject* np_object); + ObjectVar(PluginInstance* instance, NPObject* np_object); private: + PluginInstance* instance_; + // Guaranteed non-NULL, this is the underlying object used by WebKit. We // hold a reference to this object. NPObject* np_object_; diff --git a/webkit/plugins/ppapi/var_object_class.cc b/webkit/plugins/ppapi/var_object_class.cc index 282ad72..d75002a 100644 --- a/webkit/plugins/ppapi/var_object_class.cc +++ b/webkit/plugins/ppapi/var_object_class.cc @@ -26,9 +26,9 @@ class VarObjectAccessorWithIdentifier { public: VarObjectAccessorWithIdentifier(NPObject* object, NPIdentifier identifier) : exists_(false), - instance_(static_cast(object)), + instance_data_(static_cast(object)), property_(NULL) { - if (instance_) { + if (instance_data_) { const NPUTF8* string_value = NULL; int32_t int_value = 0; bool is_string = false; @@ -38,7 +38,7 @@ class VarObjectAccessorWithIdentifier { property_name_ = string_value; const VarObjectClass::PropertyMap& properties = - instance_->object_class->properties(); + instance_data_->object_class->properties(); VarObjectClass::PropertyMap::const_iterator it = properties.find(property_name_); if (it != properties.end()) { @@ -57,15 +57,17 @@ class VarObjectAccessorWithIdentifier { bool is_writable() const { return exists() && property_->setter && property_->writable; } - const VarObjectClass::InstanceData* instance() const { return instance_; } + const VarObjectClass::InstanceData* instance_data() const { + return instance_data_; + } const VarObjectClass::Property* property() const { return property_; } - PluginModule* module() const { - return instance_ ? instance_->object_class->module() : NULL; + PluginInstance* instance() const { + return instance_data_ ? instance_data_->object_class->instance() : NULL; } private: bool exists_; - const VarObjectClass::InstanceData* instance_; + const VarObjectClass::InstanceData* instance_data_; std::string property_name_; const VarObjectClass::Property* property_; @@ -97,12 +99,12 @@ bool VarObjectClassInvoke(NPObject* np_obj, NPIdentifier name, return false; PPResultAndExceptionToNPResult result_converter(np_obj, result); - PPVarArrayFromNPVariantArray arguments(accessor.module(), arg_count, args); - PPVarFromNPObject self(accessor.module(), np_obj); + PPVarArrayFromNPVariantArray arguments(accessor.instance(), arg_count, args); + PPVarFromNPObject self(accessor.instance(), np_obj); return result_converter.SetResult(accessor.property()->method( - accessor.instance()->native_data, self.var(), arguments.array(), arg_count, - result_converter.exception())); + accessor.instance_data()->native_data, self.var(), arguments.array(), + arg_count, result_converter.exception())); } bool VarObjectClassInvokeDefault(NPObject* np_obj, @@ -115,9 +117,9 @@ bool VarObjectClassInvokeDefault(NPObject* np_obj, return false; PPResultAndExceptionToNPResult result_converter(np_obj, result); - PPVarArrayFromNPVariantArray arguments(instance->object_class->module(), + PPVarArrayFromNPVariantArray arguments(instance->object_class->instance(), arg_count, args); - PPVarFromNPObject self(instance->object_class->module(), np_obj); + PPVarFromNPObject self(instance->object_class->instance(), np_obj); return result_converter.SetResult(instance->object_class->instance_invoke()( instance->native_data, self.var(), arguments.array(), arg_count, @@ -137,27 +139,27 @@ bool VarObjectClassGetProperty(NPObject* np_obj, NPIdentifier name, } PPResultAndExceptionToNPResult result_converter(np_obj, result); - PPVarFromNPObject self(accessor.module(), np_obj); + PPVarFromNPObject self(accessor.instance(), np_obj); return result_converter.SetResult(accessor.property()->getter( - accessor.instance()->native_data, self.var(), 0, 0, - result_converter.exception())); + accessor.instance_data()->native_data, self.var(), 0, 0, + result_converter.exception())); } bool VarObjectClassSetProperty(NPObject* np_obj, NPIdentifier name, - const NPVariant* variant) { + const NPVariant* variant) { VarObjectAccessorWithIdentifier accessor(np_obj, name); if (!accessor.is_writable()) { return false; } PPResultAndExceptionToNPResult result_converter(np_obj, NULL); - PPVarArrayFromNPVariantArray arguments(accessor.module(), 1, variant); - PPVarFromNPObject self(accessor.module(), np_obj); + PPVarArrayFromNPVariantArray arguments(accessor.instance(), 1, variant); + PPVarFromNPObject self(accessor.instance(), np_obj); // Ignore return value. Var::PluginReleasePPVar(accessor.property()->setter( - accessor.instance()->native_data, self.var(), arguments.array(), 1, + accessor.instance_data()->native_data, self.var(), arguments.array(), 1, result_converter.exception())); return result_converter.CheckExceptionForNoResult(); @@ -207,12 +209,12 @@ NPClass objectclassvar_class = { // PPB_Class ------------------------------------------------------------------- -PP_Resource Create(PP_Module module, PP_ClassDestructor destruct, +PP_Resource Create(PP_Instance instance_id, PP_ClassDestructor destruct, PP_ClassFunction invoke, PP_ClassProperty* properties) { - PluginModule* plugin_module = ResourceTracker::Get()->GetModule(module); - if (!properties || !plugin_module) + PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); + if (!properties || !instance) return 0; - scoped_refptr cls = new VarObjectClass(plugin_module, + scoped_refptr cls = new VarObjectClass(instance, destruct, invoke, properties); @@ -232,7 +234,7 @@ PP_Var Instantiate(PP_Resource class_object, void* native_data, static_cast(obj); instance_data->object_class = object_class; instance_data->native_data = native_data; - return ObjectVar::NPObjectToPPVar(object_class->module(), obj); + return ObjectVar::NPObjectToPPVar(object_class->instance(), obj); } } // namespace @@ -250,11 +252,11 @@ VarObjectClass::Property::Property(const PP_ClassProperty& prop) VarObjectClass::InstanceData::InstanceData() : native_data(NULL) { } -VarObjectClass::VarObjectClass(PluginModule* module, +VarObjectClass::VarObjectClass(PluginInstance* instance, PP_ClassDestructor destruct, PP_ClassFunction invoke, PP_ClassProperty* properties) - : Resource(module), + : Resource(instance), instance_native_destructor_(destruct), instance_invoke_(invoke) { PP_ClassProperty* prop = properties; diff --git a/webkit/plugins/ppapi/var_object_class.h b/webkit/plugins/ppapi/var_object_class.h index 7f9cb98..bcee3cb 100644 --- a/webkit/plugins/ppapi/var_object_class.h +++ b/webkit/plugins/ppapi/var_object_class.h @@ -17,7 +17,7 @@ namespace webkit { namespace ppapi { -class PluginModule; +class PluginInstance; class VarObjectClass : public Resource { public: @@ -40,7 +40,7 @@ class VarObjectClass : public Resource { typedef base::hash_map PropertyMap; - VarObjectClass(PluginModule* module, PP_ClassDestructor destruct, + VarObjectClass(PluginInstance* instance, PP_ClassDestructor destruct, PP_ClassFunction invoke, PP_ClassProperty* properties); virtual ~VarObjectClass(); -- cgit v1.1