tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

WebTaskSchedulerMainThread.cpp (2265B)


      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/. */
      7 
      8 #include "WebTaskSchedulerMainThread.h"
      9 
     10 #include "mozilla/dom/TimeoutManager.h"
     11 #include "nsContentUtils.h"
     12 #include "nsGlobalWindowInner.h"
     13 
     14 namespace mozilla::dom {
     15 
     16 constinit uint32_t gNumNormalOrHighPriorityQueuesHaveTaskScheduledMainThread =
     17    0;
     18 
     19 NS_IMETHODIMP WebTaskMainThreadRunnable::Run() {
     20  if (mScheduler) {
     21    RefPtr<WebTask> task = mScheduler->GetNextTask(true /* aIsMainThread */);
     22    if (task) {
     23      task->Run();
     24    }
     25  }
     26  return NS_OK;
     27 }
     28 
     29 nsresult WebTaskSchedulerMainThread::SetTimeoutForDelayedTask(
     30    WebTask* aTask, uint64_t aDelay, EventQueuePriority aPriority) {
     31  JSContext* cx = nsContentUtils::GetCurrentJSContext();
     32  if (!cx) {
     33    return NS_ERROR_UNEXPECTED;
     34  }
     35  nsIGlobalObject* global = GetParentObject();
     36  MOZ_ASSERT(global);
     37 
     38  RefPtr<DelayedWebTaskHandler> handler =
     39      new DelayedWebTaskHandler(cx, this, aTask, aPriority);
     40 
     41  int32_t delay = aDelay > INT32_MAX ? INT32_MAX : (int32_t)aDelay;
     42 
     43  int32_t handle;
     44  return nsGlobalWindowInner::Cast(global->GetAsInnerWindow())
     45      ->GetTimeoutManager()
     46      ->SetTimeout(handler, delay, /* aIsInterval */ false,
     47                   Timeout::Reason::eDelayedWebTaskTimeout, &handle);
     48 }
     49 
     50 bool WebTaskSchedulerMainThread::DispatchEventLoopRunnable(
     51    EventQueuePriority aPriority) {
     52  RefPtr<WebTaskMainThreadRunnable> runnable =
     53      new WebTaskMainThreadRunnable(this);
     54 
     55  MOZ_ALWAYS_SUCCEEDS(
     56      NS_DispatchToMainThreadQueue(runnable.forget(), aPriority));
     57  return true;
     58 }
     59 
     60 void WebTaskSchedulerMainThread::
     61    IncreaseNumNormalOrHighPriorityQueuesHaveTaskScheduled() {
     62  ++gNumNormalOrHighPriorityQueuesHaveTaskScheduledMainThread;
     63 }
     64 
     65 void WebTaskSchedulerMainThread::
     66    DecreaseNumNormalOrHighPriorityQueuesHaveTaskScheduled() {
     67  MOZ_DIAGNOSTIC_ASSERT(
     68      gNumNormalOrHighPriorityQueuesHaveTaskScheduledMainThread > 0);
     69  --gNumNormalOrHighPriorityQueuesHaveTaskScheduledMainThread;
     70 }
     71 }  // namespace mozilla::dom