| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "sky/engine/core/html/parser/HTMLScriptRunner.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "sky/engine/core/app/AbstractModule.h" | |
| 9 #include "sky/engine/core/dom/Document.h" | |
| 10 #include "sky/engine/core/dom/Microtask.h" | |
| 11 #include "sky/engine/core/frame/LocalFrame.h" | |
| 12 #include "sky/engine/core/html/HTMLScriptElement.h" | |
| 13 #include "sky/engine/core/script/dart_controller.h" | |
| 14 | |
| 15 namespace blink { | |
| 16 | |
| 17 PassOwnPtr<HTMLScriptRunner> HTMLScriptRunner::createForScript( | |
| 18 PassRefPtr<HTMLScriptElement> element, | |
| 19 TextPosition position, | |
| 20 HTMLScriptRunnerHost* host) { | |
| 21 return adoptPtr(new HTMLScriptRunner(element, position, host)); | |
| 22 } | |
| 23 | |
| 24 HTMLScriptRunner::HTMLScriptRunner(PassRefPtr<HTMLScriptElement> element, | |
| 25 TextPosition position, | |
| 26 HTMLScriptRunnerHost* host) | |
| 27 : m_host(host), | |
| 28 m_element(element), | |
| 29 m_position(position), | |
| 30 m_state(StateInitial), | |
| 31 m_weakFactory(this) { | |
| 32 } | |
| 33 | |
| 34 HTMLScriptRunner::~HTMLScriptRunner() | |
| 35 { | |
| 36 // If we hit this ASSERT we failed to notify the ScriptRunnerHost! | |
| 37 ASSERT(m_state == StateCompleted); | |
| 38 } | |
| 39 | |
| 40 bool HTMLScriptRunner::isExecutingScript() const { | |
| 41 return m_state == StateExecuting; | |
| 42 } | |
| 43 | |
| 44 void HTMLScriptRunner::advanceTo(State state, AdvanceType advanceType) { | |
| 45 if (advanceType == ExecutionNormal) { | |
| 46 switch (m_state) { | |
| 47 case StateInitial: | |
| 48 ASSERT(state == StateLoading); | |
| 49 break; | |
| 50 case StateLoading: | |
| 51 ASSERT(state == StateExecuting); | |
| 52 break; | |
| 53 case StateExecuting: | |
| 54 ASSERT(state == StateCompleted); | |
| 55 break; | |
| 56 case StateCompleted: | |
| 57 ASSERT_NOT_REACHED(); | |
| 58 } | |
| 59 } | |
| 60 m_state = state; | |
| 61 | |
| 62 if (m_state == StateCompleted) | |
| 63 m_host->scriptExecutionCompleted(); | |
| 64 // We may be deleted by scriptExecutionCompleted(). | |
| 65 } | |
| 66 | |
| 67 static LocalFrame* contextFrame(Element* element) { | |
| 68 Document* contextDocument = element->document().contextDocument().get(); | |
| 69 if (!contextDocument) | |
| 70 return nullptr; | |
| 71 | |
| 72 LocalFrame* frame = contextDocument->frame(); | |
| 73 if (!frame) | |
| 74 return nullptr; | |
| 75 return frame; | |
| 76 } | |
| 77 | |
| 78 void HTMLScriptRunner::scriptFailed() { | |
| 79 advanceTo(StateCompleted, ExecutionFailure); | |
| 80 } | |
| 81 | |
| 82 void HTMLScriptRunner::start() { | |
| 83 ASSERT(m_state == StateInitial); | |
| 84 ASSERT(m_element->document().haveImportsLoaded()); | |
| 85 | |
| 86 Document& sourceDocument = m_element->document(); | |
| 87 String source = m_element->textContent(); | |
| 88 | |
| 89 LocalFrame* frame = contextFrame(m_element.get()); | |
| 90 if (!frame) | |
| 91 return scriptFailed(); | |
| 92 | |
| 93 advanceTo(StateLoading); | |
| 94 | |
| 95 ASSERT(sourceDocument.module()); | |
| 96 DartController::LoadFinishedCallback loadFinished = base::Bind( | |
| 97 &HTMLScriptRunner::executeLibrary, m_weakFactory.GetWeakPtr()); | |
| 98 frame->dart().LoadScriptInModule(sourceDocument.module(), source, | |
| 99 m_position, loadFinished); | |
| 100 } | |
| 101 | |
| 102 // Enforce that the caller holds refs using RefPtr. | |
| 103 // FIXME: Neither of these should need refs, the Script should hold onto the | |
| 104 // library the document should keep the Module alive. | |
| 105 void HTMLScriptRunner::executeLibrary(RefPtr<AbstractModule> module, | |
| 106 RefPtr<DartValue> library) { | |
| 107 if (!module) | |
| 108 return scriptFailed(); | |
| 109 | |
| 110 advanceTo(StateExecuting); | |
| 111 | |
| 112 // Ian says we'll remove microtasks, but for now execute them right before | |
| 113 // we "run" the script (call 'init'), not at dependency resolution | |
| 114 // or script failures, etc. | |
| 115 Microtask::performCheckpoint(); | |
| 116 | |
| 117 if (LocalFrame* frame = contextFrame(m_element.get())) { | |
| 118 frame->dart().ExecuteLibraryInModule(module.get(), | |
| 119 library->dart_value(), | |
| 120 m_element.get()); | |
| 121 } | |
| 122 | |
| 123 advanceTo(StateCompleted); | |
| 124 // We may be deleted at this point. | |
| 125 } | |
| 126 | |
| 127 } | |
| OLD | NEW |