Bug 1032573 part 4 - Add AnimationTimeline::ToTimelineTime helper method; r=dbaron
[gecko.git] / dom / events / DeviceMotionEvent.cpp
blob5ec72db435356cad0ba048a3630267177a12a2cf
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/DeviceMotionEvent.h"
8 #include "nsContentUtils.h"
10 namespace mozilla {
11 namespace dom {
13 /******************************************************************************
14 * DeviceMotionEvent
15 *****************************************************************************/
17 NS_IMPL_CYCLE_COLLECTION_INHERITED(DeviceMotionEvent, Event,
18 mAcceleration,
19 mAccelerationIncludingGravity,
20 mRotationRate)
22 NS_IMPL_ADDREF_INHERITED(DeviceMotionEvent, Event)
23 NS_IMPL_RELEASE_INHERITED(DeviceMotionEvent, Event)
25 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(DeviceMotionEvent)
26 NS_INTERFACE_MAP_END_INHERITING(Event)
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 Nullable<double> aInterval,
37 ErrorResult& aRv)
39 aRv = Event::InitEvent(aType, aCanBubble, aCancelable);
40 if (aRv.Failed()) {
41 return;
44 mAcceleration = new DeviceAcceleration(this, aAcceleration.mX,
45 aAcceleration.mY,
46 aAcceleration.mZ);
48 mAccelerationIncludingGravity =
49 new DeviceAcceleration(this, aAccelIncludingGravity.mX,
50 aAccelIncludingGravity.mY,
51 aAccelIncludingGravity.mZ);
53 mRotationRate = new DeviceRotationRate(this, aRotationRate.mAlpha,
54 aRotationRate.mBeta,
55 aRotationRate.mGamma);
56 mInterval = aInterval;
59 already_AddRefed<DeviceMotionEvent>
60 DeviceMotionEvent::Constructor(const GlobalObject& aGlobal,
61 const nsAString& aType,
62 const DeviceMotionEventInit& aEventInitDict,
63 ErrorResult& aRv)
65 nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.GetAsSupports());
66 nsRefPtr<DeviceMotionEvent> e = new DeviceMotionEvent(t, nullptr, nullptr);
67 aRv = e->InitEvent(aType, aEventInitDict.mBubbles, aEventInitDict.mCancelable);
68 if (aRv.Failed()) {
69 return nullptr;
71 bool trusted = e->Init(t);
73 e->mAcceleration = new DeviceAcceleration(e,
74 aEventInitDict.mAcceleration.mX,
75 aEventInitDict.mAcceleration.mY,
76 aEventInitDict.mAcceleration.mZ);
78 e->mAccelerationIncludingGravity = new DeviceAcceleration(e,
79 aEventInitDict.mAccelerationIncludingGravity.mX,
80 aEventInitDict.mAccelerationIncludingGravity.mY,
81 aEventInitDict.mAccelerationIncludingGravity.mZ);
83 e->mRotationRate = new DeviceRotationRate(e,
84 aEventInitDict.mRotationRate.mAlpha,
85 aEventInitDict.mRotationRate.mBeta,
86 aEventInitDict.mRotationRate.mGamma);
88 e->mInterval = aEventInitDict.mInterval;
89 e->SetTrusted(trusted);
91 return e.forget();
94 /******************************************************************************
95 * DeviceAcceleration
96 *****************************************************************************/
98 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DeviceAcceleration, mOwner)
100 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(DeviceAcceleration, AddRef)
101 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(DeviceAcceleration, Release)
103 DeviceAcceleration::DeviceAcceleration(DeviceMotionEvent* aOwner,
104 Nullable<double> aX,
105 Nullable<double> aY,
106 Nullable<double> aZ)
107 : mOwner(aOwner)
108 , mX(aX)
109 , mY(aY)
110 , mZ(aZ)
112 SetIsDOMBinding();
115 DeviceAcceleration::~DeviceAcceleration()
119 /******************************************************************************
120 * DeviceRotationRate
121 *****************************************************************************/
123 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DeviceRotationRate, mOwner)
125 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(DeviceRotationRate, AddRef)
126 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(DeviceRotationRate, Release)
128 DeviceRotationRate::DeviceRotationRate(DeviceMotionEvent* aOwner,
129 Nullable<double> aAlpha,
130 Nullable<double> aBeta,
131 Nullable<double> aGamma)
132 : mOwner(aOwner)
133 , mAlpha(aAlpha)
134 , mBeta(aBeta)
135 , mGamma(aGamma)
137 SetIsDOMBinding();
140 DeviceRotationRate::~DeviceRotationRate()
144 } // namespace dom
145 } // namespace mozilla
147 using namespace mozilla;
148 using namespace mozilla::dom;
150 nsresult
151 NS_NewDOMDeviceMotionEvent(nsIDOMEvent** aInstancePtrResult,
152 EventTarget* aOwner,
153 nsPresContext* aPresContext,
154 WidgetEvent* aEvent)
156 NS_ENSURE_ARG_POINTER(aInstancePtrResult);
158 DeviceMotionEvent* it = new DeviceMotionEvent(aOwner, aPresContext, aEvent);
159 NS_ADDREF(it);
160 *aInstancePtrResult = static_cast<Event*>(it);
161 return NS_OK;