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"
11 #include "mozilla/dom/Document.h"
12 #include "mozilla/dom/IdleDeadlineBinding.h"
13 #include "mozilla/dom/Performance.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
)
29 IdleDeadline::IdleDeadline(nsPIDOMWindowInner
* aWindow
, bool aDidTimeout
,
30 DOMHighResTimeStamp aDeadline
)
31 : mWindow(aWindow
), mDidTimeout(aDidTimeout
), mDeadline(aDeadline
) {
33 mGlobal
= aWindow
->GetDoc()->GetScriptHandlingObject(hasHadSHO
);
36 IdleDeadline::IdleDeadline(nsIGlobalObject
* aGlobal
, bool aDidTimeout
,
37 DOMHighResTimeStamp aDeadline
)
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() {
56 RefPtr
<Performance
> performance
= mWindow
->GetPerformance();
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.
63 return std::max(mDeadline
- performance
->Now(), 0.0);
66 // If there's no window, we're in a system scope, and can just use
67 // a high-resolution TimeStamp::Now();
68 auto timestamp
= TimeStamp::Now() - TimeStamp::ProcessCreation();
69 return std::max(mDeadline
- timestamp
.ToMilliseconds(), 0.0);
72 bool IdleDeadline::DidTimeout() const { return mDidTimeout
; }
74 } // namespace mozilla::dom