diff options
author | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-27 05:53:14 +0000 |
---|---|---|
committer | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-27 05:53:14 +0000 |
commit | 3dbd395ce974856d7a2a769e26a6184732518c0b (patch) | |
tree | e2d3cec325244b301beed71155aa70a9e56376c7 /webkit/port | |
parent | 420bfe34fa837807195fb021cb8db67d51a94953 (diff) | |
download | chromium_src-3dbd395ce974856d7a2a769e26a6184732518c0b.zip chromium_src-3dbd395ce974856d7a2a769e26a6184732518c0b.tar.gz chromium_src-3dbd395ce974856d7a2a769e26a6184732518c0b.tar.bz2 |
Fix indentation and naming in the V8 NP files to match WebKit. The only
non-trivial change in in np_v8object's NPIdentifierToV8Identifier which I redid
to avoid STL usage.
Review URL: http://codereview.chromium.org/27241
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@10590 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/port')
-rw-r--r-- | webkit/port/bindings/v8/np_v8object.cpp | 671 | ||||
-rw-r--r-- | webkit/port/bindings/v8/np_v8object.h | 15 | ||||
-rw-r--r-- | webkit/port/bindings/v8/v8_helpers.cpp | 19 | ||||
-rw-r--r-- | webkit/port/bindings/v8/v8_np_utils.cpp | 149 | ||||
-rw-r--r-- | webkit/port/bindings/v8/v8_np_utils.h | 14 | ||||
-rw-r--r-- | webkit/port/bindings/v8/v8_npobject.cpp | 468 | ||||
-rw-r--r-- | webkit/port/bindings/v8/v8_npobject.h | 10 |
7 files changed, 655 insertions, 691 deletions
diff --git a/webkit/port/bindings/v8/np_v8object.cpp b/webkit/port/bindings/v8/np_v8object.cpp index 917771a..99d1582 100644 --- a/webkit/port/bindings/v8/np_v8object.cpp +++ b/webkit/port/bindings/v8/np_v8object.cpp @@ -28,8 +28,6 @@ #define max max #define min min -#include <string> -#include <sstream> #include <v8.h> #include "np_v8object.h" #include "ChromiumBridge.h" @@ -48,43 +46,44 @@ using WebCore::V8ClassIndex; using WebCore::V8Custom; using WebCore::V8Proxy; -namespace { - // TODO(mbelshe): comments on why use malloc and free. -static NPObject* AllocV8NPObject(NPP, NPClass*) { - return static_cast<NPObject*>(malloc(sizeof(V8NPObject))); +static NPObject* AllocV8NPObject(NPP, NPClass*) +{ + return static_cast<NPObject*>(malloc(sizeof(V8NPObject))); } -static void FreeV8NPObject(NPObject* npobj) { - V8NPObject *object = reinterpret_cast<V8NPObject*>(npobj); +static void FreeV8NPObject(NPObject* npobj) +{ + V8NPObject *object = reinterpret_cast<V8NPObject*>(npobj); #ifndef NDEBUG - V8Proxy::UnregisterGlobalHandle(object, object->v8_object); + V8Proxy::UnregisterGlobalHandle(object, object->v8Object); #endif - object->v8_object.Dispose(); - free(object); + object->v8Object.Dispose(); + free(object); } static v8::Handle<v8::Value>* listFromVariantArgs(const NPVariant* args, uint32_t argCount, - NPObject *owner) { - v8::Handle<v8::Value>* argv = new v8::Handle<v8::Value>[argCount]; - for (uint32_t index = 0; index < argCount; index++) { - const NPVariant *arg = &args[index]; - argv[index] = ConvertNPVariantToV8Object(arg, owner); - } - return argv; + NPObject *owner) +{ + v8::Handle<v8::Value>* argv = new v8::Handle<v8::Value>[argCount]; + for (uint32_t index = 0; index < argCount; index++) { + const NPVariant *arg = &args[index]; + argv[index] = ConvertNPVariantToV8Object(arg, owner); + } + return argv; } // Create an identifier (null terminated utf8 char*) from the NPIdentifier. -static void NPIdentifierToV8Identifier(NPIdentifier name, std::string &string) { - PrivateIdentifier* identifier = static_cast<PrivateIdentifier*>(name); - if (identifier->isString) { - string = static_cast<const char *>(identifier->value.string); - } else { - std::ostringstream o; - o << identifier->value.number; - string = o.str(); - } +static v8::Local<v8::String> NPIdentifierToV8Identifier(NPIdentifier name) +{ + PrivateIdentifier* identifier = static_cast<PrivateIdentifier*>(name); + if (identifier->isString) + return v8::String::New(static_cast<const char *>(identifier->value.string)); + + char buf[32]; + snprintf(buf, 32, "%d", identifier->value.number); + return v8::String::New(buf); } static NPClass V8NPObjectClass = { NP_CLASS_STRUCT_VERSION, @@ -92,175 +91,174 @@ static NPClass V8NPObjectClass = { NP_CLASS_STRUCT_VERSION, FreeV8NPObject, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; -} // namespace - -// // NPAPI's npruntime functions -// NPClass* NPScriptObjectClass = &V8NPObjectClass; -NPObject* NPN_CreateScriptObject(NPP npp, v8::Handle<v8::Object> object, - WebCore::DOMWindow* root) { - // Check to see if this object is already wrapped. - if (object->InternalFieldCount() == V8Custom::kNPObjectInternalFieldCount && - object->GetInternalField(V8Custom::kDOMWrapperTypeIndex)->IsNumber() && - object->GetInternalField(V8Custom::kDOMWrapperTypeIndex)->Uint32Value() == - V8ClassIndex::NPOBJECT) { - NPObject* rv = V8Proxy::ToNativeObject<NPObject>(V8ClassIndex::NPOBJECT, - object); - NPN_RetainObject(rv); - return rv; - } - - V8NPObject* obj = - reinterpret_cast<V8NPObject*>(NPN_CreateObject(npp, &V8NPObjectClass)); - obj->v8_object = v8::Persistent<v8::Object>::New(object); +NPObject* NPN_CreateScriptObject(NPP npp, v8::Handle<v8::Object> object, WebCore::DOMWindow* root) +{ + // Check to see if this object is already wrapped. + if (object->InternalFieldCount() == V8Custom::kNPObjectInternalFieldCount && + object->GetInternalField(V8Custom::kDOMWrapperTypeIndex)->IsNumber() && + object->GetInternalField(V8Custom::kDOMWrapperTypeIndex)->Uint32Value() == V8ClassIndex::NPOBJECT) { + + NPObject* rv = V8Proxy::ToNativeObject<NPObject>(V8ClassIndex::NPOBJECT, object); + NPN_RetainObject(rv); + return rv; + } + + V8NPObject* obj = reinterpret_cast<V8NPObject*>(NPN_CreateObject(npp, &V8NPObjectClass)); + obj->v8Object = v8::Persistent<v8::Object>::New(object); #ifndef NDEBUG - V8Proxy::RegisterGlobalHandle(WebCore::NPOBJECT, obj, obj->v8_object); + V8Proxy::RegisterGlobalHandle(WebCore::NPOBJECT, obj, obj->v8Object); #endif - obj->root_object = root; - return reinterpret_cast<NPObject*>(obj); + obj->rootObject = root; + return reinterpret_cast<NPObject*>(obj); } bool NPN_Invoke(NPP npp, NPObject *npobj, NPIdentifier methodName, - const NPVariant *args, uint32_t argCount, NPVariant *result) { - if (npobj == NULL) - return false; - - if (npobj->_class == NPScriptObjectClass) { - V8NPObject *object = reinterpret_cast<V8NPObject*>(npobj); - - PrivateIdentifier *identifier = static_cast<PrivateIdentifier*>(methodName); - if (!identifier->isString) - return false; - - v8::HandleScope handle_scope; - // TODO: should use the plugin's owner frame as the security context - v8::Handle<v8::Context> context = GetV8Context(npp, npobj); - if (context.IsEmpty()) return false; - - v8::Context::Scope scope(context); - - // Special case the "eval" method. - if (methodName == NPN_GetStringIdentifier("eval")) { - if (argCount != 1) - return false; - if (args[0].type != NPVariantType_String) + const NPVariant *args, uint32_t argCount, NPVariant *result) +{ + if (!npobj) return false; - return NPN_Evaluate(npp, npobj, - const_cast<NPString*>(&args[0].value.stringValue), result); - } - v8::Handle<v8::Value> func_obj = - object->v8_object->Get(v8::String::New(identifier->value.string)); - if (func_obj.IsEmpty() || func_obj->IsNull()) { - NULL_TO_NPVARIANT(*result); - return false; - } - if (func_obj->IsUndefined()) { - VOID_TO_NPVARIANT(*result); - return false; + if (npobj->_class == NPScriptObjectClass) { + V8NPObject *object = reinterpret_cast<V8NPObject*>(npobj); + + PrivateIdentifier *identifier = static_cast<PrivateIdentifier*>(methodName); + if (!identifier->isString) + return false; + + v8::HandleScope handleScope; + // TODO: should use the plugin's owner frame as the security context + v8::Handle<v8::Context> context = GetV8Context(npp, npobj); + if (context.IsEmpty()) + return false; + + v8::Context::Scope scope(context); + + // Special case the "eval" method. + if (methodName == NPN_GetStringIdentifier("eval")) { + if (argCount != 1) + return false; + if (args[0].type != NPVariantType_String) + return false; + return NPN_Evaluate(npp, npobj, const_cast<NPString*>(&args[0].value.stringValue), result); + } + + v8::Handle<v8::Value> funcObj = object->v8Object->Get(v8::String::New(identifier->value.string)); + if (funcObj.IsEmpty() || funcObj->IsNull()) { + NULL_TO_NPVARIANT(*result); + return false; + } + if (funcObj->IsUndefined()) { + VOID_TO_NPVARIANT(*result); + return false; + } + + WebCore::V8Proxy* proxy = GetV8Proxy(npobj); + ASSERT(proxy); // must not be null + + // TODO: fix variable naming + // Call the function object + v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(funcObj); + // Create list of args to pass to v8 + v8::Handle<v8::Value>* argv = listFromVariantArgs(args, argCount, npobj); + v8::Local<v8::Value> resultObj = proxy->CallFunction(func, object->v8Object, argCount, argv); + delete[] argv; + + // If we had an error, return false. The spec is a little unclear here, but + // says "Returns true if the method was successfully invoked". If we get an + // error return value, was that successfully invoked? + if (resultObj.IsEmpty()) + return false; + + // Convert the result back to an NPVariant + ConvertV8ObjectToNPVariant(resultObj, npobj, result); + return true; } - WebCore::V8Proxy* proxy = GetV8Proxy(npobj); - ASSERT(proxy); // must not be null - - // TODO: fix variable naming - // Call the function object - v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(func_obj); - // Create list of args to pass to v8 - v8::Handle<v8::Value>* argv = listFromVariantArgs(args, argCount, npobj); - v8::Local<v8::Value> resultObj = - proxy->CallFunction(func, object->v8_object, argCount, argv); - delete[] argv; - - // If we had an error, return false. The spec is a little unclear here, but - // says "Returns true if the method was successfully invoked". If we get an - // error return value, was that successfully invoked? - if (resultObj.IsEmpty()) return false; - - // Convert the result back to an NPVariant - ConvertV8ObjectToNPVariant(resultObj, npobj, result); - return true; - } - - if (npobj->_class->invoke) - return npobj->_class->invoke(npobj, methodName, args, argCount, result); + if (npobj->_class->invoke) + return npobj->_class->invoke(npobj, methodName, args, argCount, result); - VOID_TO_NPVARIANT(*result); - return true; + VOID_TO_NPVARIANT(*result); + return true; } // TODO: Fix it same as NPN_Invoke (HandleScope and such) bool NPN_InvokeDefault(NPP npp, NPObject *npobj, const NPVariant *args, - uint32_t argCount, NPVariant *result) { - if (npobj == NULL) - return false; - - if (npobj->_class == NPScriptObjectClass) { - V8NPObject *object = reinterpret_cast<V8NPObject*>(npobj); - - VOID_TO_NPVARIANT(*result); - - v8::HandleScope handle_scope; - v8::Handle<v8::Context> context = GetV8Context(npp, npobj); - if (context.IsEmpty()) return false; - - v8::Context::Scope scope(context); + uint32_t argCount, NPVariant *result) +{ + if (!npobj) + return false; - // Lookup the function object - v8::Handle<v8::Object> funcObj(object->v8_object); - if (!funcObj->IsFunction()) - return false; - - // Call the function object - v8::Local<v8::Value> resultObj; - v8::Handle<v8::Function> func(v8::Function::Cast(*funcObj)); - if (!func->IsNull()) { - WebCore::V8Proxy* proxy = GetV8Proxy(npobj); - ASSERT(proxy); - - // Create list of args to pass to v8 - v8::Handle<v8::Value>* argv = listFromVariantArgs(args, argCount, npobj); - resultObj = proxy->CallFunction(func, funcObj, argCount, argv); - delete[] argv; + if (npobj->_class == NPScriptObjectClass) { + V8NPObject *object = reinterpret_cast<V8NPObject*>(npobj); + + VOID_TO_NPVARIANT(*result); + + v8::HandleScope handleScope; + v8::Handle<v8::Context> context = GetV8Context(npp, npobj); + if (context.IsEmpty()) + return false; + + v8::Context::Scope scope(context); + + // Lookup the function object + v8::Handle<v8::Object> funcObj(object->v8Object); + if (!funcObj->IsFunction()) + return false; + + // Call the function object + v8::Local<v8::Value> resultObj; + v8::Handle<v8::Function> func(v8::Function::Cast(*funcObj)); + if (!func->IsNull()) { + WebCore::V8Proxy* proxy = GetV8Proxy(npobj); + ASSERT(proxy); + + // Create list of args to pass to v8 + v8::Handle<v8::Value>* argv = listFromVariantArgs(args, argCount, npobj); + resultObj = proxy->CallFunction(func, funcObj, argCount, argv); + delete[] argv; + } + + // If we had an error, return false. The spec is a little unclear here, but + // says "Returns true if the method was successfully invoked". If we get an + // error return value, was that successfully invoked? + if (resultObj.IsEmpty()) + return false; + + // Convert the result back to an NPVariant. + ConvertV8ObjectToNPVariant(resultObj, npobj, result); + return true; } - // If we had an error, return false. The spec is a little unclear here, but - // says "Returns true if the method was successfully invoked". If we get an - // error return value, was that successfully invoked? - if (resultObj.IsEmpty()) return false; + if (npobj->_class->invokeDefault) + return npobj->_class->invokeDefault(npobj, args, argCount, result); - // Convert the result back to an NPVariant - ConvertV8ObjectToNPVariant(resultObj, npobj, result); + VOID_TO_NPVARIANT(*result); return true; - } - - if (npobj->_class->invokeDefault) - return npobj->_class->invokeDefault(npobj, args, argCount, result); - - VOID_TO_NPVARIANT(*result); - return true; } -bool NPN_Evaluate(NPP npp, NPObject *npobj, NPString *npscript, - NPVariant *result) { - bool popups_allowed = WebCore::ChromiumBridge::popupsAllowed(npp); - return NPN_EvaluateHelper(npp, popups_allowed, npobj, npscript, result); +bool NPN_Evaluate(NPP npp, NPObject *npobj, NPString *npscript, NPVariant *result) +{ + bool popupsAllowed = WebCore::ChromiumBridge::popupsAllowed(npp); + return NPN_EvaluateHelper(npp, popupsAllowed, npobj, npscript, result); } -bool NPN_EvaluateHelper(NPP npp, bool popups_allowed, NPObject* npobj, - NPString* npscript, NPVariant *result) { - VOID_TO_NPVARIANT(*result); - if (npobj == NULL) - return false; +bool NPN_EvaluateHelper(NPP npp, bool popupsAllowed, NPObject* npobj, NPString* npscript, NPVariant *result) +{ + VOID_TO_NPVARIANT(*result); + if (!npobj) + return false; + + if (npobj->_class != NPScriptObjectClass) + return false; - if (npobj->_class == NPScriptObjectClass) { - v8::HandleScope handle_scope; + v8::HandleScope handleScope; v8::Handle<v8::Context> context = GetV8Context(npp, npobj); if (context.IsEmpty()) - return false; + return false; WebCore::V8Proxy* proxy = GetV8Proxy(npobj); ASSERT(proxy); @@ -268,244 +266,229 @@ bool NPN_EvaluateHelper(NPP npp, bool popups_allowed, NPObject* npobj, v8::Context::Scope scope(context); WebCore::String filename; - if (!popups_allowed) - filename = "npscript"; + if (!popupsAllowed) + filename = "npscript"; // Convert UTF-8 stream to WebCore::String. - WebCore::String script = WebCore::String::fromUTF8( - npscript->UTF8Characters, npscript->UTF8Length); - v8::Local<v8::Value> v8result = - proxy->Evaluate(filename, 0, script, NULL); + WebCore::String script = WebCore::String::fromUTF8(npscript->UTF8Characters, npscript->UTF8Length); + v8::Local<v8::Value> v8result = proxy->Evaluate(filename, 0, script, 0); // If we had an error, return false. - if (v8result.IsEmpty()) return false; + if (v8result.IsEmpty()) + return false; ConvertV8ObjectToNPVariant(v8result, npobj, result); return true; - } - - return false; } -bool NPN_GetProperty(NPP npp, NPObject *npobj, NPIdentifier propertyName, - NPVariant *result) { - if (npobj == NULL) - return false; - - if (npobj->_class == NPScriptObjectClass) { - V8NPObject *object = reinterpret_cast<V8NPObject*>(npobj); +bool NPN_GetProperty(NPP npp, NPObject *npobj, NPIdentifier propertyName, NPVariant *result) +{ + if (!npobj) + return false; - v8::HandleScope handle_scope; - v8::Handle<v8::Context> context = GetV8Context(npp, npobj); - if (context.IsEmpty()) return false; + if (npobj->_class == NPScriptObjectClass) { + V8NPObject *object = reinterpret_cast<V8NPObject*>(npobj); - v8::Context::Scope scope(context); + v8::HandleScope handleScope; + v8::Handle<v8::Context> context = GetV8Context(npp, npobj); + if (context.IsEmpty()) + return false; - v8::Handle<v8::Object> obj(object->v8_object); + v8::Context::Scope scope(context); - std::string identifier; - NPIdentifierToV8Identifier(propertyName, identifier); - v8::Local<v8::Value> v8result = - obj->Get(v8::String::New(identifier.c_str())); + v8::Handle<v8::Object> obj(object->v8Object); + v8::Local<v8::Value> v8result = obj->Get(NPIdentifierToV8Identifier(propertyName)); - ConvertV8ObjectToNPVariant(v8result, npobj, result); - return true; - } + ConvertV8ObjectToNPVariant(v8result, npobj, result); + return true; + } - if (npobj->_class->hasProperty && npobj->_class->getProperty) - if (npobj->_class->hasProperty(npobj, propertyName)) - return npobj->_class->getProperty(npobj, propertyName, result); + if (npobj->_class->hasProperty && npobj->_class->getProperty) { + if (npobj->_class->hasProperty(npobj, propertyName)) + return npobj->_class->getProperty(npobj, propertyName, result); + } - VOID_TO_NPVARIANT(*result); - return false; + VOID_TO_NPVARIANT(*result); + return false; } -bool NPN_SetProperty(NPP npp, NPObject *npobj, NPIdentifier propertyName, - const NPVariant *value) { - if (npobj == NULL) - return false; +bool NPN_SetProperty(NPP npp, NPObject *npobj, NPIdentifier propertyName, const NPVariant *value) +{ + if (!npobj) + return false; - if (npobj->_class == NPScriptObjectClass) { - V8NPObject *object = reinterpret_cast<V8NPObject*>(npobj); + if (npobj->_class == NPScriptObjectClass) { + V8NPObject *object = reinterpret_cast<V8NPObject*>(npobj); - v8::HandleScope handle_scope; - v8::Handle<v8::Context> context = GetV8Context(npp, npobj); - if (context.IsEmpty()) return false; + v8::HandleScope handleScope; + v8::Handle<v8::Context> context = GetV8Context(npp, npobj); + if (context.IsEmpty()) + return false; - v8::Context::Scope scope(context); + v8::Context::Scope scope(context); - v8::Handle<v8::Object> obj(object->v8_object); - std::string identifier; - NPIdentifierToV8Identifier(propertyName, identifier); - obj->Set(v8::String::New(identifier.c_str()), - ConvertNPVariantToV8Object(value, - object->root_object->frame()->script()->windowScriptNPObject())); - return true; - } + v8::Handle<v8::Object> obj(object->v8Object); + obj->Set(NPIdentifierToV8Identifier(propertyName), + ConvertNPVariantToV8Object(value, object->rootObject->frame()->script()->windowScriptNPObject())); + return true; + } - if (npobj->_class->setProperty) - return npobj->_class->setProperty(npobj, propertyName, value); + if (npobj->_class->setProperty) + return npobj->_class->setProperty(npobj, propertyName, value); - return false; + return false; } -bool NPN_RemoveProperty(NPP npp, NPObject *npobj, NPIdentifier propertyName) { - if (npobj == NULL) - return false; +bool NPN_RemoveProperty(NPP npp, NPObject *npobj, NPIdentifier propertyName) +{ + if (!npobj) + return false; + if (npobj->_class != NPScriptObjectClass) + return false; - if (npobj->_class == NPScriptObjectClass) { V8NPObject *object = reinterpret_cast<V8NPObject*>(npobj); - v8::HandleScope handle_scope; + v8::HandleScope handleScope; v8::Handle<v8::Context> context = GetV8Context(npp, npobj); - if (context.IsEmpty()) return false; + if (context.IsEmpty()) + return false; v8::Context::Scope scope(context); - v8::Handle<v8::Object> obj(object->v8_object); - std::string identifier; - NPIdentifierToV8Identifier(propertyName, identifier); + v8::Handle<v8::Object> obj(object->v8Object); // TODO(mbelshe) - verify that setting to undefined is right. - obj->Set(v8::String::New(identifier.c_str()), v8::Undefined()); + obj->Set(NPIdentifierToV8Identifier(propertyName), v8::Undefined()); return true; - } - - return false; } -bool NPN_HasProperty(NPP npp, NPObject *npobj, NPIdentifier propertyName) { - if (npobj == NULL) - return false; - - if (npobj->_class == NPScriptObjectClass) { - V8NPObject *object = reinterpret_cast<V8NPObject*>(npobj); +bool NPN_HasProperty(NPP npp, NPObject *npobj, NPIdentifier propertyName) +{ + if (!npobj) + return false; - v8::HandleScope handle_scope; - v8::Handle<v8::Context> context = GetV8Context(npp, npobj); - if (context.IsEmpty()) return false; - v8::Context::Scope scope(context); + if (npobj->_class == NPScriptObjectClass) { + V8NPObject *object = reinterpret_cast<V8NPObject*>(npobj); - v8::Handle<v8::Object> obj(object->v8_object); - std::string identifier; - NPIdentifierToV8Identifier(propertyName, identifier); - return obj->Has(v8::String::New(identifier.c_str())); - } + v8::HandleScope handleScope; + v8::Handle<v8::Context> context = GetV8Context(npp, npobj); + if (context.IsEmpty()) + return false; + v8::Context::Scope scope(context); - if (npobj->_class->hasProperty) - return npobj->_class->hasProperty(npobj, propertyName); + v8::Handle<v8::Object> obj(object->v8Object); + return obj->Has(NPIdentifierToV8Identifier(propertyName)); + } - return false; + if (npobj->_class->hasProperty) + return npobj->_class->hasProperty(npobj, propertyName); + return false; } -bool NPN_HasMethod(NPP npp, NPObject *npobj, NPIdentifier methodName) { - if (npobj == NULL) return false; - - if (npobj->_class == NPScriptObjectClass) { - V8NPObject *object = reinterpret_cast<V8NPObject*>(npobj); +bool NPN_HasMethod(NPP npp, NPObject *npobj, NPIdentifier methodName) +{ + if (!npobj) + return false; - v8::HandleScope handle_scope; - v8::Handle<v8::Context> context = GetV8Context(npp, npobj); - if (context.IsEmpty()) return false; - v8::Context::Scope scope(context); + if (npobj->_class == NPScriptObjectClass) { + V8NPObject *object = reinterpret_cast<V8NPObject*>(npobj); - v8::Handle<v8::Object> obj(object->v8_object); - std::string identifier; - NPIdentifierToV8Identifier(methodName, identifier); - v8::Handle<v8::Value> prop = obj->Get(v8::String::New(identifier.c_str())); - return prop->IsFunction(); - } + v8::HandleScope handleScope; + v8::Handle<v8::Context> context = GetV8Context(npp, npobj); + if (context.IsEmpty()) + return false; + v8::Context::Scope scope(context); - if (npobj->_class->hasMethod) - return npobj->_class->hasMethod(npobj, methodName); + v8::Handle<v8::Object> obj(object->v8Object); + v8::Handle<v8::Value> prop = obj->Get(NPIdentifierToV8Identifier(methodName)); + return prop->IsFunction(); + } - return false; + if (npobj->_class->hasMethod) + return npobj->_class->hasMethod(npobj, methodName); + return false; } -void NPN_SetException(NPObject *npobj, const NPUTF8 *message) { - if (npobj->_class == NPScriptObjectClass) { - v8::HandleScope handle_scope; - v8::Handle<v8::Context> context = GetV8Context(NULL, npobj); - if (context.IsEmpty()) return; +void NPN_SetException(NPObject *npobj, const NPUTF8 *message) +{ + if (npobj->_class != NPScriptObjectClass) + return; + v8::HandleScope handleScope; + v8::Handle<v8::Context> context = GetV8Context(0, npobj); + if (context.IsEmpty()) + return; v8::Context::Scope scope(context); - V8Proxy::ThrowError(V8Proxy::GENERAL_ERROR, message); - } } -bool NPN_Enumerate(NPP npp, NPObject *npobj, NPIdentifier **identifier, - uint32_t *count) { - if (npobj == NULL) return false; - - if (npobj->_class == NPScriptObjectClass) { - V8NPObject *object = reinterpret_cast<V8NPObject*>(npobj); - - v8::HandleScope handle_scope; - v8::Handle<v8::Context> context = GetV8Context(npp, npobj); - if (context.IsEmpty()) return false; - v8::Context::Scope scope(context); +bool NPN_Enumerate(NPP npp, NPObject *npobj, NPIdentifier **identifier, uint32_t *count) +{ + if (!npobj) + return false; - v8::Handle<v8::Object> obj(object->v8_object); - - // TODO(fqian): http://b/issue?id=1210340: Use a v8::Object::Keys() method - // when it exists, instead of evaluating javascript. - - // TODO(mpcomplete): figure out how to cache this helper function. - // Run a helper function that collects the properties on the object into - // an array. - const char kEnumeratorCode[] = - "(function (obj) {" - " var props = [];" - " for (var prop in obj) {" - " props[props.length] = prop;" - " }" - " return props;" - "});"; - v8::Handle<v8::String> source = v8::String::New(kEnumeratorCode); - v8::Handle<v8::Script> script = v8::Script::Compile(source, NULL); - v8::Handle<v8::Value> enumerator_obj = script->Run(); - v8::Handle<v8::Function> enumerator = - v8::Handle<v8::Function>::Cast(enumerator_obj); - v8::Handle<v8::Value> argv[] = { obj }; - v8::Local<v8::Value> props_obj = - enumerator->Call(v8::Handle<v8::Object>::Cast(enumerator_obj), - ARRAYSIZE_UNSAFE(argv), argv); - if (props_obj.IsEmpty()) - return false; - - // Convert the results into an array of NPIdentifiers. - v8::Handle<v8::Array> props = v8::Handle<v8::Array>::Cast(props_obj); - *count = props->Length(); - *identifier = - static_cast<NPIdentifier*>(malloc(sizeof(NPIdentifier*) * *count)); - for (uint32_t i = 0; i < *count; ++i) { - v8::Local<v8::Value> name = props->Get(v8::Integer::New(i)); - (*identifier)[i] = GetStringIdentifier(v8::Local<v8::String>::Cast(name)); + if (npobj->_class == NPScriptObjectClass) { + V8NPObject *object = reinterpret_cast<V8NPObject*>(npobj); + + v8::HandleScope handleScope; + v8::Handle<v8::Context> context = GetV8Context(npp, npobj); + if (context.IsEmpty()) + return false; + v8::Context::Scope scope(context); + + v8::Handle<v8::Object> obj(object->v8Object); + + // TODO(fqian): http://b/issue?id=1210340: Use a v8::Object::Keys() method + // when it exists, instead of evaluating javascript. + + // TODO(mpcomplete): figure out how to cache this helper function. + // Run a helper function that collects the properties on the object into + // an array. + const char kEnumeratorCode[] = + "(function (obj) {" + " var props = [];" + " for (var prop in obj) {" + " props[props.length] = prop;" + " }" + " return props;" + "});"; + v8::Handle<v8::String> source = v8::String::New(kEnumeratorCode); + v8::Handle<v8::Script> script = v8::Script::Compile(source, 0); + v8::Handle<v8::Value> enumeratorObj = script->Run(); + v8::Handle<v8::Function> enumerator = v8::Handle<v8::Function>::Cast(enumeratorObj); + v8::Handle<v8::Value> argv[] = { obj }; + v8::Local<v8::Value> propsObj = enumerator->Call(v8::Handle<v8::Object>::Cast(enumeratorObj), ARRAYSIZE_UNSAFE(argv), argv); + if (propsObj.IsEmpty()) + return false; + + // Convert the results into an array of NPIdentifiers. + v8::Handle<v8::Array> props = v8::Handle<v8::Array>::Cast(propsObj); + *count = props->Length(); + *identifier = static_cast<NPIdentifier*>(malloc(sizeof(NPIdentifier*) * *count)); + for (uint32_t i = 0; i < *count; ++i) { + v8::Local<v8::Value> name = props->Get(v8::Integer::New(i)); + (*identifier)[i] = GetStringIdentifier(v8::Local<v8::String>::Cast(name)); + } + return true; } - return true; - } - if (NP_CLASS_STRUCT_VERSION_HAS_ENUM(npobj->_class) && - npobj->_class->enumerate) { - return npobj->_class->enumerate(npobj, identifier, count); - } + if (NP_CLASS_STRUCT_VERSION_HAS_ENUM(npobj->_class) && npobj->_class->enumerate) + return npobj->_class->enumerate(npobj, identifier, count); - return false; + return false; } -bool NPN_Construct(NPP npp, NPObject* npobj, const NPVariant* args, - uint32_t argCount, NPVariant* result) { - if (npobj == NULL) return false; +bool NPN_Construct(NPP npp, NPObject* npobj, const NPVariant* args, uint32_t argCount, NPVariant* result) +{ + if (!npobj) + return false; - // TODO(estade): implement this case. - if (npobj->_class == NPScriptObjectClass) { - VOID_TO_NPVARIANT(*result); - return false; - } + // TODO(estade): implement this case. + if (npobj->_class == NPScriptObjectClass) { + VOID_TO_NPVARIANT(*result); + return false; + } - if (NP_CLASS_STRUCT_VERSION_HAS_CTOR(npobj->_class) && - npobj->_class->construct) { - return npobj->_class->construct(npobj, args, argCount, result); - } + if (NP_CLASS_STRUCT_VERSION_HAS_CTOR(npobj->_class) && npobj->_class->construct) + return npobj->_class->construct(npobj, args, argCount, result); - return false; + return false; } diff --git a/webkit/port/bindings/v8/np_v8object.h b/webkit/port/bindings/v8/np_v8object.h index e6797ed..cd89b5a 100644 --- a/webkit/port/bindings/v8/np_v8object.h +++ b/webkit/port/bindings/v8/np_v8object.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef NP_V8OBJECT_H__ -#define NP_V8OBJECT_H__ +#ifndef np_v8object_h +#define np_v8object_h #include "third_party/npapi/bindings/npruntime.h" #include <v8.h> @@ -19,8 +19,8 @@ extern NPClass* NPScriptObjectClass; // and FreeV8NPObject() methods. struct V8NPObject { NPObject object; - v8::Persistent<v8::Object> v8_object; - WebCore::DOMWindow* root_object; + v8::Persistent<v8::Object> v8Object; + WebCore::DOMWindow* rootObject; }; struct PrivateIdentifier { @@ -31,9 +31,8 @@ struct PrivateIdentifier { bool isString; }; -NPObject* NPN_CreateScriptObject(NPP npp, v8::Handle<v8::Object>, - WebCore::DOMWindow*); -NPObject* NPN_CreateNoScriptObject(void); +NPObject* NPN_CreateScriptObject(NPP npp, v8::Handle<v8::Object>, WebCore::DOMWindow*); +NPObject* NPN_CreateNoScriptObject(); -#endif // NP_V8OBJECT_H__ +#endif // np_v8object_h diff --git a/webkit/port/bindings/v8/v8_helpers.cpp b/webkit/port/bindings/v8/v8_helpers.cpp index be820c2..089aedd 100644 --- a/webkit/port/bindings/v8/v8_helpers.cpp +++ b/webkit/port/bindings/v8/v8_helpers.cpp @@ -40,17 +40,20 @@ using WebCore::V8Custom; -void WrapNPObject(v8::Handle<v8::Object> obj, NPObject* npobj) { +void WrapNPObject(v8::Handle<v8::Object> obj, NPObject* npobj) +{ WebCore::V8Proxy::SetDOMWrapper(obj, WebCore::V8ClassIndex::NPOBJECT, npobj); } -v8::Local<v8::Context> GetV8Context(NPP npp, NPObject* npobj) { - V8NPObject* object = reinterpret_cast<V8NPObject*>(npobj); - return WebCore::V8Proxy::GetContext(object->root_object->frame()); +v8::Local<v8::Context> GetV8Context(NPP npp, NPObject* npobj) +{ + V8NPObject* object = reinterpret_cast<V8NPObject*>(npobj); + return WebCore::V8Proxy::GetContext(object->rootObject->frame()); } -WebCore::V8Proxy* GetV8Proxy(NPObject* npobj) { - V8NPObject* object = reinterpret_cast<V8NPObject*>(npobj); - WebCore::Frame* frame = object->root_object->frame(); - return WebCore::V8Proxy::retrieve(frame); +WebCore::V8Proxy* GetV8Proxy(NPObject* npobj) +{ + V8NPObject* object = reinterpret_cast<V8NPObject*>(npobj); + WebCore::Frame* frame = object->rootObject->frame(); + return WebCore::V8Proxy::retrieve(frame); } diff --git a/webkit/port/bindings/v8/v8_np_utils.cpp b/webkit/port/bindings/v8/v8_np_utils.cpp index c178999..957aa6f 100644 --- a/webkit/port/bindings/v8/v8_np_utils.cpp +++ b/webkit/port/bindings/v8/v8_np_utils.cpp @@ -41,95 +41,84 @@ #include "v8_npobject.h" #include "v8_proxy.h" -void ConvertV8ObjectToNPVariant(v8::Local<v8::Value> object, NPObject *owner, - NPVariant* result) { - VOID_TO_NPVARIANT(*result); - - // It is really the caller's responsibility to deal with the empty handle - // case because there could be different actions to take in different - // contexts. - ASSERT(!object.IsEmpty()); - - if (object.IsEmpty()) return; - - if (object->IsInt32()) { - INT32_TO_NPVARIANT(object->NumberValue(), *result); - - } else if (object->IsNumber()) { - DOUBLE_TO_NPVARIANT(object->NumberValue(), *result); - - } else if (object->IsBoolean()) { - BOOLEAN_TO_NPVARIANT(object->BooleanValue(), *result); - - } else if (object->IsNull()) { - NULL_TO_NPVARIANT(*result); - - } else if (object->IsUndefined()) { +void ConvertV8ObjectToNPVariant(v8::Local<v8::Value> object, NPObject *owner, NPVariant* result) +{ VOID_TO_NPVARIANT(*result); - } else if (object->IsString()) { - v8::String::Utf8Value utf8(object); - char* utf8_chars = strdup(*utf8); - STRINGN_TO_NPVARIANT(utf8_chars, utf8.length(), *result); - } else if (object->IsObject()) { - WebCore::DOMWindow* window = WebCore::V8Proxy::retrieveWindow(); - NPObject* npobject = NPN_CreateScriptObject( - 0, v8::Handle<v8::Object>::Cast(object), window); - if (npobject) { - _NPN_RegisterObject(npobject, owner); + // It is really the caller's responsibility to deal with the empty handle + // case because there could be different actions to take in different + // contexts. + ASSERT(!object.IsEmpty()); + + if (object.IsEmpty()) + return; + + if (object->IsInt32()) + INT32_TO_NPVARIANT(object->NumberValue(), *result); + else if (object->IsNumber()) + DOUBLE_TO_NPVARIANT(object->NumberValue(), *result); + else if (object->IsBoolean()) + BOOLEAN_TO_NPVARIANT(object->BooleanValue(), *result); + else if (object->IsNull()) + NULL_TO_NPVARIANT(*result); + else if (object->IsUndefined()) + VOID_TO_NPVARIANT(*result); + else if (object->IsString()) { + v8::String::Utf8Value utf8(object); + char* utf8_chars = strdup(*utf8); + STRINGN_TO_NPVARIANT(utf8_chars, utf8.length(), *result); + } else if (object->IsObject()) { + WebCore::DOMWindow* window = WebCore::V8Proxy::retrieveWindow(); + NPObject* npobject = NPN_CreateScriptObject(0, v8::Handle<v8::Object>::Cast(object), window); + if (npobject) + _NPN_RegisterObject(npobject, owner); + OBJECT_TO_NPVARIANT(npobject, *result); } - OBJECT_TO_NPVARIANT(npobject, *result); - } } -v8::Handle<v8::Value> ConvertNPVariantToV8Object(const NPVariant* variant, - NPObject* npobject) { - NPVariantType type = variant->type; - - if (type == NPVariantType_Int32) { - return v8::Integer::New(NPVARIANT_TO_INT32(*variant)); - } - if (type == NPVariantType_Double) { - return v8::Number::New(NPVARIANT_TO_DOUBLE(*variant)); - } - if (type == NPVariantType_Bool) { - return NPVARIANT_TO_BOOLEAN(*variant) ? v8::True() : v8::False(); - } - if (type == NPVariantType_Null) { - return v8::Null(); - } - if (type == NPVariantType_Void) { - return v8::Undefined(); - } - if (type == NPVariantType_String) { - NPString src = NPVARIANT_TO_STRING(*variant); - return v8::String::New(src.UTF8Characters, src.UTF8Length); - } - if (type == NPVariantType_Object) { - NPObject* obj = NPVARIANT_TO_OBJECT(*variant); - if (obj->_class == NPScriptObjectClass) { - return reinterpret_cast<V8NPObject*>(obj)->v8_object; +v8::Handle<v8::Value> ConvertNPVariantToV8Object(const NPVariant* variant, NPObject* npobject) +{ + NPVariantType type = variant->type; + + if (type == NPVariantType_Int32) + return v8::Integer::New(NPVARIANT_TO_INT32(*variant)); + if (type == NPVariantType_Double) + return v8::Number::New(NPVARIANT_TO_DOUBLE(*variant)); + if (type == NPVariantType_Bool) + return NPVARIANT_TO_BOOLEAN(*variant) ? v8::True() : v8::False(); + if (type == NPVariantType_Null) + return v8::Null(); + if (type == NPVariantType_Void) + return v8::Undefined(); + if (type == NPVariantType_String) { + NPString src = NPVARIANT_TO_STRING(*variant); + return v8::String::New(src.UTF8Characters, src.UTF8Length); + } + if (type == NPVariantType_Object) { + NPObject* obj = NPVARIANT_TO_OBJECT(*variant); + if (obj->_class == NPScriptObjectClass) + return reinterpret_cast<V8NPObject*>(obj)->v8Object; + return CreateV8ObjectForNPObject(obj, npobject); } - return CreateV8ObjectForNPObject(obj, npobject); - } - return v8::Undefined(); + return v8::Undefined(); } // Helper function to create an NPN String Identifier from a v8 string. -NPIdentifier GetStringIdentifier(v8::Handle<v8::String> str) { - const int kStackBufSize = 100; - - int buf_len = str->Length() + 1; - if (buf_len <= kStackBufSize) { - // Use local stack buffer to avoid heap allocations for small strings. - // Here we should only use the stack space for stack_buf when it's used, - // not when we use the heap. - char stack_buf[kStackBufSize]; - str->WriteAscii(stack_buf); - return NPN_GetStringIdentifier(stack_buf); - } +NPIdentifier GetStringIdentifier(v8::Handle<v8::String> str) +{ + const int kStackBufSize = 100; + + int bufLen = str->Length() + 1; + if (bufLen <= kStackBufSize) { + // Use local stack buffer to avoid heap allocations for small strings. + // Here we should only use the stack space for stack_buf when it's used, + // not when we use the heap. + char stackBuf[kStackBufSize]; + str->WriteAscii(stackBuf); + return NPN_GetStringIdentifier(stackBuf); + } - v8::String::AsciiValue ascii(str); - return NPN_GetStringIdentifier(*ascii); + v8::String::AsciiValue ascii(str); + return NPN_GetStringIdentifier(*ascii); } diff --git a/webkit/port/bindings/v8/v8_np_utils.h b/webkit/port/bindings/v8/v8_np_utils.h index 4563e01..6e8290b 100644 --- a/webkit/port/bindings/v8/v8_np_utils.h +++ b/webkit/port/bindings/v8/v8_np_utils.h @@ -2,29 +2,27 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef V8_NP_UTILS_H__ -#define V8_NP_UTILS_H__ +#ifndef v8_np_utils_h +#define v8_np_utils_h #include <v8.h> #include "third_party/npapi/bindings/npruntime.h" namespace WebCore { - class Frame; + class Frame; } // Convert a V8 Value of any type (string, bool, object, etc) to a NPVariant. -void ConvertV8ObjectToNPVariant(v8::Local<v8::Value> object, NPObject *owner, - NPVariant* result); +void ConvertV8ObjectToNPVariant(v8::Local<v8::Value> object, NPObject *owner, NPVariant* result); // Convert a NPVariant (string, bool, object, etc) back to a V8 Value. // The owner object is the NPObject which relates to the object, if the object // is an Object. The created NPObject will be tied to the lifetime of the // owner. -v8::Handle<v8::Value> ConvertNPVariantToV8Object(const NPVariant* value, - NPObject* owner); +v8::Handle<v8::Value> ConvertNPVariantToV8Object(const NPVariant* value, NPObject* owner); // Helper function to create an NPN String Identifier from a v8 string. NPIdentifier GetStringIdentifier(v8::Handle<v8::String> str); -#endif // V8_NP_UTILS_H__ +#endif // v8_np_utils_h diff --git a/webkit/port/bindings/v8/v8_npobject.cpp b/webkit/port/bindings/v8/v8_npobject.cpp index 4c43251..b39078e2 100644 --- a/webkit/port/bindings/v8/v8_npobject.cpp +++ b/webkit/port/bindings/v8/v8_npobject.cpp @@ -45,99 +45,91 @@ using namespace WebCore; enum InvokeFunctionType { - INVOKE_METHOD = 1, - INVOKE_DEFAULT = 2 + INVOKE_METHOD = 1, + INVOKE_DEFAULT = 2 }; // TODO(mbelshe): need comments. // Params: holder could be HTMLEmbedElement or NPObject -static v8::Handle<v8::Value> NPObjectInvokeImpl( - const v8::Arguments& args, InvokeFunctionType func_id) { - NPObject* npobject; - - // These three types are subtypes of HTMLPlugInElement. - if (V8HTMLAppletElement::HasInstance(args.Holder()) || - V8HTMLEmbedElement::HasInstance(args.Holder()) || - V8HTMLObjectElement::HasInstance(args.Holder())) { - // The holder object is a subtype of HTMLPlugInElement. - HTMLPlugInElement* imp = - V8Proxy::DOMWrapperToNode<HTMLPlugInElement>(args.Holder()); - ScriptInstance script_instance = imp->getInstance(); - if (script_instance) { - npobject = V8Proxy::ToNativeObject<NPObject>( - V8ClassIndex::NPOBJECT, script_instance->instance()); +static v8::Handle<v8::Value> NPObjectInvokeImpl(const v8::Arguments& args, InvokeFunctionType funcId) +{ + NPObject* npobject; + + // These three types are subtypes of HTMLPlugInElement. + if (V8HTMLAppletElement::HasInstance(args.Holder()) || + V8HTMLEmbedElement::HasInstance(args.Holder()) || + V8HTMLObjectElement::HasInstance(args.Holder())) { + // The holder object is a subtype of HTMLPlugInElement. + HTMLPlugInElement* imp = V8Proxy::DOMWrapperToNode<HTMLPlugInElement>(args.Holder()); + ScriptInstance scriptInstance = imp->getInstance(); + if (scriptInstance) + npobject = V8Proxy::ToNativeObject<NPObject>(V8ClassIndex::NPOBJECT, scriptInstance->instance()); + else + npobject = NULL; } else { - npobject = NULL; + // The holder object is not a subtype of HTMLPlugInElement, it + // must be an NPObject which has three internal fields. + if (args.Holder()->InternalFieldCount() != V8Custom::kNPObjectInternalFieldCount) { + V8Proxy::ThrowError(V8Proxy::REFERENCE_ERROR, "NPMethod called on non-NPObject"); + return v8::Undefined(); + } + npobject = V8Proxy::ToNativeObject<NPObject>(V8ClassIndex::NPOBJECT, args.Holder()); } - } else { - // The holder object is not a subtype of HTMLPlugInElement, it - // must be an NPObject which has three internal fields. - if (args.Holder()->InternalFieldCount() != - V8Custom::kNPObjectInternalFieldCount) { - V8Proxy::ThrowError(V8Proxy::REFERENCE_ERROR, - "NPMethod called on non-NPObject"); - return v8::Undefined(); + // Verify that our wrapper wasn't using a NPObject which + // has already been deleted. + if (!npobject || !_NPN_IsAlive(npobject)) { + V8Proxy::ThrowError(V8Proxy::REFERENCE_ERROR, "NPObject deleted"); + return v8::Undefined(); } - npobject = V8Proxy::ToNativeObject<NPObject>( - V8ClassIndex::NPOBJECT, args.Holder()); - } - // Verify that our wrapper wasn't using a NPObject which - // has already been deleted. - if (!npobject || !_NPN_IsAlive(npobject)) { - V8Proxy::ThrowError(V8Proxy::REFERENCE_ERROR, "NPObject deleted"); - return v8::Undefined(); - } + // wrap up parameters + int argc = args.Length(); + NPVariant* npArgs = new NPVariant[argc]; - // wrap up parameters - int argc = args.Length(); - NPVariant* np_args = new NPVariant[argc]; + for (int i = 0; i < argc; i++) + ConvertV8ObjectToNPVariant(args[i], npobject, &npArgs[i]); - for (int i = 0; i < argc; i++) { - ConvertV8ObjectToNPVariant(args[i], npobject, &np_args[i]); - } - - NPVariant result; - VOID_TO_NPVARIANT(result); + NPVariant result; + VOID_TO_NPVARIANT(result); - switch (func_id) { + switch (funcId) { case INVOKE_METHOD: - if (npobject->_class->invoke) { - v8::Handle<v8::String> function_name(v8::String::Cast(*args.Data())); - NPIdentifier ident = GetStringIdentifier(function_name); - npobject->_class->invoke(npobject, ident, np_args, argc, &result); - } - break; + if (npobject->_class->invoke) { + v8::Handle<v8::String> function_name(v8::String::Cast(*args.Data())); + NPIdentifier ident = GetStringIdentifier(function_name); + npobject->_class->invoke(npobject, ident, npArgs, argc, &result); + } + break; case INVOKE_DEFAULT: - if (npobject->_class->invokeDefault) { - npobject->_class->invokeDefault(npobject, np_args, argc, &result); - } - break; + if (npobject->_class->invokeDefault) + npobject->_class->invokeDefault(npobject, npArgs, argc, &result); + break; default: - break; - } + break; + } - for (int i=0; i < argc; i++) { - NPN_ReleaseVariantValue(&np_args[i]); - } - delete[] np_args; + for (int i=0; i < argc; i++) + NPN_ReleaseVariantValue(&npArgs[i]); + delete[] npArgs; - // unwrap return values - v8::Handle<v8::Value> rv = ConvertNPVariantToV8Object(&result, npobject); - NPN_ReleaseVariantValue(&result); + // unwrap return values + v8::Handle<v8::Value> rv = ConvertNPVariantToV8Object(&result, npobject); + NPN_ReleaseVariantValue(&result); - return rv; + return rv; } -v8::Handle<v8::Value> NPObjectMethodHandler(const v8::Arguments& args) { - return NPObjectInvokeImpl(args, INVOKE_METHOD); +v8::Handle<v8::Value> NPObjectMethodHandler(const v8::Arguments& args) +{ + return NPObjectInvokeImpl(args, INVOKE_METHOD); } -v8::Handle<v8::Value> NPObjectInvokeDefaultHandler(const v8::Arguments& args) { - return NPObjectInvokeImpl(args, INVOKE_DEFAULT); +v8::Handle<v8::Value> NPObjectInvokeDefaultHandler(const v8::Arguments& args) +{ + return NPObjectInvokeImpl(args, INVOKE_DEFAULT); } @@ -147,229 +139,231 @@ static void WeakTemplateCallback(v8::Persistent<v8::Value> obj, void* param); static WeakReferenceMap<PrivateIdentifier, v8::FunctionTemplate> \ static_template_map(&WeakTemplateCallback); -static void WeakTemplateCallback(v8::Persistent<v8::Value> obj, - void* param) { - PrivateIdentifier* iden = static_cast<PrivateIdentifier*>(param); - ASSERT(iden != NULL); - ASSERT(static_template_map.contains(iden)); +static void WeakTemplateCallback(v8::Persistent<v8::Value> obj, void* param) +{ + PrivateIdentifier* iden = static_cast<PrivateIdentifier*>(param); + ASSERT(iden != NULL); + ASSERT(static_template_map.contains(iden)); - static_template_map.forget(iden); + static_template_map.forget(iden); } static v8::Handle<v8::Value> NPObjectGetProperty(v8::Local<v8::Object> self, NPIdentifier ident, - v8::Local<v8::Value> key) { - NPObject* npobject = V8Proxy::ToNativeObject<NPObject>(V8ClassIndex::NPOBJECT, - self); - - // Verify that our wrapper wasn't using a NPObject which - // has already been deleted. - if (!npobject || !_NPN_IsAlive(npobject)) { - V8Proxy::ThrowError(V8Proxy::REFERENCE_ERROR, "NPObject deleted"); - return v8::Handle<v8::Value>(); - } - - if (npobject->_class->hasProperty && - npobject->_class->hasProperty(npobject, ident) && - npobject->_class->getProperty) { - NPVariant result; - VOID_TO_NPVARIANT(result); - if (!npobject->_class->getProperty(npobject, ident, &result)) { - return v8::Handle<v8::Value>(); + v8::Local<v8::Value> key) +{ + NPObject* npobject = V8Proxy::ToNativeObject<NPObject>(V8ClassIndex::NPOBJECT, self); + + // Verify that our wrapper wasn't using a NPObject which + // has already been deleted. + if (!npobject || !_NPN_IsAlive(npobject)) { + V8Proxy::ThrowError(V8Proxy::REFERENCE_ERROR, "NPObject deleted"); + return v8::Handle<v8::Value>(); } - v8::Handle<v8::Value> rv = ConvertNPVariantToV8Object(&result, npobject); - NPN_ReleaseVariantValue(&result); - return rv; - - } else if (key->IsString() && - npobject->_class->hasMethod && - npobject->_class->hasMethod(npobject, ident)) { - PrivateIdentifier* id = static_cast<PrivateIdentifier*>(ident); - v8::Persistent<v8::FunctionTemplate> desc = static_template_map.get(id); - // Cache templates using identifier as the key. - if (desc.IsEmpty()) { - // Create a new template - v8::Local<v8::FunctionTemplate> temp = v8::FunctionTemplate::New(); - temp->SetCallHandler(NPObjectMethodHandler, key); - desc = v8::Persistent<v8::FunctionTemplate>::New(temp); - static_template_map.set(id, desc); + if (npobject->_class->hasProperty && + npobject->_class->hasProperty(npobject, ident) && + npobject->_class->getProperty) { + + NPVariant result; + VOID_TO_NPVARIANT(result); + if (!npobject->_class->getProperty(npobject, ident, &result)) + return v8::Handle<v8::Value>(); + + v8::Handle<v8::Value> rv = ConvertNPVariantToV8Object(&result, npobject); + NPN_ReleaseVariantValue(&result); + return rv; + } else if (key->IsString() && npobject->_class->hasMethod && npobject->_class->hasMethod(npobject, ident)) { + PrivateIdentifier* id = static_cast<PrivateIdentifier*>(ident); + v8::Persistent<v8::FunctionTemplate> desc = static_template_map.get(id); + // Cache templates using identifier as the key. + if (desc.IsEmpty()) { + // Create a new template + v8::Local<v8::FunctionTemplate> temp = v8::FunctionTemplate::New(); + temp->SetCallHandler(NPObjectMethodHandler, key); + desc = v8::Persistent<v8::FunctionTemplate>::New(temp); + static_template_map.set(id, desc); + } + + // FunctionTemplate caches function for each context. + v8::Local<v8::Function> func = desc->GetFunction(); + func->SetName(v8::Handle<v8::String>::Cast(key)); + return func; } - // FunctionTemplate caches function for each context. - v8::Local<v8::Function> func = desc->GetFunction(); - func->SetName(v8::Handle<v8::String>::Cast(key)); - return func; - } - - return v8::Handle<v8::Value>(); + return v8::Handle<v8::Value>(); } v8::Handle<v8::Value> NPObjectNamedPropertyGetter(v8::Local<v8::String> name, - const v8::AccessorInfo& info) { - NPIdentifier ident = GetStringIdentifier(name); - return NPObjectGetProperty(info.Holder(), ident, name); + const v8::AccessorInfo& info) +{ + NPIdentifier ident = GetStringIdentifier(name); + return NPObjectGetProperty(info.Holder(), ident, name); } v8::Handle<v8::Value> NPObjectIndexedPropertyGetter(uint32_t index, - const v8::AccessorInfo& info) { - NPIdentifier ident = NPN_GetIntIdentifier(index); - return NPObjectGetProperty(info.Holder(), ident, v8::Number::New(index)); + const v8::AccessorInfo& info) +{ + NPIdentifier ident = NPN_GetIntIdentifier(index); + return NPObjectGetProperty(info.Holder(), ident, v8::Number::New(index)); } v8::Handle<v8::Value> NPObjectGetNamedProperty(v8::Local<v8::Object> self, - v8::Local<v8::String> name) { - NPIdentifier ident = GetStringIdentifier(name); - return NPObjectGetProperty(self, ident, name); + v8::Local<v8::String> name) +{ + NPIdentifier ident = GetStringIdentifier(name); + return NPObjectGetProperty(self, ident, name); } v8::Handle<v8::Value> NPObjectGetIndexedProperty(v8::Local<v8::Object> self, - uint32_t index) { - NPIdentifier ident = NPN_GetIntIdentifier(index); - return NPObjectGetProperty(self, ident, v8::Number::New(index)); + uint32_t index) +{ + NPIdentifier ident = NPN_GetIntIdentifier(index); + return NPObjectGetProperty(self, ident, v8::Number::New(index)); } static v8::Handle<v8::Value> NPObjectSetProperty(v8::Local<v8::Object> self, NPIdentifier ident, - v8::Local<v8::Value> value) { - NPObject* npobject = - V8Proxy::ToNativeObject<NPObject>(V8ClassIndex::NPOBJECT, self); - - // Verify that our wrapper wasn't using a NPObject which - // has already been deleted. - if (!npobject || !_NPN_IsAlive(npobject)) { - V8Proxy::ThrowError(V8Proxy::REFERENCE_ERROR, "NPObject deleted"); - return value; // intercepted, but an exception was thrown - } - - if (npobject->_class->hasProperty && - npobject->_class->hasProperty(npobject, ident) && - npobject->_class->setProperty) { - NPVariant npvalue; - VOID_TO_NPVARIANT(npvalue); - ConvertV8ObjectToNPVariant(value, npobject, &npvalue); - bool succ = npobject->_class->setProperty(npobject, ident, &npvalue); - NPN_ReleaseVariantValue(&npvalue); - if (succ) return value; // intercept the call - } - return v8::Local<v8::Value>(); // do not intercept the call + v8::Local<v8::Value> value) +{ + NPObject* npobject = V8Proxy::ToNativeObject<NPObject>(V8ClassIndex::NPOBJECT, self); + + // Verify that our wrapper wasn't using a NPObject which + // has already been deleted. + if (!npobject || !_NPN_IsAlive(npobject)) { + V8Proxy::ThrowError(V8Proxy::REFERENCE_ERROR, "NPObject deleted"); + return value; // intercepted, but an exception was thrown + } + + if (npobject->_class->hasProperty && + npobject->_class->hasProperty(npobject, ident) && + npobject->_class->setProperty) { + + NPVariant npvalue; + VOID_TO_NPVARIANT(npvalue); + ConvertV8ObjectToNPVariant(value, npobject, &npvalue); + bool succ = npobject->_class->setProperty(npobject, ident, &npvalue); + NPN_ReleaseVariantValue(&npvalue); + if (succ) + return value; // intercept the call + } + return v8::Local<v8::Value>(); // do not intercept the call } v8::Handle<v8::Value> NPObjectNamedPropertySetter(v8::Local<v8::String> name, v8::Local<v8::Value> value, - const v8::AccessorInfo& info) { - NPIdentifier ident = GetStringIdentifier(name); - return NPObjectSetProperty(info.Holder(), ident, value); + const v8::AccessorInfo& info) +{ + NPIdentifier ident = GetStringIdentifier(name); + return NPObjectSetProperty(info.Holder(), ident, value); } v8::Handle<v8::Value> NPObjectIndexedPropertySetter(uint32_t index, v8::Local<v8::Value> value, - const v8::AccessorInfo& info) { - NPIdentifier ident = NPN_GetIntIdentifier(index); - return NPObjectSetProperty(info.Holder(), ident, value); + const v8::AccessorInfo& info) +{ + NPIdentifier ident = NPN_GetIntIdentifier(index); + return NPObjectSetProperty(info.Holder(), ident, value); } v8::Handle<v8::Value> NPObjectSetNamedProperty(v8::Local<v8::Object> self, v8::Local<v8::String> name, - v8::Local<v8::Value> value) { - NPIdentifier ident = GetStringIdentifier(name); - return NPObjectSetProperty(self, ident, value); + v8::Local<v8::Value> value) +{ + NPIdentifier ident = GetStringIdentifier(name); + return NPObjectSetProperty(self, ident, value); } v8::Handle<v8::Value> NPObjectSetIndexedProperty(v8::Local<v8::Object> self, uint32_t index, - v8::Local<v8::Value> value) { - NPIdentifier ident = NPN_GetIntIdentifier(index); - return NPObjectSetProperty(self, ident, value); + v8::Local<v8::Value> value) +{ + NPIdentifier ident = NPN_GetIntIdentifier(index); + return NPObjectSetProperty(self, ident, value); } static void WeakNPObjectCallback(v8::Persistent<v8::Value> obj, void* param); -static DOMWrapperMap<NPObject> static_npobject_map(&WeakNPObjectCallback); +static DOMWrapperMap<NPObject> staticNpobjectMap(&WeakNPObjectCallback); -static void WeakNPObjectCallback(v8::Persistent<v8::Value> obj, - void* param) { - NPObject* npobject = static_cast<NPObject*>(param); - ASSERT(static_npobject_map.contains(npobject)); - ASSERT(npobject != NULL); +static void WeakNPObjectCallback(v8::Persistent<v8::Value> obj, void* param) +{ + NPObject* npobject = static_cast<NPObject*>(param); + ASSERT(staticNpobjectMap.contains(npobject)); + ASSERT(npobject != NULL); - // Must remove from our map before calling NPN_ReleaseObject(). - // NPN_ReleaseObject can call ForgetV8ObjectForNPObject, which - // uses the table as well. - static_npobject_map.forget(npobject); + // Must remove from our map before calling NPN_ReleaseObject(). + // NPN_ReleaseObject can call ForgetV8ObjectForNPObject, which + // uses the table as well. + staticNpobjectMap.forget(npobject); - if (_NPN_IsAlive(npobject)) - NPN_ReleaseObject(npobject); + if (_NPN_IsAlive(npobject)) + NPN_ReleaseObject(npobject); } -v8::Local<v8::Object> CreateV8ObjectForNPObject(NPObject* object, - NPObject* root) { - static v8::Persistent<v8::FunctionTemplate> np_object_desc; - - ASSERT(v8::Context::InContext()); - - // If this is a v8 object, just return it. - if (object->_class == NPScriptObjectClass) { - V8NPObject* v8npobject = reinterpret_cast<V8NPObject*>(object); - return v8::Local<v8::Object>::New(v8npobject->v8_object); - } - - // If we've already wrapped this object, just return it. - if (static_npobject_map.contains(object)) - return v8::Local<v8::Object>::New(static_npobject_map.get(object)); - - // TODO: we should create a Wrapper type as a subclass of JSObject. - // It has two internal fields, field 0 is the wrapped pointer, - // and field 1 is the type. There should be an api function that - // returns unused type id. - // The same Wrapper type can be used by DOM bindings. - if (np_object_desc.IsEmpty()) { - np_object_desc = - v8::Persistent<v8::FunctionTemplate>::New(v8::FunctionTemplate::New()); - np_object_desc->InstanceTemplate()->SetInternalFieldCount( - V8Custom::kNPObjectInternalFieldCount); - np_object_desc->InstanceTemplate()->SetNamedPropertyHandler( - NPObjectNamedPropertyGetter, NPObjectNamedPropertySetter); - np_object_desc->InstanceTemplate()->SetIndexedPropertyHandler( - NPObjectIndexedPropertyGetter, NPObjectIndexedPropertySetter); - np_object_desc->InstanceTemplate()->SetCallAsFunctionHandler( - NPObjectInvokeDefaultHandler); - } - - v8::Handle<v8::Function> func = np_object_desc->GetFunction(); - v8::Local<v8::Object> value = SafeAllocation::NewInstance(func); - - // If we were unable to allocate the instance we avoid wrapping - // and registering the NP object. - if (value.IsEmpty()) - return value; - - WrapNPObject(value, object); - - // KJS retains the object as part of its wrapper (see Bindings::CInstance) - NPN_RetainObject(object); - - _NPN_RegisterObject(object, root); - - // Maintain a weak pointer for v8 so we can cleanup the object. - v8::Persistent<v8::Object> weak_ref = v8::Persistent<v8::Object>::New(value); - static_npobject_map.set(object, weak_ref); - - return value; +v8::Local<v8::Object> CreateV8ObjectForNPObject(NPObject* object, NPObject* root) +{ + static v8::Persistent<v8::FunctionTemplate> npObjectDesc; + + ASSERT(v8::Context::InContext()); + + // If this is a v8 object, just return it. + if (object->_class == NPScriptObjectClass) { + V8NPObject* v8npobject = reinterpret_cast<V8NPObject*>(object); + return v8::Local<v8::Object>::New(v8npobject->v8Object); + } + + // If we've already wrapped this object, just return it. + if (staticNpobjectMap.contains(object)) + return v8::Local<v8::Object>::New(staticNpobjectMap.get(object)); + + // TODO: we should create a Wrapper type as a subclass of JSObject. + // It has two internal fields, field 0 is the wrapped pointer, + // and field 1 is the type. There should be an api function that + // returns unused type id. + // The same Wrapper type can be used by DOM bindings. + if (npObjectDesc.IsEmpty()) { + npObjectDesc = v8::Persistent<v8::FunctionTemplate>::New(v8::FunctionTemplate::New()); + npObjectDesc->InstanceTemplate()->SetInternalFieldCount(V8Custom::kNPObjectInternalFieldCount); + npObjectDesc->InstanceTemplate()->SetNamedPropertyHandler(NPObjectNamedPropertyGetter, NPObjectNamedPropertySetter); + npObjectDesc->InstanceTemplate()->SetIndexedPropertyHandler(NPObjectIndexedPropertyGetter, NPObjectIndexedPropertySetter); + npObjectDesc->InstanceTemplate()->SetCallAsFunctionHandler(NPObjectInvokeDefaultHandler); + } + + v8::Handle<v8::Function> func = npObjectDesc->GetFunction(); + v8::Local<v8::Object> value = SafeAllocation::NewInstance(func); + + // If we were unable to allocate the instance we avoid wrapping + // and registering the NP object. + if (value.IsEmpty()) + return value; + + WrapNPObject(value, object); + + // KJS retains the object as part of its wrapper (see Bindings::CInstance) + NPN_RetainObject(object); + + _NPN_RegisterObject(object, root); + + // Maintain a weak pointer for v8 so we can cleanup the object. + v8::Persistent<v8::Object> weakRef = v8::Persistent<v8::Object>::New(value); + staticNpobjectMap.set(object, weakRef); + + return value; } -void ForgetV8ObjectForNPObject(NPObject* object) { - if (static_npobject_map.contains(object)) { - v8::HandleScope scope; - v8::Persistent<v8::Object> handle(static_npobject_map.get(object)); - WebCore::V8Proxy::SetDOMWrapper(handle, - WebCore::V8ClassIndex::NPOBJECT, NULL); - static_npobject_map.forget(object); - NPN_ReleaseObject(object); - } +void ForgetV8ObjectForNPObject(NPObject* object) +{ + if (staticNpobjectMap.contains(object)) { + v8::HandleScope scope; + v8::Persistent<v8::Object> handle(staticNpobjectMap.get(object)); + WebCore::V8Proxy::SetDOMWrapper(handle, WebCore::V8ClassIndex::NPOBJECT, NULL); + staticNpobjectMap.forget(object); + NPN_ReleaseObject(object); + } } diff --git a/webkit/port/bindings/v8/v8_npobject.h b/webkit/port/bindings/v8/v8_npobject.h index 75a501d8..c236c60 100644 --- a/webkit/port/bindings/v8/v8_npobject.h +++ b/webkit/port/bindings/v8/v8_npobject.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef V8_NPOBJECT_H__ -#define V8_NPOBJECT_H__ +#ifndef v8_npobject_h +#define v8_npobject_h #include <v8.h> #include "third_party/npapi/bindings/npruntime.h" @@ -40,8 +40,7 @@ v8::Handle<v8::Value> NPObjectInvokeDefaultHandler(const v8::Arguments& args); // If the object is not wrapped, wrap it, and give V8 a weak // reference to the wrapper which will cleanup when there are // no more JS references to the object. -v8::Local<v8::Object> CreateV8ObjectForNPObject(NPObject* object, - NPObject *root); +v8::Local<v8::Object> CreateV8ObjectForNPObject(NPObject* object, NPObject *root); // Tell V8 to forcibly remove an object. // This is used at plugin teardown so that the caller can @@ -51,5 +50,4 @@ v8::Local<v8::Object> CreateV8ObjectForNPObject(NPObject* object, // it cannot be referred to. void ForgetV8ObjectForNPObject(NPObject*object); -#endif // V8_NPOBJECT_H__ - +#endif // v8_npobject_h |