tor-browser

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

IdleRequest.cpp (2739B)


      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/. */
      6 
      7 #include "IdleRequest.h"
      8 
      9 #include "mozilla/TimeStamp.h"
     10 #include "mozilla/dom/IdleDeadline.h"
     11 #include "mozilla/dom/PerformanceTiming.h"
     12 #include "mozilla/dom/TimeoutManager.h"
     13 #include "mozilla/dom/WebTaskScheduler.h"
     14 #include "mozilla/dom/WindowBinding.h"
     15 #include "nsComponentManagerUtils.h"
     16 #include "nsGlobalWindowInner.h"
     17 #include "nsPIDOMWindow.h"
     18 
     19 namespace mozilla::dom {
     20 
     21 IdleRequest::IdleRequest(IdleRequestCallback* aCallback, uint32_t aHandle)
     22    : mCallback(aCallback), mHandle(aHandle), mTimeoutHandle(Nothing()) {
     23  MOZ_DIAGNOSTIC_ASSERT(mCallback);
     24 }
     25 
     26 IdleRequest::~IdleRequest() = default;
     27 
     28 NS_IMPL_CYCLE_COLLECTION_CLASS(IdleRequest)
     29 
     30 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(IdleRequest)
     31  NS_IMPL_CYCLE_COLLECTION_UNLINK(mCallback)
     32  if (tmp->isInList()) {
     33    tmp->remove();
     34  }
     35 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
     36 
     37 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(IdleRequest)
     38  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCallback)
     39 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
     40 
     41 void IdleRequest::SetTimeoutHandle(int32_t aHandle) {
     42  mTimeoutHandle = Some(aHandle);
     43 }
     44 
     45 int32_t IdleRequest::GetTimeoutHandle() const {
     46  MOZ_DIAGNOSTIC_ASSERT(mTimeoutHandle.isSome());
     47  return mTimeoutHandle.value();
     48 }
     49 
     50 void IdleRequest::IdleRun(nsPIDOMWindowInner* aWindow,
     51                          DOMHighResTimeStamp aDeadline, bool aDidTimeout) {
     52  MOZ_ASSERT(NS_IsMainThread());
     53  MOZ_DIAGNOSTIC_ASSERT(mCallback);
     54 
     55  RefPtr<IdleDeadline> deadline =
     56      new IdleDeadline(aWindow, aDidTimeout, aDeadline);
     57  RefPtr<IdleRequestCallback> callback(std::move(mCallback));
     58  MOZ_ASSERT(!mCallback);
     59 
     60  RefPtr<nsGlobalWindowInner> innerWindow = nsGlobalWindowInner::Cast(aWindow);
     61  // https://wicg.github.io/scheduling-apis/#sec-patches-invoke-idle-callbacks
     62  // Let state be a new scheduling state.
     63  RefPtr<WebTaskSchedulingState> newState = new WebTaskSchedulingState();
     64  // Set state’s priority source to the result of creating a fixed priority
     65  // unabortable task signal given "background" and realm.
     66  newState->SetPrioritySource(
     67      TaskSignal::Create(aWindow->AsGlobal(), TaskPriority::Background));
     68  // Set event loop’s current scheduling state to state.
     69  innerWindow->SetWebTaskSchedulingState(newState);
     70  callback->Call(*deadline, "requestIdleCallback handler");
     71  // Set event loop’s current scheduling state to null.
     72  innerWindow->SetWebTaskSchedulingState(nullptr);
     73 }
     74 
     75 }  // namespace mozilla::dom