Bug 1824753 [wpt PR 39216] - [FLEDGE] Add WPT test that FLEDGE is not allowed in...
[gecko.git] / dom / webscheduling / WebTaskSchedulerMainThread.cpp
blobed06fd0f0a532061c47d6407e7722f65f7138ab7
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:expandtab:shiftwidth=2:tabstop=2:
3 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "mozilla/TaskController.h"
9 #include "mozilla/dom/TimeoutManager.h"
11 #include "nsContentUtils.h"
12 #include "WebTaskSchedulerMainThread.h"
14 namespace mozilla::dom {
16 NS_IMETHODIMP WebTaskMainThreadRunnable::Run() {
17 if (mScheduler) {
18 RefPtr<WebTask> task = mScheduler->GetNextTask();
19 if (task) {
20 task->Run();
23 return NS_OK;
26 nsresult WebTaskSchedulerMainThread::SetTimeoutForDelayedTask(WebTask* aTask,
27 uint64_t aDelay) {
28 JSContext* cx = nsContentUtils::GetCurrentJSContext();
29 if (!cx) {
30 return NS_ERROR_UNEXPECTED;
32 nsIGlobalObject* global = GetParentObject();
33 MOZ_ASSERT(global);
35 RefPtr<DelayedWebTaskHandler> handler =
36 new DelayedWebTaskHandler(cx, this, aTask);
38 int32_t delay = aDelay > INT32_MAX ? INT32_MAX : (int32_t)aDelay;
40 int32_t handle;
41 return global->AsInnerWindow()->TimeoutManager().SetTimeout(
42 handler, delay, /* aIsInterval */ false,
43 Timeout::Reason::eDelayedWebTaskTimeout, &handle);
46 bool WebTaskSchedulerMainThread::DispatchEventLoopRunnable() {
47 RefPtr<WebTaskMainThreadRunnable> runnable =
48 new WebTaskMainThreadRunnable(this);
50 MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThreadQueue(runnable.forget(),
51 EventQueuePriority::Normal));
52 return true;
54 } // namespace mozilla::dom