Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / parser / HTMLParserThread.cpp
1 /*
2  * Copyright (C) 2013 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "core/html/parser/HTMLParserThread.h"
33
34 #include "platform/Task.h"
35 #include "platform/TaskSynchronizer.h"
36 #include "public/platform/Platform.h"
37 #include "wtf/PassOwnPtr.h"
38
39 namespace WebCore {
40
41 static HTMLParserThread* s_sharedThread = 0;
42
43 HTMLParserThread::HTMLParserThread()
44 {
45 }
46
47 HTMLParserThread::~HTMLParserThread()
48 {
49 }
50
51 void HTMLParserThread::init()
52 {
53     ASSERT(!s_sharedThread);
54     s_sharedThread = new HTMLParserThread;
55 }
56
57 void HTMLParserThread::setupHTMLParserThread()
58 {
59     m_pendingGCRunner = adoptPtr(new PendingGCRunner);
60     m_messageLoopInterruptor = adoptPtr(new MessageLoopInterruptor(&platformThread()));
61     platformThread().addTaskObserver(m_pendingGCRunner.get());
62     ThreadState::attach();
63     ThreadState::current()->addInterruptor(m_messageLoopInterruptor.get());
64 }
65
66 void HTMLParserThread::shutdown()
67 {
68     ASSERT(s_sharedThread);
69     // currentThread will always be non-null in production, but can be null in Chromium unit tests.
70     if (blink::Platform::current()->currentThread() && s_sharedThread->isRunning()) {
71         TaskSynchronizer taskSynchronizer;
72         s_sharedThread->postTask(WTF::bind(&HTMLParserThread::cleanupHTMLParserThread, s_sharedThread, &taskSynchronizer));
73         taskSynchronizer.waitForTaskCompletion();
74     }
75     delete s_sharedThread;
76     s_sharedThread = 0;
77 }
78
79 void HTMLParserThread::cleanupHTMLParserThread(TaskSynchronizer* taskSynchronizer)
80 {
81     ThreadState::current()->removeInterruptor(m_messageLoopInterruptor.get());
82     ThreadState::detach();
83     platformThread().removeTaskObserver(m_pendingGCRunner.get());
84     m_pendingGCRunner = nullptr;
85     m_messageLoopInterruptor = nullptr;
86     taskSynchronizer->taskCompleted();
87 }
88
89 HTMLParserThread* HTMLParserThread::shared()
90 {
91     return s_sharedThread;
92 }
93
94 blink::WebThread& HTMLParserThread::platformThread()
95 {
96     if (!isRunning()) {
97         m_thread = adoptPtr(blink::Platform::current()->createThread("HTMLParserThread"));
98         postTask(WTF::bind(&HTMLParserThread::setupHTMLParserThread, this));
99     }
100     return *m_thread;
101 }
102
103 bool HTMLParserThread::isRunning()
104 {
105     return !!m_thread;
106 }
107
108 void HTMLParserThread::postTask(const Closure& closure)
109 {
110     platformThread().postTask(new Task(closure));
111 }
112
113 } // namespace WebCore