tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

DeviceMotionEvent.cpp (5567B)


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