1/* 2 * Copyright (C) 2010 Google, Inc. All Rights Reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 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. 24 */ 25 26#include "config.h" 27#include "ScriptRunner.h" 28 29#include "CachedScript.h" 30#include "Element.h" 31#include "PendingScript.h" 32#include "ScriptElement.h" 33 34namespace WebCore { 35 36ScriptRunner::ScriptRunner(Document& document) 37 : m_document(document) 38 , m_timer(this, &ScriptRunner::timerFired) 39{ 40} 41 42ScriptRunner::~ScriptRunner() 43{ 44 for (size_t i = 0; i < m_scriptsToExecuteSoon.size(); ++i) 45 m_document.decrementLoadEventDelayCount(); 46 for (size_t i = 0; i < m_scriptsToExecuteInOrder.size(); ++i) 47 m_document.decrementLoadEventDelayCount(); 48 for (int i = 0; i < m_pendingAsyncScripts.size(); ++i) 49 m_document.decrementLoadEventDelayCount(); 50} 51 52void ScriptRunner::queueScriptForExecution(ScriptElement* scriptElement, CachedResourceHandle<CachedScript> cachedScript, ExecutionType executionType) 53{ 54 ASSERT(scriptElement); 55 ASSERT(cachedScript.get()); 56 57 Element& element = scriptElement->element(); 58 ASSERT(element.inDocument()); 59 60 m_document.incrementLoadEventDelayCount(); 61 62 switch (executionType) { 63 case ASYNC_EXECUTION: 64 m_pendingAsyncScripts.add(scriptElement, PendingScript(&element, cachedScript.get())); 65 break; 66 67 case IN_ORDER_EXECUTION: 68 m_scriptsToExecuteInOrder.append(PendingScript(&element, cachedScript.get())); 69 break; 70 } 71} 72 73void ScriptRunner::suspend() 74{ 75 m_timer.stop(); 76} 77 78void ScriptRunner::resume() 79{ 80 if (hasPendingScripts()) 81 m_timer.startOneShot(0); 82} 83 84void ScriptRunner::notifyScriptReady(ScriptElement* scriptElement, ExecutionType executionType) 85{ 86 switch (executionType) { 87 case ASYNC_EXECUTION: 88 ASSERT(m_pendingAsyncScripts.contains(scriptElement)); 89 m_scriptsToExecuteSoon.append(m_pendingAsyncScripts.take(scriptElement)); 90 break; 91 92 case IN_ORDER_EXECUTION: 93 ASSERT(!m_scriptsToExecuteInOrder.isEmpty()); 94 break; 95 } 96 m_timer.startOneShot(0); 97} 98 99void ScriptRunner::timerFired(Timer<ScriptRunner>& timer) 100{ 101 ASSERT_UNUSED(timer, &timer == &m_timer); 102 103 Ref<Document> protect(m_document); 104 105 Vector<PendingScript> scripts; 106 scripts.swap(m_scriptsToExecuteSoon); 107 108 size_t numInOrderScriptsToExecute = 0; 109 for (; numInOrderScriptsToExecute < m_scriptsToExecuteInOrder.size() && m_scriptsToExecuteInOrder[numInOrderScriptsToExecute].cachedScript()->isLoaded(); ++numInOrderScriptsToExecute) 110 scripts.append(m_scriptsToExecuteInOrder[numInOrderScriptsToExecute]); 111 if (numInOrderScriptsToExecute) 112 m_scriptsToExecuteInOrder.remove(0, numInOrderScriptsToExecute); 113 114 size_t size = scripts.size(); 115 for (size_t i = 0; i < size; ++i) { 116 CachedScript* cachedScript = scripts[i].cachedScript(); 117 RefPtr<Element> element = scripts[i].releaseElementAndClear(); 118 toScriptElementIfPossible(element.get())->execute(cachedScript); 119 m_document.decrementLoadEventDelayCount(); 120 } 121} 122 123} 124