DeviceMotionEvent.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "mozilla/dom/DeviceMotionEvent.h"
  6. #include "nsContentUtils.h"
  7. namespace mozilla {
  8. namespace dom {
  9. /******************************************************************************
  10. * DeviceMotionEvent
  11. *****************************************************************************/
  12. NS_IMPL_CYCLE_COLLECTION_INHERITED(DeviceMotionEvent, Event,
  13. mAcceleration,
  14. mAccelerationIncludingGravity,
  15. mRotationRate)
  16. NS_IMPL_ADDREF_INHERITED(DeviceMotionEvent, Event)
  17. NS_IMPL_RELEASE_INHERITED(DeviceMotionEvent, Event)
  18. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(DeviceMotionEvent)
  19. NS_INTERFACE_MAP_END_INHERITING(Event)
  20. void
  21. DeviceMotionEvent::InitDeviceMotionEvent(
  22. const nsAString& aType,
  23. bool aCanBubble,
  24. bool aCancelable,
  25. const DeviceAccelerationInit& aAcceleration,
  26. const DeviceAccelerationInit& aAccelIncludingGravity,
  27. const DeviceRotationRateInit& aRotationRate,
  28. Nullable<double> aInterval)
  29. {
  30. InitDeviceMotionEvent(aType, aCanBubble, aCancelable, aAcceleration,
  31. aAccelIncludingGravity, aRotationRate, aInterval,
  32. Nullable<uint64_t>());
  33. }
  34. void
  35. DeviceMotionEvent::InitDeviceMotionEvent(
  36. const nsAString& aType,
  37. bool aCanBubble,
  38. bool aCancelable,
  39. const DeviceAccelerationInit& aAcceleration,
  40. const DeviceAccelerationInit& aAccelIncludingGravity,
  41. const DeviceRotationRateInit& aRotationRate,
  42. Nullable<double> aInterval,
  43. Nullable<uint64_t> aTimeStamp)
  44. {
  45. NS_ENSURE_TRUE_VOID(!mEvent->mFlags.mIsBeingDispatched);
  46. Event::InitEvent(aType, aCanBubble, aCancelable);
  47. mAcceleration = new DeviceAcceleration(this, aAcceleration.mX,
  48. aAcceleration.mY,
  49. aAcceleration.mZ);
  50. mAccelerationIncludingGravity =
  51. new DeviceAcceleration(this, aAccelIncludingGravity.mX,
  52. aAccelIncludingGravity.mY,
  53. aAccelIncludingGravity.mZ);
  54. mRotationRate = new DeviceRotationRate(this, aRotationRate.mAlpha,
  55. aRotationRate.mBeta,
  56. aRotationRate.mGamma);
  57. mInterval = aInterval;
  58. if (!aTimeStamp.IsNull()) {
  59. mEvent->mTime = aTimeStamp.Value();
  60. }
  61. }
  62. already_AddRefed<DeviceMotionEvent>
  63. DeviceMotionEvent::Constructor(const GlobalObject& aGlobal,
  64. const nsAString& aType,
  65. const DeviceMotionEventInit& aEventInitDict,
  66. ErrorResult& aRv)
  67. {
  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);
  72. e->mAcceleration = new DeviceAcceleration(e,
  73. aEventInitDict.mAcceleration.mX,
  74. aEventInitDict.mAcceleration.mY,
  75. aEventInitDict.mAcceleration.mZ);
  76. e->mAccelerationIncludingGravity = new DeviceAcceleration(e,
  77. aEventInitDict.mAccelerationIncludingGravity.mX,
  78. aEventInitDict.mAccelerationIncludingGravity.mY,
  79. aEventInitDict.mAccelerationIncludingGravity.mZ);
  80. e->mRotationRate = new DeviceRotationRate(e,
  81. aEventInitDict.mRotationRate.mAlpha,
  82. aEventInitDict.mRotationRate.mBeta,
  83. aEventInitDict.mRotationRate.mGamma);
  84. e->mInterval = aEventInitDict.mInterval;
  85. e->SetTrusted(trusted);
  86. e->SetComposed(aEventInitDict.mComposed);
  87. return e.forget();
  88. }
  89. /******************************************************************************
  90. * DeviceAcceleration
  91. *****************************************************************************/
  92. NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DeviceAcceleration, mOwner)
  93. NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(DeviceAcceleration, AddRef)
  94. NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(DeviceAcceleration, Release)
  95. DeviceAcceleration::DeviceAcceleration(DeviceMotionEvent* aOwner,
  96. Nullable<double> aX,
  97. Nullable<double> aY,
  98. Nullable<double> aZ)
  99. : mOwner(aOwner)
  100. , mX(aX)
  101. , mY(aY)
  102. , mZ(aZ)
  103. {
  104. }
  105. DeviceAcceleration::~DeviceAcceleration()
  106. {
  107. }
  108. /******************************************************************************
  109. * DeviceRotationRate
  110. *****************************************************************************/
  111. NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DeviceRotationRate, mOwner)
  112. NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(DeviceRotationRate, AddRef)
  113. NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(DeviceRotationRate, Release)
  114. DeviceRotationRate::DeviceRotationRate(DeviceMotionEvent* aOwner,
  115. Nullable<double> aAlpha,
  116. Nullable<double> aBeta,
  117. Nullable<double> aGamma)
  118. : mOwner(aOwner)
  119. , mAlpha(aAlpha)
  120. , mBeta(aBeta)
  121. , mGamma(aGamma)
  122. {
  123. }
  124. DeviceRotationRate::~DeviceRotationRate()
  125. {
  126. }
  127. } // namespace dom
  128. } // namespace mozilla
  129. using namespace mozilla;
  130. using namespace mozilla::dom;
  131. already_AddRefed<DeviceMotionEvent>
  132. NS_NewDOMDeviceMotionEvent(EventTarget* aOwner,
  133. nsPresContext* aPresContext,
  134. WidgetEvent* aEvent)
  135. {
  136. RefPtr<DeviceMotionEvent> it =
  137. new DeviceMotionEvent(aOwner, aPresContext, aEvent);
  138. return it.forget();
  139. }