/platform/external/webkit/WebCore/bindings/v8/V8NPObject.cpp

https://github.com/aharish/totoro-gb-opensource-update2 · C++ · 401 lines · 266 code · 78 blank · 57 comment · 50 complexity · 3c0f6759c77a878526f324d595ff23b5 MD5 · raw file

  1. /*
  2. * Copyright (C) 2006, 2007, 2008, 2009 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "config.h"
  31. #include "V8NPObject.h"
  32. #include "HTMLPlugInElement.h"
  33. #include "IdentifierRep.h"
  34. #include "NPV8Object.h"
  35. #include "V8DOMMap.h"
  36. #include "V8HTMLAppletElement.h"
  37. #include "V8HTMLEmbedElement.h"
  38. #include "V8HTMLObjectElement.h"
  39. #include "V8Helpers.h"
  40. #include "V8NPUtils.h"
  41. #include "V8Proxy.h"
  42. #include "npruntime_impl.h"
  43. #include "npruntime_priv.h"
  44. #include <wtf/OwnArrayPtr.h>
  45. using namespace WebCore;
  46. enum InvokeFunctionType {
  47. InvokeMethod = 1,
  48. InvokeConstruct = 2,
  49. InvokeDefault = 3
  50. };
  51. // FIXME: need comments.
  52. // Params: holder could be HTMLEmbedElement or NPObject
  53. static v8::Handle<v8::Value> npObjectInvokeImpl(const v8::Arguments& args, InvokeFunctionType functionId)
  54. {
  55. NPObject* npObject;
  56. // These three types are subtypes of HTMLPlugInElement.
  57. if (V8HTMLAppletElement::HasInstance(args.Holder()) || V8HTMLEmbedElement::HasInstance(args.Holder())
  58. || V8HTMLObjectElement::HasInstance(args.Holder())) {
  59. // The holder object is a subtype of HTMLPlugInElement.
  60. HTMLPlugInElement* element;
  61. if (V8HTMLAppletElement::HasInstance(args.Holder()))
  62. element = V8HTMLAppletElement::toNative(args.Holder());
  63. else if (V8HTMLEmbedElement::HasInstance(args.Holder()))
  64. element = V8HTMLEmbedElement::toNative(args.Holder());
  65. else
  66. element = V8HTMLObjectElement::toNative(args.Holder());
  67. ScriptInstance scriptInstance = element->getInstance();
  68. if (scriptInstance)
  69. npObject = v8ObjectToNPObject(scriptInstance->instance());
  70. else
  71. npObject = 0;
  72. } else {
  73. // The holder object is not a subtype of HTMLPlugInElement, it must be an NPObject which has three
  74. // internal fields.
  75. if (args.Holder()->InternalFieldCount() != npObjectInternalFieldCount)
  76. return throwError("NPMethod called on non-NPObject", V8Proxy::ReferenceError);
  77. npObject = v8ObjectToNPObject(args.Holder());
  78. }
  79. // Verify that our wrapper wasn't using a NPObject which has already been deleted.
  80. if (!npObject || !_NPN_IsAlive(npObject))
  81. return throwError("NPObject deleted", V8Proxy::ReferenceError);
  82. // Wrap up parameters.
  83. int numArgs = args.Length();
  84. OwnArrayPtr<NPVariant> npArgs(new NPVariant[numArgs]);
  85. for (int i = 0; i < numArgs; i++)
  86. convertV8ObjectToNPVariant(args[i], npObject, &npArgs[i]);
  87. NPVariant result;
  88. VOID_TO_NPVARIANT(result);
  89. bool retval = true;
  90. switch (functionId) {
  91. case InvokeMethod:
  92. if (npObject->_class->invoke) {
  93. v8::Handle<v8::String> functionName(v8::String::Cast(*args.Data()));
  94. NPIdentifier identifier = getStringIdentifier(functionName);
  95. retval = npObject->_class->invoke(npObject, identifier, npArgs.get(), numArgs, &result);
  96. }
  97. break;
  98. case InvokeConstruct:
  99. if (npObject->_class->construct)
  100. retval = npObject->_class->construct(npObject, npArgs.get(), numArgs, &result);
  101. break;
  102. case InvokeDefault:
  103. if (npObject->_class->invokeDefault)
  104. retval = npObject->_class->invokeDefault(npObject, npArgs.get(), numArgs, &result);
  105. break;
  106. default:
  107. break;
  108. }
  109. if (!retval)
  110. throwError("Error calling method on NPObject!", V8Proxy::GeneralError);
  111. for (int i = 0; i < numArgs; i++)
  112. _NPN_ReleaseVariantValue(&npArgs[i]);
  113. // Unwrap return values.
  114. v8::Handle<v8::Value> returnValue = convertNPVariantToV8Object(&result, npObject);
  115. _NPN_ReleaseVariantValue(&result);
  116. return returnValue;
  117. }
  118. v8::Handle<v8::Value> npObjectMethodHandler(const v8::Arguments& args)
  119. {
  120. return npObjectInvokeImpl(args, InvokeMethod);
  121. }
  122. v8::Handle<v8::Value> npObjectInvokeDefaultHandler(const v8::Arguments& args)
  123. {
  124. if (args.IsConstructCall())
  125. return npObjectInvokeImpl(args, InvokeConstruct);
  126. return npObjectInvokeImpl(args, InvokeDefault);
  127. }
  128. static void weakTemplateCallback(v8::Persistent<v8::Value>, void* parameter);
  129. // NPIdentifier is PrivateIdentifier*.
  130. static WeakReferenceMap<PrivateIdentifier, v8::FunctionTemplate> staticTemplateMap(&weakTemplateCallback);
  131. static void weakTemplateCallback(v8::Persistent<v8::Value> object, void* parameter)
  132. {
  133. PrivateIdentifier* identifier = static_cast<PrivateIdentifier*>(parameter);
  134. ASSERT(identifier);
  135. ASSERT(staticTemplateMap.contains(identifier));
  136. staticTemplateMap.forget(identifier);
  137. }
  138. static v8::Handle<v8::Value> npObjectGetProperty(v8::Local<v8::Object> self, NPIdentifier identifier, v8::Local<v8::Value> key)
  139. {
  140. NPObject* npObject = v8ObjectToNPObject(self);
  141. // Verify that our wrapper wasn't using a NPObject which
  142. // has already been deleted.
  143. if (!npObject || !_NPN_IsAlive(npObject))
  144. return throwError("NPObject deleted", V8Proxy::ReferenceError);
  145. if (npObject->_class->hasProperty && npObject->_class->hasProperty(npObject, identifier)
  146. && npObject->_class->getProperty) {
  147. NPVariant result;
  148. VOID_TO_NPVARIANT(result);
  149. if (!npObject->_class->getProperty(npObject, identifier, &result))
  150. return v8::Handle<v8::Value>();
  151. v8::Handle<v8::Value> returnValue = convertNPVariantToV8Object(&result, npObject);
  152. _NPN_ReleaseVariantValue(&result);
  153. return returnValue;
  154. }
  155. if (key->IsString() && npObject->_class->hasMethod && npObject->_class->hasMethod(npObject, identifier)) {
  156. PrivateIdentifier* id = static_cast<PrivateIdentifier*>(identifier);
  157. v8::Persistent<v8::FunctionTemplate> functionTemplate = staticTemplateMap.get(id);
  158. // Cache templates using identifier as the key.
  159. if (functionTemplate.IsEmpty()) {
  160. // Create a new template.
  161. v8::Local<v8::FunctionTemplate> temp = v8::FunctionTemplate::New();
  162. temp->SetCallHandler(npObjectMethodHandler, key);
  163. functionTemplate = v8::Persistent<v8::FunctionTemplate>::New(temp);
  164. staticTemplateMap.set(id, functionTemplate);
  165. }
  166. // FunctionTemplate caches function for each context.
  167. v8::Local<v8::Function> v8Function = functionTemplate->GetFunction();
  168. v8Function->SetName(v8::Handle<v8::String>::Cast(key));
  169. return v8Function;
  170. }
  171. return v8::Handle<v8::Value>();
  172. }
  173. v8::Handle<v8::Value> npObjectNamedPropertyGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
  174. {
  175. NPIdentifier identifier = getStringIdentifier(name);
  176. return npObjectGetProperty(info.Holder(), identifier, name);
  177. }
  178. v8::Handle<v8::Value> npObjectIndexedPropertyGetter(uint32_t index, const v8::AccessorInfo& info)
  179. {
  180. NPIdentifier identifier = _NPN_GetIntIdentifier(index);
  181. return npObjectGetProperty(info.Holder(), identifier, v8::Number::New(index));
  182. }
  183. v8::Handle<v8::Value> npObjectGetNamedProperty(v8::Local<v8::Object> self, v8::Local<v8::String> name)
  184. {
  185. NPIdentifier identifier = getStringIdentifier(name);
  186. return npObjectGetProperty(self, identifier, name);
  187. }
  188. v8::Handle<v8::Value> npObjectGetIndexedProperty(v8::Local<v8::Object> self, uint32_t index)
  189. {
  190. NPIdentifier identifier = _NPN_GetIntIdentifier(index);
  191. return npObjectGetProperty(self, identifier, v8::Number::New(index));
  192. }
  193. static v8::Handle<v8::Value> npObjectSetProperty(v8::Local<v8::Object> self, NPIdentifier identifier, v8::Local<v8::Value> value)
  194. {
  195. NPObject* npObject = v8ObjectToNPObject(self);
  196. // Verify that our wrapper wasn't using a NPObject which has already been deleted.
  197. if (!npObject || !_NPN_IsAlive(npObject)) {
  198. throwError("NPObject deleted", V8Proxy::ReferenceError);
  199. return value; // Intercepted, but an exception was thrown.
  200. }
  201. if (npObject->_class->hasProperty && npObject->_class->hasProperty(npObject, identifier)
  202. && npObject->_class->setProperty) {
  203. NPVariant npValue;
  204. VOID_TO_NPVARIANT(npValue);
  205. convertV8ObjectToNPVariant(value, npObject, &npValue);
  206. bool success = npObject->_class->setProperty(npObject, identifier, &npValue);
  207. _NPN_ReleaseVariantValue(&npValue);
  208. if (success)
  209. return value; // Intercept the call.
  210. }
  211. return notHandledByInterceptor();
  212. }
  213. v8::Handle<v8::Value> npObjectNamedPropertySetter(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
  214. {
  215. NPIdentifier identifier = getStringIdentifier(name);
  216. return npObjectSetProperty(info.Holder(), identifier, value);
  217. }
  218. v8::Handle<v8::Value> npObjectIndexedPropertySetter(uint32_t index, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
  219. {
  220. NPIdentifier identifier = _NPN_GetIntIdentifier(index);
  221. return npObjectSetProperty(info.Holder(), identifier, value);
  222. }
  223. v8::Handle<v8::Value> npObjectSetNamedProperty(v8::Local<v8::Object> self, v8::Local<v8::String> name, v8::Local<v8::Value> value)
  224. {
  225. NPIdentifier identifier = getStringIdentifier(name);
  226. return npObjectSetProperty(self, identifier, value);
  227. }
  228. v8::Handle<v8::Value> npObjectSetIndexedProperty(v8::Local<v8::Object> self, uint32_t index, v8::Local<v8::Value> value)
  229. {
  230. NPIdentifier identifier = _NPN_GetIntIdentifier(index);
  231. return npObjectSetProperty(self, identifier, value);
  232. }
  233. v8::Handle<v8::Array> npObjectPropertyEnumerator(const v8::AccessorInfo& info, bool namedProperty)
  234. {
  235. NPObject* npObject = v8ObjectToNPObject(info.Holder());
  236. // Verify that our wrapper wasn't using a NPObject which
  237. // has already been deleted.
  238. if (!npObject || !_NPN_IsAlive(npObject))
  239. throwError("NPObject deleted", V8Proxy::ReferenceError);
  240. if (NP_CLASS_STRUCT_VERSION_HAS_ENUM(npObject->_class) && npObject->_class->enumerate) {
  241. uint32_t count;
  242. NPIdentifier* identifiers;
  243. if (npObject->_class->enumerate(npObject, &identifiers, &count)) {
  244. v8::Handle<v8::Array> properties = v8::Array::New(count);
  245. for (uint32_t i = 0; i < count; ++i) {
  246. IdentifierRep* identifier = static_cast<IdentifierRep*>(identifiers[i]);
  247. if (namedProperty)
  248. properties->Set(v8::Integer::New(i), v8::String::New(identifier->string()));
  249. else
  250. properties->Set(v8::Integer::New(i), v8::Integer::New(identifier->number()));
  251. }
  252. return properties;
  253. }
  254. }
  255. return v8::Handle<v8::Array>();
  256. }
  257. v8::Handle<v8::Array> npObjectNamedPropertyEnumerator(const v8::AccessorInfo& info)
  258. {
  259. return npObjectPropertyEnumerator(info, true);
  260. }
  261. v8::Handle<v8::Array> npObjectIndexedPropertyEnumerator(const v8::AccessorInfo& info)
  262. {
  263. return npObjectPropertyEnumerator(info, false);
  264. }
  265. static void weakNPObjectCallback(v8::Persistent<v8::Value>, void* parameter);
  266. static DOMWrapperMap<NPObject> staticNPObjectMap(&weakNPObjectCallback);
  267. static void weakNPObjectCallback(v8::Persistent<v8::Value> object, void* parameter)
  268. {
  269. NPObject* npObject = static_cast<NPObject*>(parameter);
  270. ASSERT(staticNPObjectMap.contains(npObject));
  271. ASSERT(npObject);
  272. // Must remove from our map before calling _NPN_ReleaseObject(). _NPN_ReleaseObject can call ForgetV8ObjectForNPObject, which
  273. // uses the table as well.
  274. staticNPObjectMap.forget(npObject);
  275. if (_NPN_IsAlive(npObject))
  276. _NPN_ReleaseObject(npObject);
  277. }
  278. v8::Local<v8::Object> createV8ObjectForNPObject(NPObject* object, NPObject* root)
  279. {
  280. static v8::Persistent<v8::FunctionTemplate> npObjectDesc;
  281. ASSERT(v8::Context::InContext());
  282. // If this is a v8 object, just return it.
  283. if (object->_class == npScriptObjectClass) {
  284. V8NPObject* v8NPObject = reinterpret_cast<V8NPObject*>(object);
  285. return v8::Local<v8::Object>::New(v8NPObject->v8Object);
  286. }
  287. // If we've already wrapped this object, just return it.
  288. if (staticNPObjectMap.contains(object))
  289. return v8::Local<v8::Object>::New(staticNPObjectMap.get(object));
  290. // FIXME: we should create a Wrapper type as a subclass of JSObject. It has two internal fields, field 0 is the wrapped
  291. // pointer, and field 1 is the type. There should be an api function that returns unused type id. The same Wrapper type
  292. // can be used by DOM bindings.
  293. if (npObjectDesc.IsEmpty()) {
  294. npObjectDesc = v8::Persistent<v8::FunctionTemplate>::New(v8::FunctionTemplate::New());
  295. npObjectDesc->InstanceTemplate()->SetInternalFieldCount(npObjectInternalFieldCount);
  296. npObjectDesc->InstanceTemplate()->SetNamedPropertyHandler(npObjectNamedPropertyGetter, npObjectNamedPropertySetter, 0, 0, npObjectNamedPropertyEnumerator);
  297. npObjectDesc->InstanceTemplate()->SetIndexedPropertyHandler(npObjectIndexedPropertyGetter, npObjectIndexedPropertySetter, 0, 0, npObjectIndexedPropertyEnumerator);
  298. npObjectDesc->InstanceTemplate()->SetCallAsFunctionHandler(npObjectInvokeDefaultHandler);
  299. }
  300. v8::Handle<v8::Function> v8Function = npObjectDesc->GetFunction();
  301. v8::Local<v8::Object> value = SafeAllocation::newInstance(v8Function);
  302. // If we were unable to allocate the instance, we avoid wrapping and registering the NP object.
  303. if (value.IsEmpty())
  304. return value;
  305. wrapNPObject(value, object);
  306. // KJS retains the object as part of its wrapper (see Bindings::CInstance).
  307. _NPN_RetainObject(object);
  308. _NPN_RegisterObject(object, root);
  309. // Maintain a weak pointer for v8 so we can cleanup the object.
  310. v8::Persistent<v8::Object> weakRef = v8::Persistent<v8::Object>::New(value);
  311. staticNPObjectMap.set(object, weakRef);
  312. return value;
  313. }
  314. void forgetV8ObjectForNPObject(NPObject* object)
  315. {
  316. if (staticNPObjectMap.contains(object)) {
  317. v8::HandleScope scope;
  318. v8::Persistent<v8::Object> handle(staticNPObjectMap.get(object));
  319. V8DOMWrapper::setDOMWrapper(handle, WebCore::V8ClassIndex::NPOBJECT, 0);
  320. staticNPObjectMap.forget(object);
  321. _NPN_ReleaseObject(object);
  322. }
  323. }