Bug 1892041 - Part 1: Update test262 features. r=spidermonkey-reviewers,dminor
[gecko.git] / dom / base / IdleDeadline.cpp
blobac9cbdbe03ddcd403da085e825b725d662929bff
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 "mozilla/dom/IdleDeadline.h"
9 #include <algorithm>
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"
19 namespace mozilla::dom {
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
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);
36 IdleDeadline::IdleDeadline(nsIGlobalObject* aGlobal, bool aDidTimeout,
37 DOMHighResTimeStamp aDeadline)
38 : mWindow(nullptr),
39 mGlobal(aGlobal),
40 mDidTimeout(aDidTimeout),
41 mDeadline(aDeadline) {}
43 IdleDeadline::~IdleDeadline() = default;
45 JSObject* IdleDeadline::WrapObject(JSContext* aCx,
46 JS::Handle<JSObject*> aGivenProto) {
47 return IdleDeadline_Binding::Wrap(aCx, this, aGivenProto);
50 DOMHighResTimeStamp IdleDeadline::TimeRemaining() {
51 if (mDidTimeout) {
52 return 0.0;
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;
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::min(std::max(mDeadline - performance->Now(), 0.0), 50.0);
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);
74 bool IdleDeadline::DidTimeout() const { return mDidTimeout; }
76 } // namespace mozilla::dom