PointerLockManager.h (2680B)
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 mozilla_PointerLockManager_h 8 #define mozilla_PointerLockManager_h 9 10 #include "mozilla/AlreadyAddRefed.h" 11 #include "nsIWeakReferenceUtils.h" 12 #include "nsThreadUtils.h" 13 14 namespace mozilla { 15 enum class StyleCursorKind : uint8_t; 16 17 namespace dom { 18 class BrowsingContext; 19 class BrowserParent; 20 enum class CallerType : uint32_t; 21 class Document; 22 class Element; 23 } // namespace dom 24 25 class PointerLockManager final { 26 public: 27 static void RequestLock(dom::Element* aElement, dom::CallerType aCallerType); 28 29 MOZ_CAN_RUN_SCRIPT_BOUNDARY 30 static void Unlock(const char* aReason, dom::Document* aDoc = nullptr); 31 32 static bool IsLocked() { return sIsLocked; } 33 34 static already_AddRefed<dom::Element> GetLockedElement(); 35 36 static already_AddRefed<dom::Document> GetLockedDocument(); 37 38 static dom::BrowserParent* GetLockedRemoteTarget(); 39 40 /** 41 * Returns true if aContext and the current pointer locked document 42 * have common top BrowsingContext. 43 * Note that this method returns true only if caller is in the same process 44 * as pointer locked document. 45 */ 46 static bool IsInLockContext(mozilla::dom::BrowsingContext* aContext); 47 48 // Set/release pointer lock remote target. Should only be called in parent 49 // process. 50 static void SetLockedRemoteTarget(dom::BrowserParent* aBrowserParent, 51 nsACString& aError); 52 static void ReleaseLockedRemoteTarget(dom::BrowserParent* aBrowserParent); 53 54 private: 55 class PointerLockRequest final : public Runnable { 56 public: 57 PointerLockRequest(dom::Element* aElement, bool aUserInputOrChromeCaller); 58 MOZ_CAN_RUN_SCRIPT_BOUNDARY NS_IMETHOD Run() final; 59 60 private: 61 nsWeakPtr mElement; 62 nsWeakPtr mDocument; 63 bool mUserInputOrChromeCaller; 64 }; 65 66 static void ChangePointerLockedElement(dom::Element* aElement, 67 dom::Document* aDocument, 68 dom::Element* aPointerLockedElement); 69 70 MOZ_CAN_RUN_SCRIPT_BOUNDARY 71 static bool StartSetPointerLock(dom::Element* aElement, 72 dom::Document* aDocument); 73 74 MOZ_CAN_RUN_SCRIPT 75 static bool SetPointerLock(dom::Element* aElement, dom::Document* aDocument, 76 StyleCursorKind); 77 78 static bool sIsLocked; 79 }; 80 81 } // namespace mozilla 82 83 #endif // mozilla_PointerLockManager_h