Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / core / dom / MainThreadTaskRunner.cpp
blob9a5e873306dbaa1fd1acf5f9370bdbea3532f114
1 /*
2 * Copyright (C) 2013 Google Inc. All Rights Reserved.
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.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, 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 COMPUTER, 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.
27 #include "config.h"
28 #include "core/dom/MainThreadTaskRunner.h"
30 #include "core/dom/ExecutionContext.h"
31 #include "core/dom/ExecutionContextTask.h"
32 #include "core/inspector/InspectorInstrumentation.h"
33 #include "public/platform/Platform.h"
34 #include "wtf/Assertions.h"
36 namespace blink {
38 class MainThreadTask : public WebTaskRunner::Task {
39 WTF_MAKE_NONCOPYABLE(MainThreadTask); WTF_MAKE_FAST_ALLOCATED(MainThreadTask);
40 public:
41 MainThreadTask(WeakPtrWillBeRawPtr<MainThreadTaskRunner> runner, PassOwnPtr<ExecutionContextTask> task, bool isInspectorTask)
42 : m_runner(runner)
43 , m_task(task)
44 , m_isInspectorTask(isInspectorTask)
48 void run() override;
50 private:
51 WeakPtrWillBeCrossThreadWeakPersistent<MainThreadTaskRunner> m_runner;
52 OwnPtr<ExecutionContextTask> m_task;
53 bool m_isInspectorTask;
56 void MainThreadTask::run()
58 ASSERT(isMainThread());
60 if (!m_runner.get())
61 return;
62 m_runner->perform(m_task.release(), m_isInspectorTask);
65 MainThreadTaskRunner::MainThreadTaskRunner(ExecutionContext* context)
66 : m_context(context)
67 #if !ENABLE(OILPAN)
68 , m_weakFactory(this)
69 #endif
70 , m_pendingTasksTimer(this, &MainThreadTaskRunner::pendingTasksTimerFired)
71 , m_suspended(false)
75 MainThreadTaskRunner::~MainThreadTaskRunner()
79 DEFINE_TRACE(MainThreadTaskRunner)
81 visitor->trace(m_context);
84 void MainThreadTaskRunner::postTask(const WebTraceLocation& location, PassOwnPtr<ExecutionContextTask> task)
86 if (!task->taskNameForInstrumentation().isEmpty())
87 InspectorInstrumentation::didPostExecutionContextTask(m_context, task.get());
88 Platform::current()->mainThread()->taskRunner()->postTask(location, new MainThreadTask(createWeakPointerToSelf(), task, false));
91 void MainThreadTaskRunner::postInspectorTask(const WebTraceLocation& location, PassOwnPtr<ExecutionContextTask> task)
93 Platform::current()->mainThread()->taskRunner()->postTask(location, new MainThreadTask(createWeakPointerToSelf(), task, true));
96 void MainThreadTaskRunner::perform(PassOwnPtr<ExecutionContextTask> task, bool isInspectorTask)
98 if (!isInspectorTask && (m_context->tasksNeedSuspension() || !m_pendingTasks.isEmpty())) {
99 m_pendingTasks.append(task);
100 return;
103 const bool instrumenting = !isInspectorTask && !task->taskNameForInstrumentation().isEmpty();
104 if (instrumenting)
105 InspectorInstrumentation::willPerformExecutionContextTask(m_context, task.get());
106 task->performTask(m_context);
107 if (instrumenting)
108 InspectorInstrumentation::didPerformExecutionContextTask(m_context);
111 void MainThreadTaskRunner::suspend()
113 ASSERT(!m_suspended);
114 m_pendingTasksTimer.stop();
115 m_suspended = true;
118 void MainThreadTaskRunner::resume()
120 ASSERT(m_suspended);
121 if (!m_pendingTasks.isEmpty())
122 m_pendingTasksTimer.startOneShot(0, FROM_HERE);
124 m_suspended = false;
127 void MainThreadTaskRunner::pendingTasksTimerFired(Timer<MainThreadTaskRunner>*)
129 while (!m_pendingTasks.isEmpty()) {
130 OwnPtr<ExecutionContextTask> task = m_pendingTasks[0].release();
131 m_pendingTasks.remove(0);
132 const bool instrumenting = !task->taskNameForInstrumentation().isEmpty();
133 if (instrumenting)
134 InspectorInstrumentation::willPerformExecutionContextTask(m_context, task.get());
135 task->performTask(m_context);
136 if (instrumenting)
137 InspectorInstrumentation::didPerformExecutionContextTask(m_context);
141 WeakPtrWillBeRawPtr<MainThreadTaskRunner> MainThreadTaskRunner::createWeakPointerToSelf()
143 #if ENABLE(OILPAN)
144 return this;
145 #else
146 return m_weakFactory.createWeakPtr();
147 #endif
150 } // namespace