1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:expandtab:shiftwidth=2:tabstop=2:
*/
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_dom_WebTaskScheduler_h
#define mozilla_dom_WebTaskScheduler_h
#include "nsThreadUtils.h"
#include "nsPIDOMWindow.h"
#include "nsWrapperCache.h"
#include "nsClassHashtable.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/AbortFollower.h"
#include "mozilla/dom/TimeoutHandler.h"
#include "mozilla/dom/WebTaskSchedulingBinding.h"
namespace mozilla::dom {
class WebTask : public LinkedListElement<RefPtr<WebTask>>,
public AbortFollower,
public SupportsWeakPtr {
friend class WebTaskScheduler;
public:
MOZ_CAN_RUN_SCRIPT bool Run();
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_CLASS(WebTask)
WebTask(uint32_t aEnqueueOrder, SchedulerPostTaskCallback& aCallback,
Promise* aPromise)
: mEnqueueOrder(aEnqueueOrder),
mCallback(&aCallback),
mPromise(aPromise),
mHasScheduled(false) {}
void RunAbortAlgorithm() override;
bool HasScheduled() const { return mHasScheduled; }
uint32_t EnqueueOrder() const { return mEnqueueOrder; }
private:
void SetHasScheduled(bool aHasScheduled) { mHasScheduled = aHasScheduled; }
uint32_t mEnqueueOrder;
RefPtr<SchedulerPostTaskCallback> mCallback;
RefPtr<Promise> mPromise;
bool mHasScheduled;
~WebTask() = default;
};
class WebTaskQueue {
public:
WebTaskQueue() = default;
TaskPriority Priority() const { return mPriority; }
void SetPriority(TaskPriority aNewPriority) { mPriority = aNewPriority; }
LinkedList<RefPtr<WebTask>>& Tasks() { return mTasks; }
void AddTask(WebTask* aTask) { mTasks.insertBack(aTask); }
// TODO: To optimize it, we could have the scheduled and unscheduled
// tasks stored separately.
WebTask* GetFirstScheduledTask() {
for (const auto& task : mTasks) {
if (task->HasScheduled()) {
return task;
}
}
return nullptr;
}
~WebTaskQueue() { mTasks.clear(); }
private:
TaskPriority mPriority = TaskPriority::User_visible;
LinkedList<RefPtr<WebTask>> mTasks;
};
class WebTaskSchedulerMainThread;
class WebTaskSchedulerWorker;
class WebTaskScheduler : public nsWrapperCache, public SupportsWeakPtr {
friend class DelayedWebTaskHandler;
public:
NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(WebTaskScheduler)
NS_DECL_CYCLE_COLLECTION_NATIVE_WRAPPERCACHE_CLASS(WebTaskScheduler)
static already_AddRefed<WebTaskSchedulerMainThread> CreateForMainThread(
nsGlobalWindowInner* aWindow);
static already_AddRefed<WebTaskSchedulerWorker> CreateForWorker(
WorkerPrivate* aWorkerPrivate);
explicit WebTaskScheduler(nsIGlobalObject* aParent);
already_AddRefed<Promise> PostTask(SchedulerPostTaskCallback& aCallback,
const SchedulerPostTaskOptions& aOptions);
nsIGlobalObject* GetParentObject() const { return mParent; }
virtual JSObject* WrapObject(JSContext* cx,
JS::Handle<JSObject*> aGivenProto) override;
WebTask* GetNextTask() const;
virtual void Disconnect();
void RunTaskSignalPriorityChange(TaskSignal* aTaskSignal);
protected:
virtual ~WebTaskScheduler() = default;
nsCOMPtr<nsIGlobalObject> mParent;
uint32_t mNextEnqueueOrder;
private:
already_AddRefed<WebTask> CreateTask(
WebTaskQueue& aQueue, const Optional<OwningNonNull<AbortSignal>>& aSignal,
SchedulerPostTaskCallback& aCallback, Promise* aPromise);
bool QueueTask(WebTask* aTask);
WebTaskQueue& SelectTaskQueue(
const Optional<OwningNonNull<AbortSignal>>& aSignal,
const Optional<TaskPriority>& aPriority);
virtual nsresult SetTimeoutForDelayedTask(WebTask* aTask,
uint64_t aDelay) = 0;
virtual bool DispatchEventLoopRunnable() = 0;
nsClassHashtable<nsUint32HashKey, WebTaskQueue> mStaticPriorityTaskQueues;
nsClassHashtable<nsPtrHashKey<TaskSignal>, WebTaskQueue>
mDynamicPriorityTaskQueues;
};
class DelayedWebTaskHandler final : public TimeoutHandler {
public:
DelayedWebTaskHandler(JSContext* aCx, WebTaskScheduler* aScheduler,
WebTask* aTask)
: TimeoutHandler(aCx), mScheduler(aScheduler), mWebTask(aTask) {}
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_CLASS(DelayedWebTaskHandler)
MOZ_CAN_RUN_SCRIPT bool Call(const char* /* unused */) override {
if (mScheduler && mWebTask) {
MOZ_ASSERT(!mWebTask->HasScheduled());
if (!mScheduler->QueueTask(mWebTask)) {
return false;
}
}
return true;
}
private:
~DelayedWebTaskHandler() override = default;
WeakPtr<WebTaskScheduler> mScheduler;
// WebTask gets added to WebTaskQueue, and WebTaskQueue keeps its alive.
WeakPtr<WebTask> mWebTask;
};
} // namespace mozilla::dom
#endif
|