Coverage Report

Created: 2018-09-25 14:53

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