1 | /* |
2 | * Copyright (C) 2009 Google Inc. All rights reserved. |
3 | * Copyright (C) 2017 Apple Inc. All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are |
7 | * met: |
8 | * |
9 | * * Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * * Redistributions in binary form must reproduce the above |
12 | * copyright notice, this list of conditions and the following disclaimer |
13 | * in the documentation and/or other materials provided with the |
14 | * distribution. |
15 | * * Neither the name of Google Inc. nor the names of its |
16 | * contributors may be used to endorse or promote products derived from |
17 | * this software without specific prior written permission. |
18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
31 | |
32 | #pragma once |
33 | |
34 | #include "ScriptExecutionContext.h" |
35 | #include <memory> |
36 | #include <wtf/MessageQueue.h> |
37 | |
38 | namespace WebCore { |
39 | |
40 | class ModePredicate; |
41 | class WorkerGlobalScope; |
42 | class WorkerSharedTimer; |
43 | |
44 | class WorkerRunLoop { |
45 | public: |
46 | WorkerRunLoop(); |
47 | ~WorkerRunLoop(); |
48 | |
49 | // Blocking call. Waits for tasks and timers, invokes the callbacks. |
50 | void run(WorkerGlobalScope*); |
51 | |
52 | enum WaitMode { WaitForMessage, DontWaitForMessage }; |
53 | |
54 | // Waits for a single task and returns. |
55 | MessageQueueWaitResult runInMode(WorkerGlobalScope*, const String& mode, WaitMode = WaitForMessage); |
56 | MessageQueueWaitResult runInDebuggerMode(WorkerGlobalScope&); |
57 | |
58 | void terminate(); |
59 | bool terminated() const { return m_messageQueue.killed(); } |
60 | |
61 | void postTask(ScriptExecutionContext::Task&&); |
62 | void postTaskAndTerminate(ScriptExecutionContext::Task&&); |
63 | void postTaskForMode(ScriptExecutionContext::Task&&, const String& mode); |
64 | void postDebuggerTask(ScriptExecutionContext::Task&&); |
65 | |
66 | unsigned long createUniqueId() { return ++m_uniqueId; } |
67 | |
68 | static String defaultMode(); |
69 | class Task { |
70 | WTF_MAKE_NONCOPYABLE(Task); WTF_MAKE_FAST_ALLOCATED; |
71 | public: |
72 | Task(ScriptExecutionContext::Task&&, const String& mode); |
73 | const String& mode() const { return m_mode; } |
74 | |
75 | private: |
76 | void performTask(WorkerGlobalScope*); |
77 | |
78 | ScriptExecutionContext::Task m_task; |
79 | String m_mode; |
80 | |
81 | friend class WorkerRunLoop; |
82 | }; |
83 | |
84 | private: |
85 | friend class RunLoopSetup; |
86 | MessageQueueWaitResult runInMode(WorkerGlobalScope*, const ModePredicate&, WaitMode); |
87 | |
88 | // Runs any clean up tasks that are currently in the queue and returns. |
89 | // This should only be called when the context is closed or loop has been terminated. |
90 | void runCleanupTasks(WorkerGlobalScope*); |
91 | |
92 | bool isBeingDebugged() const { return m_debugCount >= 1; } |
93 | |
94 | MessageQueue<Task> m_messageQueue; |
95 | std::unique_ptr<WorkerSharedTimer> m_sharedTimer; |
96 | int m_nestedCount { 0 }; |
97 | int m_debugCount { 0 }; |
98 | unsigned long m_uniqueId { 0 }; |
99 | }; |
100 | |
101 | } // namespace WebCore |
102 | |