| 1 | /* |
| 2 | * Copyright (C) 2014 Yoav Weiss (yoav@yoav.ws) |
| 3 | * Copyright (C) 2015 Akamai Technologies Inc. All rights reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Library General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Library General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Library General Public License |
| 16 | * along with this library; see the file COPYING.LIB. If not, write to |
| 17 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 18 | * Boston, MA 02110-1301, USA. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #pragma once |
| 23 | |
| 24 | #include "Timer.h" |
| 25 | #include <wtf/Forward.h> |
| 26 | #include <wtf/Vector.h> |
| 27 | |
| 28 | namespace WebCore { |
| 29 | |
| 30 | class MicrotaskQueue; |
| 31 | class ScriptExecutionContext; |
| 32 | |
| 33 | class Microtask { |
| 34 | public: |
| 35 | virtual ~Microtask() |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | enum class Result { |
| 40 | Done, |
| 41 | KeepInQueue |
| 42 | }; |
| 43 | |
| 44 | virtual Result run() = 0; |
| 45 | |
| 46 | protected: |
| 47 | void removeSelfFromQueue(MicrotaskQueue&); |
| 48 | }; |
| 49 | |
| 50 | class VoidMicrotask final : public Microtask { |
| 51 | WTF_MAKE_FAST_ALLOCATED; |
| 52 | public: |
| 53 | explicit VoidMicrotask(Function<void()>&& function) |
| 54 | : m_function(WTFMove(function)) |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | private: |
| 59 | Result run() final |
| 60 | { |
| 61 | m_function(); |
| 62 | return Result::Done; |
| 63 | } |
| 64 | |
| 65 | Function<void()> m_function; |
| 66 | }; |
| 67 | |
| 68 | class MicrotaskQueue { |
| 69 | friend NeverDestroyed<MicrotaskQueue>; |
| 70 | friend class Microtask; |
| 71 | public: |
| 72 | WEBCORE_EXPORT static MicrotaskQueue& mainThreadQueue(); |
| 73 | WEBCORE_EXPORT static MicrotaskQueue& contextQueue(ScriptExecutionContext&); |
| 74 | |
| 75 | WEBCORE_EXPORT MicrotaskQueue(); |
| 76 | WEBCORE_EXPORT ~MicrotaskQueue(); |
| 77 | |
| 78 | WEBCORE_EXPORT void append(std::unique_ptr<Microtask>&&); |
| 79 | WEBCORE_EXPORT void performMicrotaskCheckpoint(); |
| 80 | |
| 81 | private: |
| 82 | WEBCORE_EXPORT void remove(const Microtask&); |
| 83 | |
| 84 | void timerFired(); |
| 85 | |
| 86 | bool m_performingMicrotaskCheckpoint = false; |
| 87 | Vector<std::unique_ptr<Microtask>> m_microtaskQueue; |
| 88 | |
| 89 | // FIXME: Instead of a Timer, we should have a centralized Event Loop that calls performMicrotaskCheckpoint() |
| 90 | // on every iteration, implementing https://html.spec.whatwg.org/multipage/webappapis.html#processing-model-9 |
| 91 | Timer m_timer; |
| 92 | }; |
| 93 | |
| 94 | } // namespace WebCore |
| 95 | |