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"
15 #include "nsWrapperCache.h"
16 #include "nsISupports.h"
17 #include "nsTArrayForwardDeclare.h"
21 class nsPIDOMWindowInner
;
26 class Storage
: public nsISupports
, public nsWrapperCache
{
28 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
29 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Storage
)
31 Storage(nsPIDOMWindowInner
* aWindow
, nsIPrincipal
* aPrincipal
,
32 nsIPrincipal
* aStoragePrincipal
);
34 static bool StoragePrefIsEnabled();
39 ePartitionedLocalStorage
,
42 virtual StorageType
Type() const = 0;
44 virtual bool IsForkOf(const Storage
* aStorage
) const = 0;
46 virtual int64_t GetOriginQuotaUsage() const = 0;
48 virtual void Disconnect() {}
50 nsIPrincipal
* Principal() const { return mPrincipal
; }
52 nsIPrincipal
* StoragePrincipal() const { return mStoragePrincipal
; }
54 bool IsPrivateBrowsing() const { return mPrivateBrowsing
; }
56 bool IsSessionScopedOrLess() const { return mSessionScopedOrLess
; }
59 JSObject
* WrapObject(JSContext
* aCx
,
60 JS::Handle
<JSObject
*> aGivenProto
) override
;
62 nsPIDOMWindowInner
* GetParentObject() const { return mWindow
; }
64 virtual uint32_t GetLength(nsIPrincipal
& aSubjectPrincipal
,
65 ErrorResult
& aRv
) = 0;
67 virtual void Key(uint32_t aIndex
, nsAString
& aResult
,
68 nsIPrincipal
& aSubjectPrincipal
, ErrorResult
& aRv
) = 0;
70 virtual void GetItem(const nsAString
& aKey
, nsAString
& aResult
,
71 nsIPrincipal
& aSubjectPrincipal
, ErrorResult
& aRv
) = 0;
73 virtual void GetSupportedNames(nsTArray
<nsString
>& aKeys
) = 0;
75 void NamedGetter(const nsAString
& aKey
, bool& aFound
, nsAString
& aResult
,
76 nsIPrincipal
& aSubjectPrincipal
, ErrorResult
& aRv
) {
77 GetItem(aKey
, aResult
, aSubjectPrincipal
, aRv
);
78 aFound
= !aResult
.IsVoid();
81 virtual void SetItem(const nsAString
& aKey
, const nsAString
& aValue
,
82 nsIPrincipal
& aSubjectPrincipal
, ErrorResult
& aRv
) = 0;
84 void NamedSetter(const nsAString
& aKey
, const nsAString
& aValue
,
85 nsIPrincipal
& aSubjectPrincipal
, ErrorResult
& aRv
) {
86 SetItem(aKey
, aValue
, aSubjectPrincipal
, aRv
);
89 virtual void RemoveItem(const nsAString
& aKey
,
90 nsIPrincipal
& aSubjectPrincipal
,
91 ErrorResult
& aRv
) = 0;
93 void NamedDeleter(const nsAString
& aKey
, bool& aFound
,
94 nsIPrincipal
& aSubjectPrincipal
, ErrorResult
& aRv
) {
95 RemoveItem(aKey
, aSubjectPrincipal
, aRv
);
97 aFound
= !aRv
.ErrorCodeIs(NS_SUCCESS_DOM_NO_OPERATION
);
100 virtual void Clear(nsIPrincipal
& aSubjectPrincipal
, ErrorResult
& aRv
) = 0;
102 // The attribute in the WebIDL interface has rather confusing name. So we
103 // shouldn't use this method internally. IsSessionScopedOrLess should be used
105 bool IsSessionOnly() const { return IsSessionScopedOrLess(); }
107 //////////////////////////////////////////////////////////////////////////////
110 // These methods are exposed on the `Storage` WebIDL interface behind a
111 // preference for the benefit of automated-tests. They are not exposed to
112 // content. See `Storage.webidl` for more details.
114 virtual void Open(nsIPrincipal
& aSubjectPrincipal
, ErrorResult
& aRv
) {}
116 virtual void Close(nsIPrincipal
& aSubjectPrincipal
, ErrorResult
& aRv
) {}
118 virtual void BeginExplicitSnapshot(nsIPrincipal
& aSubjectPrincipal
,
121 virtual void EndExplicitSnapshot(nsIPrincipal
& aSubjectPrincipal
,
124 virtual bool GetHasActiveSnapshot(nsIPrincipal
& aSubjectPrincipal
,
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
);
155 // The method checks whether the caller can use a storage.
156 bool CanUseStorage(nsIPrincipal
& aSubjectPrincipal
);
158 virtual void LastRelease() {}
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 mSessionScopedOrLess
: 1;
174 } // namespace mozilla
176 #endif // mozilla_dom_Storage_h