StorageAccessPermissionRequest.h (3147B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ 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 StorageAccessPermissionRequest_h_ 8 #define StorageAccessPermissionRequest_h_ 9 10 #include <functional> 11 12 #include "mozilla/Maybe.h" 13 #include "mozilla/MozPromise.h" 14 #include "nsContentPermissionHelper.h" 15 16 class nsPIDOMWindowInner; 17 18 namespace mozilla::dom { 19 20 class StorageAccessPermissionRequest final 21 : public ContentPermissionRequestBase { 22 public: 23 NS_DECL_ISUPPORTS_INHERITED 24 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(StorageAccessPermissionRequest, 25 ContentPermissionRequestBase) 26 27 // nsIContentPermissionRequest 28 NS_IMETHOD Cancel(void) override; 29 NS_IMETHOD Allow(JS::Handle<JS::Value> choices) override; 30 NS_IMETHOD GetTypes(nsIArray** aTypes) override; 31 32 using AllowCallback = std::function<void()>; 33 using CancelCallback = std::function<void()>; 34 35 static already_AddRefed<StorageAccessPermissionRequest> Create( 36 nsPIDOMWindowInner* aWindow, AllowCallback&& aAllowCallback, 37 CancelCallback&& aCancelCallback); 38 39 static already_AddRefed<StorageAccessPermissionRequest> Create( 40 nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal, 41 AllowCallback&& aAllowCallback, CancelCallback&& aCancelCallback); 42 43 // The argument aTopLevelBaseDomain is used here to optionally indicate what 44 // the top-level site of the permission requested will be. This is used in 45 // the requestStorageAccessUnderSite call because that call is not made from 46 // an embedded context. If aTopLevelBaseDomain is Nothing() the base domain 47 // of aPrincipal's Top browsing context is used. 48 static already_AddRefed<StorageAccessPermissionRequest> Create( 49 nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal, 50 const Maybe<nsCString>& aTopLevelBaseDomain, bool aFrameOnly, 51 AllowCallback&& aAllowCallback, CancelCallback&& aCancelCallback); 52 53 using AutoGrantDelayPromise = MozPromise<bool, bool, true>; 54 RefPtr<AutoGrantDelayPromise> MaybeDelayAutomaticGrants(); 55 56 private: 57 StorageAccessPermissionRequest(nsPIDOMWindowInner* aWindow, 58 nsIPrincipal* aNodePrincipal, 59 const Maybe<nsCString>& aTopLevelBaseDomain, 60 bool aFrameOnly, 61 AllowCallback&& aAllowCallback, 62 CancelCallback&& aCancelCallback); 63 ~StorageAccessPermissionRequest() { 64 // Invoke Cancel() to ensure we call a callback even if the request has 65 // been destroyed before the request is completed. 66 Cancel(); 67 } 68 69 unsigned CalculateSimulatedDelay(); 70 71 AllowCallback mAllowCallback; 72 CancelCallback mCancelCallback; 73 nsTArray<nsString> mOptions; 74 nsTArray<PermissionRequest> mPermissionRequests; 75 bool mCallbackCalled; 76 }; 77 78 } // namespace mozilla::dom 79 80 #endif // StorageAccessPermissionRequest_h_