From ce701cd1471af1386f770cf18b25b7785d94d0b5 Mon Sep 17 00:00:00 2001 From: "brettw@chromium.org" Date: Mon, 1 Aug 2011 21:47:04 +0000 Subject: Move Var and StringVar to the ppapi/shared_impl so they can be used in the proxy. This does not, however, actually use these in the proxy yet, I'll do that in a subsequent pass. ObjectVar is now renamed NPObjectVar and continues to be in webkit/plugins since it uses NPObjects. There will be a corresponding future class in the proxy for proxied objects. The TryCatch that was in var.h is now in npapi_glue.h Most of the rest of the change is updating namespaces. TEST=unit tests BUG=none Review URL: http://codereview.chromium.org/7488062 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@94986 0039d316-1c4b-4281-b951-d872f2087c98 --- webkit/plugins/ppapi/message_channel.cc | 3 +- webkit/plugins/ppapi/npapi_glue.cc | 71 +++++- webkit/plugins/ppapi/npapi_glue.h | 55 +++++ webkit/plugins/ppapi/npobject_var.cc | 65 ++++++ webkit/plugins/ppapi/npobject_var.h | 71 ++++++ webkit/plugins/ppapi/plugin_module.cc | 1 - webkit/plugins/ppapi/plugin_object.cc | 4 +- webkit/plugins/ppapi/ppapi_plugin_instance.cc | 4 +- webkit/plugins/ppapi/ppapi_webplugin_impl.cc | 7 +- webkit/plugins/ppapi/ppb_char_set_impl.cc | 5 +- webkit/plugins/ppapi/ppb_console_impl.cc | 3 +- webkit/plugins/ppapi/ppb_file_ref_impl.cc | 5 +- webkit/plugins/ppapi/ppb_flash_clipboard_impl.cc | 4 +- webkit/plugins/ppapi/ppb_flash_impl.cc | 3 +- webkit/plugins/ppapi/ppb_flash_impl_linux.cc | 3 +- webkit/plugins/ppapi/ppb_font_impl.cc | 3 +- webkit/plugins/ppapi/ppb_input_event_impl.cc | 3 +- webkit/plugins/ppapi/ppb_transport_impl.cc | 3 +- webkit/plugins/ppapi/ppb_uma_private_impl.cc | 4 +- webkit/plugins/ppapi/ppb_url_request_info_impl.cc | 3 +- webkit/plugins/ppapi/ppb_url_response_info_impl.cc | 3 +- webkit/plugins/ppapi/ppb_url_util_impl.cc | 7 +- webkit/plugins/ppapi/ppb_var_impl.cc | 15 +- webkit/plugins/ppapi/ppb_video_decoder_impl.cc | 1 - webkit/plugins/ppapi/resource_creation_impl.cc | 3 +- webkit/plugins/ppapi/resource_tracker.cc | 75 +++--- webkit/plugins/ppapi/resource_tracker.h | 41 ++-- webkit/plugins/ppapi/resource_tracker_unittest.cc | 6 +- webkit/plugins/ppapi/var.cc | 255 -------------------- webkit/plugins/ppapi/var.h | 260 --------------------- 30 files changed, 371 insertions(+), 615 deletions(-) create mode 100644 webkit/plugins/ppapi/npobject_var.cc create mode 100644 webkit/plugins/ppapi/npobject_var.h delete mode 100644 webkit/plugins/ppapi/var.cc delete mode 100644 webkit/plugins/ppapi/var.h (limited to 'webkit/plugins') diff --git a/webkit/plugins/ppapi/message_channel.cc b/webkit/plugins/ppapi/message_channel.cc index 2edfe13..71368a6 100644 --- a/webkit/plugins/ppapi/message_channel.cc +++ b/webkit/plugins/ppapi/message_channel.cc @@ -14,10 +14,11 @@ #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" +#include "ppapi/shared_impl/var.h" #include "webkit/plugins/ppapi/npapi_glue.h" #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" -#include "webkit/plugins/ppapi/var.h" +using ppapi::StringVar; using WebKit::WebBindings; namespace webkit { diff --git a/webkit/plugins/ppapi/npapi_glue.cc b/webkit/plugins/ppapi/npapi_glue.cc index 2d2c342..93c57ed 100644 --- a/webkit/plugins/ppapi/npapi_glue.cc +++ b/webkit/plugins/ppapi/npapi_glue.cc @@ -7,15 +7,18 @@ #include "base/logging.h" #include "base/memory/ref_counted.h" #include "base/string_util.h" +#include "webkit/plugins/ppapi/npobject_var.h" #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 "webkit/plugins/ppapi/var.h" #include "third_party/npapi/bindings/npapi.h" #include "third_party/npapi/bindings/npruntime.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" +using ppapi::NPObjectVar; +using ppapi::StringVar; +using ppapi::Var; using WebKit::WebBindings; namespace webkit { @@ -59,7 +62,7 @@ bool PPVarToNPVariant(PP_Var var, NPVariant* result) { break; } case PP_VARTYPE_OBJECT: { - scoped_refptr object(ObjectVar::FromPPVar(var)); + scoped_refptr object(NPObjectVar::FromPPVar(var)); if (!object) { VOID_TO_NPVARIANT(*result); return false; @@ -128,12 +131,12 @@ PP_Var NPIdentifierToPPVar(PP_Module module, NPIdentifier id) { PP_Var NPObjectToPPVar(PluginInstance* instance, NPObject* object) { DCHECK(object); - scoped_refptr object_var( - ResourceTracker::Get()->ObjectVarForNPObject(instance->pp_instance(), - object)); + scoped_refptr object_var( + ResourceTracker::Get()->NPObjectVarForNPObject(instance->pp_instance(), + object)); if (!object_var) { // No object for this module yet, make a new one. - object_var = new ObjectVar(instance->module()->pp_module(), - instance->pp_instance(), object); + object_var = new NPObjectVar(instance->module()->pp_module(), + instance->pp_instance(), object); } return object_var->GetPPVar(); } @@ -156,7 +159,7 @@ PPResultAndExceptionToNPResult::~PPResultAndExceptionToNPResult() { // been lost. DCHECK(checked_exception_); - ObjectVar::PluginReleasePPVar(exception_); + NPObjectVar::PluginReleasePPVar(exception_); } // Call this with the return value of the PPAPI function. It will convert @@ -181,7 +184,7 @@ bool PPResultAndExceptionToNPResult::SetResult(PP_Var result) { // No matter what happened, we need to release the reference to the // value passed in. On success, a reference to this value will be in // the np_result_. - Var::PluginReleasePPVar(result); + ::ppapi::Var::PluginReleasePPVar(result); return success_; } @@ -235,7 +238,7 @@ PPVarArrayFromNPVariantArray::PPVarArrayFromNPVariantArray( PPVarArrayFromNPVariantArray::~PPVarArrayFromNPVariantArray() { for (size_t i = 0; i < size_; i++) - Var::PluginReleasePPVar(array_[i]); + ::ppapi::Var::PluginReleasePPVar(array_[i]); } // PPVarFromNPObject ----------------------------------------------------------- @@ -245,7 +248,7 @@ PPVarFromNPObject::PPVarFromNPObject(PluginInstance* instance, NPObject* object) } PPVarFromNPObject::~PPVarFromNPObject() { - Var::PluginReleasePPVar(var_); + ::ppapi::Var::PluginReleasePPVar(var_); } // NPObjectAccessorWithIdentifier ---------------------------------------------- @@ -265,7 +268,51 @@ NPObjectAccessorWithIdentifier::NPObjectAccessorWithIdentifier( } NPObjectAccessorWithIdentifier::~NPObjectAccessorWithIdentifier() { - Var::PluginReleasePPVar(identifier_); + ::ppapi::Var::PluginReleasePPVar(identifier_); +} + +// TryCatch -------------------------------------------------------------------- + +TryCatch::TryCatch(PP_Module module, PP_Var* exception) + : pp_module_(module), + has_exception_(exception && exception->type != PP_VARTYPE_UNDEFINED), + exception_(exception) { + WebBindings::pushExceptionHandler(&TryCatch::Catch, this); +} + +TryCatch::~TryCatch() { + WebBindings::popExceptionHandler(); +} + +void TryCatch::SetException(const char* message) { + if (!pp_module_) { + // Don't have a module to make the string. + SetInvalidObjectException(); + return; + } + + if (!has_exception()) { + has_exception_ = true; + if (exception_) { + *exception_ = ::ppapi::StringVar::StringToPPVar(pp_module_, + message, strlen(message)); + } + } +} + +void TryCatch::SetInvalidObjectException() { + if (!has_exception()) { + has_exception_ = true; + // TODO(brettw) bug 54504: Have a global singleton string that can hold + // a generic error message. + if (exception_) + *exception_ = PP_MakeInt32(1); + } +} + +// static +void TryCatch::Catch(void* self, const char* message) { + static_cast(self)->SetException(message); } } // namespace ppapi diff --git a/webkit/plugins/ppapi/npapi_glue.h b/webkit/plugins/ppapi/npapi_glue.h index ced0fe6..2ca7772 100644 --- a/webkit/plugins/ppapi/npapi_glue.h +++ b/webkit/plugins/ppapi/npapi_glue.h @@ -220,6 +220,61 @@ class NPObjectAccessorWithIdentifier { DISALLOW_COPY_AND_ASSIGN(NPObjectAccessorWithIdentifier); }; +// TryCatch -------------------------------------------------------------------- + +// Instantiate this object on the stack to catch V8 exceptions and pass them +// to an optional out parameter supplied by the plugin. +class TryCatch { + public: + // The given exception may be NULL if the consumer isn't interested in + // catching exceptions. If non-NULL, the given var will be updated if any + // exception is thrown (so it must outlive the TryCatch object). + // + // The module associated with the exception is passed so we know which module + // to associate any exception string with. It may be NULL if you don't know + // the module at construction time, in which case you should set it later + // by calling set_module(). + // + // If an exception is thrown when the module is NULL, setting *any* exception + // will result in using the InvalidObjectException. + TryCatch(PP_Module module, PP_Var* exception); + ~TryCatch(); + + // Get and set the module. This may be NULL (see the constructor). + PP_Module pp_module() { return pp_module_; } + void set_pp_module(PP_Module module) { pp_module_ = module; } + + // Returns true is an exception has been thrown. This can be true immediately + // after construction if the var passed to the constructor is non-void. + bool has_exception() const { return has_exception_; } + + // Sets the given exception. If no module has been set yet, the message will + // be ignored (since we have no module to associate the string with) and the + // SetInvalidObjectException() will be used instead. + // + // If an exception has been previously set, this function will do nothing + // (normally you want only the first exception). + void SetException(const char* message); + + // Sets the exception to be a generic message contained in a magic string + // not associated with any module. + void SetInvalidObjectException(); + + private: + static void Catch(void* self, const char* message); + + PP_Module pp_module_; + + // True if an exception has been thrown. Since the exception itself may be + // NULL if the plugin isn't interested in getting the exception, this will + // always indicate if SetException has been called, regardless of whether + // the exception itself has been stored. + bool has_exception_; + + // May be null if the consumer isn't interesting in catching exceptions. + PP_Var* exception_; +}; + } // namespace ppapi } // namespace webkit diff --git a/webkit/plugins/ppapi/npobject_var.cc b/webkit/plugins/ppapi/npobject_var.cc new file mode 100644 index 0000000..b951cde --- /dev/null +++ b/webkit/plugins/ppapi/npobject_var.cc @@ -0,0 +1,65 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "webkit/plugins/ppapi/npobject_var.h" + +#include "base/logging.h" +#include "ppapi/c/pp_var.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" +#include "webkit/plugins/ppapi/resource_tracker.h" + +using WebKit::WebBindings; + +namespace ppapi { + +// NPObjectVar ----------------------------------------------------------------- + +NPObjectVar::NPObjectVar(PP_Module module, + PP_Instance instance, + NPObject* np_object) + : Var(module), + pp_instance_(instance), + np_object_(np_object) { + WebBindings::retainObject(np_object_); + webkit::ppapi::ResourceTracker::Get()->AddNPObjectVar(this); +} + +NPObjectVar::~NPObjectVar() { + if (pp_instance()) + webkit::ppapi::ResourceTracker::Get()->RemoveNPObjectVar(this); + WebBindings::releaseObject(np_object_); +} + +NPObjectVar* NPObjectVar::AsNPObjectVar() { + return this; +} + +PP_Var NPObjectVar::GetPPVar() { + int32 id = GetOrCreateVarID(); + if (!id) + return PP_MakeNull(); + + PP_Var result; + result.type = PP_VARTYPE_OBJECT; + result.value.as_id = id; + return result; +} + +void NPObjectVar::InstanceDeleted() { + DCHECK(pp_instance_); + pp_instance_ = 0; +} + +// static +scoped_refptr NPObjectVar::FromPPVar(PP_Var var) { + if (var.type != PP_VARTYPE_OBJECT) + return scoped_refptr(NULL); + scoped_refptr var_object(webkit::ppapi::ResourceTracker::Get()->GetVar( + static_cast(var.value.as_id))); + if (!var_object) + return scoped_refptr(); + return scoped_refptr(var_object->AsNPObjectVar()); +} + +} // namespace ppapi diff --git a/webkit/plugins/ppapi/npobject_var.h b/webkit/plugins/ppapi/npobject_var.h new file mode 100644 index 0000000..d0ba122 --- /dev/null +++ b/webkit/plugins/ppapi/npobject_var.h @@ -0,0 +1,71 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef WEBKIT_PLUGINS_PPAPI_NPOBJECT_VAR_H_ +#define WEBKIT_PLUGINS_PPAPI_NPOBJECT_VAR_H_ + +#include + +#include "base/compiler_specific.h" +#include "ppapi/c/pp_instance.h" +#include "ppapi/c/pp_module.h" +#include "ppapi/shared_impl/var.h" + +typedef struct NPObject NPObject; +typedef struct _NPVariant NPVariant; +typedef void* NPIdentifier; + +namespace ppapi { + +// NPObjectVar ----------------------------------------------------------------- + +// Represents a JavaScript object Var. By itself, this represents random +// NPObjects that a given plugin (identified by the resource's module) wants to +// reference. If two different modules reference the same NPObject (like the +// "window" object), then there will be different NPObjectVar's (and hence +// PP_Var IDs) for each module. This allows us to track all references owned by +// a given module and free them when the plugin exits independently of other +// plugins that may be running at the same time. +class NPObjectVar : public Var { + public: + // You should always use FromNPObject to create an NPObjectVar. This function + // guarantees that we maintain the 1:1 mapping between NPObject and + // NPObjectVar. + NPObjectVar(PP_Module module, PP_Instance instance, NPObject* np_object); + + virtual ~NPObjectVar(); + + // Var overrides. + virtual NPObjectVar* AsNPObjectVar() OVERRIDE; + virtual PP_Var GetPPVar() OVERRIDE; + + // Returns the underlying NPObject corresponding to this NPObjectVar. + // Guaranteed non-NULL. + NPObject* np_object() const { return np_object_; } + + // Notification that the instance was deleted, the internal reference will be + // zeroed out. + void InstanceDeleted(); + + // Possibly 0 if the object has outlived its instance. + PP_Instance pp_instance() const { return pp_instance_; } + + // 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); + + private: + // Possibly 0 if the object has outlived its instance. + PP_Instance pp_instance_; + + // Guaranteed non-NULL, this is the underlying object used by WebKit. We + // hold a reference to this object. + NPObject* np_object_; + + DISALLOW_COPY_AND_ASSIGN(NPObjectVar); +}; + +} // namespace + +#endif // WEBKIT_PLUGINS_PPAPI_NPOBJECT_VAR_H_ diff --git a/webkit/plugins/ppapi/plugin_module.cc b/webkit/plugins/ppapi/plugin_module.cc index 3c77a6e..145ba4d 100644 --- a/webkit/plugins/ppapi/plugin_module.cc +++ b/webkit/plugins/ppapi/plugin_module.cc @@ -105,7 +105,6 @@ #include "webkit/plugins/ppapi/ppb_video_decoder_impl.h" #include "webkit/plugins/ppapi/ppb_video_layer_impl.h" #include "webkit/plugins/ppapi/resource_tracker.h" -#include "webkit/plugins/ppapi/var.h" #include "webkit/plugins/ppapi/webkit_forwarding_impl.h" using ppapi::TimeTicksToPPTimeTicks; diff --git a/webkit/plugins/ppapi/plugin_object.cc b/webkit/plugins/ppapi/plugin_object.cc index b20b9ff..95d8fba 100644 --- a/webkit/plugins/ppapi/plugin_object.cc +++ b/webkit/plugins/ppapi/plugin_object.cc @@ -15,14 +15,16 @@ #include "ppapi/c/dev/ppp_class_deprecated.h" #include "ppapi/c/pp_resource.h" #include "ppapi/c/pp_var.h" +#include "ppapi/shared_impl/var.h" #include "third_party/WebKit/Source/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" +using ppapi::StringVar; +using ppapi::Var; using WebKit::WebBindings; namespace webkit { diff --git a/webkit/plugins/ppapi/ppapi_plugin_instance.cc b/webkit/plugins/ppapi/ppapi_plugin_instance.cc index 08b1d16..5770ca8 100644 --- a/webkit/plugins/ppapi/ppapi_plugin_instance.cc +++ b/webkit/plugins/ppapi/ppapi_plugin_instance.cc @@ -29,6 +29,7 @@ #include "ppapi/c/ppp_messaging.h" #include "ppapi/c/private/ppb_instance_private.h" #include "ppapi/c/private/ppp_instance_private.h" +#include "ppapi/shared_impl/var.h" #include "ppapi/thunk/enter.h" #include "ppapi/thunk/ppb_buffer_api.h" #include "printing/units.h" @@ -65,7 +66,6 @@ #include "webkit/plugins/ppapi/ppb_url_request_info_impl.h" #include "webkit/plugins/ppapi/ppp_pdf.h" #include "webkit/plugins/ppapi/string.h" -#include "webkit/plugins/ppapi/var.h" #include "webkit/plugins/sad_plugin.h" #if defined(OS_MACOSX) @@ -89,6 +89,7 @@ #include "skia/ext/skia_utils_mac.h" #endif +using ppapi::StringVar; using ppapi::thunk::EnterResourceNoLock; using ppapi::thunk::PPB_Buffer_API; using ppapi::thunk::PPB_Graphics2D_API; @@ -96,6 +97,7 @@ using ppapi::thunk::PPB_Graphics3D_API; using ppapi::thunk::PPB_ImageData_API; using ppapi::thunk::PPB_Instance_FunctionAPI; using ppapi::thunk::PPB_Surface3D_API; +using ppapi::Var; using WebKit::WebBindings; using WebKit::WebCanvas; using WebKit::WebCursorInfo; diff --git a/webkit/plugins/ppapi/ppapi_webplugin_impl.cc b/webkit/plugins/ppapi/ppapi_webplugin_impl.cc index 80328ff..e302fc6 100644 --- a/webkit/plugins/ppapi/ppapi_webplugin_impl.cc +++ b/webkit/plugins/ppapi/ppapi_webplugin_impl.cc @@ -15,11 +15,12 @@ #include "third_party/WebKit/Source/WebKit/chromium/public/WebRect.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" #include "webkit/plugins/ppapi/message_channel.h" +#include "webkit/plugins/ppapi/npobject_var.h" #include "webkit/plugins/ppapi/plugin_module.h" #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" #include "webkit/plugins/ppapi/ppb_url_loader_impl.h" -#include "webkit/plugins/ppapi/var.h" +using ppapi::NPObjectVar; using WebKit::WebCanvas; using WebKit::WebPluginContainer; using WebKit::WebPluginParams; @@ -94,8 +95,8 @@ void WebPluginImpl::destroy() { } NPObject* WebPluginImpl::scriptableObject() { - scoped_refptr object( - ObjectVar::FromPPVar(instance_->GetInstanceObject())); + scoped_refptr object( + NPObjectVar::FromPPVar(instance_->GetInstanceObject())); // GetInstanceObject talked to the plugin which may have removed the instance // from the DOM, in which case instance_ would be NULL now. if (!instance_) diff --git a/webkit/plugins/ppapi/ppb_char_set_impl.cc b/webkit/plugins/ppapi/ppb_char_set_impl.cc index 28d9101..a21b221 100644 --- a/webkit/plugins/ppapi/ppb_char_set_impl.cc +++ b/webkit/plugins/ppapi/ppb_char_set_impl.cc @@ -7,13 +7,14 @@ #include "ppapi/c/dev/ppb_char_set_dev.h" #include "ppapi/c/dev/ppb_memory_dev.h" #include "ppapi/shared_impl/char_set_impl.h" +#include "ppapi/shared_impl/var.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" -using ::ppapi::thunk::PPB_CharSet_FunctionAPI; +using ppapi::StringVar; +using ppapi::thunk::PPB_CharSet_FunctionAPI; namespace webkit { namespace ppapi { diff --git a/webkit/plugins/ppapi/ppb_console_impl.cc b/webkit/plugins/ppapi/ppb_console_impl.cc index 3ca773c..1b08ebf 100644 --- a/webkit/plugins/ppapi/ppb_console_impl.cc +++ b/webkit/plugins/ppapi/ppb_console_impl.cc @@ -7,6 +7,7 @@ #include "base/string_util.h" #include "base/utf_string_conversions.h" #include "ppapi/c/dev/ppb_console_dev.h" +#include "ppapi/shared_impl/var.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" @@ -15,8 +16,8 @@ #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" +using ppapi::Var; using WebKit::WebConsoleMessage; using WebKit::WebString; diff --git a/webkit/plugins/ppapi/ppb_file_ref_impl.cc b/webkit/plugins/ppapi/ppb_file_ref_impl.cc index 4581d42..1db9a22 100644 --- a/webkit/plugins/ppapi/ppb_file_ref_impl.cc +++ b/webkit/plugins/ppapi/ppb_file_ref_impl.cc @@ -8,9 +8,10 @@ #include "base/utf_string_conversions.h" #include "googleurl/src/gurl.h" #include "ppapi/c/pp_errors.h" -#include "ppapi/shared_impl/time_conversion.h" #include "ppapi/thunk/enter.h" #include "ppapi/thunk/ppb_file_system_api.h" +#include "ppapi/shared_impl/time_conversion.h" +#include "ppapi/shared_impl/var.h" #include "webkit/plugins/ppapi/common.h" #include "webkit/plugins/ppapi/file_callbacks.h" #include "webkit/plugins/ppapi/plugin_delegate.h" @@ -18,9 +19,9 @@ #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" #include "webkit/plugins/ppapi/ppb_directory_reader_impl.h" #include "webkit/plugins/ppapi/ppb_file_system_impl.h" -#include "webkit/plugins/ppapi/var.h" using ppapi::PPTimeToTime; +using ppapi::StringVar; using ppapi::thunk::EnterResourceNoLock; using ppapi::thunk::PPB_FileRef_API; using ppapi::thunk::PPB_FileSystem_API; diff --git a/webkit/plugins/ppapi/ppb_flash_clipboard_impl.cc b/webkit/plugins/ppapi/ppb_flash_clipboard_impl.cc index 404260f..b35391a 100644 --- a/webkit/plugins/ppapi/ppb_flash_clipboard_impl.cc +++ b/webkit/plugins/ppapi/ppb_flash_clipboard_impl.cc @@ -11,6 +11,7 @@ #include "base/memory/ref_counted.h" #include "ppapi/c/pp_errors.h" #include "ppapi/c/private/ppb_flash_clipboard.h" +#include "ppapi/shared_impl/var.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebClipboard.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebCString.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" @@ -20,7 +21,8 @@ #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" + +using ppapi::StringVar; namespace webkit { namespace ppapi { diff --git a/webkit/plugins/ppapi/ppb_flash_impl.cc b/webkit/plugins/ppapi/ppb_flash_impl.cc index 82d1656..b38932d 100644 --- a/webkit/plugins/ppapi/ppb_flash_impl.cc +++ b/webkit/plugins/ppapi/ppb_flash_impl.cc @@ -11,6 +11,7 @@ #include "googleurl/src/gurl.h" #include "ppapi/c/private/ppb_flash.h" #include "ppapi/shared_impl/time_conversion.h" +#include "ppapi/shared_impl/var.h" #include "ppapi/thunk/enter.h" #include "webkit/plugins/ppapi/common.h" #include "webkit/plugins/ppapi/plugin_delegate.h" @@ -18,9 +19,9 @@ #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" #include "webkit/plugins/ppapi/ppb_url_request_info_impl.h" #include "webkit/plugins/ppapi/resource_tracker.h" -#include "webkit/plugins/ppapi/var.h" using ppapi::PPTimeToTime; +using ppapi::StringVar; using ppapi::thunk::EnterResource; using ppapi::thunk::PPB_URLRequestInfo_API; diff --git a/webkit/plugins/ppapi/ppb_flash_impl_linux.cc b/webkit/plugins/ppapi/ppb_flash_impl_linux.cc index c698620..55b7d79 100644 --- a/webkit/plugins/ppapi/ppb_flash_impl_linux.cc +++ b/webkit/plugins/ppapi/ppb_flash_impl_linux.cc @@ -8,6 +8,7 @@ #include "ppapi/c/pp_point.h" #include "ppapi/c/pp_rect.h" #include "ppapi/c/dev/ppb_font_dev.h" +#include "ppapi/shared_impl/var.h" #include "ppapi/thunk/enter.h" #include "third_party/skia/include/core/SkCanvas.h" #include "third_party/skia/include/core/SkMatrix.h" @@ -16,8 +17,8 @@ #include "third_party/skia/include/core/SkTemplates.h" #include "third_party/skia/include/core/SkTypeface.h" #include "webkit/plugins/ppapi/ppb_image_data_impl.h" -#include "webkit/plugins/ppapi/var.h" +using ppapi::StringVar; using ppapi::thunk::EnterResource; using ppapi::thunk::PPB_ImageData_API; diff --git a/webkit/plugins/ppapi/ppb_font_impl.cc b/webkit/plugins/ppapi/ppb_font_impl.cc index 8ad95b5..0173dea 100644 --- a/webkit/plugins/ppapi/ppb_font_impl.cc +++ b/webkit/plugins/ppapi/ppb_font_impl.cc @@ -7,6 +7,7 @@ #include "ppapi/c/dev/ppb_font_dev.h" #include "ppapi/shared_impl/font_impl.h" #include "ppapi/shared_impl/ppapi_preferences.h" +#include "ppapi/shared_impl/var.h" #include "ppapi/thunk/enter.h" #include "ppapi/thunk/thunk.h" #include "webkit/plugins/ppapi/common.h" @@ -14,8 +15,8 @@ #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" +using ppapi::StringVar; using ppapi::thunk::EnterResource; using ppapi::thunk::PPB_ImageData_API; using ppapi::WebKitForwarding; diff --git a/webkit/plugins/ppapi/ppb_input_event_impl.cc b/webkit/plugins/ppapi/ppb_input_event_impl.cc index 5139704..ffb6cba 100644 --- a/webkit/plugins/ppapi/ppb_input_event_impl.cc +++ b/webkit/plugins/ppapi/ppb_input_event_impl.cc @@ -4,12 +4,13 @@ #include "webkit/plugins/ppapi/ppb_input_event_impl.h" +#include "ppapi/shared_impl/var.h" #include "webkit/plugins/ppapi/plugin_module.h" #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" -#include "webkit/plugins/ppapi/var.h" using ppapi::InputEventData; using ppapi::InputEventImpl; +using ppapi::StringVar; using ppapi::thunk::PPB_InputEvent_API; namespace webkit { diff --git a/webkit/plugins/ppapi/ppb_transport_impl.cc b/webkit/plugins/ppapi/ppb_transport_impl.cc index 968bbdb..0e68931 100644 --- a/webkit/plugins/ppapi/ppb_transport_impl.cc +++ b/webkit/plugins/ppapi/ppb_transport_impl.cc @@ -12,11 +12,12 @@ #include "ppapi/c/dev/ppb_transport_dev.h" #include "ppapi/c/pp_completion_callback.h" #include "ppapi/c/pp_errors.h" +#include "ppapi/shared_impl/var.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/var.h" +using ppapi::StringVar; using ppapi::thunk::PPB_Transport_API; using webkit_glue::P2PTransport; diff --git a/webkit/plugins/ppapi/ppb_uma_private_impl.cc b/webkit/plugins/ppapi/ppb_uma_private_impl.cc index f08e1df..10807cb 100644 --- a/webkit/plugins/ppapi/ppb_uma_private_impl.cc +++ b/webkit/plugins/ppapi/ppb_uma_private_impl.cc @@ -7,8 +7,10 @@ #include "base/metrics/histogram.h" #include "ppapi/c/pp_var.h" #include "ppapi/c/private/ppb_uma_private.h" +#include "ppapi/shared_impl/var.h" #include "webkit/glue/webkit_glue.h" -#include "webkit/plugins/ppapi/var.h" + +using ppapi::StringVar; namespace webkit { namespace ppapi { diff --git a/webkit/plugins/ppapi/ppb_url_request_info_impl.cc b/webkit/plugins/ppapi/ppb_url_request_info_impl.cc index 617e9a7..44aa161 100644 --- a/webkit/plugins/ppapi/ppb_url_request_info_impl.cc +++ b/webkit/plugins/ppapi/ppb_url_request_info_impl.cc @@ -10,6 +10,7 @@ #include "googleurl/src/url_util.h" #include "net/http/http_util.h" #include "ppapi/c/pp_var.h" +#include "ppapi/shared_impl/var.h" #include "ppapi/thunk/enter.h" #include "ppapi/thunk/ppb_file_ref_api.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebData.h" @@ -22,9 +23,9 @@ #include "webkit/plugins/ppapi/plugin_module.h" #include "webkit/plugins/ppapi/ppb_file_ref_impl.h" #include "webkit/plugins/ppapi/string.h" -#include "webkit/plugins/ppapi/var.h" #include "webkit/glue/webkit_glue.h" +using ppapi::StringVar; using ppapi::thunk::EnterResourceNoLock; using ppapi::thunk::PPB_FileRef_API; using ppapi::thunk::PPB_URLRequestInfo_API; diff --git a/webkit/plugins/ppapi/ppb_url_response_info_impl.cc b/webkit/plugins/ppapi/ppb_url_response_info_impl.cc index 286ecb2..b1ec1fd 100644 --- a/webkit/plugins/ppapi/ppb_url_response_info_impl.cc +++ b/webkit/plugins/ppapi/ppb_url_response_info_impl.cc @@ -6,6 +6,7 @@ #include "base/logging.h" #include "ppapi/c/pp_var.h" +#include "ppapi/shared_impl/var.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebHTTPHeaderVisitor.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" @@ -14,9 +15,9 @@ #include "webkit/plugins/ppapi/plugin_module.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" +using ppapi::StringVar; using ppapi::thunk::PPB_URLResponseInfo_API; using WebKit::WebHTTPHeaderVisitor; using WebKit::WebString; diff --git a/webkit/plugins/ppapi/ppb_url_util_impl.cc b/webkit/plugins/ppapi/ppb_url_util_impl.cc index 6791de1..ebd8921 100644 --- a/webkit/plugins/ppapi/ppb_url_util_impl.cc +++ b/webkit/plugins/ppapi/ppb_url_util_impl.cc @@ -8,6 +8,7 @@ #include "ppapi/c/dev/ppb_url_util_dev.h" #include "ppapi/c/ppb_var.h" #include "ppapi/shared_impl/url_util_impl.h" +#include "ppapi/shared_impl/var.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" @@ -21,13 +22,13 @@ #include "webkit/plugins/ppapi/ppb_var_impl.h" #include "webkit/plugins/ppapi/resource_tracker.h" #include "webkit/plugins/ppapi/string.h" -#include "webkit/plugins/ppapi/var.h" + +using ppapi::StringVar; +using ppapi::URLUtilImpl; namespace webkit { namespace ppapi { -using ::ppapi::URLUtilImpl; - namespace { // Returns the PP_Module associated with the given string, or 0 on failure. diff --git a/webkit/plugins/ppapi/ppb_var_impl.cc b/webkit/plugins/ppapi/ppb_var_impl.cc index a3cf3d5d..49576b3 100644 --- a/webkit/plugins/ppapi/ppb_var_impl.cc +++ b/webkit/plugins/ppapi/ppb_var_impl.cc @@ -12,13 +12,16 @@ #include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" #include "webkit/plugins/ppapi/common.h" #include "webkit/plugins/ppapi/npapi_glue.h" +#include "webkit/plugins/ppapi/npobject_var.h" #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 "webkit/plugins/ppapi/var.h" #include "v8/include/v8.h" +using ppapi::NPObjectVar; +using ppapi::StringVar; +using ppapi::Var; using WebKit::WebBindings; namespace webkit { @@ -75,7 +78,7 @@ bool PPVarToNPVariantNoCopy(PP_Var var, NPVariant* result) { break; } case PP_VARTYPE_OBJECT: { - scoped_refptr object(ObjectVar::FromPPVar(var)); + scoped_refptr object(NPObjectVar::FromPPVar(var)); if (!object) { VOID_TO_NPVARIANT(*result); return false; @@ -106,7 +109,7 @@ class ObjectAccessorTryCatch : public TryCatch { public: ObjectAccessorTryCatch(PP_Var object, PP_Var* exception) : TryCatch(0, exception), - object_(ObjectVar::FromPPVar(object)) { + object_(NPObjectVar::FromPPVar(object)) { if (!object_) { // No object or an invalid object was given. This means we have no module // to associated with the exception text, so use the magic invalid object @@ -118,14 +121,14 @@ class ObjectAccessorTryCatch : public TryCatch { } } - ObjectVar* object() { return object_.get(); } + NPObjectVar* object() { return object_.get(); } PluginInstance* GetPluginInstance() { return ResourceTracker::Get()->GetInstance(object()->pp_instance()); } protected: - scoped_refptr object_; + scoped_refptr object_; DISALLOW_COPY_AND_ASSIGN(ObjectAccessorTryCatch); }; @@ -385,7 +388,7 @@ PP_Var Construct(PP_Var var, bool IsInstanceOfDeprecated(PP_Var var, const PPP_Class_Deprecated* ppp_class, void** ppp_class_data) { - scoped_refptr object(ObjectVar::FromPPVar(var)); + scoped_refptr object(NPObjectVar::FromPPVar(var)); if (!object) return false; // Not an object at all. diff --git a/webkit/plugins/ppapi/ppb_video_decoder_impl.cc b/webkit/plugins/ppapi/ppb_video_decoder_impl.cc index 562fe3e..bb25863 100644 --- a/webkit/plugins/ppapi/ppb_video_decoder_impl.cc +++ b/webkit/plugins/ppapi/ppb_video_decoder_impl.cc @@ -22,7 +22,6 @@ #include "webkit/plugins/ppapi/ppb_buffer_impl.h" #include "webkit/plugins/ppapi/ppb_context_3d_impl.h" #include "webkit/plugins/ppapi/resource_tracker.h" -#include "webkit/plugins/ppapi/var.h" using ppapi::thunk::EnterResourceNoLock; using ppapi::thunk::PPB_Buffer_API; diff --git a/webkit/plugins/ppapi/resource_creation_impl.cc b/webkit/plugins/ppapi/resource_creation_impl.cc index e68d167..354e62c 100644 --- a/webkit/plugins/ppapi/resource_creation_impl.cc +++ b/webkit/plugins/ppapi/resource_creation_impl.cc @@ -6,6 +6,7 @@ #include "ppapi/c/pp_size.h" #include "ppapi/shared_impl/input_event_impl.h" +#include "ppapi/shared_impl/var.h" #include "webkit/plugins/ppapi/common.h" #include "webkit/plugins/ppapi/ppb_audio_impl.h" #include "webkit/plugins/ppapi/ppb_broker_impl.h" @@ -30,9 +31,9 @@ #include "webkit/plugins/ppapi/ppb_url_request_info_impl.h" #include "webkit/plugins/ppapi/ppb_video_decoder_impl.h" #include "webkit/plugins/ppapi/ppb_video_layer_impl.h" -#include "webkit/plugins/ppapi/var.h" using ppapi::InputEventData; +using ppapi::StringVar; namespace webkit { namespace ppapi { diff --git a/webkit/plugins/ppapi/resource_tracker.cc b/webkit/plugins/ppapi/resource_tracker.cc index 7e75fe9..3d050a5 100644 --- a/webkit/plugins/ppapi/resource_tracker.cc +++ b/webkit/plugins/ppapi/resource_tracker.cc @@ -13,6 +13,7 @@ #include "ppapi/c/pp_var.h" #include "ppapi/shared_impl/function_group_base.h" #include "ppapi/shared_impl/tracker_base.h" +#include "webkit/plugins/ppapi/npobject_var.h" #include "webkit/plugins/ppapi/plugin_module.h" #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" #include "webkit/plugins/ppapi/ppb_char_set_impl.h" @@ -21,7 +22,9 @@ #include "webkit/plugins/ppapi/ppb_font_impl.h" #include "webkit/plugins/ppapi/resource.h" #include "webkit/plugins/ppapi/resource_creation_impl.h" -#include "webkit/plugins/ppapi/var.h" + +using ppapi::NPObjectVar; +using ppapi::Var; enum PPIdType { PP_ID_TYPE_MODULE, @@ -35,6 +38,9 @@ static const unsigned int kPPIdTypeBits = 2; COMPILE_ASSERT(PP_ID_TYPE_COUNT <= (1<::max() >> kPPIdTypeBits; + template static inline T MakeTypedId(T value, PPIdType type) { return (value << kPPIdTypeBits) | static_cast(type); } @@ -58,7 +64,7 @@ namespace { } // namespace -typedef std::map NPObjectToObjectVarMap; +typedef std::map NPObjectToNPObjectVarMap; struct ResourceTracker::InstanceData { InstanceData() : instance(0) {} @@ -71,10 +77,11 @@ struct ResourceTracker::InstanceData { ResourceSet ref_resources; std::set assoc_resources; - // Tracks all live ObjectVars used by this module so we can map NPObjects to - // the corresponding object, and also release these properly if the instance - // goes away when there are still refs. These are non-owning references. - NPObjectToObjectVarMap np_object_to_object_var; + // Tracks all live NPObjectVars used by this module so we can map NPObjects + // to the corresponding object, and also release these properly if the + // instance goes away when there are still refs. These are non-owning + // references. + NPObjectToNPObjectVarMap np_object_to_object_var; // Lazily allocated function proxies for the different interfaces. scoped_ptr< ::ppapi::FunctionGroupBase > @@ -152,22 +159,6 @@ PP_Resource ResourceTracker::AddResource(Resource* resource) { return new_id; } -int32 ResourceTracker::AddVar(Var* var) { - // If the plugin manages to create 1B strings... - if (last_var_id_ == std::numeric_limits::max() >> kPPIdTypeBits) - return 0; - - // Validate the module. - if (!GetModule(var->pp_module())) - return 0; - - // Add the resource with plugin use-count 1. - int32 new_id = MakeTypedId(++last_var_id_, PP_ID_TYPE_VAR); - live_vars_.insert(std::make_pair(new_id, std::make_pair(var, 1))); - - return new_id; -} - bool ResourceTracker::AddRefResource(PP_Resource res) { DLOG_IF(ERROR, !CheckIdType(res, PP_ID_TYPE_RESOURCE)) << res << " is not a PP_Resource."; @@ -241,11 +232,11 @@ void ResourceTracker::CleanupInstanceData(PP_Instance instance, // Force delete all var references. Need to make a copy so we can iterate over // the map while deleting stuff from it. - NPObjectToObjectVarMap np_object_map_copy = data.np_object_to_object_var; - NPObjectToObjectVarMap::iterator cur_var = + NPObjectToNPObjectVarMap np_object_map_copy = data.np_object_to_object_var; + NPObjectToNPObjectVarMap::iterator cur_var = np_object_map_copy.begin(); while (cur_var != np_object_map_copy.end()) { - NPObjectToObjectVarMap::iterator current = cur_var++; + NPObjectToNPObjectVarMap::iterator current = cur_var++; // Clear the object from the var mapping and the live instance object list. int32 var_id = current->second->GetExistingVarID(); @@ -336,6 +327,22 @@ PP_Instance ResourceTracker::GetInstanceForResource(PP_Resource pp_resource) { return resource->instance()->pp_instance(); } +int32 ResourceTracker::AddVar(Var* var) { + // If the plugin manages to create 1B strings... + if (last_var_id_ == kMaxPPIdType) + return 0; + + // Validate the module. + if (!GetModule(var->pp_module())) + return 0; + + // Add the resource with plugin use-count 1. + int32 new_id = MakeTypedId(++last_var_id_, PP_ID_TYPE_VAR); + live_vars_.insert(std::make_pair(new_id, std::make_pair(var, 1))); + + return new_id; +} + scoped_refptr ResourceTracker::GetVar(int32 var_id) const { DLOG_IF(ERROR, !CheckIdType(var_id, PP_ID_TYPE_VAR)) << var_id << " is not a PP_Var ID."; @@ -371,38 +378,38 @@ bool ResourceTracker::UnrefVar(int32 var_id) { return false; } -void ResourceTracker::AddNPObjectVar(ObjectVar* object_var) { +void ResourceTracker::AddNPObjectVar(NPObjectVar* object_var) { DCHECK(instance_map_.find(object_var->pp_instance()) != instance_map_.end()); InstanceData& data = *instance_map_[object_var->pp_instance()].get(); DCHECK(data.np_object_to_object_var.find(object_var->np_object()) == - data.np_object_to_object_var.end()) << "ObjectVar already in map"; + data.np_object_to_object_var.end()) << "NPObjectVar already in map"; data.np_object_to_object_var[object_var->np_object()] = object_var; } -void ResourceTracker::RemoveNPObjectVar(ObjectVar* object_var) { +void ResourceTracker::RemoveNPObjectVar(NPObjectVar* object_var) { DCHECK(instance_map_.find(object_var->pp_instance()) != instance_map_.end()); InstanceData& data = *instance_map_[object_var->pp_instance()].get(); - NPObjectToObjectVarMap::iterator found = + NPObjectToNPObjectVarMap::iterator found = data.np_object_to_object_var.find(object_var->np_object()); if (found == data.np_object_to_object_var.end()) { - NOTREACHED() << "ObjectVar not registered."; + NOTREACHED() << "NPObjectVar not registered."; return; } if (found->second != object_var) { - NOTREACHED() << "ObjectVar doesn't match."; + NOTREACHED() << "NPObjectVar doesn't match."; return; } data.np_object_to_object_var.erase(found); } -ObjectVar* ResourceTracker::ObjectVarForNPObject(PP_Instance instance, - NPObject* np_object) { +NPObjectVar* ResourceTracker::NPObjectVarForNPObject(PP_Instance instance, + NPObject* np_object) { DCHECK(instance_map_.find(instance) != instance_map_.end()); InstanceData& data = *instance_map_[instance].get(); - NPObjectToObjectVarMap::iterator found = + NPObjectToNPObjectVarMap::iterator found = data.np_object_to_object_var.find(np_object); if (found == data.np_object_to_object_var.end()) return NULL; diff --git a/webkit/plugins/ppapi/resource_tracker.h b/webkit/plugins/ppapi/resource_tracker.h index a079c02..faf5aec 100644 --- a/webkit/plugins/ppapi/resource_tracker.h +++ b/webkit/plugins/ppapi/resource_tracker.h @@ -24,15 +24,18 @@ typedef struct NPObject NPObject; +namespace ppapi { +class NPObjectVar; +class Var; +} + namespace webkit { namespace ppapi { -class ObjectVar; 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. @@ -70,22 +73,24 @@ class ResourceTracker : public ::ppapi::TrackerBase { // PP_Vars ------------------------------------------------------------------- - scoped_refptr GetVar(int32 var_id) const; - - bool AddRefVar(int32 var_id); - bool UnrefVar(int32 var_id); + // TrackerBase implementation. + virtual int32 AddVar(::ppapi::Var* var) OVERRIDE; + virtual scoped_refptr< ::ppapi::Var > GetVar(int32 var_id) const OVERRIDE; + virtual bool AddRefVar(int32 var_id) OVERRIDE; + virtual bool UnrefVar(int32 var_id) OVERRIDE; - // Tracks all live ObjectVar. This is so we can map between instance + - // NPObject and get the ObjectVar corresponding to it. This Add/Remove - // function is called by the ObjectVar when it is created and + // Tracks all live NPObjectVar. This is so we can map between instance + + // NPObject and get the NPObjectVar corresponding to it. This Add/Remove + // function is called by the NPObjectVar when it is created and // destroyed. - void AddNPObjectVar(ObjectVar* object_var); - void RemoveNPObjectVar(ObjectVar* object_var); + void AddNPObjectVar(::ppapi::NPObjectVar* object_var); + void RemoveNPObjectVar(::ppapi::NPObjectVar* object_var); - // Looks up a previously registered ObjectVar for the given NPObject and - // instance. Returns NULL if there is no ObjectVar corresponding to the given - // NPObject for the given instance. See AddNPObjectVar above. - ObjectVar* ObjectVarForNPObject(PP_Instance instance, NPObject* np_object); + // Looks up a previously registered NPObjectVar for the given NPObject and + // instance. Returns NULL if there is no NPObjectVar corresponding to the + // given NPObject for the given instance. See AddNPObjectVar above. + ::ppapi::NPObjectVar* NPObjectVarForNPObject(PP_Instance instance, + NPObject* np_object); // PP_Modules ---------------------------------------------------------------- @@ -121,7 +126,6 @@ class ResourceTracker : public ::ppapi::TrackerBase { private: friend class Resource; friend class ResourceTrackerTest; - friend class Var; typedef std::set ResourceSet; @@ -144,9 +148,6 @@ class ResourceTracker : public ::ppapi::TrackerBase { // Resource class. PP_Resource AddResource(Resource* resource); - // The same as AddResource but for Var, and returns the new Var ID. - int32 AddVar(Var* var); - // Force frees all vars and resources associated with the given instance. // If delete_instance is true, the instance tracking information will also // be deleted. @@ -197,7 +198,7 @@ class ResourceTracker : public ::ppapi::TrackerBase { ResourceMap live_resources_; // Like ResourceAndRefCount but for vars, which are associated with modules. - typedef std::pair, size_t> VarAndRefCount; + typedef std::pair, size_t> VarAndRefCount; typedef base::hash_map VarMap; VarMap live_vars_; diff --git a/webkit/plugins/ppapi/resource_tracker_unittest.cc b/webkit/plugins/ppapi/resource_tracker_unittest.cc index c1199dd..89590e8 100644 --- a/webkit/plugins/ppapi/resource_tracker_unittest.cc +++ b/webkit/plugins/ppapi/resource_tracker_unittest.cc @@ -11,9 +11,11 @@ #include "webkit/plugins/ppapi/mock_plugin_delegate.h" #include "webkit/plugins/ppapi/mock_resource.h" #include "webkit/plugins/ppapi/npapi_glue.h" +#include "webkit/plugins/ppapi/npobject_var.h" #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" #include "webkit/plugins/ppapi/resource_tracker.h" -#include "webkit/plugins/ppapi/var.h" + +using ppapi::NPObjectVar; namespace webkit { namespace ppapi { @@ -207,7 +209,7 @@ TEST_F(ResourceTrackerTest, ReuseVar) { // This ObjectVar must be released before we do NPObjectToPPVar again // below so it gets freed and we get a new identifier. { - scoped_refptr check_object(ObjectVar::FromPPVar(pp_object1)); + scoped_refptr check_object(NPObjectVar::FromPPVar(pp_object1)); ASSERT_TRUE(check_object.get()); EXPECT_EQ(instance()->pp_instance(), check_object->pp_instance()); EXPECT_EQ(npobject.get(), check_object->np_object()); diff --git a/webkit/plugins/ppapi/var.cc b/webkit/plugins/ppapi/var.cc deleted file mode 100644 index 914cdaf..0000000 --- a/webkit/plugins/ppapi/var.cc +++ /dev/null @@ -1,255 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "webkit/plugins/ppapi/var.h" - -#include - -#include "base/logging.h" -#include "base/memory/scoped_ptr.h" -#include "base/string_number_conversions.h" -#include "base/string_util.h" -#include "ppapi/c/dev/ppb_var_deprecated.h" -#include "ppapi/c/ppb_var.h" -#include "ppapi/c/pp_var.h" -#include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h" -#include "webkit/plugins/ppapi/common.h" -#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" - -using WebKit::WebBindings; - -namespace webkit { -namespace ppapi { - -// Var ------------------------------------------------------------------------- - -Var::Var(PP_Module module) : pp_module_(module), var_id_(0) { -} - -Var::~Var() { -} - -// static -std::string Var::PPVarToLogString(PP_Var var) { - switch (var.type) { - case PP_VARTYPE_UNDEFINED: - return "[Undefined]"; - case PP_VARTYPE_NULL: - return "[Null]"; - case PP_VARTYPE_BOOL: - return var.value.as_bool ? "[True]" : "[False]"; - case PP_VARTYPE_INT32: - return base::IntToString(var.value.as_int); - case PP_VARTYPE_DOUBLE: - return base::DoubleToString(var.value.as_double); - case PP_VARTYPE_STRING: { - scoped_refptr string(StringVar::FromPPVar(var)); - if (!string) - return "[Invalid string]"; - - // Since this is for logging, escape NULLs. - std::string result = string->value(); - std::string null; - null.push_back(0); - ReplaceSubstringsAfterOffset(&result, 0, null, "\\0"); - return result; - } - case PP_VARTYPE_OBJECT: - return "[Object]"; - default: - return "[Invalid var]"; - } -} - -// static -void Var::PluginAddRefPPVar(PP_Var var) { - if (var.type == PP_VARTYPE_STRING || var.type == PP_VARTYPE_OBJECT) { - 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) { - if (!ResourceTracker::Get()->UnrefVar(static_cast(var.value.as_id))) - DLOG(WARNING) << "ReleaseVar()ing a nonexistent string/object var."; - } -} - -StringVar* Var::AsStringVar() { - return NULL; -} - -ObjectVar* Var::AsObjectVar() { - return NULL; -} - -int32 Var::GetExistingVarID() const { - return var_id_; -} - -int32 Var::GetOrCreateVarID() { - ResourceTracker *tracker = ResourceTracker::Get(); - if (var_id_) { - if (!tracker->AddRefVar(var_id_)) - return 0; - } else { - var_id_ = tracker->AddVar(this); - if (!var_id_) - return 0; - } - return var_id_; -} - -// StringVar ------------------------------------------------------------------- - -StringVar::StringVar(PP_Module module, const char* str, uint32 len) - : Var(module), - value_(str, len) { -} - -StringVar::~StringVar() { -} - -StringVar* StringVar::AsStringVar() { - return this; -} - -PP_Var StringVar::GetPPVar() { - int32 id = GetOrCreateVarID(); - if (!id) - return PP_MakeNull(); - - PP_Var result; - result.type = PP_VARTYPE_STRING; - result.value.as_id = id; - return result; -} - -// static -PP_Var StringVar::StringToPPVar(PP_Module module, const std::string& var) { - return StringToPPVar(module, var.c_str(), var.size()); -} - -// static -PP_Var StringVar::StringToPPVar(PP_Module module, - const char* data, uint32 len) { - scoped_refptr str(new StringVar(module, data, len)); - if (!str || !IsStringUTF8(str->value())) - return PP_MakeNull(); - return str->GetPPVar(); -} - -// static -scoped_refptr StringVar::FromPPVar(PP_Var var) { - if (var.type != PP_VARTYPE_STRING) - 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(PP_Module module, - PP_Instance instance, - NPObject* np_object) - : Var(module), - pp_instance_(instance), - np_object_(np_object) { - WebBindings::retainObject(np_object_); - ResourceTracker::Get()->AddNPObjectVar(this); -} - -ObjectVar::~ObjectVar() { - if (pp_instance_) - ResourceTracker::Get()->RemoveNPObjectVar(this); - - WebBindings::releaseObject(np_object_); -} - -ObjectVar* ObjectVar::AsObjectVar() { - return this; -} - -PP_Var ObjectVar::GetPPVar() { - int32 id = GetOrCreateVarID(); - if (!id) - return PP_MakeNull(); - - PP_Var result; - result.type = PP_VARTYPE_OBJECT; - result.value.as_id = id; - return result; -} - -void ObjectVar::InstanceDeleted() { - DCHECK(pp_instance_); - pp_instance_ = 0; -} - -// static -scoped_refptr ObjectVar::FromPPVar(PP_Var var) { - if (var.type != PP_VARTYPE_OBJECT) - return scoped_refptr(NULL); - 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 -------------------------------------------------------------------- - -TryCatch::TryCatch(PP_Module module, PP_Var* exception) - : pp_module_(module), - has_exception_(exception && exception->type != PP_VARTYPE_UNDEFINED), - exception_(exception) { - WebBindings::pushExceptionHandler(&TryCatch::Catch, this); -} - -TryCatch::~TryCatch() { - WebBindings::popExceptionHandler(); -} - -void TryCatch::SetException(const char* message) { - if (!pp_module_) { - // Don't have a module to make the string. - SetInvalidObjectException(); - return; - } - - if (!has_exception()) { - has_exception_ = true; - if (exception_) { - *exception_ = StringVar::StringToPPVar(pp_module_, - message, strlen(message)); - } - } -} - -void TryCatch::SetInvalidObjectException() { - if (!has_exception()) { - has_exception_ = true; - // TODO(brettw) bug 54504: Have a global singleton string that can hold - // a generic error message. - if (exception_) - *exception_ = PP_MakeInt32(1); - } -} - -// static -void TryCatch::Catch(void* self, const char* message) { - static_cast(self)->SetException(message); -} - -} // namespace ppapi -} // namespace webkit - diff --git a/webkit/plugins/ppapi/var.h b/webkit/plugins/ppapi/var.h deleted file mode 100644 index edbcec7..0000000 --- a/webkit/plugins/ppapi/var.h +++ /dev/null @@ -1,260 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef WEBKIT_PLUGINS_PPAPI_VAR_H_ -#define WEBKIT_PLUGINS_PPAPI_VAR_H_ - -#include - -#include "base/compiler_specific.h" -#include "base/memory/ref_counted.h" -#include "ppapi/c/pp_instance.h" -#include "ppapi/c/pp_module.h" - -struct PP_Var; -struct PPB_Var; -struct PPB_Var_Deprecated; -typedef struct NPObject NPObject; -typedef struct _NPVariant NPVariant; -typedef void* NPIdentifier; - -namespace webkit { -namespace ppapi { - -class ObjectVar; -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 base::RefCounted { - public: - virtual ~Var(); - - // Returns a string representing the given var for logging purposes. - static std::string PPVarToLogString(PP_Var var); - - // Provides access to the manual refcounting of a PP_Var from the plugin's - // perspective. This is different than the AddRef/Release on this scoped - // object. This uses the ResourceTracker, which keeps a separate "plugin - // refcount" that prevents the plugin from messing up our refcounting or - // freeing something out from under us. - // - // You should not generally need to use these functions. However, if you - // call a plugin function that returns a var, it will transfer a ref to us - // (the caller) which in the case of a string or object var will need to - // be released. - // - // Example, assuming we're expecting the plugin to return a string: - // PP_Var rv = some_ppp_interface->DoSomething(a, b, c); - // - // // Get the string value. This will take a reference to the object which - // // will prevent it from being deleted out from under us when we call - // // PluginReleasePPVar(). - // scoped_refptr string(StringVar::FromPPVar(rv)); - // - // // Release the reference the plugin gave us when returning the value. - // // This is legal to do for all types of vars. - // Var::PluginReleasePPVar(rv); - // - // // Use the string. - // if (!string) - // return false; // It didn't return a proper string. - // UseTheString(string->value()); - static void PluginAddRefPPVar(PP_Var var); - static void PluginReleasePPVar(PP_Var var); - - virtual StringVar* AsStringVar(); - virtual ObjectVar* AsObjectVar(); - - // Creates a PP_Var corresponding to this object. The return value will have - // one reference addrefed on behalf of the caller. - virtual PP_Var GetPPVar() = 0; - - // Returns the ID corresponing to the string or object if it exists already, - // or 0 if an ID hasn't been generated for this object (the plugin is holding - // no refs). - // - // Contrast to GetOrCreateVarID which creates the ID and a ref on behalf of - // the plugin. - int32 GetExistingVarID() const; - - PP_Module pp_module() const { return pp_module_; } - - protected: - // This can only be constructed as a StringVar or an ObjectVar. - explicit Var(PP_Module module); - - // Returns the unique ID associated with this string or object, creating it - // if necessary. The return value will be 0 if the string or object is - // invalid. - // - // This function will take a reference to the var that will be passed to the - // caller. - int32 GetOrCreateVarID(); - - private: - PP_Module pp_module_; - - // This will be 0 if no ID has been assigned (this happens lazily). - int32 var_id_; - - DISALLOW_COPY_AND_ASSIGN(Var); -}; - -// StringVar ------------------------------------------------------------------- - -// Represents a string-based Var. -// -// Returning a given string as a PP_Var: -// return StringVar::StringToPPVar(module, my_string); -// -// Converting a PP_Var to a string: -// scoped_refptr string(StringVar::FromPPVar(var)); -// if (!string) -// return false; // Not a string or an invalid var. -// DoSomethingWithTheString(string->value()); -class StringVar : public Var { - public: - StringVar(PP_Module module, const char* str, uint32 len); - virtual ~StringVar(); - - const std::string& value() const { return value_; } - - // Var override. - virtual StringVar* AsStringVar() OVERRIDE; - virtual PP_Var GetPPVar() 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 - // is not valid UTF-8, a NULL var will be returned. - // - // The return value will have a reference count of 1. Internally, this will - // create a StringVar, associate it with a module, and return the reference - // to it in the var. - static PP_Var StringToPPVar(PP_Module module, const std::string& str); - static PP_Var StringToPPVar(PP_Module module, const char* str, uint32 len); - - // Helper function that converts a PP_Var to a string. This will return NULL - // if the PP_Var is not of string type or the string is invalid. - static scoped_refptr FromPPVar(PP_Var var); - - private: - std::string value_; - - DISALLOW_COPY_AND_ASSIGN(StringVar); -}; - -// ObjectVar ------------------------------------------------------------------- - -// Represents a JavaScript object Var. By itself, this represents random -// NPObjects that a given plugin (identified by the resource's module) wants to -// reference. If two different modules reference the same NPObject (like the -// "window" object), then there will be different ObjectVar's (and hence PP_Var -// IDs) for each module. This allows us to track all references owned by a -// given module and free them when the plugin exits independently of other -// plugins that may be running at the same time. -// -// See StringVar for examples, except obviously using NPObjects instead of -// strings. -class ObjectVar : public Var { - public: - // You should always use FromNPObject to create an ObjectVar. This function - // guarantees that we maintain the 1:1 mapping between NPObject and - // ObjectVar. - ObjectVar(PP_Module module, PP_Instance instance, NPObject* np_object); - - virtual ~ObjectVar(); - - // Var overrides. - virtual ObjectVar* AsObjectVar() OVERRIDE; - virtual PP_Var GetPPVar() OVERRIDE; - - // Returns the underlying NPObject corresponding to this ObjectVar. - // Guaranteed non-NULL. - NPObject* np_object() const { return np_object_; } - - // Notification that the instance was deleted, the internal reference will be - // zeroed out. - void InstanceDeleted(); - - // Possibly 0 if the object has outlived its instance. - PP_Instance pp_instance() const { return pp_instance_; } - - // 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); - - private: - // Possibly 0 if the object has outlived its instance. - PP_Instance pp_instance_; - - // Guaranteed non-NULL, this is the underlying object used by WebKit. We - // hold a reference to this object. - NPObject* np_object_; - - DISALLOW_COPY_AND_ASSIGN(ObjectVar); -}; - -// TryCatch -------------------------------------------------------------------- - -// Instantiate this object on the stack to catch V8 exceptions and pass them -// to an optional out parameter supplied by the plugin. -class TryCatch { - public: - // The given exception may be NULL if the consumer isn't interested in - // catching exceptions. If non-NULL, the given var will be updated if any - // exception is thrown (so it must outlive the TryCatch object). - // - // The module associated with the exception is passed so we know which module - // to associate any exception string with. It may be NULL if you don't know - // the module at construction time, in which case you should set it later - // by calling set_module(). - // - // If an exception is thrown when the module is NULL, setting *any* exception - // will result in using the InvalidObjectException. - TryCatch(PP_Module module, PP_Var* exception); - ~TryCatch(); - - // Get and set the module. This may be NULL (see the constructor). - PP_Module pp_module() { return pp_module_; } - void set_pp_module(PP_Module module) { pp_module_ = module; } - - // Returns true is an exception has been thrown. This can be true immediately - // after construction if the var passed to the constructor is non-void. - bool has_exception() const { return has_exception_; } - - // Sets the given exception. If no module has been set yet, the message will - // be ignored (since we have no module to associate the string with) and the - // SetInvalidObjectException() will be used instead. - // - // If an exception has been previously set, this function will do nothing - // (normally you want only the first exception). - void SetException(const char* message); - - // Sets the exception to be a generic message contained in a magic string - // not associated with any module. - void SetInvalidObjectException(); - - private: - static void Catch(void* self, const char* message); - - PP_Module pp_module_; - - // True if an exception has been thrown. Since the exception itself may be - // NULL if the plugin isn't interested in getting the exception, this will - // always indicate if SetException has been called, regardless of whether - // the exception itself has been stored. - bool has_exception_; - - // May be null if the consumer isn't interesting in catching exceptions. - PP_Var* exception_; -}; - -} // namespace ppapi -} // namespace webkit - -#endif // WEBKIT_PLUGINS_PPAPI_VAR_H_ -- cgit v1.1