Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 Loading... | |
| 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()); |
|
Sami
2015/02/10 18:40:01
Should we check whether we already have a callback
alex clarke (OOO till 29th)
2015/02/18 18:28:20
Both m_timer.startOneShot(0, FROM_HERE); and
Sc
| |
| 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/10 18:40:01
Ditto.
alex clarke (OOO till 29th)
2015/02/18 18:28:20
Acknowledged.
| |
| 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(); |
| 113 break; | 115 break; |
| 114 | 116 |
| 115 case IN_ORDER_EXECUTION: | 117 case IN_ORDER_EXECUTION: |
| 116 ASSERT(!m_scriptsToExecuteInOrder.isEmpty()); | 118 ASSERT(!m_scriptsToExecuteInOrder.isEmpty()); |
| 117 break; | 119 break; |
| 118 } | 120 } |
| 119 } | 121 } |
| 120 | 122 |
| 121 void ScriptRunner::movePendingAsyncScript(ScriptRunner* newRunner, ScriptLoader* scriptLoader) | 123 void ScriptRunner::movePendingAsyncScript(ScriptRunner* newRunner, ScriptLoader* scriptLoader) |
| 122 { | 124 { |
| 123 if (m_pendingAsyncScripts.contains(scriptLoader)) { | 125 if (m_pendingAsyncScripts.contains(scriptLoader)) { |
| 124 newRunner->addPendingAsyncScript(scriptLoader); | 126 newRunner->addPendingAsyncScript(scriptLoader); |
| 125 m_pendingAsyncScripts.remove(scriptLoader); | 127 m_pendingAsyncScripts.remove(scriptLoader); |
| 126 m_document->decrementLoadEventDelayCount(); | 128 m_document->decrementLoadEventDelayCount(); |
| 127 } | 129 } |
| 128 } | 130 } |
| 129 | 131 |
| 130 void ScriptRunner::timerFired(Timer<ScriptRunner>* timer) | 132 void ScriptRunner::executeScripts() |
| 131 { | 133 { |
| 132 ASSERT_UNUSED(timer, timer == &m_timer); | |
| 133 | |
| 134 RefPtrWillBeRawPtr<Document> protect(m_document.get()); | 134 RefPtrWillBeRawPtr<Document> protect(m_document.get()); |
| 135 | 135 |
| 136 WillBeHeapVector<RawPtrWillBeMember<ScriptLoader> > scriptLoaders; | 136 while (!m_scriptsToExecuteSoon.isEmpty()) { |
| 137 scriptLoaders.swap(m_scriptsToExecuteSoon); | 137 if (Scheduler::shared()->shouldYieldForHighPriorityWork()) { |
|
Sami
2015/02/10 18:40:01
Should we still take a snapshot of the pending scr
alex clarke (OOO till 29th)
2015/02/18 18:28:20
Good spot, that is a problem.
| |
| 138 Scheduler::shared()->postLoadingTask(FROM_HERE, m_executeScriptsTask Factory.task()); | |
| 139 return; | |
| 140 } | |
| 141 m_scriptsToExecuteSoon.takeFirst()->execute(); | |
| 142 m_document->decrementLoadEventDelayCount(); | |
| 143 } | |
| 138 | 144 |
| 139 size_t numInOrderScriptsToExecute = 0; | 145 while (!m_scriptsToExecuteInOrder.isEmpty()) { |
| 140 for (; numInOrderScriptsToExecute < m_scriptsToExecuteInOrder.size() && m_sc riptsToExecuteInOrder[numInOrderScriptsToExecute]->isReady(); ++numInOrderScript sToExecute) | 146 if (!m_scriptsToExecuteInOrder.first()->isReady()) { |
| 141 scriptLoaders.append(m_scriptsToExecuteInOrder[numInOrderScriptsToExecut e]); | 147 break; |
| 142 if (numInOrderScriptsToExecute) | 148 } |
| 143 m_scriptsToExecuteInOrder.remove(0, numInOrderScriptsToExecute); | 149 if (Scheduler::shared()->shouldYieldForHighPriorityWork()) { |
| 144 | 150 Scheduler::shared()->postLoadingTask(FROM_HERE, m_executeScriptsTask Factory.task()); |
| 145 size_t size = scriptLoaders.size(); | 151 return; |
| 146 for (size_t i = 0; i < size; ++i) { | 152 } |
| 147 scriptLoaders[i]->execute(); | 153 m_scriptsToExecuteInOrder.takeFirst()->execute(); |
| 148 m_document->decrementLoadEventDelayCount(); | 154 m_document->decrementLoadEventDelayCount(); |
| 149 } | 155 } |
| 150 } | 156 } |
| 151 | 157 |
| 152 void ScriptRunner::trace(Visitor* visitor) | 158 void ScriptRunner::trace(Visitor* visitor) |
| 153 { | 159 { |
| 154 #if ENABLE(OILPAN) | 160 #if ENABLE(OILPAN) |
| 155 visitor->trace(m_document); | 161 visitor->trace(m_document); |
| 156 visitor->trace(m_scriptsToExecuteInOrder); | 162 visitor->trace(m_scriptsToExecuteInOrder); |
| 157 visitor->trace(m_scriptsToExecuteSoon); | 163 visitor->trace(m_scriptsToExecuteSoon); |
| 158 visitor->trace(m_pendingAsyncScripts); | 164 visitor->trace(m_pendingAsyncScripts); |
| 159 #endif | 165 #endif |
| 160 } | 166 } |
| 161 | 167 |
| 162 } | 168 } |
| OLD | NEW |