Bug 1771337 [wpt PR 34217] - Convert `popup=popup` to `popup=auto` or just `popup...
[gecko.git] / dom / storage / Storage.h
blobbea115e9d948a08cc9d558e734f2584c472a08d5
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/. */
7 #ifndef mozilla_dom_Storage_h
8 #define mozilla_dom_Storage_h
10 #include "mozilla/Attributes.h"
11 #include "mozilla/ErrorResult.h"
12 #include "mozilla/Maybe.h"
13 #include "nsCycleCollectionParticipant.h"
14 #include "nsCOMPtr.h"
15 #include "nsWrapperCache.h"
16 #include "nsISupports.h"
17 #include "nsTArrayForwardDeclare.h"
18 #include "nsString.h"
20 class nsIPrincipal;
21 class nsPIDOMWindowInner;
23 namespace mozilla::dom {
25 class Storage : public nsISupports, public nsWrapperCache {
26 public:
27 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
28 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Storage)
30 Storage(nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal,
31 nsIPrincipal* aStoragePrincipal);
33 static bool StoragePrefIsEnabled();
35 enum StorageType {
36 eSessionStorage,
37 eLocalStorage,
38 ePartitionedLocalStorage,
41 virtual StorageType Type() const = 0;
43 virtual bool IsForkOf(const Storage* aStorage) const = 0;
45 virtual int64_t GetOriginQuotaUsage() const = 0;
47 virtual void Disconnect() {}
49 nsIPrincipal* Principal() const { return mPrincipal; }
51 nsIPrincipal* StoragePrincipal() const { return mStoragePrincipal; }
53 bool IsPrivateBrowsing() const { return mPrivateBrowsing; }
55 bool IsSessionScopedOrLess() const { return mSessionScopedOrLess; }
57 // WebIDL
58 JSObject* WrapObject(JSContext* aCx,
59 JS::Handle<JSObject*> aGivenProto) override;
61 nsPIDOMWindowInner* GetParentObject() const { return mWindow; }
63 virtual uint32_t GetLength(nsIPrincipal& aSubjectPrincipal,
64 ErrorResult& aRv) = 0;
66 virtual void Key(uint32_t aIndex, nsAString& aResult,
67 nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) = 0;
69 virtual void GetItem(const nsAString& aKey, nsAString& aResult,
70 nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) = 0;
72 virtual void GetSupportedNames(nsTArray<nsString>& aKeys) = 0;
74 void NamedGetter(const nsAString& aKey, bool& aFound, nsAString& aResult,
75 nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {
76 GetItem(aKey, aResult, aSubjectPrincipal, aRv);
77 aFound = !aResult.IsVoid();
80 virtual void SetItem(const nsAString& aKey, const nsAString& aValue,
81 nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) = 0;
83 void NamedSetter(const nsAString& aKey, const nsAString& aValue,
84 nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {
85 SetItem(aKey, aValue, aSubjectPrincipal, aRv);
88 virtual void RemoveItem(const nsAString& aKey,
89 nsIPrincipal& aSubjectPrincipal,
90 ErrorResult& aRv) = 0;
92 void NamedDeleter(const nsAString& aKey, bool& aFound,
93 nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {
94 RemoveItem(aKey, aSubjectPrincipal, aRv);
96 aFound = !aRv.ErrorCodeIs(NS_SUCCESS_DOM_NO_OPERATION);
99 virtual void Clear(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) = 0;
101 // The attribute in the WebIDL interface has rather confusing name. So we
102 // shouldn't use this method internally. IsSessionScopedOrLess should be used
103 // directly.
104 bool IsSessionOnly() const { return IsSessionScopedOrLess(); }
106 //////////////////////////////////////////////////////////////////////////////
107 // Testing Methods:
109 // These methods are exposed on the `Storage` WebIDL interface behind a
110 // preference for the benefit of automated-tests. They are not exposed to
111 // content. See `Storage.webidl` for more details.
113 virtual void Open(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {}
115 virtual void Close(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {}
117 virtual void BeginExplicitSnapshot(nsIPrincipal& aSubjectPrincipal,
118 ErrorResult& aRv) {}
120 virtual void CheckpointExplicitSnapshot(nsIPrincipal& aSubjectPrincipal,
121 ErrorResult& aRv) {}
123 virtual void EndExplicitSnapshot(nsIPrincipal& aSubjectPrincipal,
124 ErrorResult& aRv) {}
126 virtual bool GetHasSnapshot(nsIPrincipal& aSubjectPrincipal,
127 ErrorResult& aRv) {
128 return false;
131 virtual int64_t GetSnapshotUsage(nsIPrincipal& aSubjectPrincipal,
132 ErrorResult& aRv);
134 //////////////////////////////////////////////////////////////////////////////
136 // Dispatch storage notification events on all impacted pages in the current
137 // process as well as for consumption by devtools. Pages receive the
138 // notification via StorageNotifierService (not observers like in the past),
139 // while devtools does receive the notification via the observer service.
141 // aStorage can be null if this method is called by LocalStorageCacheChild.
143 // aImmediateDispatch is for use by child IPC code (LocalStorageCacheChild)
144 // so that PBackground ordering can be maintained. Without this, the event
145 // would be enqueued and run in a future turn of the event loop, potentially
146 // allowing other PBackground Recv* methods to trigger script that wants to
147 // assume our localstorage changes have already been applied. This is the
148 // case for message manager messages which are used by ContentTask testing
149 // logic and webextensions.
150 static void NotifyChange(Storage* aStorage, nsIPrincipal* aPrincipal,
151 const nsAString& aKey, const nsAString& aOldValue,
152 const nsAString& aNewValue,
153 const char16_t* aStorageType,
154 const nsAString& aDocumentURI, bool aIsPrivate,
155 bool aImmediateDispatch);
157 protected:
158 virtual ~Storage();
160 // The method checks whether the caller can use a storage.
161 bool CanUseStorage(nsIPrincipal& aSubjectPrincipal);
163 virtual void LastRelease() {}
165 private:
166 nsCOMPtr<nsPIDOMWindowInner> mWindow;
167 nsCOMPtr<nsIPrincipal> mPrincipal;
168 nsCOMPtr<nsIPrincipal> mStoragePrincipal;
170 bool mPrivateBrowsing : 1;
172 // Whether storage is set to persist data only per session, may change
173 // dynamically and is set by CanUseStorage function that is called
174 // before any operation on the storage.
175 bool mSessionScopedOrLess : 1;
178 } // namespace mozilla::dom
180 #endif // mozilla_dom_Storage_h