| 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 495 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 // We need to hold onto the LocalFrame here because executing script can | 506 // We need to hold onto the LocalFrame here because executing script can |
| 507 // destroy the frame. | 507 // destroy the frame. |
| 508 RefPtr<LocalFrame> protector(m_frame); | 508 RefPtr<LocalFrame> protector(m_frame); |
| 509 RefPtrWillBeRawPtr<Document> ownerDocument(m_frame->document()); | 509 RefPtrWillBeRawPtr<Document> ownerDocument(m_frame->document()); |
| 510 | 510 |
| 511 const int javascriptSchemeLength = sizeof("javascript:") - 1; | 511 const int javascriptSchemeLength = sizeof("javascript:") - 1; |
| 512 | 512 |
| 513 bool locationChangeBefore = m_frame->navigationScheduler().locationChangePen
ding(); | 513 bool locationChangeBefore = m_frame->navigationScheduler().locationChangePen
ding(); |
| 514 | 514 |
| 515 String decodedURL = decodeURLEscapeSequences(url.string()); | 515 String decodedURL = decodeURLEscapeSequences(url.string()); |
| 516 v8::HandleScope handleScope(m_isolate); | 516 ScriptValue result = evaluateScriptInMainWorld(ScriptSourceCode(decodedURL.s
ubstring(javascriptSchemeLength)), NotSharableCrossOrigin, DoNotExecuteScriptWhe
nScriptsDisabled); |
| 517 v8::Local<v8::Value> result = evaluateScriptInMainWorld(ScriptSourceCode(dec
odedURL.substring(javascriptSchemeLength)), NotSharableCrossOrigin, DoNotExecute
ScriptWhenScriptsDisabled); | |
| 518 | 517 |
| 519 // If executing script caused this frame to be removed from the page, we | 518 // If executing script caused this frame to be removed from the page, we |
| 520 // don't want to try to replace its document! | 519 // don't want to try to replace its document! |
| 521 if (!m_frame->page()) | 520 if (!m_frame->page()) |
| 522 return true; | 521 return true; |
| 523 | 522 |
| 524 if (result.IsEmpty() || !result->IsString()) | 523 String scriptResult; |
| 524 if (!result.toString(scriptResult)) |
| 525 return true; | 525 return true; |
| 526 String scriptResult = toCoreString(v8::Handle<v8::String>::Cast(result)); | |
| 527 | 526 |
| 528 // We're still in a frame, so there should be a DocumentLoader. | 527 // We're still in a frame, so there should be a DocumentLoader. |
| 529 ASSERT(m_frame->document()->loader()); | 528 ASSERT(m_frame->document()->loader()); |
| 529 |
| 530 if (!locationChangeBefore && m_frame->navigationScheduler().locationChangePe
nding()) | 530 if (!locationChangeBefore && m_frame->navigationScheduler().locationChangePe
nding()) |
| 531 return true; | 531 return true; |
| 532 | 532 |
| 533 // DocumentWriter::replaceDocument can cause the DocumentLoader to get deref
'ed and possible destroyed, | 533 // DocumentWriter::replaceDocument can cause the DocumentLoader to get deref
'ed and possible destroyed, |
| 534 // so protect it with a RefPtr. | 534 // so protect it with a RefPtr. |
| 535 if (RefPtr<DocumentLoader> loader = m_frame->document()->loader()) { | 535 if (RefPtr<DocumentLoader> loader = m_frame->document()->loader()) { |
| 536 UseCounter::count(*m_frame->document(), UseCounter::ReplaceDocumentViaJa
vaScriptURL); | 536 UseCounter::count(*m_frame->document(), UseCounter::ReplaceDocumentViaJa
vaScriptURL); |
| 537 loader->replaceDocument(scriptResult, ownerDocument.get()); | 537 loader->replaceDocument(scriptResult, ownerDocument.get()); |
| 538 } | 538 } |
| 539 return true; | 539 return true; |
| 540 } | 540 } |
| 541 | 541 |
| 542 void ScriptController::executeScriptInMainWorld(const String& script, ExecuteScr
iptPolicy policy) | 542 void ScriptController::executeScriptInMainWorld(const String& script, ExecuteScr
iptPolicy policy) |
| 543 { | 543 { |
| 544 v8::HandleScope handleScope(m_isolate); | |
| 545 evaluateScriptInMainWorld(ScriptSourceCode(script), NotSharableCrossOrigin,
policy); | 544 evaluateScriptInMainWorld(ScriptSourceCode(script), NotSharableCrossOrigin,
policy); |
| 546 } | 545 } |
| 547 | 546 |
| 548 void ScriptController::executeScriptInMainWorld(const ScriptSourceCode& sourceCo
de, AccessControlStatus corsStatus) | 547 void ScriptController::executeScriptInMainWorld(const ScriptSourceCode& sourceCo
de, AccessControlStatus corsStatus) |
| 549 { | 548 { |
| 550 v8::HandleScope handleScope(m_isolate); | |
| 551 evaluateScriptInMainWorld(sourceCode, corsStatus, DoNotExecuteScriptWhenScri
ptsDisabled); | 549 evaluateScriptInMainWorld(sourceCode, corsStatus, DoNotExecuteScriptWhenScri
ptsDisabled); |
| 552 } | 550 } |
| 553 | 551 |
| 554 v8::Local<v8::Value> ScriptController::executeScriptInMainWorldAndReturnValue(co
nst ScriptSourceCode& sourceCode) | 552 ScriptValue ScriptController::executeScriptInMainWorldAndReturnValue(const Scrip
tSourceCode& sourceCode) |
| 555 { | 553 { |
| 556 return evaluateScriptInMainWorld(sourceCode, NotSharableCrossOrigin, DoNotEx
ecuteScriptWhenScriptsDisabled); | 554 return evaluateScriptInMainWorld(sourceCode, NotSharableCrossOrigin, DoNotEx
ecuteScriptWhenScriptsDisabled); |
| 557 } | 555 } |
| 558 | 556 |
| 559 v8::Local<v8::Value> ScriptController::evaluateScriptInMainWorld(const ScriptSou
rceCode& sourceCode, AccessControlStatus corsStatus, ExecuteScriptPolicy policy) | 557 ScriptValue ScriptController::evaluateScriptInMainWorld(const ScriptSourceCode&
sourceCode, AccessControlStatus corsStatus, ExecuteScriptPolicy policy) |
| 560 { | 558 { |
| 561 if (policy == DoNotExecuteScriptWhenScriptsDisabled && !canExecuteScripts(Ab
outToExecuteScript)) | 559 if (policy == DoNotExecuteScriptWhenScriptsDisabled && !canExecuteScripts(Ab
outToExecuteScript)) |
| 562 return v8::Local<v8::Value>(); | 560 return ScriptValue(); |
| 563 | 561 |
| 564 String sourceURL = sourceCode.url(); | 562 String sourceURL = sourceCode.url(); |
| 565 const String* savedSourceURL = m_sourceURL; | 563 const String* savedSourceURL = m_sourceURL; |
| 566 m_sourceURL = &sourceURL; | 564 m_sourceURL = &sourceURL; |
| 567 | 565 |
| 568 ScriptState* scriptState = ScriptState::forMainWorld(m_frame); | 566 ScriptState* scriptState = ScriptState::forMainWorld(m_frame); |
| 569 if (scriptState->contextIsEmpty()) | 567 if (scriptState->contextIsEmpty()) |
| 570 return v8::Local<v8::Value>(); | 568 return ScriptValue(); |
| 571 | 569 |
| 572 v8::EscapableHandleScope handleScope(scriptState->isolate()); | |
| 573 ScriptState::Scope scope(scriptState); | 570 ScriptState::Scope scope(scriptState); |
| 574 | 571 |
| 575 RefPtr<LocalFrame> protect(m_frame); | 572 RefPtr<LocalFrame> protect(m_frame); |
| 576 if (m_frame->loader().stateMachine()->isDisplayingInitialEmptyDocument()) | 573 if (m_frame->loader().stateMachine()->isDisplayingInitialEmptyDocument()) |
| 577 m_frame->loader().didAccessInitialDocument(); | 574 m_frame->loader().didAccessInitialDocument(); |
| 578 | 575 |
| 579 OwnPtr<ScriptSourceCode> maybeProcessedSourceCode = InspectorInstrumentatio
n::preprocess(m_frame, sourceCode); | 576 OwnPtr<ScriptSourceCode> maybeProcessedSourceCode = InspectorInstrumentatio
n::preprocess(m_frame, sourceCode); |
| 580 const ScriptSourceCode& sourceCodeToCompile = maybeProcessedSourceCode ? *ma
ybeProcessedSourceCode : sourceCode; | 577 const ScriptSourceCode& sourceCodeToCompile = maybeProcessedSourceCode ? *ma
ybeProcessedSourceCode : sourceCode; |
| 581 | 578 |
| 582 v8::Local<v8::Value> object = executeScriptAndReturnValue(scriptState->conte
xt(), sourceCodeToCompile, corsStatus); | 579 v8::Local<v8::Value> object = executeScriptAndReturnValue(scriptState->conte
xt(), sourceCodeToCompile, corsStatus); |
| 583 m_sourceURL = savedSourceURL; | 580 m_sourceURL = savedSourceURL; |
| 584 | 581 |
| 585 if (object.IsEmpty()) | 582 if (object.IsEmpty()) |
| 586 return v8::Local<v8::Value>(); | 583 return ScriptValue(); |
| 587 | 584 |
| 588 return handleScope.Escape(object); | 585 return ScriptValue(scriptState, object); |
| 589 } | 586 } |
| 590 | 587 |
| 591 void ScriptController::executeScriptInIsolatedWorld(int worldID, const Vector<Sc
riptSourceCode>& sources, int extensionGroup, Vector<v8::Local<v8::Value> >* res
ults) | 588 void ScriptController::executeScriptInIsolatedWorld(int worldID, const Vector<Sc
riptSourceCode>& sources, int extensionGroup, Vector<ScriptValue>* results) |
| 592 { | 589 { |
| 593 ASSERT(worldID > 0); | 590 ASSERT(worldID > 0); |
| 594 | 591 |
| 595 RefPtr<DOMWrapperWorld> world = DOMWrapperWorld::ensureIsolatedWorld(worldID
, extensionGroup); | 592 RefPtr<DOMWrapperWorld> world = DOMWrapperWorld::ensureIsolatedWorld(worldID
, extensionGroup); |
| 596 V8WindowShell* isolatedWorldShell = windowShell(*world); | 593 V8WindowShell* isolatedWorldShell = windowShell(*world); |
| 597 if (!isolatedWorldShell->isContextInitialized()) | 594 if (!isolatedWorldShell->isContextInitialized()) |
| 598 return; | 595 return; |
| 599 | 596 |
| 600 ScriptState* scriptState = isolatedWorldShell->scriptState(); | 597 ScriptState* scriptState = isolatedWorldShell->scriptState(); |
| 601 v8::EscapableHandleScope handleScope(scriptState->isolate()); | |
| 602 ScriptState::Scope scope(scriptState); | 598 ScriptState::Scope scope(scriptState); |
| 603 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())
; |
| 604 | 600 |
| 605 for (size_t i = 0; i < sources.size(); ++i) { | 601 for (size_t i = 0; i < sources.size(); ++i) { |
| 606 v8::Local<v8::Value> evaluationResult = executeScriptAndReturnValue(scri
ptState->context(), sources[i]); | 602 v8::Local<v8::Value> evaluationResult = executeScriptAndReturnValue(scri
ptState->context(), sources[i]); |
| 607 if (evaluationResult.IsEmpty()) | 603 if (evaluationResult.IsEmpty()) |
| 608 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)); |
| 609 resultArray->Set(i, evaluationResult); | 605 resultArray->Set(i, evaluationResult); |
| 610 } | 606 } |
| 611 | 607 |
| 612 if (results) { | 608 if (results) { |
| 613 for (size_t i = 0; i < resultArray->Length(); ++i) | 609 for (size_t i = 0; i < resultArray->Length(); ++i) |
| 614 results->append(handleScope.Escape(resultArray->Get(i))); | 610 results->append(ScriptValue(scriptState, resultArray->Get(i))); |
| 615 } | 611 } |
| 616 } | 612 } |
| 617 | 613 |
| 618 } // namespace WebCore | 614 } // namespace WebCore |
| OLD | NEW |