NotificationUtils.h (4954B)
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 DOM_NOTIFICATION_NOTIFICATIONUTILS_H_ 8 #define DOM_NOTIFICATION_NOTIFICATIONUTILS_H_ 9 10 #include <cstdint> 11 12 #include "mozilla/dom/DOMTypes.h" 13 #include "nsCOMPtr.h" 14 #include "nsINotificationStorage.h" 15 #include "nsStringFwd.h" 16 17 enum class nsresult : uint32_t; 18 class nsIAlertNotification; 19 class nsIPrincipal; 20 class nsINotificationStorage; 21 namespace mozilla::dom { 22 enum class NotificationPermission : uint8_t; 23 class Document; 24 } // namespace mozilla::dom 25 26 namespace mozilla::dom::notification { 27 28 // The spec defines maxActions to depend on system limitation, but that can be 29 // used for fingerprinting. 30 // See also https://github.com/whatwg/notifications/issues/110. 31 static constexpr uint8_t kMaxActions = 2; 32 33 /** 34 * Retrieves raw notification permission directly from PermissionManager. 35 */ 36 NotificationPermission GetRawNotificationPermission(nsIPrincipal* aPrincipal); 37 38 enum class PermissionCheckPurpose : uint8_t { 39 PermissionRequest, 40 PermissionAttribute, 41 NotificationShow, 42 }; 43 44 /** 45 * Returns true if the current principal must be given notification 46 * permission, regardless of the permission status. This one should be dominant 47 * compared to FobbiddenFor below. 48 */ 49 bool IsNotificationAllowedFor(nsIPrincipal* aPrincipal); 50 51 /** 52 * Returns true if the current principal must not be given notification 53 * permission, regardless of the permission status. 54 * 55 * @param aRequestorDoc The Document object from the page requesting permission. 56 * Pass only when this is for requestNotification(). 57 */ 58 bool IsNotificationForbiddenFor(nsIPrincipal* aPrincipal, 59 nsIPrincipal* aEffectiveStoragePrincipal, 60 bool isSecureContext, 61 PermissionCheckPurpose aPurpose, 62 Document* aRequestorDoc = nullptr); 63 64 /** 65 * Retrieves notification permission based on the context. 66 */ 67 NotificationPermission GetNotificationPermission( 68 nsIPrincipal* aPrincipal, nsIPrincipal* aEffectiveStoragePrincipal, 69 bool isSecureContext, PermissionCheckPurpose aPurpose); 70 71 nsCOMPtr<nsINotificationStorage> GetNotificationStorage(bool isPrivate); 72 73 using NotificationsPromise = 74 MozPromise<CopyableTArray<IPCNotification>, nsresult, false>; 75 76 already_AddRefed<NotificationsPromise> GetStoredNotificationsForScope( 77 nsIPrincipal* aPrincipal, const nsACString& aScope, const nsAString& aTag); 78 79 nsresult GetOrigin(nsIPrincipal* aPrincipal, nsString& aOrigin); 80 81 nsresult PersistNotification(nsIPrincipal* aPrincipal, 82 const IPCNotification& aNotification, 83 const nsString& aScope); 84 nsresult UnpersistNotification(nsIPrincipal* aPrincipal, const nsString& aId); 85 86 enum class CloseMode { 87 CloseMethod, 88 // Either on global teardown or freeze 89 InactiveGlobal, 90 }; 91 void UnregisterNotification(nsIPrincipal* aPrincipal, const nsString& aId); 92 93 // Show an alert and clean up any previously stored notifications that 94 // aren't currently known to the notification backend. 95 // 96 // The cleanup happens when this is globally the first call, or always if 97 // dom.webnotifications.testing.force_storage_cleanup.enabled is set. 98 nsresult ShowAlertWithCleanup(nsIAlertNotification* aAlert, 99 nsIObserver* aAlertListener); 100 101 nsresult RemovePermission(nsIPrincipal* aPrincipal); 102 nsresult OpenSettings(nsIPrincipal* aPrincipal); 103 104 enum class NotificationStatusChange { Shown, Closed }; 105 nsresult AdjustPushQuota(nsIPrincipal* aPrincipal, 106 NotificationStatusChange aChange); 107 108 class NotificationActionStorageEntry 109 : public nsINotificationActionStorageEntry { 110 public: 111 NS_DECL_ISUPPORTS 112 NS_DECL_NSINOTIFICATIONACTIONSTORAGEENTRY 113 explicit NotificationActionStorageEntry( 114 const IPCNotificationAction& aIPCAction) 115 : mIPCAction(aIPCAction) {} 116 117 static Result<IPCNotificationAction, nsresult> ToIPC( 118 nsINotificationActionStorageEntry& aEntry); 119 120 private: 121 virtual ~NotificationActionStorageEntry() = default; 122 123 IPCNotificationAction mIPCAction; 124 }; 125 126 class NotificationStorageEntry : public nsINotificationStorageEntry { 127 public: 128 NS_DECL_ISUPPORTS 129 NS_DECL_NSINOTIFICATIONSTORAGEENTRY 130 explicit NotificationStorageEntry(const IPCNotification& aIPCNotification) 131 : mIPCNotification(aIPCNotification) {} 132 133 static Result<IPCNotification, nsresult> ToIPC( 134 nsINotificationStorageEntry& aEntry); 135 136 private: 137 virtual ~NotificationStorageEntry() = default; 138 139 IPCNotification mIPCNotification; 140 }; 141 142 } // namespace mozilla::dom::notification 143 144 #endif // DOM_NOTIFICATION_NOTIFICATIONUTILS_H_