tor-browser

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

IdleDeadline.cpp (2638B)


      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 "mozilla/dom/IdleDeadline.h"
      8 
      9 #include <algorithm>
     10 
     11 #include "mozilla/dom/Document.h"
     12 #include "mozilla/dom/IdleDeadlineBinding.h"
     13 #include "mozilla/dom/Performance.h"
     14 #include "nsCOMPtr.h"
     15 #include "nsCycleCollectionParticipant.h"
     16 #include "nsDOMNavigationTiming.h"
     17 #include "nsPIDOMWindow.h"
     18 
     19 namespace mozilla::dom {
     20 
     21 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(IdleDeadline, mWindow, mGlobal)
     22 NS_IMPL_CYCLE_COLLECTING_ADDREF(IdleDeadline)
     23 NS_IMPL_CYCLE_COLLECTING_RELEASE(IdleDeadline)
     24 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(IdleDeadline)
     25  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
     26  NS_INTERFACE_MAP_ENTRY(nsISupports)
     27 NS_INTERFACE_MAP_END
     28 
     29 IdleDeadline::IdleDeadline(nsPIDOMWindowInner* aWindow, bool aDidTimeout,
     30                           DOMHighResTimeStamp aDeadline)
     31    : mWindow(aWindow), mDidTimeout(aDidTimeout), mDeadline(aDeadline) {
     32  bool hasHadSHO;
     33  mGlobal = aWindow->GetDoc()->GetScriptHandlingObject(hasHadSHO);
     34 }
     35 
     36 IdleDeadline::IdleDeadline(nsIGlobalObject* aGlobal, bool aDidTimeout,
     37                           DOMHighResTimeStamp aDeadline)
     38    : mWindow(nullptr),
     39      mGlobal(aGlobal),
     40      mDidTimeout(aDidTimeout),
     41      mDeadline(aDeadline) {}
     42 
     43 IdleDeadline::~IdleDeadline() = default;
     44 
     45 JSObject* IdleDeadline::WrapObject(JSContext* aCx,
     46                                   JS::Handle<JSObject*> aGivenProto) {
     47  return IdleDeadline_Binding::Wrap(aCx, this, aGivenProto);
     48 }
     49 
     50 DOMHighResTimeStamp IdleDeadline::TimeRemaining() {
     51  if (mDidTimeout) {
     52    return 0.0;
     53  }
     54 
     55  if (mWindow) {
     56    RefPtr<Performance> performance = mWindow->GetPerformance();
     57    if (!performance) {
     58      // If there is no performance object the window is partially torn
     59      // down, so we can safely say that there is no time remaining.
     60      return 0.0;
     61    }
     62 
     63    // The web API doesn't expect deadlines > 50ms, but conversion from the
     64    // internal API may lead to some rounding errors.
     65    return std::clamp(mDeadline - performance->Now(), 0.0, 50.0);
     66  }
     67 
     68  // If there's no window, we're in a system scope, and can just use
     69  // a high-resolution TimeStamp::Now();
     70  auto timestamp = TimeStamp::Now() - TimeStamp::ProcessCreation();
     71  return std::max(mDeadline - timestamp.ToMilliseconds(), 0.0);
     72 }
     73 
     74 bool IdleDeadline::DidTimeout() const { return mDidTimeout; }
     75 
     76 }  // namespace mozilla::dom