| OLD | NEW |
| (Empty) |
| 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 | |
| 31 #include "bindings/core/v8/V8NPObject.h" | |
| 32 | |
| 33 #include "bindings/core/v8/NPV8Object.h" | |
| 34 #include "bindings/core/v8/V8Binding.h" | |
| 35 #include "bindings/core/v8/V8GlobalValueMap.h" | |
| 36 #include "bindings/core/v8/V8HTMLEmbedElement.h" | |
| 37 #include "bindings/core/v8/V8HTMLObjectElement.h" | |
| 38 #include "bindings/core/v8/V8NPUtils.h" | |
| 39 #include "bindings/core/v8/V8ObjectConstructor.h" | |
| 40 #include "bindings/core/v8/npruntime_impl.h" | |
| 41 #include "bindings/core/v8/npruntime_priv.h" | |
| 42 #include "core/html/HTMLPlugInElement.h" | |
| 43 #include "v8-util.h" | |
| 44 #include "wtf/OwnPtr.h" | |
| 45 | |
| 46 namespace blink { | |
| 47 | |
| 48 enum InvokeFunctionType { | |
| 49 InvokeMethod = 1, | |
| 50 InvokeConstruct = 2, | |
| 51 InvokeDefault = 3 | |
| 52 }; | |
| 53 | |
| 54 struct IdentifierRep { | |
| 55 int number() const { return m_isString ? 0 : m_value.m_number; } | |
| 56 const char* string() const { return m_isString ? m_value.m_string : 0; } | |
| 57 | |
| 58 union { | |
| 59 const char* m_string; | |
| 60 int m_number; | |
| 61 } m_value; | |
| 62 bool m_isString; | |
| 63 }; | |
| 64 | |
| 65 // FIXME: need comments. | |
| 66 // Params: holder could be HTMLEmbedElement or NPObject | |
| 67 static void npObjectInvokeImpl(const v8::FunctionCallbackInfo<v8::Value>& info,
InvokeFunctionType functionId) | |
| 68 { | |
| 69 NPObject* npObject; | |
| 70 v8::Isolate* isolate = info.GetIsolate(); | |
| 71 | |
| 72 // These two types are subtypes of HTMLPlugInElement. | |
| 73 HTMLPlugInElement* element = V8HTMLEmbedElement::toImplWithTypeCheck(isolate
, info.Holder()); | |
| 74 if (!element) | |
| 75 element = V8HTMLObjectElement::toImplWithTypeCheck(isolate, info.Holder(
)); | |
| 76 if (element) { | |
| 77 if (RefPtr<SharedPersistent<v8::Object>> wrapper = element->pluginWrappe
r()) { | |
| 78 v8::HandleScope handleScope(isolate); | |
| 79 npObject = v8ObjectToNPObject(wrapper->newLocal(isolate)); | |
| 80 } else { | |
| 81 npObject = 0; | |
| 82 } | |
| 83 } else { | |
| 84 // The holder object is not a subtype of HTMLPlugInElement, it must be a
n NPObject which has three | |
| 85 // internal fields. | |
| 86 if (info.Holder()->InternalFieldCount() != npObjectInternalFieldCount) { | |
| 87 V8ThrowException::throwReferenceError(info.GetIsolate(), "NPMethod c
alled on non-NPObject"); | |
| 88 return; | |
| 89 } | |
| 90 | |
| 91 npObject = v8ObjectToNPObject(info.Holder()); | |
| 92 } | |
| 93 | |
| 94 // Verify that our wrapper wasn't using a NPObject which has already been de
leted. | |
| 95 if (!npObject || !_NPN_IsAlive(npObject)) { | |
| 96 V8ThrowException::throwReferenceError(isolate, "NPObject deleted"); | |
| 97 return; | |
| 98 } | |
| 99 | |
| 100 // Wrap up parameters. | |
| 101 int numArgs = info.Length(); | |
| 102 OwnPtr<NPVariant[]> npArgs = adoptArrayPtr(new NPVariant[numArgs]); | |
| 103 | |
| 104 for (int i = 0; i < numArgs; i++) | |
| 105 convertV8ObjectToNPVariant(isolate, info[i], npObject, &npArgs[i]); | |
| 106 | |
| 107 NPVariant result; | |
| 108 VOID_TO_NPVARIANT(result); | |
| 109 | |
| 110 bool retval = true; | |
| 111 switch (functionId) { | |
| 112 case InvokeMethod: | |
| 113 if (npObject->_class->invoke) { | |
| 114 v8::Local<v8::String> functionName = v8::Local<v8::String>::Cast(inf
o.Data()); | |
| 115 NPIdentifier identifier = getStringIdentifier(functionName); | |
| 116 retval = npObject->_class->invoke(npObject, identifier, npArgs.get()
, numArgs, &result); | |
| 117 } | |
| 118 break; | |
| 119 case InvokeConstruct: | |
| 120 if (npObject->_class->construct) | |
| 121 retval = npObject->_class->construct(npObject, npArgs.get(), numArgs
, &result); | |
| 122 break; | |
| 123 case InvokeDefault: | |
| 124 if (npObject->_class->invokeDefault) | |
| 125 retval = npObject->_class->invokeDefault(npObject, npArgs.get(), num
Args, &result); | |
| 126 break; | |
| 127 default: | |
| 128 break; | |
| 129 } | |
| 130 | |
| 131 if (!retval) | |
| 132 V8ThrowException::throwGeneralError(isolate, "Error calling method on NP
Object."); | |
| 133 | |
| 134 for (int i = 0; i < numArgs; i++) | |
| 135 _NPN_ReleaseVariantValue(&npArgs[i]); | |
| 136 | |
| 137 // Unwrap return values. | |
| 138 v8::Local<v8::Value> returnValue; | |
| 139 if (_NPN_IsAlive(npObject)) | |
| 140 returnValue = convertNPVariantToV8Object(isolate, &result, npObject); | |
| 141 _NPN_ReleaseVariantValue(&result); | |
| 142 | |
| 143 v8SetReturnValue(info, returnValue); | |
| 144 } | |
| 145 | |
| 146 | |
| 147 void npObjectMethodHandler(const v8::FunctionCallbackInfo<v8::Value>& info) | |
| 148 { | |
| 149 return npObjectInvokeImpl(info, InvokeMethod); | |
| 150 } | |
| 151 | |
| 152 | |
| 153 void npObjectInvokeDefaultHandler(const v8::FunctionCallbackInfo<v8::Value>& inf
o) | |
| 154 { | |
| 155 if (info.IsConstructCall()) { | |
| 156 npObjectInvokeImpl(info, InvokeConstruct); | |
| 157 return; | |
| 158 } | |
| 159 | |
| 160 npObjectInvokeImpl(info, InvokeDefault); | |
| 161 } | |
| 162 | |
| 163 class V8TemplateMapTraits : public V8GlobalValueMapTraits<PrivateIdentifier*, v8
::FunctionTemplate, v8::kWeakWithParameter> { | |
| 164 public: | |
| 165 typedef v8::GlobalValueMap<PrivateIdentifier*, v8::FunctionTemplate, V8Templ
ateMapTraits> MapType; | |
| 166 typedef PrivateIdentifier WeakCallbackDataType; | |
| 167 | |
| 168 static WeakCallbackDataType* WeakCallbackParameter(MapType* map, PrivateIden
tifier* key, const v8::Local<v8::FunctionTemplate>& value) | |
| 169 { | |
| 170 return key; | |
| 171 } | |
| 172 | |
| 173 static void DisposeCallbackData(WeakCallbackDataType* callbackData) { } | |
| 174 | |
| 175 static MapType* MapFromWeakCallbackInfo( | |
| 176 const v8::WeakCallbackInfo<WeakCallbackDataType>&); | |
| 177 | |
| 178 static PrivateIdentifier* KeyFromWeakCallbackInfo( | |
| 179 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) | |
| 180 { | |
| 181 return data.GetParameter(); | |
| 182 } | |
| 183 | |
| 184 // Dispose traits: | |
| 185 static void OnWeakCallback(const v8::WeakCallbackInfo<WeakCallbackDataType>&
data) { } | |
| 186 static void Dispose(v8::Isolate* isolate, v8::Global<v8::FunctionTemplate> v
alue, PrivateIdentifier* key) { } | |
| 187 static void DisposeWeak(const v8::WeakCallbackInfo<WeakCallbackDataType>& da
ta) { } | |
| 188 }; | |
| 189 | |
| 190 | |
| 191 class V8NPTemplateMap { | |
| 192 public: | |
| 193 // NPIdentifier is PrivateIdentifier*. | |
| 194 typedef v8::GlobalValueMap<PrivateIdentifier*, v8::FunctionTemplate, V8Templ
ateMapTraits> MapType; | |
| 195 | |
| 196 v8::Local<v8::FunctionTemplate> get(PrivateIdentifier* key) | |
| 197 { | |
| 198 return m_map.Get(key); | |
| 199 } | |
| 200 | |
| 201 void set(PrivateIdentifier* key, v8::Local<v8::FunctionTemplate> handle) | |
| 202 { | |
| 203 ASSERT(!m_map.Contains(key)); | |
| 204 m_map.Set(key, handle); | |
| 205 } | |
| 206 | |
| 207 static V8NPTemplateMap& sharedInstance(v8::Isolate* isolate) | |
| 208 { | |
| 209 DEFINE_STATIC_LOCAL(V8NPTemplateMap, map, (isolate)); | |
| 210 ASSERT(isolate == map.m_map.GetIsolate()); | |
| 211 return map; | |
| 212 } | |
| 213 | |
| 214 friend class V8TemplateMapTraits; | |
| 215 | |
| 216 private: | |
| 217 explicit V8NPTemplateMap(v8::Isolate* isolate) | |
| 218 : m_map(isolate) | |
| 219 { | |
| 220 } | |
| 221 | |
| 222 MapType m_map; | |
| 223 }; | |
| 224 | |
| 225 V8TemplateMapTraits::MapType* V8TemplateMapTraits::MapFromWeakCallbackInfo(const
v8::WeakCallbackInfo<WeakCallbackDataType>& data) | |
| 226 { | |
| 227 return &V8NPTemplateMap::sharedInstance(data.GetIsolate()).m_map; | |
| 228 } | |
| 229 | |
| 230 | |
| 231 static v8::Local<v8::Value> npObjectGetProperty(v8::Isolate* isolate, v8::Local<
v8::Object> self, NPIdentifier identifier, v8::Local<v8::Value> key) | |
| 232 { | |
| 233 NPObject* npObject = v8ObjectToNPObject(self); | |
| 234 | |
| 235 // Verify that our wrapper wasn't using a NPObject which | |
| 236 // has already been deleted. | |
| 237 if (!npObject || !_NPN_IsAlive(npObject)) | |
| 238 return V8ThrowException::throwReferenceError(isolate, "NPObject deleted"
); | |
| 239 | |
| 240 | |
| 241 if (npObject->_class->hasProperty && npObject->_class->getProperty && npObje
ct->_class->hasProperty(npObject, identifier)) { | |
| 242 if (!_NPN_IsAlive(npObject)) | |
| 243 return V8ThrowException::throwReferenceError(isolate, "NPObject dele
ted"); | |
| 244 | |
| 245 NPVariant result; | |
| 246 VOID_TO_NPVARIANT(result); | |
| 247 if (!npObject->_class->getProperty(npObject, identifier, &result)) | |
| 248 return v8Undefined(); | |
| 249 | |
| 250 v8::Local<v8::Value> returnValue; | |
| 251 if (_NPN_IsAlive(npObject)) | |
| 252 returnValue = convertNPVariantToV8Object(isolate, &result, npObject)
; | |
| 253 _NPN_ReleaseVariantValue(&result); | |
| 254 return returnValue; | |
| 255 | |
| 256 } | |
| 257 | |
| 258 if (!_NPN_IsAlive(npObject)) | |
| 259 return V8ThrowException::throwReferenceError(isolate, "NPObject deleted"
); | |
| 260 | |
| 261 if (key->IsString() && npObject->_class->hasMethod && npObject->_class->hasM
ethod(npObject, identifier)) { | |
| 262 if (!_NPN_IsAlive(npObject)) | |
| 263 return V8ThrowException::throwReferenceError(isolate, "NPObject dele
ted"); | |
| 264 | |
| 265 PrivateIdentifier* id = static_cast<PrivateIdentifier*>(identifier); | |
| 266 v8::Local<v8::FunctionTemplate> functionTemplate = V8NPTemplateMap::shar
edInstance(isolate).get(id); | |
| 267 // Cache templates using identifier as the key. | |
| 268 if (functionTemplate.IsEmpty()) { | |
| 269 // Create a new template. | |
| 270 functionTemplate = v8::FunctionTemplate::New(isolate); | |
| 271 functionTemplate->SetCallHandler(npObjectMethodHandler, key); | |
| 272 V8NPTemplateMap::sharedInstance(isolate).set(id, functionTemplate); | |
| 273 } | |
| 274 v8::Local<v8::Function> v8Function; | |
| 275 if (!functionTemplate->GetFunction(isolate->GetCurrentContext()).ToLocal
(&v8Function)) | |
| 276 return v8Undefined(); | |
| 277 v8Function->SetName(v8::Local<v8::String>::Cast(key)); | |
| 278 return v8Function; | |
| 279 } | |
| 280 | |
| 281 return v8Undefined(); | |
| 282 } | |
| 283 | |
| 284 void npObjectNamedPropertyGetter(v8::Local<v8::String> name, const v8::PropertyC
allbackInfo<v8::Value>& info) | |
| 285 { | |
| 286 NPIdentifier identifier = getStringIdentifier(name); | |
| 287 v8SetReturnValue(info, npObjectGetProperty(info.GetIsolate(), info.Holder(),
identifier, name)); | |
| 288 } | |
| 289 | |
| 290 void npObjectIndexedPropertyGetter(uint32_t index, const v8::PropertyCallbackInf
o<v8::Value>& info) | |
| 291 { | |
| 292 NPIdentifier identifier = _NPN_GetIntIdentifier(index); | |
| 293 v8SetReturnValue(info, npObjectGetProperty(info.GetIsolate(), info.Holder(),
identifier, v8::Number::New(info.GetIsolate(), index))); | |
| 294 } | |
| 295 | |
| 296 void npObjectGetNamedProperty(v8::Local<v8::Object> self, v8::Local<v8::String>
name, const v8::PropertyCallbackInfo<v8::Value>& info) | |
| 297 { | |
| 298 NPIdentifier identifier = getStringIdentifier(name); | |
| 299 v8SetReturnValue(info, npObjectGetProperty(info.GetIsolate(), self, identifi
er, name)); | |
| 300 } | |
| 301 | |
| 302 void npObjectGetIndexedProperty(v8::Local<v8::Object> self, uint32_t index, cons
t v8::PropertyCallbackInfo<v8::Value>& info) | |
| 303 { | |
| 304 NPIdentifier identifier = _NPN_GetIntIdentifier(index); | |
| 305 v8SetReturnValue(info, npObjectGetProperty(info.GetIsolate(), self, identifi
er, v8::Number::New(info.GetIsolate(), index))); | |
| 306 } | |
| 307 | |
| 308 void npObjectQueryProperty(v8::Local<v8::String> name, const v8::PropertyCallbac
kInfo<v8::Integer>& info) | |
| 309 { | |
| 310 NPIdentifier identifier = getStringIdentifier(name); | |
| 311 if (npObjectGetProperty(info.GetIsolate(), info.Holder(), identifier, name).
IsEmpty()) | |
| 312 return; | |
| 313 v8SetReturnValueInt(info, 0); | |
| 314 } | |
| 315 | |
| 316 static v8::Local<v8::Value> npObjectSetProperty(v8::Local<v8::Object> self, NPId
entifier identifier, v8::Local<v8::Value> value, v8::Isolate* isolate) | |
| 317 { | |
| 318 NPObject* npObject = v8ObjectToNPObject(self); | |
| 319 | |
| 320 // Verify that our wrapper wasn't using a NPObject which has already been de
leted. | |
| 321 if (!npObject || !_NPN_IsAlive(npObject)) { | |
| 322 V8ThrowException::throwReferenceError(isolate, "NPObject deleted"); | |
| 323 return value; // Intercepted, but an exception was thrown. | |
| 324 } | |
| 325 | |
| 326 if (npObject->_class->hasProperty && npObject->_class->setProperty && npObje
ct->_class->hasProperty(npObject, identifier)) { | |
| 327 if (!_NPN_IsAlive(npObject)) | |
| 328 return V8ThrowException::throwReferenceError(isolate, "NPObject dele
ted"); | |
| 329 | |
| 330 NPVariant npValue; | |
| 331 VOID_TO_NPVARIANT(npValue); | |
| 332 convertV8ObjectToNPVariant(isolate, value, npObject, &npValue); | |
| 333 bool success = npObject->_class->setProperty(npObject, identifier, &npVa
lue); | |
| 334 _NPN_ReleaseVariantValue(&npValue); | |
| 335 if (success) | |
| 336 return value; // Intercept the call. | |
| 337 } | |
| 338 return v8Undefined(); | |
| 339 } | |
| 340 | |
| 341 | |
| 342 void npObjectNamedPropertySetter(v8::Local<v8::String> name, v8::Local<v8::Value
> value, const v8::PropertyCallbackInfo<v8::Value>& info) | |
| 343 { | |
| 344 NPIdentifier identifier = getStringIdentifier(name); | |
| 345 v8SetReturnValue(info, npObjectSetProperty(info.Holder(), identifier, value,
info.GetIsolate())); | |
| 346 } | |
| 347 | |
| 348 | |
| 349 void npObjectIndexedPropertySetter(uint32_t index, v8::Local<v8::Value> value, c
onst v8::PropertyCallbackInfo<v8::Value>& info) | |
| 350 { | |
| 351 NPIdentifier identifier = _NPN_GetIntIdentifier(index); | |
| 352 v8SetReturnValue(info, npObjectSetProperty(info.Holder(), identifier, value,
info.GetIsolate())); | |
| 353 } | |
| 354 | |
| 355 void npObjectSetNamedProperty(v8::Local<v8::Object> self, v8::Local<v8::String>
name, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& inf
o) | |
| 356 { | |
| 357 NPIdentifier identifier = getStringIdentifier(name); | |
| 358 v8SetReturnValue(info, npObjectSetProperty(self, identifier, value, info.Get
Isolate())); | |
| 359 } | |
| 360 | |
| 361 void npObjectSetIndexedProperty(v8::Local<v8::Object> self, uint32_t index, v8::
Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info) | |
| 362 { | |
| 363 NPIdentifier identifier = _NPN_GetIntIdentifier(index); | |
| 364 v8SetReturnValue(info, npObjectSetProperty(self, identifier, value, info.Get
Isolate())); | |
| 365 } | |
| 366 | |
| 367 void npObjectPropertyEnumerator(const v8::PropertyCallbackInfo<v8::Array>& info,
bool namedProperty) | |
| 368 { | |
| 369 NPObject* npObject = v8ObjectToNPObject(info.Holder()); | |
| 370 | |
| 371 // Verify that our wrapper wasn't using a NPObject which | |
| 372 // has already been deleted. | |
| 373 if (!npObject || !_NPN_IsAlive(npObject)) { | |
| 374 V8ThrowException::throwReferenceError(info.GetIsolate(), "NPObject delet
ed"); | |
| 375 return; | |
| 376 } | |
| 377 | |
| 378 if (NP_CLASS_STRUCT_VERSION_HAS_ENUM(npObject->_class) && npObject->_class->
enumerate) { | |
| 379 uint32_t count; | |
| 380 NPIdentifier* identifiers; | |
| 381 if (npObject->_class->enumerate(npObject, &identifiers, &count)) { | |
| 382 uint32_t propertiesCount = 0; | |
| 383 for (uint32_t i = 0; i < count; ++i) { | |
| 384 IdentifierRep* identifier = static_cast<IdentifierRep*>(identifi
ers[i]); | |
| 385 if (namedProperty == identifier->m_isString) | |
| 386 ++propertiesCount; | |
| 387 } | |
| 388 v8::Local<v8::Array> properties = v8::Array::New(info.GetIsolate(),
propertiesCount); | |
| 389 for (uint32_t i = 0, propertyIndex = 0; i < count; ++i) { | |
| 390 IdentifierRep* identifier = static_cast<IdentifierRep*>(identifi
ers[i]); | |
| 391 if (namedProperty == identifier->m_isString) { | |
| 392 ASSERT(propertyIndex < propertiesCount); | |
| 393 v8::Local<v8::Value> value; | |
| 394 if (namedProperty) | |
| 395 value = v8AtomicString(info.GetIsolate(), identifier->st
ring()); | |
| 396 else | |
| 397 value = v8::Integer::New(info.GetIsolate(), identifier->
number()); | |
| 398 v8::Local<v8::Number> index = v8::Integer::New(info.GetIsola
te(), propertyIndex++); | |
| 399 if (!v8CallBoolean(properties->Set(info.GetIsolate()->GetCur
rentContext(), index, value))) | |
| 400 return; | |
| 401 } | |
| 402 } | |
| 403 | |
| 404 v8SetReturnValue(info, properties); | |
| 405 return; | |
| 406 } | |
| 407 } | |
| 408 } | |
| 409 | |
| 410 void npObjectNamedPropertyEnumerator(const v8::PropertyCallbackInfo<v8::Array>&
info) | |
| 411 { | |
| 412 npObjectPropertyEnumerator(info, true); | |
| 413 } | |
| 414 | |
| 415 void npObjectIndexedPropertyEnumerator(const v8::PropertyCallbackInfo<v8::Array>
& info) | |
| 416 { | |
| 417 npObjectPropertyEnumerator(info, false); | |
| 418 } | |
| 419 | |
| 420 static DOMWrapperMap<NPObject>& staticNPObjectMap() | |
| 421 { | |
| 422 DEFINE_STATIC_LOCAL(DOMWrapperMap<NPObject>, npObjectMap, (v8::Isolate::GetC
urrent())); | |
| 423 return npObjectMap; | |
| 424 } | |
| 425 | |
| 426 template<> | |
| 427 inline void DOMWrapperMap<NPObject>::PersistentValueMapTraits::Dispose(v8::Isola
te*, v8::Global<v8::Object> value, NPObject* key) | |
| 428 { | |
| 429 auto npObject = key; | |
| 430 ASSERT(npObject); | |
| 431 if (_NPN_IsAlive(npObject)) | |
| 432 _NPN_ReleaseObject(npObject); | |
| 433 } | |
| 434 | |
| 435 template<> | |
| 436 inline void DOMWrapperMap<NPObject>::PersistentValueMapTraits::DisposeWeak(const
v8::WeakCallbackInfo<WeakCallbackDataType>& data) | |
| 437 { | |
| 438 auto npObject = KeyFromWeakCallbackInfo(data); | |
| 439 ASSERT(npObject); | |
| 440 if (_NPN_IsAlive(npObject)) | |
| 441 _NPN_ReleaseObject(npObject); | |
| 442 } | |
| 443 | |
| 444 v8::Local<v8::Object> createV8ObjectForNPObject(v8::Isolate* isolate, NPObject*
object, NPObject* root) | |
| 445 { | |
| 446 static v8::Eternal<v8::FunctionTemplate> npObjectDesc; | |
| 447 | |
| 448 ASSERT(isolate->InContext()); | |
| 449 | |
| 450 // If this is a v8 object, just return it. | |
| 451 V8NPObject* v8NPObject = npObjectToV8NPObject(object); | |
| 452 if (v8NPObject) | |
| 453 return v8::Local<v8::Object>::New(isolate, v8NPObject->v8Object); | |
| 454 | |
| 455 // If we've already wrapped this object, just return it. | |
| 456 v8::Local<v8::Object> wrapper = staticNPObjectMap().newLocal(isolate, object
); | |
| 457 if (!wrapper.IsEmpty()) | |
| 458 return wrapper; | |
| 459 | |
| 460 // FIXME: we should create a Wrapper type as a subclass of JSObject. It has
two internal fields, field 0 is the wrapped | |
| 461 // pointer, and field 1 is the type. There should be an api function that re
turns unused type id. The same Wrapper type | |
| 462 // can be used by DOM bindings. | |
| 463 if (npObjectDesc.IsEmpty()) { | |
| 464 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(isolat
e); | |
| 465 templ->InstanceTemplate()->SetInternalFieldCount(npObjectInternalFieldCo
unt); | |
| 466 templ->InstanceTemplate()->SetNamedPropertyHandler(npObjectNamedProperty
Getter, npObjectNamedPropertySetter, npObjectQueryProperty, 0, npObjectNamedProp
ertyEnumerator); | |
| 467 templ->InstanceTemplate()->SetIndexedPropertyHandler(npObjectIndexedProp
ertyGetter, npObjectIndexedPropertySetter, 0, 0, npObjectIndexedPropertyEnumerat
or); | |
| 468 templ->InstanceTemplate()->SetCallAsFunctionHandler(npObjectInvokeDefaul
tHandler); | |
| 469 npObjectDesc.Set(isolate, templ); | |
| 470 } | |
| 471 | |
| 472 // FIXME: Move staticNPObjectMap() to DOMDataStore. | |
| 473 // Use V8DOMWrapper::createWrapper() and | |
| 474 // V8DOMWrapper::associateObjectWithWrapper() | |
| 475 // to create a wrapper object. | |
| 476 v8::Local<v8::Function> v8Function; | |
| 477 if (!npObjectDesc.Get(isolate)->GetFunction(isolate->GetCurrentContext()).To
Local(&v8Function)) | |
| 478 return v8::Local<v8::Object>(); | |
| 479 v8::Local<v8::Object> value; | |
| 480 if (!V8ObjectConstructor::newInstance(isolate, v8Function).ToLocal(&value)) | |
| 481 return v8::Local<v8::Object>(); | |
| 482 | |
| 483 V8DOMWrapper::setNativeInfo(value, npObjectTypeInfo(), npObjectToScriptWrapp
able(object)); | |
| 484 | |
| 485 // KJS retains the object as part of its wrapper (see Bindings::CInstance). | |
| 486 _NPN_RetainObject(object); | |
| 487 _NPN_RegisterObject(object, root); | |
| 488 | |
| 489 bool wrapperDidNotExist = staticNPObjectMap().set(object, npObjectTypeInfo()
, value); | |
| 490 ASSERT_UNUSED(wrapperDidNotExist, wrapperDidNotExist); | |
| 491 ASSERT(V8DOMWrapper::hasInternalFieldsSet(value)); | |
| 492 return value; | |
| 493 } | |
| 494 | |
| 495 void forgetV8ObjectForNPObject(NPObject* object) | |
| 496 { | |
| 497 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 498 v8::HandleScope scope(isolate); | |
| 499 v8::Local<v8::Object> wrapper = staticNPObjectMap().newLocal(isolate, object
); | |
| 500 if (!wrapper.IsEmpty()) { | |
| 501 V8DOMWrapper::clearNativeInfo(wrapper, npObjectTypeInfo()); | |
| 502 staticNPObjectMap().removeAndDispose(object); | |
| 503 _NPN_ReleaseObject(object); | |
| 504 } | |
| 505 } | |
| 506 | |
| 507 } // namespace blink | |
| OLD | NEW |