Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: Source/core/dom/ScriptRunner.cpp

Issue 866273005: Teach ScriptRunner how to yield and post on loading task queue (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Fix spelling mistake Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010 Google, Inc. All Rights Reserved. 2 * Copyright (C) 2010 Google, Inc. All Rights Reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 12 matching lines...) Expand all
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */ 24 */
25 25
26 #include "config.h" 26 #include "config.h"
27 #include "core/dom/ScriptRunner.h" 27 #include "core/dom/ScriptRunner.h"
28 28
29 #include "core/dom/Document.h" 29 #include "core/dom/Document.h"
30 #include "core/dom/Element.h" 30 #include "core/dom/Element.h"
31 #include "core/dom/ScriptLoader.h" 31 #include "core/dom/ScriptLoader.h"
32 #include "platform/heap/Handle.h" 32 #include "platform/heap/Handle.h"
33 #include "platform/scheduler/Scheduler.h"
34 #include "wtf/Functional.h"
33 35
34 namespace blink { 36 namespace blink {
35 37
36 ScriptRunner::ScriptRunner(Document* document) 38 ScriptRunner::ScriptRunner(Document* document)
37 : m_document(document) 39 : m_document(document)
38 , m_timer(this, &ScriptRunner::timerFired) 40 , m_executeScriptsTaskFactory(WTF::bind(&ScriptRunner::executeScripts, this) )
39 { 41 {
40 ASSERT(document); 42 ASSERT(document);
41 } 43 }
42 44
43 ScriptRunner::~ScriptRunner() 45 ScriptRunner::~ScriptRunner()
44 { 46 {
45 #if !ENABLE(OILPAN) 47 #if !ENABLE(OILPAN)
46 // Make sure that ScriptLoaders don't keep their PendingScripts alive. 48 // Make sure that ScriptLoaders don't keep their PendingScripts alive.
47 for (ScriptLoader* scriptLoader : m_scriptsToExecuteInOrder) 49 for (ScriptLoader* scriptLoader : m_scriptsToExecuteInOrder)
48 scriptLoader->detach(); 50 scriptLoader->detach();
(...skipping 21 matching lines...) Expand all
70 72
71 case IN_ORDER_EXECUTION: 73 case IN_ORDER_EXECUTION:
72 m_document->incrementLoadEventDelayCount(); 74 m_document->incrementLoadEventDelayCount();
73 m_scriptsToExecuteInOrder.append(scriptLoader); 75 m_scriptsToExecuteInOrder.append(scriptLoader);
74 break; 76 break;
75 } 77 }
76 } 78 }
77 79
78 void ScriptRunner::suspend() 80 void ScriptRunner::suspend()
79 { 81 {
80 m_timer.stop(); 82 m_executeScriptsTaskFactory.cancel();
81 } 83 }
82 84
83 void ScriptRunner::resume() 85 void ScriptRunner::resume()
84 { 86 {
85 if (hasPendingScripts()) 87 if (hasPendingScripts())
86 m_timer.startOneShot(0, FROM_HERE); 88 Scheduler::shared()->postLoadingTask(FROM_HERE, m_executeScriptsTaskFact ory.task());
87 } 89 }
88 90
89 void ScriptRunner::notifyScriptReady(ScriptLoader* scriptLoader, ExecutionType e xecutionType) 91 void ScriptRunner::notifyScriptReady(ScriptLoader* scriptLoader, ExecutionType e xecutionType)
90 { 92 {
91 switch (executionType) { 93 switch (executionType) {
92 case ASYNC_EXECUTION: 94 case ASYNC_EXECUTION:
93 ASSERT(m_pendingAsyncScripts.contains(scriptLoader)); 95 ASSERT(m_pendingAsyncScripts.contains(scriptLoader));
94 m_scriptsToExecuteSoon.append(scriptLoader); 96 m_scriptsToExecuteSoon.append(scriptLoader);
95 m_pendingAsyncScripts.remove(scriptLoader); 97 m_pendingAsyncScripts.remove(scriptLoader);
96 break; 98 break;
97 99
98 case IN_ORDER_EXECUTION: 100 case IN_ORDER_EXECUTION:
99 ASSERT(!m_scriptsToExecuteInOrder.isEmpty()); 101 ASSERT(!m_scriptsToExecuteInOrder.isEmpty());
100 break; 102 break;
101 } 103 }
102 m_timer.startOneShot(0, FROM_HERE); 104 // FIXME: Rename task() so that it's obvious it cancels any pending task.
105 Scheduler::shared()->postLoadingTask(FROM_HERE, m_executeScriptsTaskFactory. task());
marja 2015/02/20 09:34:37 Hmm, so, does this mean that if the task is alread
alex clarke (OOO till 29th) 2015/02/20 11:59:34 Good point, this lead to some discussion in the of
103 } 106 }
104 107
105 void ScriptRunner::notifyScriptLoadError(ScriptLoader* scriptLoader, ExecutionTy pe executionType) 108 void ScriptRunner::notifyScriptLoadError(ScriptLoader* scriptLoader, ExecutionTy pe executionType)
106 { 109 {
107 switch (executionType) { 110 switch (executionType) {
108 case ASYNC_EXECUTION: 111 case ASYNC_EXECUTION:
109 ASSERT(m_pendingAsyncScripts.contains(scriptLoader)); 112 ASSERT(m_pendingAsyncScripts.contains(scriptLoader));
110 m_pendingAsyncScripts.remove(scriptLoader); 113 m_pendingAsyncScripts.remove(scriptLoader);
111 scriptLoader->detach(); 114 scriptLoader->detach();
112 m_document->decrementLoadEventDelayCount(); 115 m_document->decrementLoadEventDelayCount();
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 149
147 void ScriptRunner::movePendingAsyncScript(ScriptRunner* newRunner, ScriptLoader* scriptLoader) 150 void ScriptRunner::movePendingAsyncScript(ScriptRunner* newRunner, ScriptLoader* scriptLoader)
148 { 151 {
149 if (m_pendingAsyncScripts.contains(scriptLoader)) { 152 if (m_pendingAsyncScripts.contains(scriptLoader)) {
150 newRunner->addPendingAsyncScript(scriptLoader); 153 newRunner->addPendingAsyncScript(scriptLoader);
151 m_pendingAsyncScripts.remove(scriptLoader); 154 m_pendingAsyncScripts.remove(scriptLoader);
152 m_document->decrementLoadEventDelayCount(); 155 m_document->decrementLoadEventDelayCount();
153 } 156 }
154 } 157 }
155 158
156 void ScriptRunner::timerFired(Timer<ScriptRunner>* timer) 159 void ScriptRunner::executeScripts()
157 { 160 {
158 ASSERT_UNUSED(timer, timer == &m_timer);
159
160 RefPtrWillBeRawPtr<Document> protect(m_document.get()); 161 RefPtrWillBeRawPtr<Document> protect(m_document.get());
161 162
162 WillBeHeapVector<RawPtrWillBeMember<ScriptLoader> > scriptLoaders; 163 // New scripts are always appended to m_scriptsToExecuteSoon and m_scriptsTo ExecuteInOrder (never prepended)
163 scriptLoaders.swap(m_scriptsToExecuteSoon); 164 // so as long as we keep track of the current totals, we can ensure the orde r of execution if new scripts
165 // are added while executing the current ones.
166 // NOTE a yield followed by a notifyScriptReady(... ASYNC_EXECUTION) will re sult in that script executing
167 // before any pre-existing ScriptsToExecuteInOrder.
168 size_t numScriptsToExecuteSoon = m_scriptsToExecuteSoon.size();
169 size_t numScriptsToExecuteInOrder = m_scriptsToExecuteInOrder.size();
170 for (size_t i = 0; i < numScriptsToExecuteSoon; i++) {
171 if (Scheduler::shared()->shouldYieldForHighPriorityWork()) {
sof 2015/02/20 07:48:50 Add a private helper method to avoid the repetitio
alex clarke (OOO till 29th) 2015/02/20 11:59:34 Done.
172 Scheduler::shared()->postLoadingTask(FROM_HERE, m_executeScriptsTask Factory.task());
173 return;
174 }
175 m_scriptsToExecuteSoon.takeFirst()->execute();
176 m_document->decrementLoadEventDelayCount();
177 }
164 178
165 size_t numInOrderScriptsToExecute = 0; 179 for (size_t i = 0; i < numScriptsToExecuteInOrder; i++) {
166 for (; numInOrderScriptsToExecute < m_scriptsToExecuteInOrder.size() && m_sc riptsToExecuteInOrder[numInOrderScriptsToExecute]->isReady(); ++numInOrderScript sToExecute) 180 if (!m_scriptsToExecuteInOrder.first()->isReady()) {
sof 2015/02/20 07:48:50 Let's add an assert about non-emptiness, just in c
alex clarke (OOO till 29th) 2015/02/20 11:59:34 Done.
167 scriptLoaders.append(m_scriptsToExecuteInOrder[numInOrderScriptsToExecut e]); 181 break;
168 if (numInOrderScriptsToExecute) 182 }
sof 2015/02/20 07:48:50 Redundant braces.
alex clarke (OOO till 29th) 2015/02/20 11:59:34 Done.
169 m_scriptsToExecuteInOrder.remove(0, numInOrderScriptsToExecute); 183 if (Scheduler::shared()->shouldYieldForHighPriorityWork()) {
170 184 Scheduler::shared()->postLoadingTask(FROM_HERE, m_executeScriptsTask Factory.task());
171 size_t size = scriptLoaders.size(); 185 return;
172 for (size_t i = 0; i < size; ++i) { 186 }
173 scriptLoaders[i]->execute(); 187 m_scriptsToExecuteInOrder.takeFirst()->execute();
174 m_document->decrementLoadEventDelayCount(); 188 m_document->decrementLoadEventDelayCount();
175 } 189 }
176 } 190 }
177 191
178 void ScriptRunner::trace(Visitor* visitor) 192 void ScriptRunner::trace(Visitor* visitor)
179 { 193 {
180 #if ENABLE(OILPAN) 194 #if ENABLE(OILPAN)
181 visitor->trace(m_document); 195 visitor->trace(m_document);
182 visitor->trace(m_scriptsToExecuteInOrder); 196 visitor->trace(m_scriptsToExecuteInOrder);
183 visitor->trace(m_scriptsToExecuteSoon); 197 visitor->trace(m_scriptsToExecuteSoon);
184 visitor->trace(m_pendingAsyncScripts); 198 visitor->trace(m_pendingAsyncScripts);
185 #endif 199 #endif
186 } 200 }
187 201
188 } 202 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698