Home | History | Annotate | Download | only in custom
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "config.h"
      6 #include "core/dom/custom/CustomElementMicrotaskRunQueue.h"
      7 
      8 #include "core/dom/Microtask.h"
      9 #include "core/dom/custom/CustomElementAsyncImportMicrotaskQueue.h"
     10 #include "core/dom/custom/CustomElementSyncMicrotaskQueue.h"
     11 #include "core/html/imports/HTMLImportLoader.h"
     12 
     13 #include <stdio.h>
     14 
     15 namespace blink {
     16 
     17 DEFINE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(CustomElementMicrotaskRunQueue)
     18 
     19 CustomElementMicrotaskRunQueue::CustomElementMicrotaskRunQueue()
     20     : m_weakFactory(this)
     21     , m_syncQueue(CustomElementSyncMicrotaskQueue::create())
     22     , m_asyncQueue(CustomElementAsyncImportMicrotaskQueue::create())
     23     , m_dispatchIsPending(false)
     24 {
     25 }
     26 
     27 void CustomElementMicrotaskRunQueue::enqueue(HTMLImportLoader* parentLoader, PassOwnPtrWillBeRawPtr<CustomElementMicrotaskStep> step, bool importIsSync)
     28 {
     29     if (importIsSync) {
     30         if (parentLoader)
     31             parentLoader->microtaskQueue()->enqueue(step);
     32         else
     33             m_syncQueue->enqueue(step);
     34     } else {
     35         m_asyncQueue->enqueue(step);
     36     }
     37 
     38     requestDispatchIfNeeded();
     39 }
     40 
     41 void CustomElementMicrotaskRunQueue::dispatchIfAlive(WeakPtr<CustomElementMicrotaskRunQueue> self)
     42 {
     43     if (self.get())
     44         self->dispatch();
     45 }
     46 
     47 void CustomElementMicrotaskRunQueue::requestDispatchIfNeeded()
     48 {
     49     if (m_dispatchIsPending || isEmpty())
     50         return;
     51     Microtask::enqueueMicrotask(WTF::bind(&CustomElementMicrotaskRunQueue::dispatchIfAlive, m_weakFactory.createWeakPtr()));
     52     m_dispatchIsPending = true;
     53 }
     54 
     55 void CustomElementMicrotaskRunQueue::trace(Visitor* visitor)
     56 {
     57     visitor->trace(m_syncQueue);
     58     visitor->trace(m_asyncQueue);
     59 }
     60 
     61 void CustomElementMicrotaskRunQueue::dispatch()
     62 {
     63     m_dispatchIsPending = false;
     64     m_syncQueue->dispatch();
     65     if (m_syncQueue->isEmpty())
     66         m_asyncQueue->dispatch();
     67 }
     68 
     69 bool CustomElementMicrotaskRunQueue::isEmpty() const
     70 {
     71     return m_syncQueue->isEmpty() && m_asyncQueue->isEmpty();
     72 }
     73 
     74 #if !defined(NDEBUG)
     75 void CustomElementMicrotaskRunQueue::show(unsigned indent)
     76 {
     77     fprintf(stderr, "Sync:\n");
     78     m_syncQueue->show(indent);
     79     fprintf(stderr, "Async:\n");
     80     m_asyncQueue->show(indent);
     81 }
     82 #endif
     83 
     84 } // namespace blink
     85