| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2004, 2006 Apple Computer, Inc. All rights reserved. | |
| 3 * Copyright (C) 2007, 2008, 2009 Google, Inc. All rights reserved. | |
| 4 * Copyright (C) 2014 Opera Software ASA. All rights reserved. | |
| 5 * | |
| 6 * Redistribution and use in source and binary forms, with or without | |
| 7 * modification, are permitted provided that the following conditions | |
| 8 * are met: | |
| 9 * 1. Redistributions of source code must retain the above copyright | |
| 10 * notice, this list of conditions and the following disclaimer. | |
| 11 * 2. Redistributions in binary form must reproduce the above copyright | |
| 12 * notice, this list of conditions and the following disclaimer in the | |
| 13 * documentation and/or other materials provided with the distribution. | |
| 14 * | |
| 15 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | |
| 16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | |
| 19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
| 23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 26 */ | |
| 27 | |
| 28 #include "bindings/core/v8/NPV8Object.h" | |
| 29 | |
| 30 #include "bindings/core/v8/ScriptController.h" | |
| 31 #include "bindings/core/v8/ScriptSourceCode.h" | |
| 32 #include "bindings/core/v8/V8Binding.h" | |
| 33 #include "bindings/core/v8/V8GCController.h" | |
| 34 #include "bindings/core/v8/V8NPUtils.h" | |
| 35 #include "bindings/core/v8/V8ObjectConstructor.h" | |
| 36 #include "bindings/core/v8/V8ScriptRunner.h" | |
| 37 #include "bindings/core/v8/WrapperTypeInfo.h" | |
| 38 #include "bindings/core/v8/npruntime_impl.h" | |
| 39 #include "bindings/core/v8/npruntime_priv.h" | |
| 40 #include "core/frame/LocalDOMWindow.h" | |
| 41 #include "core/frame/LocalFrame.h" | |
| 42 #include "platform/ScriptForbiddenScope.h" | |
| 43 #include "platform/UserGestureIndicator.h" | |
| 44 #include "wtf/OwnPtr.h" | |
| 45 #include "wtf/StringExtras.h" | |
| 46 #include "wtf/text/WTFString.h" | |
| 47 #include <stdio.h> | |
| 48 | |
| 49 using namespace blink; | |
| 50 | |
| 51 namespace blink { | |
| 52 | |
| 53 namespace { | |
| 54 | |
| 55 void trace(Visitor*, ScriptWrappable*) | |
| 56 { | |
| 57 } | |
| 58 | |
| 59 } // namespace | |
| 60 | |
| 61 const WrapperTypeInfo* npObjectTypeInfo() | |
| 62 { | |
| 63 static const WrapperTypeInfo typeInfo = { gin::kEmbedderBlink, 0, 0, 0, trac
e, 0, 0, 0, 0, "NPObject", 0, WrapperTypeInfo::WrapperTypeObjectPrototype, Wrapp
erTypeInfo::ObjectClassId, WrapperTypeInfo::NotInheritFromEventTarget, WrapperTy
peInfo::Dependent, WrapperTypeInfo::RefCountedObject }; | |
| 64 return &typeInfo; | |
| 65 } | |
| 66 | |
| 67 // FIXME: Comments on why use malloc and free. | |
| 68 static NPObject* allocV8NPObject(NPP, NPClass*) | |
| 69 { | |
| 70 return static_cast<NPObject*>(malloc(sizeof(V8NPObject))); | |
| 71 } | |
| 72 | |
| 73 static void freeV8NPObject(NPObject* npObject) | |
| 74 { | |
| 75 V8NPObject* v8NpObject = reinterpret_cast<V8NPObject*>(npObject); | |
| 76 disposeUnderlyingV8Object(v8::Isolate::GetCurrent(), npObject); | |
| 77 free(v8NpObject); | |
| 78 } | |
| 79 | |
| 80 static NPClass V8NPObjectClass = { | |
| 81 NP_CLASS_STRUCT_VERSION, | |
| 82 allocV8NPObject, | |
| 83 freeV8NPObject, | |
| 84 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | |
| 85 }; | |
| 86 | |
| 87 static ScriptState* mainWorldScriptState(v8::Isolate* isolate, NPObject* npObjec
t) | |
| 88 { | |
| 89 ASSERT(npObject->_class == &V8NPObjectClass); | |
| 90 V8NPObject* object = reinterpret_cast<V8NPObject*>(npObject); | |
| 91 LocalDOMWindow* window = object->rootObject; | |
| 92 if (!window || !window->frame()) | |
| 93 return nullptr; | |
| 94 return ScriptState::forMainWorld(window->frame()); | |
| 95 } | |
| 96 | |
| 97 static PassOwnPtr<v8::Local<v8::Value>[]> createValueListFromVariantArgs(v8::Iso
late* isolate, const NPVariant* arguments, uint32_t argumentCount, NPObject* own
er) | |
| 98 { | |
| 99 OwnPtr<v8::Local<v8::Value>[]> argv = adoptArrayPtr(new v8::Local<v8::Value>
[argumentCount]); | |
| 100 for (uint32_t index = 0; index < argumentCount; index++) { | |
| 101 const NPVariant* arg = &arguments[index]; | |
| 102 argv[index] = convertNPVariantToV8Object(isolate, arg, owner); | |
| 103 } | |
| 104 return argv.release(); | |
| 105 } | |
| 106 | |
| 107 // Create an identifier (null terminated utf8 char*) from the NPIdentifier. | |
| 108 static v8::Local<v8::String> npIdentifierToV8Identifier(v8::Isolate* isolate, NP
Identifier name) | |
| 109 { | |
| 110 PrivateIdentifier* identifier = static_cast<PrivateIdentifier*>(name); | |
| 111 if (identifier->isString) | |
| 112 return v8AtomicString(isolate, static_cast<const char*>(identifier->valu
e.string)); | |
| 113 | |
| 114 char buffer[32]; | |
| 115 snprintf(buffer, sizeof(buffer), "%d", identifier->value.number); | |
| 116 return v8AtomicString(isolate, buffer); | |
| 117 } | |
| 118 | |
| 119 NPObject* v8ObjectToNPObject(v8::Local<v8::Object> object) | |
| 120 { | |
| 121 return getInternalField<NPObject, v8DOMWrapperObjectIndex>(object); | |
| 122 } | |
| 123 | |
| 124 bool isWrappedNPObject(v8::Local<v8::Object> object) | |
| 125 { | |
| 126 return object->InternalFieldCount() == npObjectInternalFieldCount | |
| 127 && toWrapperTypeInfo(object) == npObjectTypeInfo(); | |
| 128 } | |
| 129 | |
| 130 NPObject* npCreateV8ScriptObject(v8::Isolate* isolate, NPP npp, v8::Local<v8::Ob
ject> object, LocalDOMWindow* root) | |
| 131 { | |
| 132 // Check to see if this object is already wrapped. | |
| 133 if (isWrappedNPObject(object)) { | |
| 134 NPObject* returnValue = v8ObjectToNPObject(object); | |
| 135 _NPN_RetainObject(returnValue); | |
| 136 return returnValue; | |
| 137 } | |
| 138 | |
| 139 V8NPObjectVector* objectVector = 0; | |
| 140 if (V8PerContextData* perContextData = V8PerContextData::from(object->Creati
onContext())) { | |
| 141 int v8ObjectHash = object->GetIdentityHash(); | |
| 142 ASSERT(v8ObjectHash); | |
| 143 V8NPObjectMap* v8NPObjectMap = perContextData->getV8NPObjectMap(); | |
| 144 V8NPObjectMap::iterator iter = v8NPObjectMap->find(v8ObjectHash); | |
| 145 if (iter != v8NPObjectMap->end()) { | |
| 146 V8NPObjectVector& objects = iter->value; | |
| 147 for (size_t index = 0; index < objects.size(); ++index) { | |
| 148 V8NPObject* v8npObject = objects.at(index); | |
| 149 if (v8npObject->v8Object == object && v8npObject->rootObject ==
root) { | |
| 150 _NPN_RetainObject(&v8npObject->object); | |
| 151 return reinterpret_cast<NPObject*>(v8npObject); | |
| 152 } | |
| 153 } | |
| 154 objectVector = &iter->value; | |
| 155 } else { | |
| 156 objectVector = &v8NPObjectMap->set(v8ObjectHash, V8NPObjectVector())
.storedValue->value; | |
| 157 } | |
| 158 } | |
| 159 | |
| 160 V8NPObject* v8npObject = reinterpret_cast<V8NPObject*>(_NPN_CreateObject(npp
, &V8NPObjectClass)); | |
| 161 // This is uninitialized memory, we need to clear it so that | |
| 162 // Persistent::Reset won't try to Dispose anything bogus. | |
| 163 new (&v8npObject->v8Object) v8::Persistent<v8::Object>(); | |
| 164 v8npObject->v8Object.Reset(isolate, object); | |
| 165 v8npObject->rootObject = root; | |
| 166 | |
| 167 if (objectVector) | |
| 168 objectVector->append(v8npObject); | |
| 169 | |
| 170 return reinterpret_cast<NPObject*>(v8npObject); | |
| 171 } | |
| 172 | |
| 173 V8NPObject* npObjectToV8NPObject(NPObject* npObject) | |
| 174 { | |
| 175 if (npObject->_class != &V8NPObjectClass) | |
| 176 return 0; | |
| 177 V8NPObject* v8NpObject = reinterpret_cast<V8NPObject*>(npObject); | |
| 178 if (v8NpObject->v8Object.IsEmpty()) | |
| 179 return 0; | |
| 180 return v8NpObject; | |
| 181 } | |
| 182 | |
| 183 ScriptWrappable* npObjectToScriptWrappable(NPObject* npObject) | |
| 184 { | |
| 185 return reinterpret_cast<ScriptWrappable*>(npObject); | |
| 186 } | |
| 187 | |
| 188 void disposeUnderlyingV8Object(v8::Isolate* isolate, NPObject* npObject) | |
| 189 { | |
| 190 ASSERT(npObject); | |
| 191 V8NPObject* v8NpObject = npObjectToV8NPObject(npObject); | |
| 192 if (!v8NpObject) | |
| 193 return; | |
| 194 v8::HandleScope scope(isolate); | |
| 195 v8::Local<v8::Object> v8Object = v8::Local<v8::Object>::New(isolate, v8NpObj
ect->v8Object); | |
| 196 ASSERT(!v8Object->CreationContext().IsEmpty()); | |
| 197 if (V8PerContextData* perContextData = V8PerContextData::from(v8Object->Crea
tionContext())) { | |
| 198 V8NPObjectMap* v8NPObjectMap = perContextData->getV8NPObjectMap(); | |
| 199 int v8ObjectHash = v8Object->GetIdentityHash(); | |
| 200 ASSERT(v8ObjectHash); | |
| 201 V8NPObjectMap::iterator iter = v8NPObjectMap->find(v8ObjectHash); | |
| 202 if (iter != v8NPObjectMap->end()) { | |
| 203 V8NPObjectVector& objects = iter->value; | |
| 204 for (size_t index = 0; index < objects.size(); ++index) { | |
| 205 if (objects.at(index) == v8NpObject) { | |
| 206 objects.remove(index); | |
| 207 break; | |
| 208 } | |
| 209 } | |
| 210 if (objects.isEmpty()) | |
| 211 v8NPObjectMap->remove(v8ObjectHash); | |
| 212 } | |
| 213 } | |
| 214 v8NpObject->v8Object.Reset(); | |
| 215 v8NpObject->rootObject = 0; | |
| 216 } | |
| 217 | |
| 218 } // namespace blink | |
| 219 | |
| 220 bool _NPN_Invoke(NPP npp, NPObject* npObject, NPIdentifier methodName, const NPV
ariant* arguments, uint32_t argumentCount, NPVariant* result) | |
| 221 { | |
| 222 if (!npObject) | |
| 223 return false; | |
| 224 | |
| 225 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 226 | |
| 227 V8NPObject* v8NpObject = npObjectToV8NPObject(npObject); | |
| 228 if (!v8NpObject) { | |
| 229 if (npObject->_class->invoke) | |
| 230 return npObject->_class->invoke(npObject, methodName, arguments, arg
umentCount, result); | |
| 231 | |
| 232 VOID_TO_NPVARIANT(*result); | |
| 233 return true; | |
| 234 } | |
| 235 | |
| 236 PrivateIdentifier* identifier = static_cast<PrivateIdentifier*>(methodName); | |
| 237 if (!identifier->isString) | |
| 238 return false; | |
| 239 | |
| 240 if (!strcmp(identifier->value.string, "eval")) { | |
| 241 if (argumentCount != 1) | |
| 242 return false; | |
| 243 if (arguments[0].type != NPVariantType_String) | |
| 244 return false; | |
| 245 return _NPN_Evaluate(npp, npObject, const_cast<NPString*>(&arguments[0].
value.stringValue), result); | |
| 246 } | |
| 247 | |
| 248 // FIXME: should use the plugin's owner frame as the security context. | |
| 249 ScriptState* scriptState = mainWorldScriptState(isolate, npObject); | |
| 250 if (!scriptState) | |
| 251 return false; | |
| 252 | |
| 253 ScriptState::Scope scope(scriptState); | |
| 254 v8::TryCatch tryCatch(isolate); | |
| 255 | |
| 256 v8::Local<v8::Object> v8Object = v8::Local<v8::Object>::New(isolate, v8NpObj
ect->v8Object); | |
| 257 v8::Local<v8::Value> functionObject; | |
| 258 if (!v8Object->Get(scriptState->context(), v8AtomicString(scriptState->isola
te(), identifier->value.string)).ToLocal(&functionObject) || functionObject->IsN
ull()) { | |
| 259 NULL_TO_NPVARIANT(*result); | |
| 260 return false; | |
| 261 } | |
| 262 if (functionObject->IsUndefined()) { | |
| 263 VOID_TO_NPVARIANT(*result); | |
| 264 return false; | |
| 265 } | |
| 266 | |
| 267 LocalFrame* frame = v8NpObject->rootObject->frame(); | |
| 268 ASSERT(frame); | |
| 269 | |
| 270 // Call the function object. | |
| 271 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(functionObj
ect); | |
| 272 OwnPtr<v8::Local<v8::Value>[]> argv = createValueListFromVariantArgs(isolate
, arguments, argumentCount, npObject); | |
| 273 // If we had an error, return false. The spec is a little unclear here, but
says "Returns true if the method was | |
| 274 // successfully invoked". If we get an error return value, was that success
fully invoked? | |
| 275 v8::Local<v8::Value> resultObject; | |
| 276 if (!frame->script().callFunction(function, v8Object, argumentCount, argv.ge
t()).ToLocal(&resultObject)) | |
| 277 return false; | |
| 278 | |
| 279 convertV8ObjectToNPVariant(isolate, resultObject, npObject, result); | |
| 280 return true; | |
| 281 } | |
| 282 | |
| 283 // FIXME: Fix it same as _NPN_Invoke (HandleScope and such). | |
| 284 bool _NPN_InvokeDefault(NPP npp, NPObject* npObject, const NPVariant* arguments,
uint32_t argumentCount, NPVariant* result) | |
| 285 { | |
| 286 if (!npObject) | |
| 287 return false; | |
| 288 | |
| 289 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 290 | |
| 291 V8NPObject* v8NpObject = npObjectToV8NPObject(npObject); | |
| 292 if (!v8NpObject) { | |
| 293 if (npObject->_class->invokeDefault) | |
| 294 return npObject->_class->invokeDefault(npObject, arguments, argument
Count, result); | |
| 295 | |
| 296 VOID_TO_NPVARIANT(*result); | |
| 297 return true; | |
| 298 } | |
| 299 | |
| 300 VOID_TO_NPVARIANT(*result); | |
| 301 | |
| 302 ScriptState* scriptState = mainWorldScriptState(isolate, npObject); | |
| 303 if (!scriptState) | |
| 304 return false; | |
| 305 | |
| 306 ScriptState::Scope scope(scriptState); | |
| 307 v8::TryCatch tryCatch(isolate); | |
| 308 | |
| 309 // Lookup the function object and call it. | |
| 310 v8::Local<v8::Object> functionObject = v8::Local<v8::Object>::New(isolate, v
8NpObject->v8Object); | |
| 311 if (!functionObject->IsFunction()) | |
| 312 return false; | |
| 313 | |
| 314 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(functionObj
ect); | |
| 315 if (function->IsNull()) | |
| 316 return false; | |
| 317 | |
| 318 LocalFrame* frame = v8NpObject->rootObject->frame(); | |
| 319 ASSERT(frame); | |
| 320 | |
| 321 OwnPtr<v8::Local<v8::Value>[]> argv = createValueListFromVariantArgs(isolate
, arguments, argumentCount, npObject); | |
| 322 // If we had an error, return false. The spec is a little unclear here, but
says "Returns true if the method was | |
| 323 // successfully invoked". If we get an error return value, was that success
fully invoked? | |
| 324 v8::Local<v8::Value> resultObject; | |
| 325 if (!frame->script().callFunction(function, functionObject, argumentCount, a
rgv.get()).ToLocal(&resultObject)) | |
| 326 return false; | |
| 327 | |
| 328 convertV8ObjectToNPVariant(isolate, resultObject, npObject, result); | |
| 329 return true; | |
| 330 } | |
| 331 | |
| 332 bool _NPN_Evaluate(NPP npp, NPObject* npObject, NPString* npScript, NPVariant* r
esult) | |
| 333 { | |
| 334 // FIXME: Give the embedder a way to control this. | |
| 335 bool popupsAllowed = false; | |
| 336 return _NPN_EvaluateHelper(npp, popupsAllowed, npObject, npScript, result); | |
| 337 } | |
| 338 | |
| 339 bool _NPN_EvaluateHelper(NPP npp, bool popupsAllowed, NPObject* npObject, NPStri
ng* npScript, NPVariant* result) | |
| 340 { | |
| 341 VOID_TO_NPVARIANT(*result); | |
| 342 if (ScriptForbiddenScope::isScriptForbidden()) | |
| 343 return false; | |
| 344 | |
| 345 if (!npObject) | |
| 346 return false; | |
| 347 | |
| 348 V8NPObject* v8NpObject = npObjectToV8NPObject(npObject); | |
| 349 if (!v8NpObject) | |
| 350 return false; | |
| 351 | |
| 352 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 353 ScriptState* scriptState = mainWorldScriptState(isolate, npObject); | |
| 354 if (!scriptState) | |
| 355 return false; | |
| 356 | |
| 357 ScriptState::Scope scope(scriptState); | |
| 358 v8::TryCatch tryCatch(isolate); | |
| 359 | |
| 360 // FIXME: Is this branch still needed after switching to using UserGestureIn
dicator? | |
| 361 String filename; | |
| 362 if (!popupsAllowed) | |
| 363 filename = "npscript"; | |
| 364 | |
| 365 LocalFrame* frame = v8NpObject->rootObject->frame(); | |
| 366 ASSERT(frame); | |
| 367 | |
| 368 String script = String::fromUTF8(npScript->UTF8Characters, npScript->UTF8Len
gth); | |
| 369 | |
| 370 UserGestureIndicator gestureIndicator(popupsAllowed ? DefinitelyProcessingNe
wUserGesture : PossiblyProcessingUserGesture); | |
| 371 v8::Local<v8::Value> v8result = frame->script().executeScriptAndReturnValue(
scriptState->context(), ScriptSourceCode(script, KURL(ParsedURLString, filename)
)); | |
| 372 | |
| 373 if (v8result.IsEmpty()) | |
| 374 return false; | |
| 375 | |
| 376 if (_NPN_IsAlive(npObject)) | |
| 377 convertV8ObjectToNPVariant(isolate, v8result, npObject, result); | |
| 378 return true; | |
| 379 } | |
| 380 | |
| 381 bool _NPN_GetProperty(NPP npp, NPObject* npObject, NPIdentifier propertyName, NP
Variant* result) | |
| 382 { | |
| 383 if (!npObject) | |
| 384 return false; | |
| 385 | |
| 386 if (V8NPObject* object = npObjectToV8NPObject(npObject)) { | |
| 387 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 388 ScriptState* scriptState = mainWorldScriptState(isolate, npObject); | |
| 389 if (!scriptState) | |
| 390 return false; | |
| 391 | |
| 392 ScriptState::Scope scope(scriptState); | |
| 393 v8::TryCatch tryCatch(isolate); | |
| 394 | |
| 395 v8::Local<v8::Object> obj = v8::Local<v8::Object>::New(isolate, object->
v8Object); | |
| 396 v8::Local<v8::Value> v8result; | |
| 397 if (!obj->Get(scriptState->context(), npIdentifierToV8Identifier(scriptS
tate->isolate(), propertyName)).ToLocal(&v8result)) | |
| 398 return false; | |
| 399 | |
| 400 convertV8ObjectToNPVariant(isolate, v8result, npObject, result); | |
| 401 return true; | |
| 402 } | |
| 403 | |
| 404 if (npObject->_class->hasProperty && npObject->_class->getProperty) { | |
| 405 if (npObject->_class->hasProperty(npObject, propertyName)) | |
| 406 return npObject->_class->getProperty(npObject, propertyName, result)
; | |
| 407 } | |
| 408 | |
| 409 VOID_TO_NPVARIANT(*result); | |
| 410 return false; | |
| 411 } | |
| 412 | |
| 413 bool _NPN_SetProperty(NPP npp, NPObject* npObject, NPIdentifier propertyName, co
nst NPVariant* value) | |
| 414 { | |
| 415 if (!npObject) | |
| 416 return false; | |
| 417 | |
| 418 if (V8NPObject* object = npObjectToV8NPObject(npObject)) { | |
| 419 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 420 ScriptState* scriptState = mainWorldScriptState(isolate, npObject); | |
| 421 if (!scriptState) | |
| 422 return false; | |
| 423 | |
| 424 ScriptState::Scope scope(scriptState); | |
| 425 v8::TryCatch tryCatch(isolate); | |
| 426 | |
| 427 v8::Local<v8::Object> obj = v8::Local<v8::Object>::New(isolate, object->
v8Object); | |
| 428 return v8CallBoolean(obj->Set(scriptState->context(), npIdentifierToV8Id
entifier(isolate, propertyName), convertNPVariantToV8Object(isolate, value, obje
ct->rootObject->frame()->script().windowScriptNPObject()))); | |
| 429 } | |
| 430 | |
| 431 if (npObject->_class->setProperty) | |
| 432 return npObject->_class->setProperty(npObject, propertyName, value); | |
| 433 | |
| 434 return false; | |
| 435 } | |
| 436 | |
| 437 bool _NPN_RemoveProperty(NPP npp, NPObject* npObject, NPIdentifier propertyName) | |
| 438 { | |
| 439 if (!npObject) | |
| 440 return false; | |
| 441 | |
| 442 V8NPObject* object = npObjectToV8NPObject(npObject); | |
| 443 if (!object) | |
| 444 return false; | |
| 445 | |
| 446 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 447 ScriptState* scriptState = mainWorldScriptState(isolate, npObject); | |
| 448 if (!scriptState) | |
| 449 return false; | |
| 450 ScriptState::Scope scope(scriptState); | |
| 451 v8::TryCatch tryCatch(isolate); | |
| 452 | |
| 453 v8::Local<v8::Object> obj = v8::Local<v8::Object>::New(isolate, object->v8Ob
ject); | |
| 454 // FIXME: Verify that setting to undefined is right. | |
| 455 return v8CallBoolean(obj->Set(scriptState->context(), npIdentifierToV8Identi
fier(isolate, propertyName), v8::Undefined(isolate))); | |
| 456 } | |
| 457 | |
| 458 bool _NPN_HasProperty(NPP npp, NPObject* npObject, NPIdentifier propertyName) | |
| 459 { | |
| 460 if (!npObject) | |
| 461 return false; | |
| 462 | |
| 463 if (V8NPObject* object = npObjectToV8NPObject(npObject)) { | |
| 464 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 465 ScriptState* scriptState = mainWorldScriptState(isolate, npObject); | |
| 466 if (!scriptState) | |
| 467 return false; | |
| 468 ScriptState::Scope scope(scriptState); | |
| 469 v8::TryCatch tryCatch(isolate); | |
| 470 | |
| 471 v8::Local<v8::Object> obj = v8::Local<v8::Object>::New(scriptState->isol
ate(), object->v8Object); | |
| 472 return v8CallBoolean(obj->Has(scriptState->context(), npIdentifierToV8Id
entifier(scriptState->isolate(), propertyName))); | |
| 473 } | |
| 474 | |
| 475 if (npObject->_class->hasProperty) | |
| 476 return npObject->_class->hasProperty(npObject, propertyName); | |
| 477 return false; | |
| 478 } | |
| 479 | |
| 480 bool _NPN_HasMethod(NPP npp, NPObject* npObject, NPIdentifier methodName) | |
| 481 { | |
| 482 if (!npObject) | |
| 483 return false; | |
| 484 | |
| 485 if (V8NPObject* object = npObjectToV8NPObject(npObject)) { | |
| 486 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 487 ScriptState* scriptState = mainWorldScriptState(isolate, npObject); | |
| 488 if (!scriptState) | |
| 489 return false; | |
| 490 ScriptState::Scope scope(scriptState); | |
| 491 v8::TryCatch tryCatch(isolate); | |
| 492 | |
| 493 v8::Local<v8::Object> obj = v8::Local<v8::Object>::New(isolate, object->
v8Object); | |
| 494 v8::Local<v8::Value> prop; | |
| 495 if (!obj->Get(scriptState->context(), npIdentifierToV8Identifier(scriptS
tate->isolate(), methodName)).ToLocal(&prop)) | |
| 496 return false; | |
| 497 return prop->IsFunction(); | |
| 498 } | |
| 499 | |
| 500 if (npObject->_class->hasMethod) | |
| 501 return npObject->_class->hasMethod(npObject, methodName); | |
| 502 return false; | |
| 503 } | |
| 504 | |
| 505 void _NPN_SetException(NPObject* npObject, const NPUTF8 *message) | |
| 506 { | |
| 507 if (!npObject || !npObjectToV8NPObject(npObject)) { | |
| 508 // We won't be able to find a proper scope for this exception, so just t
hrow it. | |
| 509 // This is consistent with JSC, which throws a global exception all the
time. | |
| 510 V8ThrowException::throwGeneralError(v8::Isolate::GetCurrent(), message); | |
| 511 return; | |
| 512 } | |
| 513 | |
| 514 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 515 ScriptState* scriptState = mainWorldScriptState(isolate, npObject); | |
| 516 if (!scriptState) | |
| 517 return; | |
| 518 | |
| 519 ScriptState::Scope scope(scriptState); | |
| 520 v8::TryCatch tryCatch(isolate); | |
| 521 | |
| 522 V8ThrowException::throwGeneralError(isolate, message); | |
| 523 } | |
| 524 | |
| 525 bool _NPN_Enumerate(NPP npp, NPObject* npObject, NPIdentifier** identifier, uint
32_t* count) | |
| 526 { | |
| 527 if (!npObject) | |
| 528 return false; | |
| 529 | |
| 530 if (V8NPObject* object = npObjectToV8NPObject(npObject)) { | |
| 531 ScriptState* scriptState = mainWorldScriptState(v8::Isolate::GetCurrent(
), npObject); | |
| 532 if (!scriptState) | |
| 533 return false; | |
| 534 ScriptState::Scope scope(scriptState); | |
| 535 v8::TryCatch tryCatch(scriptState->isolate()); | |
| 536 | |
| 537 v8::Local<v8::Object> obj = v8::Local<v8::Object>::New(scriptState->isol
ate(), object->v8Object); | |
| 538 | |
| 539 // FIXME: http://b/issue?id=1210340: Use a v8::Object::Keys() method whe
n it exists, instead of evaluating javascript. | |
| 540 | |
| 541 // FIXME: Figure out how to cache this helper function. Run a helper fu
nction that collects the properties | |
| 542 // on the object into an array. | |
| 543 const char enumeratorCode[] = | |
| 544 "(function (obj) {" | |
| 545 " var props = [];" | |
| 546 " for (var prop in obj) {" | |
| 547 " props[props.length] = prop;" | |
| 548 " }" | |
| 549 " return props;" | |
| 550 "});"; | |
| 551 v8::Local<v8::String> source = v8AtomicString(scriptState->isolate(), en
umeratorCode); | |
| 552 v8::Local<v8::Value> result; | |
| 553 if (!V8ScriptRunner::compileAndRunInternalScript(source, scriptState->is
olate()).ToLocal(&result)) | |
| 554 return false; | |
| 555 ASSERT(result->IsFunction()); | |
| 556 v8::Local<v8::Function> enumerator = v8::Local<v8::Function>::Cast(resul
t); | |
| 557 v8::Local<v8::Value> argv[] = { obj }; | |
| 558 v8::Local<v8::Value> propsObj; | |
| 559 if (!V8ScriptRunner::callInternalFunction(enumerator, v8::Local<v8::Obje
ct>::Cast(result), WTF_ARRAY_LENGTH(argv), argv, scriptState->isolate()).ToLocal
(&propsObj)) | |
| 560 return false; | |
| 561 | |
| 562 // Convert the results into an array of NPIdentifiers. | |
| 563 v8::Local<v8::Array> props = v8::Local<v8::Array>::Cast(propsObj); | |
| 564 *count = props->Length(); | |
| 565 *identifier = static_cast<NPIdentifier*>(calloc(*count, sizeof(NPIdentif
ier))); | |
| 566 for (uint32_t i = 0; i < *count; ++i) { | |
| 567 v8::Local<v8::Value> name; | |
| 568 if (!props->Get(scriptState->context(), v8::Integer::New(scriptState
->isolate(), i)).ToLocal(&name)) | |
| 569 return false; | |
| 570 (*identifier)[i] = getStringIdentifier(v8::Local<v8::String>::Cast(n
ame)); | |
| 571 } | |
| 572 return true; | |
| 573 } | |
| 574 | |
| 575 if (NP_CLASS_STRUCT_VERSION_HAS_ENUM(npObject->_class) && npObject->_class->
enumerate) | |
| 576 return npObject->_class->enumerate(npObject, identifier, count); | |
| 577 | |
| 578 return false; | |
| 579 } | |
| 580 | |
| 581 bool _NPN_Construct(NPP npp, NPObject* npObject, const NPVariant* arguments, uin
t32_t argumentCount, NPVariant* result) | |
| 582 { | |
| 583 if (!npObject) | |
| 584 return false; | |
| 585 | |
| 586 if (V8NPObject* object = npObjectToV8NPObject(npObject)) { | |
| 587 ScriptState* scriptState = mainWorldScriptState(v8::Isolate::GetCurrent(
), npObject); | |
| 588 if (!scriptState) | |
| 589 return false; | |
| 590 ScriptState::Scope scope(scriptState); | |
| 591 v8::TryCatch tryCatch(scriptState->isolate()); | |
| 592 | |
| 593 // Lookup the constructor function. | |
| 594 v8::Local<v8::Object> ctorObj = v8::Local<v8::Object>::New(scriptState->
isolate(), object->v8Object); | |
| 595 if (!ctorObj->IsFunction()) | |
| 596 return false; | |
| 597 | |
| 598 // Call the constructor. | |
| 599 v8::Local<v8::Value> resultObject; | |
| 600 v8::Local<v8::Function> ctor = v8::Local<v8::Function>::Cast(ctorObj); | |
| 601 if (ctor->IsNull()) | |
| 602 return false; | |
| 603 | |
| 604 LocalFrame* frame = object->rootObject->frame(); | |
| 605 ASSERT(frame); | |
| 606 OwnPtr<v8::Local<v8::Value>[]> argv = createValueListFromVariantArgs(scr
iptState->isolate(), arguments, argumentCount, npObject); | |
| 607 if (!V8ObjectConstructor::newInstanceInDocument(scriptState->isolate(),
ctor, argumentCount, argv.get(), frame ? frame->document() : 0).ToLocal(&resultO
bject)) | |
| 608 return false; | |
| 609 | |
| 610 convertV8ObjectToNPVariant(scriptState->isolate(), resultObject, npObjec
t, result); | |
| 611 return true; | |
| 612 } | |
| 613 | |
| 614 if (NP_CLASS_STRUCT_VERSION_HAS_CTOR(npObject->_class) && npObject->_class->
construct) | |
| 615 return npObject->_class->construct(npObject, arguments, argumentCount, r
esult); | |
| 616 | |
| 617 return false; | |
| 618 } | |
| OLD | NEW |