Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/events/DeviceMotionEvent.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 "mozilla/dom/DeviceMotionEvent.h"
8
#include "nsContentUtils.h"
9
10
namespace mozilla {
11
namespace dom {
12
13
/******************************************************************************
14
 * DeviceMotionEvent
15
 *****************************************************************************/
16
17
NS_IMPL_CYCLE_COLLECTION_INHERITED(DeviceMotionEvent, Event,
18
                                   mAcceleration,
19
                                   mAccelerationIncludingGravity,
20
                                   mRotationRate)
21
22
NS_IMPL_ADDREF_INHERITED(DeviceMotionEvent, Event)
23
NS_IMPL_RELEASE_INHERITED(DeviceMotionEvent, Event)
24
25
0
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeviceMotionEvent)
26
0
NS_INTERFACE_MAP_END_INHERITING(Event)
27
28
void
29
DeviceMotionEvent::InitDeviceMotionEvent(
30
                     const nsAString& aType,
31
                     bool aCanBubble,
32
                     bool aCancelable,
33
                     const DeviceAccelerationInit& aAcceleration,
34
                     const DeviceAccelerationInit& aAccelIncludingGravity,
35
                     const DeviceRotationRateInit& aRotationRate,
36
                     const Nullable<double>& aInterval)
37
0
{
38
0
  InitDeviceMotionEvent(aType, aCanBubble, aCancelable, aAcceleration,
39
0
                        aAccelIncludingGravity, aRotationRate, aInterval,
40
0
                        Nullable<uint64_t>());
41
0
}
42
43
void
44
DeviceMotionEvent::InitDeviceMotionEvent(
45
                     const nsAString& aType,
46
                     bool aCanBubble,
47
                     bool aCancelable,
48
                     const DeviceAccelerationInit& aAcceleration,
49
                     const DeviceAccelerationInit& aAccelIncludingGravity,
50
                     const DeviceRotationRateInit& aRotationRate,
51
                     const Nullable<double>& aInterval,
52
                     const Nullable<uint64_t>& aTimeStamp)
53
0
{
54
0
  NS_ENSURE_TRUE_VOID(!mEvent->mFlags.mIsBeingDispatched);
55
0
56
0
  Event::InitEvent(aType, aCanBubble, aCancelable);
57
0
58
0
  mAcceleration = new DeviceAcceleration(this, aAcceleration.mX,
59
0
                                         aAcceleration.mY,
60
0
                                         aAcceleration.mZ);
61
0
62
0
  mAccelerationIncludingGravity =
63
0
    new DeviceAcceleration(this, aAccelIncludingGravity.mX,
64
0
                           aAccelIncludingGravity.mY,
65
0
                           aAccelIncludingGravity.mZ);
66
0
67
0
  mRotationRate = new DeviceRotationRate(this, aRotationRate.mAlpha,
68
0
                                         aRotationRate.mBeta,
69
0
                                         aRotationRate.mGamma);
70
0
  mInterval = aInterval;
71
0
  if (!aTimeStamp.IsNull()) {
72
0
    mEvent->mTime = aTimeStamp.Value();
73
0
74
0
    static mozilla::TimeStamp sInitialNow = mozilla::TimeStamp::Now();
75
0
    static uint64_t sInitialEventTime = aTimeStamp.Value();
76
0
    mEvent->mTimeStamp = sInitialNow + mozilla::TimeDuration::FromMicroseconds(
77
0
      aTimeStamp.Value() - sInitialEventTime);
78
0
  }
79
0
}
80
81
already_AddRefed<DeviceMotionEvent>
82
DeviceMotionEvent::Constructor(const GlobalObject& aGlobal,
83
                               const nsAString& aType,
84
                               const DeviceMotionEventInit& aEventInitDict,
85
                               ErrorResult& aRv)
86
0
{
87
0
  nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.GetAsSupports());
88
0
  RefPtr<DeviceMotionEvent> e = new DeviceMotionEvent(t, nullptr, nullptr);
89
0
  e->InitEvent(aType, aEventInitDict.mBubbles, aEventInitDict.mCancelable);
90
0
  bool trusted = e->Init(t);
91
0
92
0
  e->mAcceleration = new DeviceAcceleration(e,
93
0
    aEventInitDict.mAcceleration.mX,
94
0
    aEventInitDict.mAcceleration.mY,
95
0
    aEventInitDict.mAcceleration.mZ);
96
0
97
0
  e->mAccelerationIncludingGravity = new DeviceAcceleration(e,
98
0
    aEventInitDict.mAccelerationIncludingGravity.mX,
99
0
    aEventInitDict.mAccelerationIncludingGravity.mY,
100
0
    aEventInitDict.mAccelerationIncludingGravity.mZ);
101
0
102
0
  e->mRotationRate = new DeviceRotationRate(e,
103
0
    aEventInitDict.mRotationRate.mAlpha,
104
0
    aEventInitDict.mRotationRate.mBeta,
105
0
    aEventInitDict.mRotationRate.mGamma);
106
0
107
0
  e->mInterval = aEventInitDict.mInterval;
108
0
  e->SetTrusted(trusted);
109
0
  e->SetComposed(aEventInitDict.mComposed);
110
0
  return e.forget();
111
0
}
112
113
/******************************************************************************
114
 * DeviceAcceleration
115
 *****************************************************************************/
116
117
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DeviceAcceleration, mOwner)
118
119
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(DeviceAcceleration, AddRef)
120
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(DeviceAcceleration, Release)
121
122
DeviceAcceleration::DeviceAcceleration(DeviceMotionEvent* aOwner,
123
                                       const Nullable<double>& aX,
124
                                       const Nullable<double>& aY,
125
                                       const Nullable<double>& aZ)
126
  : mOwner(aOwner)
127
  , mX(aX)
128
  , mY(aY)
129
  , mZ(aZ)
130
0
{
131
0
}
132
133
DeviceAcceleration::~DeviceAcceleration()
134
0
{
135
0
}
136
137
/******************************************************************************
138
 * DeviceRotationRate
139
 *****************************************************************************/
140
141
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DeviceRotationRate, mOwner)
142
143
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(DeviceRotationRate, AddRef)
144
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(DeviceRotationRate, Release)
145
146
DeviceRotationRate::DeviceRotationRate(DeviceMotionEvent* aOwner,
147
                                       const Nullable<double>& aAlpha,
148
                                       const Nullable<double>& aBeta,
149
                                       const Nullable<double>& aGamma)
150
  : mOwner(aOwner)
151
  , mAlpha(aAlpha)
152
  , mBeta(aBeta)
153
  , mGamma(aGamma)
154
0
{
155
0
}
156
157
DeviceRotationRate::~DeviceRotationRate()
158
0
{
159
0
}
160
161
} // namespace dom
162
} // namespace mozilla
163
164
using namespace mozilla;
165
using namespace mozilla::dom;
166
167
already_AddRefed<DeviceMotionEvent>
168
NS_NewDOMDeviceMotionEvent(EventTarget* aOwner,
169
                           nsPresContext* aPresContext,
170
                           WidgetEvent* aEvent)
171
0
{
172
0
  RefPtr<DeviceMotionEvent> it =
173
0
    new DeviceMotionEvent(aOwner, aPresContext, aEvent);
174
0
  return it.forget();
175
0
}