Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / dom / ScriptRunner.cpp
index 2fbf959..44543ab 100644 (file)
 
 #include "core/dom/Document.h"
 #include "core/dom/Element.h"
-#include "core/dom/PendingScript.h"
 #include "core/dom/ScriptLoader.h"
-#include "core/fetch/ScriptResource.h"
+#include "platform/heap/Handle.h"
 
-namespace WebCore {
+namespace blink {
 
 ScriptRunner::ScriptRunner(Document* document)
     : m_document(document)
@@ -43,32 +42,35 @@ ScriptRunner::ScriptRunner(Document* document)
 
 ScriptRunner::~ScriptRunner()
 {
-    for (size_t i = 0; i < m_scriptsToExecuteSoon.size(); ++i)
-        m_document->decrementLoadEventDelayCount();
-    for (size_t i = 0; i < m_scriptsToExecuteInOrder.size(); ++i)
-        m_document->decrementLoadEventDelayCount();
-    for (size_t i = 0; i < m_pendingAsyncScripts.size(); ++i)
-        m_document->decrementLoadEventDelayCount();
+#if !ENABLE(OILPAN)
+    // Make sure that ScriptLoaders don't keep their PendingScripts alive.
+    for (ScriptLoader* scriptLoader : m_scriptsToExecuteInOrder)
+        scriptLoader->detach();
+    for (ScriptLoader* scriptLoader : m_scriptsToExecuteSoon)
+        scriptLoader->detach();
+    for (ScriptLoader* scriptLoader : m_pendingAsyncScripts)
+        scriptLoader->detach();
+#endif
 }
 
-void ScriptRunner::queueScriptForExecution(ScriptLoader* scriptLoader, ResourcePtr<ScriptResource> resource, ExecutionType executionType)
+void ScriptRunner::addPendingAsyncScript(ScriptLoader* scriptLoader)
 {
-    ASSERT(scriptLoader);
-    ASSERT(resource.get());
-
-    Element* element = scriptLoader->element();
-    ASSERT(element);
-    ASSERT(element->inDocument());
-
     m_document->incrementLoadEventDelayCount();
+    m_pendingAsyncScripts.add(scriptLoader);
+}
+
+void ScriptRunner::queueScriptForExecution(ScriptLoader* scriptLoader, ExecutionType executionType)
+{
+    ASSERT(scriptLoader);
 
     switch (executionType) {
     case ASYNC_EXECUTION:
-        m_pendingAsyncScripts.add(scriptLoader, PendingScript(element, resource.get()));
+        addPendingAsyncScript(scriptLoader);
         break;
 
     case IN_ORDER_EXECUTION:
-        m_scriptsToExecuteInOrder.append(PendingScript(element, resource.get()));
+        m_document->incrementLoadEventDelayCount();
+        m_scriptsToExecuteInOrder.append(scriptLoader);
         break;
     }
 }
@@ -81,7 +83,7 @@ void ScriptRunner::suspend()
 void ScriptRunner::resume()
 {
     if (hasPendingScripts())
-        m_timer.startOneShot(0);
+        m_timer.startOneShot(0, FROM_HERE);
 }
 
 void ScriptRunner::notifyScriptReady(ScriptLoader* scriptLoader, ExecutionType executionType)
@@ -89,14 +91,15 @@ void ScriptRunner::notifyScriptReady(ScriptLoader* scriptLoader, ExecutionType e
     switch (executionType) {
     case ASYNC_EXECUTION:
         ASSERT(m_pendingAsyncScripts.contains(scriptLoader));
-        m_scriptsToExecuteSoon.append(m_pendingAsyncScripts.take(scriptLoader));
+        m_scriptsToExecuteSoon.append(scriptLoader);
+        m_pendingAsyncScripts.remove(scriptLoader);
         break;
 
     case IN_ORDER_EXECUTION:
         ASSERT(!m_scriptsToExecuteInOrder.isEmpty());
         break;
     }
-    m_timer.startOneShot(0);
+    m_timer.startOneShot(0, FROM_HERE);
 }
 
 void ScriptRunner::notifyScriptLoadError(ScriptLoader* scriptLoader, ExecutionType executionType)
@@ -105,6 +108,7 @@ void ScriptRunner::notifyScriptLoadError(ScriptLoader* scriptLoader, ExecutionTy
     case ASYNC_EXECUTION:
         ASSERT(m_pendingAsyncScripts.contains(scriptLoader));
         m_pendingAsyncScripts.remove(scriptLoader);
+        scriptLoader->detach();
         m_document->decrementLoadEventDelayCount();
         break;
 
@@ -114,28 +118,45 @@ void ScriptRunner::notifyScriptLoadError(ScriptLoader* scriptLoader, ExecutionTy
     }
 }
 
+void ScriptRunner::movePendingAsyncScript(ScriptRunner* newRunner, ScriptLoader* scriptLoader)
+{
+    if (m_pendingAsyncScripts.contains(scriptLoader)) {
+        newRunner->addPendingAsyncScript(scriptLoader);
+        m_pendingAsyncScripts.remove(scriptLoader);
+        m_document->decrementLoadEventDelayCount();
+    }
+}
+
 void ScriptRunner::timerFired(Timer<ScriptRunner>* timer)
 {
     ASSERT_UNUSED(timer, timer == &m_timer);
 
-    RefPtr<Document> protect(m_document);
+    RefPtrWillBeRawPtr<Document> protect(m_document.get());
 
-    Vector<PendingScript> scripts;
-    scripts.swap(m_scriptsToExecuteSoon);
+    WillBeHeapVector<RawPtrWillBeMember<ScriptLoader> > scriptLoaders;
+    scriptLoaders.swap(m_scriptsToExecuteSoon);
 
     size_t numInOrderScriptsToExecute = 0;
-    for (; numInOrderScriptsToExecute < m_scriptsToExecuteInOrder.size() && m_scriptsToExecuteInOrder[numInOrderScriptsToExecute].resource()->isLoaded(); ++numInOrderScriptsToExecute)
-        scripts.append(m_scriptsToExecuteInOrder[numInOrderScriptsToExecute]);
+    for (; numInOrderScriptsToExecute < m_scriptsToExecuteInOrder.size() && m_scriptsToExecuteInOrder[numInOrderScriptsToExecute]->isReady(); ++numInOrderScriptsToExecute)
+        scriptLoaders.append(m_scriptsToExecuteInOrder[numInOrderScriptsToExecute]);
     if (numInOrderScriptsToExecute)
         m_scriptsToExecuteInOrder.remove(0, numInOrderScriptsToExecute);
 
-    size_t size = scripts.size();
+    size_t size = scriptLoaders.size();
     for (size_t i = 0; i < size; ++i) {
-        ScriptResource* resource = scripts[i].resource();
-        RefPtr<Element> element = scripts[i].releaseElementAndClear();
-        toScriptLoaderIfPossible(element.get())->execute(resource);
+        scriptLoaders[i]->execute();
         m_document->decrementLoadEventDelayCount();
     }
 }
 
+void ScriptRunner::trace(Visitor* visitor)
+{
+#if ENABLE(OILPAN)
+    visitor->trace(m_document);
+    visitor->trace(m_scriptsToExecuteInOrder);
+    visitor->trace(m_scriptsToExecuteSoon);
+    visitor->trace(m_pendingAsyncScripts);
+#endif
+}
+
 }