Backed out 3 changesets (bug 1901078, bug 1749048) for causing interface related...
[gecko.git] / dom / storage / Storage.h
blobcea49f3114c3da4c265b8efa77e6525b2c9a2a16
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_WRAPPERCACHE_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 IsPrivateBrowsingOrLess() const { return mPrivateBrowsingOrLess; }
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 //////////////////////////////////////////////////////////////////////////////
102 // Testing Methods:
104 // These methods are exposed on the `Storage` WebIDL interface behind a
105 // preference for the benefit of automated-tests. They are not exposed to
106 // content. See `Storage.webidl` for more details.
108 virtual void Open(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {}
110 virtual void Close(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {}
112 virtual void BeginExplicitSnapshot(nsIPrincipal& aSubjectPrincipal,
113 ErrorResult& aRv) {}
115 virtual void CheckpointExplicitSnapshot(nsIPrincipal& aSubjectPrincipal,
116 ErrorResult& aRv) {}
118 virtual void EndExplicitSnapshot(nsIPrincipal& aSubjectPrincipal,
119 ErrorResult& aRv) {}
121 virtual bool GetHasSnapshot(nsIPrincipal& aSubjectPrincipal,
122 ErrorResult& aRv) {
123 return false;
126 virtual int64_t GetSnapshotUsage(nsIPrincipal& aSubjectPrincipal,
127 ErrorResult& aRv);
129 //////////////////////////////////////////////////////////////////////////////
131 // Dispatch storage notification events on all impacted pages in the current
132 // process as well as for consumption by devtools. Pages receive the
133 // notification via StorageNotifierService (not observers like in the past),
134 // while devtools does receive the notification via the observer service.
136 // aStorage can be null if this method is called by LocalStorageCacheChild.
138 // aImmediateDispatch is for use by child IPC code (LocalStorageCacheChild)
139 // so that PBackground ordering can be maintained. Without this, the event
140 // would be enqueued and run in a future turn of the event loop, potentially
141 // allowing other PBackground Recv* methods to trigger script that wants to
142 // assume our localstorage changes have already been applied. This is the
143 // case for message manager messages which are used by ContentTask testing
144 // logic and webextensions.
145 static void NotifyChange(Storage* aStorage, nsIPrincipal* aPrincipal,
146 const nsAString& aKey, const nsAString& aOldValue,
147 const nsAString& aNewValue,
148 const char16_t* aStorageType,
149 const nsAString& aDocumentURI, bool aIsPrivate,
150 bool aImmediateDispatch);
152 protected:
153 virtual ~Storage();
155 // The method checks whether the caller can use a storage.
156 bool CanUseStorage(nsIPrincipal& aSubjectPrincipal);
158 virtual void LastRelease() {}
160 private:
161 nsCOMPtr<nsPIDOMWindowInner> mWindow;
162 nsCOMPtr<nsIPrincipal> mPrincipal;
163 nsCOMPtr<nsIPrincipal> mStoragePrincipal;
165 bool mPrivateBrowsing : 1;
167 // Whether storage is set to persist data only per session, may change
168 // dynamically and is set by CanUseStorage function that is called
169 // before any operation on the storage.
170 bool mPrivateBrowsingOrLess : 1;
173 } // namespace mozilla::dom
175 #endif // mozilla_dom_Storage_h