Home | History | Annotate | Download | only in dom
      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
      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.
     12  *
     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.
     24  *
     25  */
     26 
     27 #include "config.h"
     28 #include "core/dom/MainThreadTaskRunner.h"
     29 
     30 #include "core/dom/ExecutionContext.h"
     31 #include "core/dom/ExecutionContextTask.h"
     32 #include "core/inspector/InspectorInstrumentation.h"
     33 #include "wtf/Assertions.h"
     34 #include "wtf/MainThread.h"
     35 
     36 namespace blink {
     37 
     38 struct PerformTaskContext {
     39     WTF_MAKE_NONCOPYABLE(PerformTaskContext); WTF_MAKE_FAST_ALLOCATED;
     40 public:
     41     PerformTaskContext(WeakPtr<MainThreadTaskRunner> runner, PassOwnPtr<ExecutionContextTask> task, bool isInspectorTask)
     42         : m_runner(runner)
     43         , m_task(task)
     44         , m_isInspectorTask(isInspectorTask)
     45     {
     46     }
     47 
     48     WeakPtr<MainThreadTaskRunner> m_runner;
     49     OwnPtr<ExecutionContextTask> m_task;
     50     bool m_isInspectorTask;
     51 
     52     static void didReceiveTask(void* untypedContext);
     53 };
     54 
     55 void PerformTaskContext::didReceiveTask(void* untypedContext)
     56 {
     57     ASSERT(isMainThread());
     58 
     59     OwnPtr<PerformTaskContext> self = adoptPtr(static_cast<PerformTaskContext*>(untypedContext));
     60     ASSERT(self);
     61 
     62     MainThreadTaskRunner* runner = self->m_runner.get();
     63     if (!runner)
     64         return;
     65     runner->perform(self->m_task.release(), self->m_isInspectorTask);
     66 }
     67 
     68 MainThreadTaskRunner::MainThreadTaskRunner(ExecutionContext* context)
     69     : m_context(context)
     70     , m_weakFactory(this)
     71     , m_pendingTasksTimer(this, &MainThreadTaskRunner::pendingTasksTimerFired)
     72     , m_suspended(false)
     73 {
     74 }
     75 
     76 MainThreadTaskRunner::~MainThreadTaskRunner()
     77 {
     78 }
     79 
     80 void MainThreadTaskRunner::postTask(PassOwnPtr<ExecutionContextTask> task)
     81 {
     82     if (!task->taskNameForInstrumentation().isEmpty())
     83         InspectorInstrumentation::didPostExecutionContextTask(m_context, task.get());
     84     callOnMainThread(PerformTaskContext::didReceiveTask, new PerformTaskContext(m_weakFactory.createWeakPtr(), task, false));
     85 }
     86 
     87 void MainThreadTaskRunner::postInspectorTask(PassOwnPtr<ExecutionContextTask> task)
     88 {
     89     callOnMainThread(PerformTaskContext::didReceiveTask, new PerformTaskContext(m_weakFactory.createWeakPtr(), task, true));
     90 }
     91 
     92 void MainThreadTaskRunner::perform(PassOwnPtr<ExecutionContextTask> task, bool isInspectorTask)
     93 {
     94     if (!isInspectorTask && (m_context->tasksNeedSuspension() || !m_pendingTasks.isEmpty())) {
     95         m_pendingTasks.append(task);
     96         return;
     97     }
     98 
     99     const bool instrumenting = !isInspectorTask && !task->taskNameForInstrumentation().isEmpty();
    100     if (instrumenting)
    101         InspectorInstrumentation::willPerformExecutionContextTask(m_context, task.get());
    102     task->performTask(m_context);
    103     if (instrumenting)
    104         InspectorInstrumentation::didPerformExecutionContextTask(m_context);
    105 }
    106 
    107 void MainThreadTaskRunner::suspend()
    108 {
    109     ASSERT(!m_suspended);
    110     m_pendingTasksTimer.stop();
    111     m_suspended = true;
    112 }
    113 
    114 void MainThreadTaskRunner::resume()
    115 {
    116     ASSERT(m_suspended);
    117     if (!m_pendingTasks.isEmpty())
    118         m_pendingTasksTimer.startOneShot(0, FROM_HERE);
    119 
    120     m_suspended = false;
    121 }
    122 
    123 void MainThreadTaskRunner::pendingTasksTimerFired(Timer<MainThreadTaskRunner>*)
    124 {
    125     while (!m_pendingTasks.isEmpty()) {
    126         OwnPtr<ExecutionContextTask> task = m_pendingTasks[0].release();
    127         m_pendingTasks.remove(0);
    128         const bool instrumenting = !task->taskNameForInstrumentation().isEmpty();
    129         if (instrumenting)
    130             InspectorInstrumentation::willPerformExecutionContextTask(m_context, task.get());
    131         task->performTask(m_context);
    132         if (instrumenting)
    133             InspectorInstrumentation::didPerformExecutionContextTask(m_context);
    134     }
    135 }
    136 
    137 } // namespace
    138