summaryrefslogtreecommitdiffstats
path: root/dom/base/IdleRequest.cpp
blob: 26190f98b7fa5ade91a99a5cc4e11a4d479b5c31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "IdleRequest.h"

#include "mozilla/Function.h"
#include "mozilla/TimeStamp.h"
#include "mozilla/dom/IdleDeadline.h"
#include "mozilla/dom/Performance.h"
#include "mozilla/dom/PerformanceTiming.h"
#include "mozilla/dom/WindowBinding.h"
#include "nsComponentManagerUtils.h"
#include "nsGlobalWindow.h"
#include "nsISupportsPrimitives.h"
#include "nsPIDOMWindow.h"

namespace mozilla {
namespace dom {

IdleRequest::IdleRequest(JSContext* aCx, nsPIDOMWindowInner* aWindow,
                         IdleRequestCallback& aCallback, uint32_t aHandle)
  : mWindow(aWindow)
  , mCallback(&aCallback)
  , mHandle(aHandle)
  , mTimeoutHandle(Nothing())
{
  MOZ_ASSERT(aWindow);

  // Get the calling location.
  nsJSUtils::GetCallingLocation(aCx, mFileName, &mLineNo, &mColumn);
}

IdleRequest::~IdleRequest()
{
}

NS_IMPL_CYCLE_COLLECTION_CLASS(IdleRequest)

NS_IMPL_CYCLE_COLLECTING_ADDREF(IdleRequest)
NS_IMPL_CYCLE_COLLECTING_RELEASE(IdleRequest)

NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(IdleRequest)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mCallback)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END

NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(IdleRequest)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCallback)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(IdleRequest)
  NS_INTERFACE_MAP_ENTRY(nsIRunnable)
  NS_INTERFACE_MAP_ENTRY(nsICancelableRunnable)
  NS_INTERFACE_MAP_ENTRY(nsIIncrementalRunnable)
  NS_INTERFACE_MAP_ENTRY(nsITimeoutHandler)
  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsITimeoutHandler)
NS_INTERFACE_MAP_END

nsresult
IdleRequest::SetTimeout(uint32_t aTimeout)
{
  int32_t handle;
  nsresult rv = nsGlobalWindow::Cast(mWindow)->SetTimeoutOrInterval(
    this, aTimeout, false, Timeout::Reason::eIdleCallbackTimeout, &handle);
  mTimeoutHandle = Some(handle);

  return rv;
}

nsresult
IdleRequest::Run()
{
  if (mCallback) {
    RunIdleRequestCallback(false);
  }

  return NS_OK;
}

nsresult
IdleRequest::Cancel()
{
  mCallback = nullptr;
  CancelTimeout();
  if (isInList()) {
    remove();
  }
  Release();

  return NS_OK;
}

void
IdleRequest::SetDeadline(TimeStamp aDeadline)
{
  mozilla::dom::Performance* perf = mWindow->GetPerformance();
  mDeadline =
    perf ? perf->GetDOMTiming()->TimeStampToDOMHighRes(aDeadline) : 0.0;
}

nsresult
IdleRequest::RunIdleRequestCallback(bool aDidTimeout)
{
  MOZ_ASSERT(NS_IsMainThread());

  if (!aDidTimeout) {
    CancelTimeout();
  }

  remove();
  ErrorResult error;
  RefPtr<IdleDeadline> deadline =
    new IdleDeadline(mWindow, aDidTimeout, mDeadline);
  mCallback->Call(*deadline, error, "requestIdleCallback handler");
  mCallback = nullptr;
  Release();

  return error.StealNSResult();
}

void
IdleRequest::CancelTimeout()
{
  if (mTimeoutHandle.isSome()) {
    nsGlobalWindow::Cast(mWindow)->ClearTimeoutOrInterval(
      mTimeoutHandle.value(), Timeout::Reason::eIdleCallbackTimeout);
  }
}

nsresult
IdleRequest::Call()
{
  SetDeadline(TimeStamp::Now());
  return RunIdleRequestCallback(true);
}

void
IdleRequest::GetLocation(const char** aFileName, uint32_t* aLineNo,
                         uint32_t* aColumn)
{
  *aFileName = mFileName.get();
  *aLineNo = mLineNo;
  *aColumn = mColumn;
}

void
IdleRequest::MarkForCC()
{
  mCallback->MarkForCC();
}

} // namespace dom
} // namespace mozilla