nsDeviceSensors.h (2388B)
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 #ifndef nsDeviceSensors_h 8 #define nsDeviceSensors_h 9 10 #include "mozilla/HalSensor.h" 11 #include "mozilla/TimeStamp.h" 12 #include "mozilla/dom/DeviceMotionEvent.h" 13 #include "nsCOMArray.h" 14 #include "nsCOMPtr.h" 15 #include "nsIDeviceSensors.h" 16 #include "nsTArray.h" 17 18 class nsIDOMWindow; 19 20 namespace mozilla::dom { 21 class Document; 22 class EventTarget; 23 } // namespace mozilla::dom 24 25 class nsDeviceSensors : public nsIDeviceSensors, 26 public mozilla::hal::ISensorObserver { 27 using DeviceAccelerationInit = mozilla::dom::DeviceAccelerationInit; 28 using DeviceRotationRateInit = mozilla::dom::DeviceRotationRateInit; 29 30 public: 31 NS_DECL_ISUPPORTS 32 NS_DECL_NSIDEVICESENSORS 33 34 nsDeviceSensors(); 35 36 void Notify(const mozilla::hal::SensorData& aSensorData) override; 37 38 private: 39 virtual ~nsDeviceSensors(); 40 41 // sensor -> window listener 42 nsTArray<nsTArray<nsIDOMWindow*>*> mWindowListeners; 43 44 void FireDOMLightEvent(mozilla::dom::EventTarget* aTarget, double value); 45 46 void MaybeFireDOMUserProximityEvent(mozilla::dom::EventTarget* aTarget, 47 double aValue, double aMax); 48 49 void FireDOMUserProximityEvent(mozilla::dom::EventTarget* aTarget, 50 bool aNear); 51 52 void FireDOMOrientationEvent(mozilla::dom::EventTarget* target, double aAlpha, 53 double aBeta, double aGamma, bool aIsAbsolute); 54 55 void FireDOMMotionEvent(mozilla::dom::Document* domDoc, 56 mozilla::dom::EventTarget* target, uint32_t type, 57 PRTime timestamp, double x, double y, double z); 58 59 inline bool IsSensorEnabled(uint32_t aType) { 60 return mWindowListeners[aType]->Length() > 0; 61 } 62 63 bool IsSensorAllowedByPref(uint32_t aType, nsIDOMWindow* aWindow); 64 65 mozilla::TimeStamp mLastDOMMotionEventTime; 66 bool mIsUserProximityNear; 67 mozilla::Maybe<DeviceAccelerationInit> mLastAcceleration; 68 mozilla::Maybe<DeviceAccelerationInit> mLastAccelerationIncludingGravity; 69 mozilla::Maybe<DeviceRotationRateInit> mLastRotationRate; 70 }; 71 72 #endif