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

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: Needed to shutdown the sheduler owned by MockPlatform in TearDown() 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 Scheduler::shared()->postLoadingTask(FROM_HERE, m_executeScriptsTaskFactory. task());
Sami 2015/02/19 12:52:13 Like we talked about, let's rename |task| to somet
alex clarke (OOO till 29th) 2015/02/19 15:11:33 I'd prefer to do this in a follow up patch. I'll
103 } 105 }
104 106
105 void ScriptRunner::notifyScriptLoadError(ScriptLoader* scriptLoader, ExecutionTy pe executionType) 107 void ScriptRunner::notifyScriptLoadError(ScriptLoader* scriptLoader, ExecutionTy pe executionType)
106 { 108 {
107 switch (executionType) { 109 switch (executionType) {
108 case ASYNC_EXECUTION: 110 case ASYNC_EXECUTION:
109 ASSERT(m_pendingAsyncScripts.contains(scriptLoader)); 111 ASSERT(m_pendingAsyncScripts.contains(scriptLoader));
110 m_pendingAsyncScripts.remove(scriptLoader); 112 m_pendingAsyncScripts.remove(scriptLoader);
111 scriptLoader->detach(); 113 scriptLoader->detach();
112 m_document->decrementLoadEventDelayCount(); 114 m_document->decrementLoadEventDelayCount();
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 148
147 void ScriptRunner::movePendingAsyncScript(ScriptRunner* newRunner, ScriptLoader* scriptLoader) 149 void ScriptRunner::movePendingAsyncScript(ScriptRunner* newRunner, ScriptLoader* scriptLoader)
148 { 150 {
149 if (m_pendingAsyncScripts.contains(scriptLoader)) { 151 if (m_pendingAsyncScripts.contains(scriptLoader)) {
150 newRunner->addPendingAsyncScript(scriptLoader); 152 newRunner->addPendingAsyncScript(scriptLoader);
151 m_pendingAsyncScripts.remove(scriptLoader); 153 m_pendingAsyncScripts.remove(scriptLoader);
152 m_document->decrementLoadEventDelayCount(); 154 m_document->decrementLoadEventDelayCount();
153 } 155 }
154 } 156 }
155 157
156 void ScriptRunner::timerFired(Timer<ScriptRunner>* timer) 158 void ScriptRunner::executeScripts()
157 { 159 {
158 ASSERT_UNUSED(timer, timer == &m_timer);
159
160 RefPtrWillBeRawPtr<Document> protect(m_document.get()); 160 RefPtrWillBeRawPtr<Document> protect(m_document.get());
161 161
162 WillBeHeapVector<RawPtrWillBeMember<ScriptLoader> > scriptLoaders; 162 // New scripts are always appened to m_scriptsToExecuteSoon and m_scriptsToE xecuteInOrder (never prepended)
Sami 2015/02/19 12:52:14 typo: appended
alex clarke (OOO till 29th) 2015/02/19 15:11:33 Done.
163 scriptLoaders.swap(m_scriptsToExecuteSoon); 163 // so as long as we keep track of the current totals, we can ensure the orde r of execution if new scripts
164 // are added while executing the current ones.
165 size_t numScriptsToExecuteSoon = m_scriptsToExecuteSoon.size();
166 size_t numScriptsToExecuteInOrder = m_scriptsToExecuteInOrder.size();
167 for (size_t i = 0; i < numScriptsToExecuteSoon; i++) {
168 if (Scheduler::shared()->shouldYieldForHighPriorityWork()) {
Sami 2015/02/19 12:52:13 Let's add a test that yields at every point and ch
Sami 2015/02/19 13:40:00 Do we have different behavior in this scenario? 1
alex clarke (OOO till 29th) 2015/02/19 15:11:33 Yes that does appear to be the case, and I think i
169 Scheduler::shared()->postLoadingTask(FROM_HERE, m_executeScriptsTask Factory.task());
170 return;
171 }
172 m_scriptsToExecuteSoon.takeFirst()->execute();
173 m_document->decrementLoadEventDelayCount();
174 }
164 175
165 size_t numInOrderScriptsToExecute = 0; 176 for (size_t i = 0; i < numScriptsToExecuteInOrder; i++) {
166 for (; numInOrderScriptsToExecute < m_scriptsToExecuteInOrder.size() && m_sc riptsToExecuteInOrder[numInOrderScriptsToExecute]->isReady(); ++numInOrderScript sToExecute) 177 if (!m_scriptsToExecuteInOrder.first()->isReady()) {
167 scriptLoaders.append(m_scriptsToExecuteInOrder[numInOrderScriptsToExecut e]); 178 break;
168 if (numInOrderScriptsToExecute) 179 }
169 m_scriptsToExecuteInOrder.remove(0, numInOrderScriptsToExecute); 180 if (Scheduler::shared()->shouldYieldForHighPriorityWork()) {
170 181 Scheduler::shared()->postLoadingTask(FROM_HERE, m_executeScriptsTask Factory.task());
171 size_t size = scriptLoaders.size(); 182 return;
172 for (size_t i = 0; i < size; ++i) { 183 }
173 scriptLoaders[i]->execute(); 184 m_scriptsToExecuteInOrder.takeFirst()->execute();
174 m_document->decrementLoadEventDelayCount(); 185 m_document->decrementLoadEventDelayCount();
175 } 186 }
176 } 187 }
177 188
178 void ScriptRunner::trace(Visitor* visitor) 189 void ScriptRunner::trace(Visitor* visitor)
179 { 190 {
180 #if ENABLE(OILPAN) 191 #if ENABLE(OILPAN)
181 visitor->trace(m_document); 192 visitor->trace(m_document);
182 visitor->trace(m_scriptsToExecuteInOrder); 193 visitor->trace(m_scriptsToExecuteInOrder);
183 visitor->trace(m_scriptsToExecuteSoon); 194 visitor->trace(m_scriptsToExecuteSoon);
184 visitor->trace(m_pendingAsyncScripts); 195 visitor->trace(m_pendingAsyncScripts);
185 #endif 196 #endif
186 } 197 }
187 198
188 } 199 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698