| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2008, 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2008, 2009 Google Inc. All rights reserved. |
| 3 * Copyright (C) 2009 Apple Inc. All rights reserved. | 3 * Copyright (C) 2009 Apple Inc. All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
| 7 * met: | 7 * met: |
| 8 * | 8 * |
| 9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 { | 245 { |
| 246 ScriptableDocumentParser* parser = m_frame->document()->scriptableDocumentPa
rser(); | 246 ScriptableDocumentParser* parser = m_frame->document()->scriptableDocumentPa
rser(); |
| 247 if (parser) | 247 if (parser) |
| 248 return parser->textPosition(); | 248 return parser->textPosition(); |
| 249 return TextPosition::minimumPosition(); | 249 return TextPosition::minimumPosition(); |
| 250 } | 250 } |
| 251 | 251 |
| 252 // Create a V8 object with an interceptor of NPObjectPropertyGetter. | 252 // Create a V8 object with an interceptor of NPObjectPropertyGetter. |
| 253 void ScriptController::bindToWindowObject(LocalFrame* frame, const String& key,
NPObject* object) | 253 void ScriptController::bindToWindowObject(LocalFrame* frame, const String& key,
NPObject* object) |
| 254 { | 254 { |
| 255 v8::HandleScope handleScope(m_isolate); | 255 ScriptState* scriptState = ScriptState::forMainWorld(frame); |
| 256 | 256 if (scriptState->contextIsEmpty()) |
| 257 v8::Handle<v8::Context> v8Context = toV8Context(frame, DOMWrapperWorld::main
World()); | |
| 258 if (v8Context.IsEmpty()) | |
| 259 return; | 257 return; |
| 260 | 258 |
| 261 v8::Context::Scope scope(v8Context); | 259 ScriptState::Scope scope(scriptState); |
| 262 | |
| 263 v8::Handle<v8::Object> value = createV8ObjectForNPObject(object, 0, m_isolat
e); | 260 v8::Handle<v8::Object> value = createV8ObjectForNPObject(object, 0, m_isolat
e); |
| 264 | 261 |
| 265 // Attach to the global object. | 262 // Attach to the global object. |
| 266 v8::Handle<v8::Object> global = v8Context->Global(); | 263 scriptState->context()->Global()->Set(v8String(m_isolate, key), value); |
| 267 global->Set(v8String(m_isolate, key), value); | |
| 268 } | 264 } |
| 269 | 265 |
| 270 void ScriptController::enableEval() | 266 void ScriptController::enableEval() |
| 271 { | 267 { |
| 272 if (!m_windowShell->isContextInitialized()) | 268 if (!m_windowShell->isContextInitialized()) |
| 273 return; | 269 return; |
| 274 v8::HandleScope handleScope(m_isolate); | 270 v8::HandleScope handleScope(m_isolate); |
| 275 m_windowShell->context()->AllowCodeGenerationFromStrings(true); | 271 m_windowShell->context()->AllowCodeGenerationFromStrings(true); |
| 276 } | 272 } |
| 277 | 273 |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 } | 352 } |
| 357 | 353 |
| 358 static NPObject* createNoScriptObject() | 354 static NPObject* createNoScriptObject() |
| 359 { | 355 { |
| 360 notImplemented(); | 356 notImplemented(); |
| 361 return 0; | 357 return 0; |
| 362 } | 358 } |
| 363 | 359 |
| 364 static NPObject* createScriptObject(LocalFrame* frame, v8::Isolate* isolate) | 360 static NPObject* createScriptObject(LocalFrame* frame, v8::Isolate* isolate) |
| 365 { | 361 { |
| 366 v8::HandleScope handleScope(isolate); | 362 ScriptState* scriptState = ScriptState::forMainWorld(frame); |
| 367 v8::Handle<v8::Context> v8Context = toV8Context(frame, DOMWrapperWorld::main
World()); | 363 if (scriptState->contextIsEmpty()) |
| 368 if (v8Context.IsEmpty()) | |
| 369 return createNoScriptObject(); | 364 return createNoScriptObject(); |
| 370 | 365 |
| 371 v8::Context::Scope scope(v8Context); | 366 ScriptState::Scope scope(scriptState); |
| 372 DOMWindow* window = frame->domWindow(); | 367 DOMWindow* window = frame->domWindow(); |
| 373 v8::Handle<v8::Value> global = toV8(window, v8Context->Global(), v8Context->
GetIsolate()); | 368 v8::Handle<v8::Value> global = toV8(window, scriptState->context()->Global()
, scriptState->isolate()); |
| 374 ASSERT(global->IsObject()); | 369 ASSERT(global->IsObject()); |
| 375 | |
| 376 return npCreateV8ScriptObject(0, v8::Handle<v8::Object>::Cast(global), windo
w, isolate); | 370 return npCreateV8ScriptObject(0, v8::Handle<v8::Object>::Cast(global), windo
w, isolate); |
| 377 } | 371 } |
| 378 | 372 |
| 379 NPObject* ScriptController::windowScriptNPObject() | 373 NPObject* ScriptController::windowScriptNPObject() |
| 380 { | 374 { |
| 381 if (m_windowScriptNPObject) | 375 if (m_windowScriptNPObject) |
| 382 return m_windowScriptNPObject; | 376 return m_windowScriptNPObject; |
| 383 | 377 |
| 384 if (canExecuteScripts(NotAboutToExecuteScript)) { | 378 if (canExecuteScripts(NotAboutToExecuteScript)) { |
| 385 // JavaScript is enabled, so there is a JavaScript window object. | 379 // JavaScript is enabled, so there is a JavaScript window object. |
| 386 // Return an NPObject bound to the window object. | 380 // Return an NPObject bound to the window object. |
| 387 m_windowScriptNPObject = createScriptObject(m_frame, m_isolate); | 381 m_windowScriptNPObject = createScriptObject(m_frame, m_isolate); |
| 388 _NPN_RegisterObject(m_windowScriptNPObject, 0); | 382 _NPN_RegisterObject(m_windowScriptNPObject, 0); |
| 389 } else { | 383 } else { |
| 390 // JavaScript is not enabled, so we cannot bind the NPObject to the | 384 // JavaScript is not enabled, so we cannot bind the NPObject to the |
| 391 // JavaScript window object. Instead, we create an NPObject of a | 385 // JavaScript window object. Instead, we create an NPObject of a |
| 392 // different class, one which is not bound to a JavaScript object. | 386 // different class, one which is not bound to a JavaScript object. |
| 393 m_windowScriptNPObject = createNoScriptObject(); | 387 m_windowScriptNPObject = createNoScriptObject(); |
| 394 } | 388 } |
| 395 return m_windowScriptNPObject; | 389 return m_windowScriptNPObject; |
| 396 } | 390 } |
| 397 | 391 |
| 398 NPObject* ScriptController::createScriptObjectForPluginElement(HTMLPlugInElement
* plugin) | 392 NPObject* ScriptController::createScriptObjectForPluginElement(HTMLPlugInElement
* plugin) |
| 399 { | 393 { |
| 400 // Can't create NPObjects when JavaScript is disabled. | 394 // Can't create NPObjects when JavaScript is disabled. |
| 401 if (!canExecuteScripts(NotAboutToExecuteScript)) | 395 if (!canExecuteScripts(NotAboutToExecuteScript)) |
| 402 return createNoScriptObject(); | 396 return createNoScriptObject(); |
| 403 | 397 |
| 404 v8::HandleScope handleScope(m_isolate); | 398 ScriptState* scriptState = ScriptState::forMainWorld(m_frame); |
| 405 v8::Handle<v8::Context> v8Context = toV8Context(m_frame, DOMWrapperWorld::ma
inWorld()); | 399 if (scriptState->contextIsEmpty()) |
| 406 if (v8Context.IsEmpty()) | |
| 407 return createNoScriptObject(); | 400 return createNoScriptObject(); |
| 408 v8::Context::Scope scope(v8Context); | |
| 409 | 401 |
| 402 ScriptState::Scope scope(scriptState); |
| 410 DOMWindow* window = m_frame->domWindow(); | 403 DOMWindow* window = m_frame->domWindow(); |
| 411 v8::Handle<v8::Value> v8plugin = toV8(plugin, v8Context->Global(), v8Context
->GetIsolate()); | 404 v8::Handle<v8::Value> v8plugin = toV8(plugin, scriptState->context()->Global
(), scriptState->isolate()); |
| 412 if (!v8plugin->IsObject()) | 405 if (!v8plugin->IsObject()) |
| 413 return createNoScriptObject(); | 406 return createNoScriptObject(); |
| 414 | 407 |
| 415 return npCreateV8ScriptObject(0, v8::Handle<v8::Object>::Cast(v8plugin), win
dow, v8Context->GetIsolate()); | 408 return npCreateV8ScriptObject(0, v8::Handle<v8::Object>::Cast(v8plugin), win
dow, scriptState->isolate()); |
| 416 } | 409 } |
| 417 | 410 |
| 418 void ScriptController::clearWindowShell() | 411 void ScriptController::clearWindowShell() |
| 419 { | 412 { |
| 420 double start = currentTime(); | 413 double start = currentTime(); |
| 421 // V8 binding expects ScriptController::clearWindowShell only be called | 414 // V8 binding expects ScriptController::clearWindowShell only be called |
| 422 // when a frame is loading a new page. This creates a new context for the ne
w page. | 415 // when a frame is loading a new page. This creates a new context for the ne
w page. |
| 423 m_windowShell->clearForNavigation(); | 416 m_windowShell->clearForNavigation(); |
| 424 for (IsolatedWorldMap::iterator iter = m_isolatedWorlds.begin(); iter != m_i
solatedWorlds.end(); ++iter) | 417 for (IsolatedWorldMap::iterator iter = m_isolatedWorlds.begin(); iter != m_i
solatedWorlds.end(); ++iter) |
| 425 iter->value->clearForNavigation(); | 418 iter->value->clearForNavigation(); |
| 426 clearScriptObjects(); | 419 clearScriptObjects(); |
| 427 blink::Platform::current()->histogramCustomCounts("WebCore.ScriptController.
clearWindowShell", (currentTime() - start) * 1000, 0, 10000, 50); | 420 blink::Platform::current()->histogramCustomCounts("WebCore.ScriptController.
clearWindowShell", (currentTime() - start) * 1000, 0, 10000, 50); |
| 428 } | 421 } |
| 429 | 422 |
| 430 void ScriptController::setCaptureCallStackForUncaughtExceptions(bool value) | 423 void ScriptController::setCaptureCallStackForUncaughtExceptions(bool value) |
| 431 { | 424 { |
| 432 v8::V8::SetCaptureStackTraceForUncaughtExceptions(value, ScriptCallStack::ma
xCallStackSizeToCapture, stackTraceOptions); | 425 v8::V8::SetCaptureStackTraceForUncaughtExceptions(value, ScriptCallStack::ma
xCallStackSizeToCapture, stackTraceOptions); |
| 433 } | 426 } |
| 434 | 427 |
| 435 void ScriptController::collectIsolatedContexts(Vector<std::pair<ScriptState*, Se
curityOrigin*> >& result) | 428 void ScriptController::collectIsolatedContexts(Vector<std::pair<ScriptState*, Se
curityOrigin*> >& result) |
| 436 { | 429 { |
| 437 v8::HandleScope handleScope(m_isolate); | |
| 438 for (IsolatedWorldMap::iterator it = m_isolatedWorlds.begin(); it != m_isola
tedWorlds.end(); ++it) { | 430 for (IsolatedWorldMap::iterator it = m_isolatedWorlds.begin(); it != m_isola
tedWorlds.end(); ++it) { |
| 439 V8WindowShell* isolatedWorldShell = it->value.get(); | 431 V8WindowShell* isolatedWorldShell = it->value.get(); |
| 440 SecurityOrigin* origin = isolatedWorldShell->world().isolatedWorldSecuri
tyOrigin(); | 432 SecurityOrigin* origin = isolatedWorldShell->world().isolatedWorldSecuri
tyOrigin(); |
| 441 if (!origin) | 433 if (!origin) |
| 442 continue; | 434 continue; |
| 443 v8::Local<v8::Context> v8Context = isolatedWorldShell->context(); | 435 if (!isolatedWorldShell->isContextInitialized()) |
| 444 if (v8Context.IsEmpty()) | |
| 445 continue; | 436 continue; |
| 446 ScriptState* scriptState = ScriptState::from(v8Context); | 437 result.append(std::pair<ScriptState*, SecurityOrigin*>(isolatedWorldShel
l->scriptState(), origin)); |
| 447 result.append(std::pair<ScriptState*, SecurityOrigin*>(scriptState, orig
in)); | |
| 448 } | 438 } |
| 449 } | 439 } |
| 450 | 440 |
| 451 bool ScriptController::setContextDebugId(int debugId) | 441 bool ScriptController::setContextDebugId(int debugId) |
| 452 { | 442 { |
| 453 ASSERT(debugId > 0); | 443 ASSERT(debugId > 0); |
| 454 if (!m_windowShell->isContextInitialized()) | 444 if (!m_windowShell->isContextInitialized()) |
| 455 return false; | 445 return false; |
| 456 v8::HandleScope scope(m_isolate); | 446 v8::HandleScope scope(m_isolate); |
| 457 v8::Local<v8::Context> context = m_windowShell->context(); | 447 v8::Local<v8::Context> context = m_windowShell->context(); |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 566 | 556 |
| 567 ScriptValue ScriptController::evaluateScriptInMainWorld(const ScriptSourceCode&
sourceCode, AccessControlStatus corsStatus, ExecuteScriptPolicy policy) | 557 ScriptValue ScriptController::evaluateScriptInMainWorld(const ScriptSourceCode&
sourceCode, AccessControlStatus corsStatus, ExecuteScriptPolicy policy) |
| 568 { | 558 { |
| 569 if (policy == DoNotExecuteScriptWhenScriptsDisabled && !canExecuteScripts(Ab
outToExecuteScript)) | 559 if (policy == DoNotExecuteScriptWhenScriptsDisabled && !canExecuteScripts(Ab
outToExecuteScript)) |
| 570 return ScriptValue(); | 560 return ScriptValue(); |
| 571 | 561 |
| 572 String sourceURL = sourceCode.url(); | 562 String sourceURL = sourceCode.url(); |
| 573 const String* savedSourceURL = m_sourceURL; | 563 const String* savedSourceURL = m_sourceURL; |
| 574 m_sourceURL = &sourceURL; | 564 m_sourceURL = &sourceURL; |
| 575 | 565 |
| 576 v8::HandleScope handleScope(m_isolate); | 566 ScriptState* scriptState = ScriptState::forMainWorld(m_frame); |
| 577 v8::Handle<v8::Context> v8Context = toV8Context(m_frame, DOMWrapperWorld::ma
inWorld()); | 567 if (scriptState->contextIsEmpty()) |
| 578 if (v8Context.IsEmpty()) | |
| 579 return ScriptValue(); | 568 return ScriptValue(); |
| 580 v8::Context::Scope scope(v8Context); | 569 |
| 570 ScriptState::Scope scope(scriptState); |
| 581 | 571 |
| 582 RefPtr<LocalFrame> protect(m_frame); | 572 RefPtr<LocalFrame> protect(m_frame); |
| 583 if (m_frame->loader().stateMachine()->isDisplayingInitialEmptyDocument()) | 573 if (m_frame->loader().stateMachine()->isDisplayingInitialEmptyDocument()) |
| 584 m_frame->loader().didAccessInitialDocument(); | 574 m_frame->loader().didAccessInitialDocument(); |
| 585 | 575 |
| 586 OwnPtr<ScriptSourceCode> maybeProcessedSourceCode = InspectorInstrumentatio
n::preprocess(m_frame, sourceCode); | 576 OwnPtr<ScriptSourceCode> maybeProcessedSourceCode = InspectorInstrumentatio
n::preprocess(m_frame, sourceCode); |
| 587 const ScriptSourceCode& sourceCodeToCompile = maybeProcessedSourceCode ? *ma
ybeProcessedSourceCode : sourceCode; | 577 const ScriptSourceCode& sourceCodeToCompile = maybeProcessedSourceCode ? *ma
ybeProcessedSourceCode : sourceCode; |
| 588 | 578 |
| 589 v8::Local<v8::Value> object = executeScriptAndReturnValue(v8Context, sourceC
odeToCompile, corsStatus); | 579 v8::Local<v8::Value> object = executeScriptAndReturnValue(scriptState->conte
xt(), sourceCodeToCompile, corsStatus); |
| 590 m_sourceURL = savedSourceURL; | 580 m_sourceURL = savedSourceURL; |
| 591 | 581 |
| 592 if (object.IsEmpty()) | 582 if (object.IsEmpty()) |
| 593 return ScriptValue(); | 583 return ScriptValue(); |
| 594 | 584 |
| 595 return ScriptValue(ScriptState::from(v8Context), object); | 585 return ScriptValue(scriptState, object); |
| 596 } | 586 } |
| 597 | 587 |
| 598 void ScriptController::executeScriptInIsolatedWorld(int worldID, const Vector<Sc
riptSourceCode>& sources, int extensionGroup, Vector<ScriptValue>* results) | 588 void ScriptController::executeScriptInIsolatedWorld(int worldID, const Vector<Sc
riptSourceCode>& sources, int extensionGroup, Vector<ScriptValue>* results) |
| 599 { | 589 { |
| 600 ASSERT(worldID > 0); | 590 ASSERT(worldID > 0); |
| 601 | 591 |
| 602 v8::HandleScope handleScope(m_isolate); | |
| 603 RefPtr<DOMWrapperWorld> world = DOMWrapperWorld::ensureIsolatedWorld(worldID
, extensionGroup); | 592 RefPtr<DOMWrapperWorld> world = DOMWrapperWorld::ensureIsolatedWorld(worldID
, extensionGroup); |
| 604 V8WindowShell* isolatedWorldShell = windowShell(*world); | 593 V8WindowShell* isolatedWorldShell = windowShell(*world); |
| 605 if (!isolatedWorldShell->isContextInitialized()) | 594 if (!isolatedWorldShell->isContextInitialized()) |
| 606 return; | 595 return; |
| 607 | 596 |
| 608 v8::Local<v8::Context> context = isolatedWorldShell->context(); | 597 ScriptState* scriptState = isolatedWorldShell->scriptState(); |
| 609 v8::Context::Scope contextScope(context); | 598 ScriptState::Scope scope(scriptState); |
| 610 v8::Local<v8::Array> resultArray = v8::Array::New(m_isolate, sources.size())
; | 599 v8::Local<v8::Array> resultArray = v8::Array::New(m_isolate, sources.size())
; |
| 611 | 600 |
| 612 for (size_t i = 0; i < sources.size(); ++i) { | 601 for (size_t i = 0; i < sources.size(); ++i) { |
| 613 v8::Local<v8::Value> evaluationResult = executeScriptAndReturnValue(cont
ext, sources[i]); | 602 v8::Local<v8::Value> evaluationResult = executeScriptAndReturnValue(scri
ptState->context(), sources[i]); |
| 614 if (evaluationResult.IsEmpty()) | 603 if (evaluationResult.IsEmpty()) |
| 615 evaluationResult = v8::Local<v8::Value>::New(m_isolate, v8::Undefine
d(m_isolate)); | 604 evaluationResult = v8::Local<v8::Value>::New(m_isolate, v8::Undefine
d(m_isolate)); |
| 616 resultArray->Set(i, evaluationResult); | 605 resultArray->Set(i, evaluationResult); |
| 617 } | 606 } |
| 618 | 607 |
| 619 if (results) { | 608 if (results) { |
| 620 for (size_t i = 0; i < resultArray->Length(); ++i) | 609 for (size_t i = 0; i < resultArray->Length(); ++i) |
| 621 results->append(ScriptValue(ScriptState::from(context), resultArray-
>Get(i))); | 610 results->append(ScriptValue(scriptState, resultArray->Get(i))); |
| 622 } | 611 } |
| 623 } | 612 } |
| 624 | 613 |
| 625 } // namespace WebCore | 614 } // namespace WebCore |
| OLD | NEW |