| 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 10 matching lines...) Expand all Loading... |
| 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 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/PendingScript.h" | |
| 32 #include "core/dom/ScriptLoader.h" | 31 #include "core/dom/ScriptLoader.h" |
| 33 #include "core/fetch/ScriptResource.h" | |
| 34 #include "platform/heap/Handle.h" | 32 #include "platform/heap/Handle.h" |
| 35 | 33 |
| 36 namespace blink { | 34 namespace blink { |
| 37 | 35 |
| 38 ScriptRunner::ScriptRunner(Document* document) | 36 ScriptRunner::ScriptRunner(Document* document) |
| 39 : m_document(document) | 37 : m_document(document) |
| 40 , m_timer(this, &ScriptRunner::timerFired) | 38 , m_timer(this, &ScriptRunner::timerFired) |
| 41 { | 39 { |
| 42 ASSERT(document); | 40 ASSERT(document); |
| 43 } | 41 } |
| 44 | 42 |
| 45 ScriptRunner::~ScriptRunner() | 43 ScriptRunner::~ScriptRunner() |
| 46 { | 44 { |
| 45 // Make sure that ScriptLoaders don't keep their PendingScripts alive. |
| 46 for (size_t i = 0; i < m_scriptsToExecuteInOrder.size(); ++i) |
| 47 m_scriptsToExecuteInOrder[i]->detach(); |
| 48 for (size_t i = 0; i < m_scriptsToExecuteSoon.size(); ++i) |
| 49 m_scriptsToExecuteSoon[i]->detach(); |
| 50 for (HashSet<ScriptLoader*>::iterator it = m_pendingAsyncScripts.begin(); it
!= m_pendingAsyncScripts.end(); ++it) |
| 51 (*it)->detach(); |
| 47 } | 52 } |
| 48 | 53 |
| 49 void ScriptRunner::addPendingAsyncScript(ScriptLoader* scriptLoader, const Pendi
ngScript& pendingScript) | 54 void ScriptRunner::addPendingAsyncScript(ScriptLoader* scriptLoader) |
| 50 { | 55 { |
| 51 m_document->incrementLoadEventDelayCount(); | 56 m_document->incrementLoadEventDelayCount(); |
| 52 m_pendingAsyncScripts.add(scriptLoader, pendingScript); | 57 m_pendingAsyncScripts.add(scriptLoader); |
| 53 } | 58 } |
| 54 | 59 |
| 55 void ScriptRunner::queueScriptForExecution(ScriptLoader* scriptLoader, ResourceP
tr<ScriptResource> resource, ExecutionType executionType) | 60 void ScriptRunner::queueScriptForExecution(ScriptLoader* scriptLoader, Execution
Type executionType) |
| 56 { | 61 { |
| 57 ASSERT(scriptLoader); | 62 ASSERT(scriptLoader); |
| 58 ASSERT(resource.get()); | |
| 59 | |
| 60 Element* element = scriptLoader->element(); | |
| 61 ASSERT(element); | |
| 62 ASSERT(element->inDocument()); | |
| 63 | 63 |
| 64 switch (executionType) { | 64 switch (executionType) { |
| 65 case ASYNC_EXECUTION: | 65 case ASYNC_EXECUTION: |
| 66 addPendingAsyncScript(scriptLoader, PendingScript(element, resource.get(
))); | 66 addPendingAsyncScript(scriptLoader); |
| 67 break; | 67 break; |
| 68 | 68 |
| 69 case IN_ORDER_EXECUTION: | 69 case IN_ORDER_EXECUTION: |
| 70 m_document->incrementLoadEventDelayCount(); | 70 m_document->incrementLoadEventDelayCount(); |
| 71 m_scriptsToExecuteInOrder.append(PendingScript(element, resource.get()))
; | 71 m_scriptsToExecuteInOrder.append(scriptLoader); |
| 72 break; | 72 break; |
| 73 } | 73 } |
| 74 } | 74 } |
| 75 | 75 |
| 76 void ScriptRunner::suspend() | 76 void ScriptRunner::suspend() |
| 77 { | 77 { |
| 78 m_timer.stop(); | 78 m_timer.stop(); |
| 79 } | 79 } |
| 80 | 80 |
| 81 void ScriptRunner::resume() | 81 void ScriptRunner::resume() |
| 82 { | 82 { |
| 83 if (hasPendingScripts()) | 83 if (hasPendingScripts()) |
| 84 m_timer.startOneShot(0, FROM_HERE); | 84 m_timer.startOneShot(0, FROM_HERE); |
| 85 } | 85 } |
| 86 | 86 |
| 87 void ScriptRunner::notifyScriptReady(ScriptLoader* scriptLoader, ExecutionType e
xecutionType) | 87 void ScriptRunner::notifyScriptReady(ScriptLoader* scriptLoader, ExecutionType e
xecutionType) |
| 88 { | 88 { |
| 89 switch (executionType) { | 89 switch (executionType) { |
| 90 case ASYNC_EXECUTION: | 90 case ASYNC_EXECUTION: |
| 91 ASSERT(m_pendingAsyncScripts.contains(scriptLoader)); | 91 ASSERT(m_pendingAsyncScripts.contains(scriptLoader)); |
| 92 m_scriptsToExecuteSoon.append(m_pendingAsyncScripts.take(scriptLoader)); | 92 m_scriptsToExecuteSoon.append(scriptLoader); |
| 93 m_pendingAsyncScripts.remove(scriptLoader); |
| 93 break; | 94 break; |
| 94 | 95 |
| 95 case IN_ORDER_EXECUTION: | 96 case IN_ORDER_EXECUTION: |
| 96 ASSERT(!m_scriptsToExecuteInOrder.isEmpty()); | 97 ASSERT(!m_scriptsToExecuteInOrder.isEmpty()); |
| 97 break; | 98 break; |
| 98 } | 99 } |
| 99 m_timer.startOneShot(0, FROM_HERE); | 100 m_timer.startOneShot(0, FROM_HERE); |
| 100 } | 101 } |
| 101 | 102 |
| 102 void ScriptRunner::notifyScriptLoadError(ScriptLoader* scriptLoader, ExecutionTy
pe executionType) | 103 void ScriptRunner::notifyScriptLoadError(ScriptLoader* scriptLoader, ExecutionTy
pe executionType) |
| 103 { | 104 { |
| 104 switch (executionType) { | 105 switch (executionType) { |
| 105 case ASYNC_EXECUTION: | 106 case ASYNC_EXECUTION: |
| 106 ASSERT(m_pendingAsyncScripts.contains(scriptLoader)); | 107 ASSERT(m_pendingAsyncScripts.contains(scriptLoader)); |
| 107 m_pendingAsyncScripts.remove(scriptLoader); | 108 m_pendingAsyncScripts.remove(scriptLoader); |
| 109 scriptLoader->detach(); |
| 108 m_document->decrementLoadEventDelayCount(); | 110 m_document->decrementLoadEventDelayCount(); |
| 109 break; | 111 break; |
| 110 | 112 |
| 111 case IN_ORDER_EXECUTION: | 113 case IN_ORDER_EXECUTION: |
| 112 ASSERT(!m_scriptsToExecuteInOrder.isEmpty()); | 114 ASSERT(!m_scriptsToExecuteInOrder.isEmpty()); |
| 113 break; | 115 break; |
| 114 } | 116 } |
| 115 } | 117 } |
| 116 | 118 |
| 117 void ScriptRunner::movePendingAsyncScript(ScriptRunner* newRunner, ScriptLoader*
scriptLoader) | 119 void ScriptRunner::movePendingAsyncScript(ScriptRunner* newRunner, ScriptLoader*
scriptLoader) |
| 118 { | 120 { |
| 119 if (m_pendingAsyncScripts.contains(scriptLoader)) { | 121 if (m_pendingAsyncScripts.contains(scriptLoader)) { |
| 120 newRunner->addPendingAsyncScript(scriptLoader, m_pendingAsyncScripts.tak
e(scriptLoader)); | 122 newRunner->addPendingAsyncScript(scriptLoader); |
| 123 m_pendingAsyncScripts.remove(scriptLoader); |
| 121 m_document->decrementLoadEventDelayCount(); | 124 m_document->decrementLoadEventDelayCount(); |
| 122 } | 125 } |
| 123 } | 126 } |
| 124 | 127 |
| 125 void ScriptRunner::timerFired(Timer<ScriptRunner>* timer) | 128 void ScriptRunner::timerFired(Timer<ScriptRunner>* timer) |
| 126 { | 129 { |
| 127 ASSERT_UNUSED(timer, timer == &m_timer); | 130 ASSERT_UNUSED(timer, timer == &m_timer); |
| 128 | 131 |
| 129 RefPtrWillBeRawPtr<Document> protect(m_document.get()); | 132 RefPtrWillBeRawPtr<Document> protect(m_document.get()); |
| 130 | 133 |
| 131 Vector<PendingScript> scripts; | 134 Vector<ScriptLoader*> scriptLoaders; |
| 132 scripts.swap(m_scriptsToExecuteSoon); | 135 scriptLoaders.swap(m_scriptsToExecuteSoon); |
| 133 | 136 |
| 134 size_t numInOrderScriptsToExecute = 0; | 137 size_t numInOrderScriptsToExecute = 0; |
| 135 for (; numInOrderScriptsToExecute < m_scriptsToExecuteInOrder.size() && m_sc
riptsToExecuteInOrder[numInOrderScriptsToExecute].resource()->isLoaded(); ++numI
nOrderScriptsToExecute) | 138 for (; numInOrderScriptsToExecute < m_scriptsToExecuteInOrder.size() && m_sc
riptsToExecuteInOrder[numInOrderScriptsToExecute]->isReady(); ++numInOrderScript
sToExecute) |
| 136 scripts.append(m_scriptsToExecuteInOrder[numInOrderScriptsToExecute]); | 139 scriptLoaders.append(m_scriptsToExecuteInOrder[numInOrderScriptsToExecut
e]); |
| 137 if (numInOrderScriptsToExecute) | 140 if (numInOrderScriptsToExecute) |
| 138 m_scriptsToExecuteInOrder.remove(0, numInOrderScriptsToExecute); | 141 m_scriptsToExecuteInOrder.remove(0, numInOrderScriptsToExecute); |
| 139 | 142 |
| 140 size_t size = scripts.size(); | 143 size_t size = scriptLoaders.size(); |
| 141 for (size_t i = 0; i < size; ++i) { | 144 for (size_t i = 0; i < size; ++i) { |
| 142 ScriptResource* resource = scripts[i].resource(); | 145 scriptLoaders[i]->execute(); |
| 143 RefPtrWillBeRawPtr<Element> element = scripts[i].releaseElementAndClear(
); | |
| 144 toScriptLoaderIfPossible(element.get())->execute(resource); | |
| 145 m_document->decrementLoadEventDelayCount(); | 146 m_document->decrementLoadEventDelayCount(); |
| 146 } | 147 } |
| 147 } | 148 } |
| 148 | 149 |
| 149 void ScriptRunner::trace(Visitor* visitor) | 150 void ScriptRunner::trace(Visitor* visitor) |
| 150 { | 151 { |
| 151 #if ENABLE(OILPAN) | 152 #if ENABLE(OILPAN) |
| 152 visitor->trace(m_document); | 153 visitor->trace(m_document); |
| 153 visitor->trace(m_scriptsToExecuteInOrder); | 154 visitor->trace(m_scriptsToExecuteInOrder); |
| 154 visitor->trace(m_scriptsToExecuteSoon); | 155 visitor->trace(m_scriptsToExecuteSoon); |
| 155 visitor->trace(m_pendingAsyncScripts); | 156 visitor->trace(m_pendingAsyncScripts); |
| 156 #endif | 157 #endif |
| 157 } | 158 } |
| 158 | 159 |
| 159 } | 160 } |
| OLD | NEW |