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
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.
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"
38 class MainThreadTask
: public WebTaskRunner::Task
{
39 WTF_MAKE_NONCOPYABLE(MainThreadTask
); WTF_MAKE_FAST_ALLOCATED(MainThreadTask
);
41 MainThreadTask(WeakPtrWillBeRawPtr
<MainThreadTaskRunner
> runner
, PassOwnPtr
<ExecutionContextTask
> task
, bool isInspectorTask
)
44 , m_isInspectorTask(isInspectorTask
)
51 WeakPtrWillBeCrossThreadWeakPersistent
<MainThreadTaskRunner
> m_runner
;
52 OwnPtr
<ExecutionContextTask
> m_task
;
53 bool m_isInspectorTask
;
56 void MainThreadTask::run()
58 ASSERT(isMainThread());
62 m_runner
->perform(m_task
.release(), m_isInspectorTask
);
65 MainThreadTaskRunner::MainThreadTaskRunner(ExecutionContext
* context
)
70 , m_pendingTasksTimer(this, &MainThreadTaskRunner::pendingTasksTimerFired
)
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
);
103 const bool instrumenting
= !isInspectorTask
&& !task
->taskNameForInstrumentation().isEmpty();
105 InspectorInstrumentation::willPerformExecutionContextTask(m_context
, task
.get());
106 task
->performTask(m_context
);
108 InspectorInstrumentation::didPerformExecutionContextTask(m_context
);
111 void MainThreadTaskRunner::suspend()
113 ASSERT(!m_suspended
);
114 m_pendingTasksTimer
.stop();
118 void MainThreadTaskRunner::resume()
121 if (!m_pendingTasks
.isEmpty())
122 m_pendingTasksTimer
.startOneShot(0, FROM_HERE
);
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();
134 InspectorInstrumentation::willPerformExecutionContextTask(m_context
, task
.get());
135 task
->performTask(m_context
);
137 InspectorInstrumentation::didPerformExecutionContextTask(m_context
);
141 WeakPtrWillBeRawPtr
<MainThreadTaskRunner
> MainThreadTaskRunner::createWeakPointerToSelf()
146 return m_weakFactory
.createWeakPtr();