Bug 1610775 [wpt PR 21336] - Update urllib3 to 1.25.8, a=testonly
[gecko.git] / dom / storage / Storage.h
blob4916be9350b5a1052206a654bda3dff25680ae87
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"
18 class nsIPrincipal;
19 class nsPIDOMWindowInner;
21 namespace mozilla {
22 namespace dom {
24 class Storage : public nsISupports, public nsWrapperCache {
25 public:
26 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
27 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Storage)
29 Storage(nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal,
30 nsIPrincipal* aStoragePrincipal);
32 static bool StoragePrefIsEnabled();
34 enum StorageType {
35 eSessionStorage,
36 eLocalStorage,
37 ePartitionedLocalStorage,
40 virtual StorageType Type() const = 0;
42 virtual bool IsForkOf(const Storage* aStorage) const = 0;
44 virtual int64_t GetOriginQuotaUsage() const = 0;
46 nsIPrincipal* Principal() const { return mPrincipal; }
48 nsIPrincipal* StoragePrincipal() const { return mStoragePrincipal; }
50 // WebIDL
51 JSObject* WrapObject(JSContext* aCx,
52 JS::Handle<JSObject*> aGivenProto) override;
54 nsPIDOMWindowInner* GetParentObject() const { return mWindow; }
56 virtual uint32_t GetLength(nsIPrincipal& aSubjectPrincipal,
57 ErrorResult& aRv) = 0;
59 virtual void Key(uint32_t aIndex, nsAString& aResult,
60 nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) = 0;
62 virtual void GetItem(const nsAString& aKey, nsAString& aResult,
63 nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) = 0;
65 virtual void GetSupportedNames(nsTArray<nsString>& aKeys) = 0;
67 void NamedGetter(const nsAString& aKey, bool& aFound, nsAString& aResult,
68 nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {
69 GetItem(aKey, aResult, aSubjectPrincipal, aRv);
70 aFound = !aResult.IsVoid();
73 virtual void SetItem(const nsAString& aKey, const nsAString& aValue,
74 nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) = 0;
76 void NamedSetter(const nsAString& aKey, const nsAString& aValue,
77 nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {
78 SetItem(aKey, aValue, aSubjectPrincipal, aRv);
81 virtual void RemoveItem(const nsAString& aKey,
82 nsIPrincipal& aSubjectPrincipal,
83 ErrorResult& aRv) = 0;
85 void NamedDeleter(const nsAString& aKey, bool& aFound,
86 nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {
87 RemoveItem(aKey, aSubjectPrincipal, aRv);
89 aFound = !aRv.ErrorCodeIs(NS_SUCCESS_DOM_NO_OPERATION);
92 virtual void Clear(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) = 0;
94 bool IsSessionOnly() const { return mIsSessionOnly; }
96 //////////////////////////////////////////////////////////////////////////////
97 // Testing Methods:
99 // These methods are exposed on the `Storage` WebIDL interface behind a
100 // preference for the benefit of automated-tests. They are not exposed to
101 // content. See `Storage.webidl` for more details.
103 virtual void Open(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {}
105 virtual void Close(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {}
107 virtual void BeginExplicitSnapshot(nsIPrincipal& aSubjectPrincipal,
108 ErrorResult& aRv) {}
110 virtual void EndExplicitSnapshot(nsIPrincipal& aSubjectPrincipal,
111 ErrorResult& aRv) {}
113 virtual bool GetHasActiveSnapshot(nsIPrincipal& aSubjectPrincipal,
114 ErrorResult& aRv) {
115 return false;
118 //////////////////////////////////////////////////////////////////////////////
120 // Dispatch storage notification events on all impacted pages in the current
121 // process as well as for consumption by devtools. Pages receive the
122 // notification via StorageNotifierService (not observers like in the past),
123 // while devtools does receive the notification via the observer service.
125 // aStorage can be null if this method is called by LocalStorageCacheChild.
127 // aImmediateDispatch is for use by child IPC code (LocalStorageCacheChild)
128 // so that PBackground ordering can be maintained. Without this, the event
129 // would be enqueued and run in a future turn of the event loop, potentially
130 // allowing other PBackground Recv* methods to trigger script that wants to
131 // assume our localstorage changes have already been applied. This is the
132 // case for message manager messages which are used by ContentTask testing
133 // logic and webextensions.
134 static void NotifyChange(Storage* aStorage, nsIPrincipal* aPrincipal,
135 const nsAString& aKey, const nsAString& aOldValue,
136 const nsAString& aNewValue,
137 const char16_t* aStorageType,
138 const nsAString& aDocumentURI, bool aIsPrivate,
139 bool aImmediateDispatch);
141 protected:
142 virtual ~Storage();
144 // The method checks whether the caller can use a storage.
145 bool CanUseStorage(nsIPrincipal& aSubjectPrincipal);
147 virtual void LastRelease() {}
149 private:
150 nsCOMPtr<nsPIDOMWindowInner> mWindow;
151 nsCOMPtr<nsIPrincipal> mPrincipal;
152 nsCOMPtr<nsIPrincipal> mStoragePrincipal;
154 // Whether storage is set to persist data only per session, may change
155 // dynamically and is set by CanUseStorage function that is called
156 // before any operation on the storage.
157 bool mIsSessionOnly : 1;
160 } // namespace dom
161 } // namespace mozilla
163 #endif // mozilla_dom_Storage_h