ScreenOrientation.h (5143B)
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 file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_ScreenOrientation_h 8 #define mozilla_dom_ScreenOrientation_h 9 10 #include "mozilla/DOMEventTargetHelper.h" 11 #include "mozilla/HalScreenConfiguration.h" 12 #include "mozilla/MozPromise.h" 13 #include "mozilla/dom/BindingDeclarations.h" 14 #include "mozilla/dom/ScreenOrientationBinding.h" 15 16 class nsScreen; 17 18 namespace mozilla::dom { 19 20 class Promise; 21 22 class ScreenOrientation final : public DOMEventTargetHelper { 23 // nsScreen has deprecated API that shares implementation. 24 friend class ::nsScreen; 25 26 public: 27 NS_DECL_ISUPPORTS_INHERITED 28 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ScreenOrientation, 29 mozilla::DOMEventTargetHelper) 30 31 IMPL_EVENT_HANDLER(change) 32 33 // Called when the orientation may have changed. 34 void MaybeChanged(); 35 36 // Called when we might need to dispatch orientation change event. 37 void MaybeDispatchChangeEvent(BrowsingContext* aBrowsingContext); 38 39 // Called when we might need to dispatch orientation change events 40 // in case of orientation override being set, updated or removed. 41 void MaybeDispatchEventsForOverride(BrowsingContext* aBrowsingContext, 42 bool aOldHasOrientationOverride, 43 bool aOverrideIsDifferentThanDevice); 44 45 ScreenOrientation(nsPIDOMWindowInner* aWindow, nsScreen* aScreen); 46 47 already_AddRefed<Promise> Lock(OrientationLockType aOrientation, 48 ErrorResult& aRv); 49 50 void Unlock(ErrorResult& aRv); 51 52 // DeviceType and DeviceAngle gets the current type and angle of the device. 53 OrientationType DeviceType(CallerType aCallerType) const; 54 uint16_t DeviceAngle(CallerType aCallerType) const; 55 56 // GetType and GetAngle gets the type and angle of the responsible document 57 // (as defined in specification). 58 OrientationType GetType(CallerType aCallerType, ErrorResult& aRv) const; 59 uint16_t GetAngle(CallerType aCallerType, ErrorResult& aRv) const; 60 61 JSObject* WrapObject(JSContext* aCx, 62 JS::Handle<JSObject*> aGivenProto) override; 63 64 static void UpdateActiveOrientationLock(hal::ScreenOrientation aOrientation); 65 static void AbortInProcessOrientationPromises( 66 BrowsingContext* aBrowsingContext); 67 68 // Dispatch change event then resolve the promise. 69 // aBrowsingContext must be top level in process. 70 static void DispatchChangeEventToChildren(BrowsingContext* aBrowsingContext); 71 72 private: 73 virtual ~ScreenOrientation(); 74 75 // Listener to unlock orientation if we leave fullscreen. 76 class FullscreenEventListener; 77 78 // Listener to update document's orienation lock when document becomes 79 // visible. 80 class VisibleEventListener; 81 82 // Task to run step to lock orientation as defined in specification. 83 class LockOrientationTask; 84 85 enum LockPermission { LOCK_DENIED, FULLSCREEN_LOCK_ALLOWED, LOCK_ALLOWED }; 86 87 // This method calls into the HAL to lock the device and sets 88 // up listeners for full screen change. 89 RefPtr<GenericNonExclusivePromise> LockDeviceOrientation( 90 hal::ScreenOrientation aOrientation, bool aIsFullscreen); 91 92 // This method calls in to the HAL to unlock the device and removes 93 // full screen change listener. 94 void UnlockDeviceOrientation(); 95 void CleanupFullscreenListener(); 96 97 // This method performs the same function as |Lock| except it takes 98 // a hal::ScreenOrientation argument instead of an OrientationType. 99 // This method exists in order to share implementation with nsScreen that 100 // uses ScreenOrientation. 101 already_AddRefed<Promise> LockInternal(hal::ScreenOrientation aOrientation, 102 ErrorResult& aRv); 103 104 nsCOMPtr<nsIRunnable> DispatchChangeEventAndResolvePromise(); 105 106 // The common safety checks 107 static bool CommonSafetyChecks(nsPIDOMWindowInner* aOwner, 108 Document* aDocument, ErrorResult& aRv); 109 110 static LockPermission GetLockOrientationPermission(nsPIDOMWindowInner* aOwner, 111 Document* aDocument); 112 113 // Gets the responsible document as defined in the spec. 114 Document* GetResponsibleDocument() const; 115 116 RefPtr<nsScreen> mScreen; 117 RefPtr<FullscreenEventListener> mFullscreenListener; 118 RefPtr<VisibleEventListener> mVisibleListener; 119 OrientationType mType; 120 uint16_t mAngle; 121 // Whether we've tried to call into hal to lock the device orientation. This 122 // is needed because you don't want calling UnlockDeviceOrientation() during 123 // shutdown to initialize PHal if it hasn't been initialized earlier. Also, 124 // makes sense (there's no reason destroying a ScreenOrientation object from a 125 // different window should remove the orientation lock). 126 bool mTriedToLockDeviceOrientation = false; 127 }; 128 129 } // namespace mozilla::dom 130 131 #endif // mozilla_dom_ScreenOrientation_h