OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef TaskRunnerHelper_h | 5 #ifndef TaskRunnerHelper_h |
6 #define TaskRunnerHelper_h | 6 #define TaskRunnerHelper_h |
7 | 7 |
| 8 #include <memory> |
8 #include "core/CoreExport.h" | 9 #include "core/CoreExport.h" |
| 10 #include "core/dom/ContextLifecycleObserver.h" |
| 11 #include "platform/WebTaskRunner.h" |
| 12 #include "platform/heap/Handle.h" |
9 #include "wtf/Allocator.h" | 13 #include "wtf/Allocator.h" |
10 #include "wtf/HashTraits.h" | 14 #include "wtf/HashTraits.h" |
| 15 #include "wtf/PtrUtil.h" |
11 | 16 |
12 namespace blink { | 17 namespace blink { |
13 | 18 |
14 class Document; | 19 class Document; |
15 class ExecutionContext; | 20 class ExecutionContext; |
16 class LocalFrame; | 21 class LocalFrame; |
17 class ScriptState; | 22 class ScriptState; |
18 class WebTaskRunner; | 23 class WorkerOrWorkletGlobalScope; |
| 24 class WorkerThread; |
19 | 25 |
20 enum class TaskType : unsigned { | 26 enum class TaskType : unsigned { |
21 // Speced tasks and related internal tasks should be posted to one of | 27 // Speced tasks and related internal tasks should be posted to one of |
22 // the following task runners. These task runners may be throttled. | 28 // the following task runners. These task runners may be throttled. |
23 | 29 |
24 // https://html.spec.whatwg.org/multipage/webappapis.html#generic-task-sources | 30 // https://html.spec.whatwg.org/multipage/webappapis.html#generic-task-sources |
25 // | 31 // |
26 // This task source is used for features that react to DOM manipulations, such | 32 // This task source is used for features that react to DOM manipulations, such |
27 // as things that happen in a non-blocking fashion when an element is inserted | 33 // as things that happen in a non-blocking fashion when an element is inserted |
28 // into the document. | 34 // into the document. |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 // may be throttled. | 120 // may be throttled. |
115 // | 121 // |
116 // UnspecedLoading type should be used for all tasks associated with | 122 // UnspecedLoading type should be used for all tasks associated with |
117 // loading page content, UnspecedTimer should be used for all other purposes. | 123 // loading page content, UnspecedTimer should be used for all other purposes. |
118 UnspecedTimer, | 124 UnspecedTimer, |
119 UnspecedLoading, | 125 UnspecedLoading, |
120 | 126 |
121 // Tasks that must not be throttled should be posted here, but the usage | 127 // Tasks that must not be throttled should be posted here, but the usage |
122 // should be very limited. | 128 // should be very limited. |
123 Unthrottled, | 129 Unthrottled, |
| 130 |
| 131 // Add a new type before this. |
| 132 NumberOfTaskTypes, |
124 }; | 133 }; |
125 | 134 |
126 // HashTraits for TaskType. | 135 // HashTraits for TaskType. |
127 struct TaskTypeTraits : WTF::GenericHashTraits<TaskType> { | 136 struct TaskTypeTraits : WTF::GenericHashTraits<TaskType> { |
128 static const bool emptyValueIsZero = false; | 137 static const bool emptyValueIsZero = false; |
129 static TaskType emptyValue() { return static_cast<TaskType>(-1); } | 138 static TaskType emptyValue() { return static_cast<TaskType>(-1); } |
130 static void constructDeletedValue(TaskType& slot, bool) { | 139 static void constructDeletedValue(TaskType& slot, bool) { |
131 slot = static_cast<TaskType>(-2); | 140 slot = static_cast<TaskType>(-2); |
132 } | 141 } |
133 static bool isDeletedValue(TaskType value) { | 142 static bool isDeletedValue(TaskType value) { |
134 return value == static_cast<TaskType>(-2); | 143 return value == static_cast<TaskType>(-2); |
135 } | 144 } |
136 }; | 145 }; |
137 | 146 |
| 147 class CORE_EXPORT FrameTaskRunnersHolder final |
| 148 : public GarbageCollectedFinalized<FrameTaskRunnersHolder>, |
| 149 public ContextLifecycleObserver { |
| 150 USING_GARBAGE_COLLECTED_MIXIN(FrameTaskRunnersHolder); |
| 151 WTF_MAKE_NONCOPYABLE(FrameTaskRunnersHolder); |
| 152 |
| 153 public: |
| 154 static FrameTaskRunnersHolder* create(LocalFrame* frame) { |
| 155 return new FrameTaskRunnersHolder(frame); |
| 156 } |
| 157 |
| 158 // Might return nullptr for unsupported task types. |
| 159 RefPtr<WebTaskRunner> get(TaskType); |
| 160 |
| 161 DECLARE_VIRTUAL_TRACE(); |
| 162 |
| 163 private: |
| 164 using TaskRunnerHashMap = HashMap<TaskType, |
| 165 RefPtr<WebTaskRunner>, |
| 166 WTF::IntHash<TaskType>, |
| 167 TaskTypeTraits>; |
| 168 |
| 169 // LocalFrame could be nullptr if the worker is not associated with a |
| 170 // particular local frame. |
| 171 explicit FrameTaskRunnersHolder(LocalFrame*); |
| 172 |
| 173 void contextDestroyed(ExecutionContext*) override; |
| 174 |
| 175 Mutex m_taskRunnersMutex; |
| 176 TaskRunnerHashMap m_taskRunners; |
| 177 }; |
| 178 |
138 // A set of helper functions to get a WebTaskRunner for TaskType and a context | 179 // A set of helper functions to get a WebTaskRunner for TaskType and a context |
139 // object. The posted tasks are guaranteed to run in a sequence if they have the | 180 // object. The posted tasks are guaranteed to run in a sequence if they have the |
140 // same TaskType and the context objects belong to the same frame. | 181 // same TaskType and the context objects belong to the same frame. |
141 class CORE_EXPORT TaskRunnerHelper final { | 182 class CORE_EXPORT FrameTaskRunnerHelper { |
142 STATIC_ONLY(TaskRunnerHelper); | 183 STATIC_ONLY(FrameTaskRunnerHelper); |
143 | 184 |
144 public: | 185 public: |
| 186 static void setTaskRunners(ExecutionContext*, FrameTaskRunnersHolder*); |
| 187 |
| 188 // Used for posting a task from the main thread. |
145 static RefPtr<WebTaskRunner> get(TaskType, LocalFrame*); | 189 static RefPtr<WebTaskRunner> get(TaskType, LocalFrame*); |
146 static RefPtr<WebTaskRunner> get(TaskType, Document*); | 190 static RefPtr<WebTaskRunner> get(TaskType, Document*); |
147 static RefPtr<WebTaskRunner> get(TaskType, ExecutionContext*); | 191 static RefPtr<WebTaskRunner> get(TaskType, ExecutionContext*); |
148 static RefPtr<WebTaskRunner> get(TaskType, ScriptState*); | 192 static RefPtr<WebTaskRunner> get(TaskType, ScriptState*); |
| 193 |
| 194 // Used for posting a task from a worker thread. |
| 195 static RefPtr<WebTaskRunner> get(TaskType, WorkerThread*); |
| 196 static RefPtr<WebTaskRunner> get(TaskType, WorkerOrWorkletGlobalScope*); |
149 }; | 197 }; |
150 | 198 |
| 199 // TODO(nhiroki): Remove this workaround. |
| 200 using TaskRunnerHelper = FrameTaskRunnerHelper; |
| 201 |
151 } // namespace blink | 202 } // namespace blink |
152 | 203 |
153 #endif | 204 #endif |
OLD | NEW |