1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "WebTaskSchedulerWorker.h"
8 #include "mozilla/dom/WorkerScope.h"
9 #include "mozilla/dom/TimeoutManager.h"
11 namespace mozilla::dom
{
13 WebTaskWorkerRunnable::WebTaskWorkerRunnable(
14 WorkerPrivate
* aWorkerPrivate
, WebTaskSchedulerWorker
* aSchedulerWorker
)
15 : WorkerSameThreadRunnable(aWorkerPrivate
),
16 mSchedulerWorker(aSchedulerWorker
) {
17 MOZ_ASSERT(mSchedulerWorker
);
20 WebTaskSchedulerWorker::WebTaskSchedulerWorker(WorkerPrivate
* aWorkerPrivate
)
21 : WebTaskScheduler(aWorkerPrivate
->GlobalScope()),
22 mWorkerPrivate(aWorkerPrivate
) {}
24 bool WebTaskWorkerRunnable::WorkerRun(JSContext
* aCx
,
25 WorkerPrivate
* aWorkerPrivate
) {
26 aWorkerPrivate
->AssertIsOnWorkerThread();
28 if (mSchedulerWorker
) {
29 RefPtr
<WebTask
> task
= mSchedulerWorker
->GetNextTask();
37 nsresult
WebTaskSchedulerWorker::SetTimeoutForDelayedTask(WebTask
* aTask
,
39 JSContext
* cx
= nsContentUtils::GetCurrentJSContext();
41 return NS_ERROR_UNEXPECTED
;
43 RefPtr
<DelayedWebTaskHandler
> handler
=
44 new DelayedWebTaskHandler(cx
, this, aTask
);
47 int32_t delay
= aDelay
> INT32_MAX
? INT32_MAX
: (int32_t)aDelay
;
48 mWorkerPrivate
->SetTimeout(cx
, handler
, delay
,
49 /* aIsInterval */ false,
50 Timeout::Reason::eDelayedWebTaskTimeout
, rv
);
51 return rv
.StealNSResult();
54 bool WebTaskSchedulerWorker::DispatchEventLoopRunnable() {
55 RefPtr
<WebTaskWorkerRunnable
> runnable
=
56 new WebTaskWorkerRunnable(mWorkerPrivate
, this);
57 return runnable
->Dispatch();
59 } // namespace mozilla::dom