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/Document.h"
     31 #include "core/dom/ExecutionContextTask.h"
     32 #include "wtf/Assertions.h"
     33 
     34 namespace WebCore {
     35 
     36 struct PerformTaskContext {
     37     WTF_MAKE_NONCOPYABLE(PerformTaskContext); WTF_MAKE_FAST_ALLOCATED;
     38 public:
     39     PerformTaskContext(WeakPtr<MainThreadTaskRunner> runner, PassOwnPtr<ExecutionContextTask> task)
     40         : m_runner(runner)
     41         , m_task(task)
     42     {
     43     }
     44 
     45     WeakPtr<MainThreadTaskRunner> m_runner;
     46     OwnPtr<ExecutionContextTask> m_task;
     47 
     48     static void didReceiveTask(void* untypedContext);
     49 };
     50 
     51 void PerformTaskContext::didReceiveTask(void* untypedContext)
     52 {
     53     ASSERT(isMainThread());
     54 
     55     OwnPtr<PerformTaskContext> self = adoptPtr(static_cast<PerformTaskContext*>(untypedContext));
     56     ASSERT(self);
     57 
     58     MainThreadTaskRunner* runner = self->m_runner.get();
     59     if (!runner)
     60         return;
     61     runner->perform(self->m_task.release());
     62 }
     63 
     64 MainThreadTaskRunner::MainThreadTaskRunner(ExecutionContext* context)
     65     : m_context(context)
     66     , m_weakFactory(this)
     67     , m_pendingTasksTimer(this, &MainThreadTaskRunner::pendingTasksTimerFired)
     68     , m_suspended(false)
     69 {
     70 }
     71 
     72 MainThreadTaskRunner::~MainThreadTaskRunner()
     73 {
     74 }
     75 
     76 void MainThreadTaskRunner::postTask(PassOwnPtr<ExecutionContextTask> task)
     77 {
     78     callOnMainThread(PerformTaskContext::didReceiveTask, new PerformTaskContext(m_weakFactory.createWeakPtr(), task));
     79 }
     80 
     81 void MainThreadTaskRunner::perform(PassOwnPtr<ExecutionContextTask> task)
     82 {
     83     if (m_context->tasksNeedSuspension() || !m_pendingTasks.isEmpty()) {
     84         m_pendingTasks.append(task);
     85         return;
     86     }
     87 
     88     task->performTask(m_context);
     89 }
     90 
     91 void MainThreadTaskRunner::suspend()
     92 {
     93     ASSERT(!m_suspended);
     94     m_pendingTasksTimer.stop();
     95     m_suspended = true;
     96 }
     97 
     98 void MainThreadTaskRunner::resume()
     99 {
    100     ASSERT(m_suspended);
    101     if (!m_pendingTasks.isEmpty())
    102         m_pendingTasksTimer.startOneShot(0, FROM_HERE);
    103 
    104     m_suspended = false;
    105 }
    106 
    107 void MainThreadTaskRunner::pendingTasksTimerFired(Timer<MainThreadTaskRunner>*)
    108 {
    109     while (!m_pendingTasks.isEmpty()) {
    110         OwnPtr<ExecutionContextTask> task = m_pendingTasks[0].release();
    111         m_pendingTasks.remove(0);
    112         task->performTask(m_context);
    113     }
    114 }
    115 
    116 } // namespace
    117