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