Move HTML parser to Chromium threading

This CL converts the threaded HTML parser over to using a Chromium thread
rather than a WTF thread. Letting Chromium manage the thread lets us better
intergrate with the rest of Chromium.

Review URL: https://chromiumcodereview.appspot.com/15230004

git-svn-id: svn://svn.chromium.org/blink/trunk@150531 bbb929c8-8fbe-4397-9dbb-9b2b20218538
diff --git a/third_party/WebKit/Source/core/core.gypi b/third_party/WebKit/Source/core/core.gypi
index 7c5db95..d366dd3 100644
--- a/third_party/WebKit/Source/core/core.gypi
+++ b/third_party/WebKit/Source/core/core.gypi
@@ -2485,6 +2485,7 @@
             'platform/SharedTimer.h',
             'platform/Sound.h',
             'platform/SuddenTermination.h',
+            'platform/Task.h',
             'platform/Theme.cpp',
             'platform/ThreadGlobalData.cpp',
             'platform/ThreadTimers.cpp',
diff --git a/third_party/WebKit/Source/core/html/parser/HTMLParserThread.cpp b/third_party/WebKit/Source/core/html/parser/HTMLParserThread.cpp
index f7d00be..5b150d2 100644
--- a/third_party/WebKit/Source/core/html/parser/HTMLParserThread.cpp
+++ b/third_party/WebKit/Source/core/html/parser/HTMLParserThread.cpp
@@ -31,65 +31,32 @@
 #include "config.h"
 #include "core/html/parser/HTMLParserThread.h"
 
+#include "core/platform/Task.h"
+#include "wtf/PassOwnPtr.h"
+#include <public/Platform.h>
+
 namespace WebCore {
 
 HTMLParserThread::HTMLParserThread()
-    : m_threadID(0)
+    : m_thread(adoptPtr(WebKit::Platform::current()->createThread("HTMLParserThread")))
 {
 }
 
 HTMLParserThread::~HTMLParserThread()
 {
-    ASSERT(m_queue.killed());
-}
-
-bool HTMLParserThread::start()
-{
-    MutexLocker lock(m_threadCreationMutex);
-    if (m_threadID)
-        return true;
-    m_threadID = createThread(HTMLParserThread::threadStart, this, "WebCore: HTMLParser");
-    return m_threadID;
-}
-
-void HTMLParserThread::stop()
-{
-    m_queue.kill();
-    waitForThreadCompletion(m_threadID);
 }
 
 HTMLParserThread* HTMLParserThread::shared()
 {
     static HTMLParserThread* thread;
-    if (!thread) {
-        thread = HTMLParserThread::create().leakPtr();
-        thread->start();
-    }
+    if (!thread)
+        thread = new HTMLParserThread;
     return thread;
 }
 
-void HTMLParserThread::postTask(const Closure& function)
+void HTMLParserThread::postTask(const Closure& closure)
 {
-    m_queue.append(adoptPtr(new Closure(function)));
-}
-
-void HTMLParserThread::threadStart(void* arg)
-{
-    HTMLParserThread* thread = static_cast<HTMLParserThread*>(arg);
-    thread->runLoop();
-}
-
-void HTMLParserThread::runLoop()
-{
-    {
-        // Wait for HTMLParserThread::start() to complete to have m_threadID
-        // established before starting the main loop.
-        MutexLocker lock(m_threadCreationMutex);
-    }
-    while (OwnPtr<Closure> function = m_queue.waitForMessage())
-        (*function)();
-
-    // stop() will wait to join the thread, so we do not detach here.
+    m_thread->postTask(new Task(closure));
 }
 
 } // namespace WebCore
diff --git a/third_party/WebKit/Source/core/html/parser/HTMLParserThread.h b/third_party/WebKit/Source/core/html/parser/HTMLParserThread.h
index 543f2b3b..138979a7 100644
--- a/third_party/WebKit/Source/core/html/parser/HTMLParserThread.h
+++ b/third_party/WebKit/Source/core/html/parser/HTMLParserThread.h
@@ -31,43 +31,22 @@
 #ifndef HTMLParserThread_h
 #define HTMLParserThread_h
 
-#include <wtf/Functional.h>
-#include <wtf/MessageQueue.h>
-#include <wtf/PassOwnPtr.h>
-#include <wtf/PassRefPtr.h>
-#include <wtf/Threading.h>
+#include "wtf/Functional.h"
+#include "wtf/OwnPtr.h"
+#include <public/WebThread.h>
 
 namespace WebCore {
 
-// FIXME:: Closure is the Chromium-name for Function<void()>, but we may want something else for WebCore.
-typedef Function<void()> Closure;
-
 class HTMLParserThread {
 public:
-    static PassOwnPtr<HTMLParserThread> create()
-    {
-        return adoptPtr(new HTMLParserThread());
-    }
-    ~HTMLParserThread();
-
     static HTMLParserThread* shared();
-
-    bool start();
-    void stop();
-
     void postTask(const Closure&);
 
-    ThreadIdentifier threadId() const { return m_threadID; }
-
 private:
     HTMLParserThread();
+    ~HTMLParserThread();
 
-    static void threadStart(void*);
-    void runLoop();
-
-    Mutex m_threadCreationMutex;
-    MessageQueue<Closure> m_queue;
-    ThreadIdentifier m_threadID;
+    OwnPtr<WebKit::WebThread> m_thread;
 };
 
 } // namespace WebCore
diff --git a/third_party/WebKit/Source/core/platform/Task.h b/third_party/WebKit/Source/core/platform/Task.h
new file mode 100644
index 0000000..fe0270a8
--- /dev/null
+++ b/third_party/WebKit/Source/core/platform/Task.h
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2013 Google Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *     * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ *     * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef Task_h
+#define Task_h
+
+#include "wtf/Functional.h"
+#include <public/WebThread.h>
+
+namespace WebCore {
+
+class Task : public WebKit::WebThread::Task {
+public:
+    explicit Task(const Closure& closure)
+        : m_closure(closure)
+    {
+    }
+
+    virtual void run() OVERRIDE
+    {
+        m_closure();
+    }
+
+private:
+    Closure m_closure;
+};
+
+} // namespace WebCore
+
+#endif // Task_h
diff --git a/third_party/WebKit/Source/wtf/Functional.h b/third_party/WebKit/Source/wtf/Functional.h
index a2fc1586..0f74a1b 100644
--- a/third_party/WebKit/Source/wtf/Functional.h
+++ b/third_party/WebKit/Source/wtf/Functional.h
@@ -774,9 +774,12 @@
     return Function<typename FunctionWrapper<FunctionType>::ResultType ()>(adoptRef(new BoundFunctionImpl<FunctionWrapper<FunctionType>, typename FunctionWrapper<FunctionType>::ResultType (A1, A2, A3, A4, A5, A6)>(FunctionWrapper<FunctionType>(function), a1, a2, a3, a4, a5, a6)));
 }
 
+typedef Function<void()> Closure;
+
 }
 
 using WTF::Function;
 using WTF::bind;
+using WTF::Closure;
 
 #endif // WTF_Functional_h