Merge mozilla-central to autoland on a CLOSED TREE
[gecko.git] / dom / events / DeviceMotionEvent.cpp
blob73fcc308673b35bc67d64a4c6f1896b7963cc627
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::dom {
12 /******************************************************************************
13 * DeviceMotionEvent
14 *****************************************************************************/
16 NS_IMPL_CYCLE_COLLECTION_INHERITED(DeviceMotionEvent, Event, mAcceleration,
17 mAccelerationIncludingGravity, mRotationRate)
19 NS_IMPL_ADDREF_INHERITED(DeviceMotionEvent, Event)
20 NS_IMPL_RELEASE_INHERITED(DeviceMotionEvent, Event)
22 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DeviceMotionEvent)
23 NS_INTERFACE_MAP_END_INHERITING(Event)
25 void DeviceMotionEvent::InitDeviceMotionEvent(
26 const nsAString& aType, bool aCanBubble, bool aCancelable,
27 const DeviceAccelerationInit& aAcceleration,
28 const DeviceAccelerationInit& aAccelIncludingGravity,
29 const DeviceRotationRateInit& aRotationRate,
30 const Nullable<double>& aInterval) {
31 InitDeviceMotionEvent(aType, aCanBubble, aCancelable, aAcceleration,
32 aAccelIncludingGravity, aRotationRate, aInterval,
33 Nullable<uint64_t>());
36 void DeviceMotionEvent::InitDeviceMotionEvent(
37 const nsAString& aType, bool aCanBubble, bool aCancelable,
38 const DeviceAccelerationInit& aAcceleration,
39 const DeviceAccelerationInit& aAccelIncludingGravity,
40 const DeviceRotationRateInit& aRotationRate,
41 const Nullable<double>& aInterval, const Nullable<uint64_t>& aTimeStamp) {
42 NS_ENSURE_TRUE_VOID(!mEvent->mFlags.mIsBeingDispatched);
44 Event::InitEvent(aType, aCanBubble, aCancelable);
46 mAcceleration = new DeviceAcceleration(this, aAcceleration.mX,
47 aAcceleration.mY, aAcceleration.mZ);
49 mAccelerationIncludingGravity = new DeviceAcceleration(
50 this, aAccelIncludingGravity.mX, aAccelIncludingGravity.mY,
51 aAccelIncludingGravity.mZ);
53 mRotationRate = new DeviceRotationRate(
54 this, aRotationRate.mAlpha, aRotationRate.mBeta, aRotationRate.mGamma);
55 mInterval = aInterval;
56 if (!aTimeStamp.IsNull()) {
57 static mozilla::TimeStamp sInitialNow = mozilla::TimeStamp::Now();
58 static uint64_t sInitialEventTime = aTimeStamp.Value();
59 mEvent->mTimeStamp =
60 sInitialNow + mozilla::TimeDuration::FromMicroseconds(
61 aTimeStamp.Value() - sInitialEventTime);
65 already_AddRefed<DeviceMotionEvent> DeviceMotionEvent::Constructor(
66 const GlobalObject& aGlobal, const nsAString& aType,
67 const DeviceMotionEventInit& aEventInitDict) {
68 nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.GetAsSupports());
69 RefPtr<DeviceMotionEvent> e = new DeviceMotionEvent(t, nullptr, nullptr);
70 e->InitEvent(aType, aEventInitDict.mBubbles, aEventInitDict.mCancelable);
71 bool trusted = e->Init(t);
73 e->mAcceleration = new DeviceAcceleration(e, aEventInitDict.mAcceleration.mX,
74 aEventInitDict.mAcceleration.mY,
75 aEventInitDict.mAcceleration.mZ);
77 e->mAccelerationIncludingGravity =
78 new DeviceAcceleration(e, aEventInitDict.mAccelerationIncludingGravity.mX,
79 aEventInitDict.mAccelerationIncludingGravity.mY,
80 aEventInitDict.mAccelerationIncludingGravity.mZ);
82 e->mRotationRate = new DeviceRotationRate(
83 e, aEventInitDict.mRotationRate.mAlpha,
84 aEventInitDict.mRotationRate.mBeta, aEventInitDict.mRotationRate.mGamma);
86 e->mInterval = aEventInitDict.mInterval;
87 e->SetTrusted(trusted);
88 e->SetComposed(aEventInitDict.mComposed);
89 return e.forget();
92 /******************************************************************************
93 * DeviceAcceleration
94 *****************************************************************************/
96 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DeviceAcceleration, mOwner)
98 DeviceAcceleration::DeviceAcceleration(DeviceMotionEvent* aOwner,
99 const Nullable<double>& aX,
100 const Nullable<double>& aY,
101 const Nullable<double>& aZ)
102 : mOwner(aOwner), mX(aX), mY(aY), mZ(aZ) {}
104 DeviceAcceleration::~DeviceAcceleration() = default;
106 /******************************************************************************
107 * DeviceRotationRate
108 *****************************************************************************/
110 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DeviceRotationRate, mOwner)
112 DeviceRotationRate::DeviceRotationRate(DeviceMotionEvent* aOwner,
113 const Nullable<double>& aAlpha,
114 const Nullable<double>& aBeta,
115 const Nullable<double>& aGamma)
116 : mOwner(aOwner), mAlpha(aAlpha), mBeta(aBeta), mGamma(aGamma) {}
118 DeviceRotationRate::~DeviceRotationRate() = default;
120 } // namespace mozilla::dom
122 using namespace mozilla;
123 using namespace mozilla::dom;
125 already_AddRefed<DeviceMotionEvent> NS_NewDOMDeviceMotionEvent(
126 EventTarget* aOwner, nsPresContext* aPresContext, WidgetEvent* aEvent) {
127 RefPtr<DeviceMotionEvent> it =
128 new DeviceMotionEvent(aOwner, aPresContext, aEvent);
129 return it.forget();