XRSystem.h (6610B)
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_XRsystem_h_ 8 #define mozilla_dom_XRsystem_h_ 9 10 #include "VRManagerChild.h" 11 #include "gfxVR.h" 12 #include "mozilla/DOMEventTargetHelper.h" 13 #include "mozilla/dom/WebXRBinding.h" 14 #include "nsContentPermissionHelper.h" 15 16 namespace mozilla::dom { 17 18 struct XRSessionCreationOptions; 19 20 class IsSessionSupportedRequest { 21 public: 22 IsSessionSupportedRequest(XRSessionMode aSessionMode, Promise* aPromise) 23 : mPromise(aPromise), mSessionMode(aSessionMode) {} 24 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(IsSessionSupportedRequest) 25 NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS(IsSessionSupportedRequest) 26 27 RefPtr<Promise> mPromise; 28 XRSessionMode GetSessionMode() const; 29 30 private: 31 ~IsSessionSupportedRequest() = default; 32 XRSessionMode mSessionMode; 33 }; 34 35 class RequestSessionRequest { 36 public: 37 RequestSessionRequest( 38 XRSessionMode aSessionMode, uint32_t aPresentationGroup, 39 Promise* aPromise, 40 const nsTArray<XRReferenceSpaceType>& aRequiredReferenceSpaceTypes, 41 const nsTArray<XRReferenceSpaceType>& aOptionalReferenceSpaceTypes); 42 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(RequestSessionRequest) 43 NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS(RequestSessionRequest) 44 RefPtr<Promise> mPromise; 45 46 bool ResolveSupport( 47 const gfx::VRDisplayClient* aDisplay, 48 nsTArray<XRReferenceSpaceType>& aEnabledReferenceSpaceTypes) const; 49 bool IsImmersive() const; 50 bool WantsHardware() const; 51 bool NeedsHardware() const; 52 XRSessionMode GetSessionMode() const; 53 uint32_t GetPresentationGroup() const; 54 55 private: 56 ~RequestSessionRequest() = default; 57 XRSessionMode mSessionMode; 58 uint32_t mPresentationGroup; 59 nsTArray<XRReferenceSpaceType> mRequiredReferenceSpaceTypes; 60 nsTArray<XRReferenceSpaceType> mOptionalReferenceSpaceTypes; 61 }; 62 63 class XRRequestSessionPermissionRequest final 64 : public ContentPermissionRequestBase { 65 public: 66 NS_DECL_ISUPPORTS_INHERITED 67 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(XRRequestSessionPermissionRequest, 68 ContentPermissionRequestBase) 69 70 // nsIContentPermissionRequest 71 NS_IMETHOD Cancel(void) override; 72 NS_IMETHOD Allow(JS::Handle<JS::Value> choices) override; 73 74 using AllowCallback = std::function<void()>; 75 using AllowAnySiteCallback = std::function<void()>; 76 using CancelCallback = std::function<void()>; 77 78 static already_AddRefed<XRRequestSessionPermissionRequest> Create( 79 nsPIDOMWindowInner* aWindow, AllowCallback&& aAllowCallback, 80 AllowAnySiteCallback&& aAllowAnySiteCallback, 81 CancelCallback&& aCancelCallback); 82 83 using AutoGrantDelayPromise = MozPromise<bool, bool, true>; 84 RefPtr<AutoGrantDelayPromise> MaybeDelayAutomaticGrants(); 85 86 private: 87 XRRequestSessionPermissionRequest( 88 nsPIDOMWindowInner* aWindow, nsIPrincipal* aNodePrincipal, 89 AllowCallback&& aAllowCallback, 90 AllowAnySiteCallback&& aAllowAnySiteCallback, 91 CancelCallback&& aCancelCallback); 92 ~XRRequestSessionPermissionRequest(); 93 94 AllowCallback mAllowCallback; 95 AllowAnySiteCallback mAllowAnySiteCallback; 96 CancelCallback mCancelCallback; 97 nsTArray<PermissionRequest> mPermissionRequests; 98 bool mCallbackCalled; 99 }; 100 101 class XRSystem final : public DOMEventTargetHelper, 102 public gfx::VRManagerEventObserver { 103 public: 104 NS_DECL_ISUPPORTS_INHERITED 105 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(XRSystem, DOMEventTargetHelper) 106 107 void Shutdown(); 108 void SessionEnded(XRSession* aSession); 109 bool FeaturePolicyBlocked() const; 110 bool OnXRPermissionRequestAllow(); 111 void OnXRPermissionRequestCancel(); 112 bool HasActiveImmersiveSession() const; 113 114 // WebIDL Boilerplate 115 static already_AddRefed<XRSystem> Create(nsPIDOMWindowInner* aWindow); 116 JSObject* WrapObject(JSContext* aCx, 117 JS::Handle<JSObject*> aGivenProto) override; 118 119 // WebIDL Members 120 already_AddRefed<Promise> IsSessionSupported(XRSessionMode aMode, 121 ErrorResult& aRv); 122 already_AddRefed<Promise> RequestSession(XRSessionMode aMode, 123 const XRSessionInit& aOptions, 124 CallerType aCallerType, 125 ErrorResult& aRv); 126 IMPL_EVENT_HANDLER(devicechange); 127 128 // VRManagerEventObserver interface 129 void NotifyVRDisplayMounted(uint32_t aDisplayID) override; 130 void NotifyVRDisplayUnmounted(uint32_t aDisplayID) override; 131 void NotifyVRDisplayConnect(uint32_t aDisplayID) override; 132 void NotifyVRDisplayDisconnect(uint32_t aDisplayID) override; 133 void NotifyVRDisplayPresentChange(uint32_t aDisplayID) override; 134 void NotifyPresentationGenerationChanged(uint32_t aDisplayID) override; 135 void NotifyEnumerationCompleted() override; 136 void NotifyDetectRuntimesCompleted() override; 137 bool GetStopActivityStatus() const override; 138 139 private: 140 explicit XRSystem(nsPIDOMWindowInner* aWindow); 141 virtual ~XRSystem() = default; 142 void ResolveIsSessionSupportedRequests(); 143 void ProcessSessionRequestsWaitingForRuntimeDetection(); 144 bool CancelHardwareRequest(RequestSessionRequest* aRequest); 145 void QueueSessionRequestWithEnumeration(RequestSessionRequest* aRequest); 146 void QueueSessionRequestWithoutEnumeration(RequestSessionRequest* aRequest); 147 void ResolveSessionRequestsWithoutHardware(); 148 void ResolveSessionRequests( 149 nsTArray<RefPtr<RequestSessionRequest>>& aRequests, 150 const nsTArray<RefPtr<gfx::VRDisplayClient>>& aDisplays); 151 152 bool mShuttingDown; 153 // https://immersive-web.github.io/webxr/#pending-immersive-session 154 bool mPendingImmersiveSession; 155 // https://immersive-web.github.io/webxr/#active-immersive-session 156 RefPtr<XRSession> mActiveImmersiveSession; 157 // https://immersive-web.github.io/webxr/#list-of-inline-sessions 158 nsTArray<RefPtr<XRSession>> mInlineSessions; 159 160 bool mEnumerationInFlight; 161 162 nsTArray<RefPtr<IsSessionSupportedRequest>> mIsSessionSupportedRequests; 163 nsTArray<RefPtr<RequestSessionRequest>> 164 mRequestSessionRequestsWithoutHardware; 165 nsTArray<RefPtr<RequestSessionRequest>> 166 mRequestSessionRequestsWaitingForRuntimeDetection; 167 nsTArray<RefPtr<RequestSessionRequest>> 168 mRequestSessionRequestsWaitingForEnumeration; 169 }; 170 171 } // namespace mozilla::dom 172 173 #endif // mozilla_dom_XRsystem_h_