michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/dom/DeviceMotionEvent.h" michael@0: #include "nsContentUtils.h" michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: /****************************************************************************** michael@0: * DeviceMotionEvent michael@0: *****************************************************************************/ michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_INHERITED(DeviceMotionEvent, Event, michael@0: mAcceleration, michael@0: mAccelerationIncludingGravity, michael@0: mRotationRate) michael@0: michael@0: NS_IMPL_ADDREF_INHERITED(DeviceMotionEvent, Event) michael@0: NS_IMPL_RELEASE_INHERITED(DeviceMotionEvent, Event) michael@0: michael@0: NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(DeviceMotionEvent) michael@0: NS_INTERFACE_MAP_END_INHERITING(Event) michael@0: michael@0: void michael@0: DeviceMotionEvent::InitDeviceMotionEvent( michael@0: const nsAString& aType, michael@0: bool aCanBubble, michael@0: bool aCancelable, michael@0: const DeviceAccelerationInit& aAcceleration, michael@0: const DeviceAccelerationInit& aAccelIncludingGravity, michael@0: const DeviceRotationRateInit& aRotationRate, michael@0: Nullable aInterval, michael@0: ErrorResult& aRv) michael@0: { michael@0: aRv = Event::InitEvent(aType, aCanBubble, aCancelable); michael@0: if (aRv.Failed()) { michael@0: return; michael@0: } michael@0: michael@0: mAcceleration = new DeviceAcceleration(this, aAcceleration.mX, michael@0: aAcceleration.mY, michael@0: aAcceleration.mZ); michael@0: michael@0: mAccelerationIncludingGravity = michael@0: new DeviceAcceleration(this, aAccelIncludingGravity.mX, michael@0: aAccelIncludingGravity.mY, michael@0: aAccelIncludingGravity.mZ); michael@0: michael@0: mRotationRate = new DeviceRotationRate(this, aRotationRate.mAlpha, michael@0: aRotationRate.mBeta, michael@0: aRotationRate.mGamma); michael@0: mInterval = aInterval; michael@0: } michael@0: michael@0: already_AddRefed michael@0: DeviceMotionEvent::Constructor(const GlobalObject& aGlobal, michael@0: const nsAString& aType, michael@0: const DeviceMotionEventInit& aEventInitDict, michael@0: ErrorResult& aRv) michael@0: { michael@0: nsCOMPtr t = do_QueryInterface(aGlobal.GetAsSupports()); michael@0: nsRefPtr e = new DeviceMotionEvent(t, nullptr, nullptr); michael@0: aRv = e->InitEvent(aType, aEventInitDict.mBubbles, aEventInitDict.mCancelable); michael@0: if (aRv.Failed()) { michael@0: return nullptr; michael@0: } michael@0: bool trusted = e->Init(t); michael@0: michael@0: e->mAcceleration = new DeviceAcceleration(e, michael@0: aEventInitDict.mAcceleration.mX, michael@0: aEventInitDict.mAcceleration.mY, michael@0: aEventInitDict.mAcceleration.mZ); michael@0: michael@0: e->mAccelerationIncludingGravity = new DeviceAcceleration(e, michael@0: aEventInitDict.mAccelerationIncludingGravity.mX, michael@0: aEventInitDict.mAccelerationIncludingGravity.mY, michael@0: aEventInitDict.mAccelerationIncludingGravity.mZ); michael@0: michael@0: e->mRotationRate = new DeviceRotationRate(e, michael@0: aEventInitDict.mRotationRate.mAlpha, michael@0: aEventInitDict.mRotationRate.mBeta, michael@0: aEventInitDict.mRotationRate.mGamma); michael@0: michael@0: e->mInterval = aEventInitDict.mInterval; michael@0: e->SetTrusted(trusted); michael@0: michael@0: return e.forget(); michael@0: } michael@0: michael@0: /****************************************************************************** michael@0: * DeviceAcceleration michael@0: *****************************************************************************/ michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(DeviceAcceleration, mOwner) michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(DeviceAcceleration, AddRef) michael@0: NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(DeviceAcceleration, Release) michael@0: michael@0: DeviceAcceleration::DeviceAcceleration(DeviceMotionEvent* aOwner, michael@0: Nullable aX, michael@0: Nullable aY, michael@0: Nullable aZ) michael@0: : mOwner(aOwner) michael@0: , mX(aX) michael@0: , mY(aY) michael@0: , mZ(aZ) michael@0: { michael@0: SetIsDOMBinding(); michael@0: } michael@0: michael@0: DeviceAcceleration::~DeviceAcceleration() michael@0: { michael@0: } michael@0: michael@0: /****************************************************************************** michael@0: * DeviceRotationRate michael@0: *****************************************************************************/ michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(DeviceRotationRate, mOwner) michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(DeviceRotationRate, AddRef) michael@0: NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(DeviceRotationRate, Release) michael@0: michael@0: DeviceRotationRate::DeviceRotationRate(DeviceMotionEvent* aOwner, michael@0: Nullable aAlpha, michael@0: Nullable aBeta, michael@0: Nullable aGamma) michael@0: : mOwner(aOwner) michael@0: , mAlpha(aAlpha) michael@0: , mBeta(aBeta) michael@0: , mGamma(aGamma) michael@0: { michael@0: SetIsDOMBinding(); michael@0: } michael@0: michael@0: DeviceRotationRate::~DeviceRotationRate() michael@0: { michael@0: } michael@0: michael@0: } // namespace dom michael@0: } // namespace mozilla michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::dom; michael@0: michael@0: nsresult michael@0: NS_NewDOMDeviceMotionEvent(nsIDOMEvent** aInstancePtrResult, michael@0: EventTarget* aOwner, michael@0: nsPresContext* aPresContext, michael@0: WidgetEvent* aEvent) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aInstancePtrResult); michael@0: michael@0: DeviceMotionEvent* it = new DeviceMotionEvent(aOwner, aPresContext, aEvent); michael@0: NS_ADDREF(it); michael@0: *aInstancePtrResult = static_cast(it); michael@0: return NS_OK; michael@0: }