Source code

Revision control

Other Tools

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/. */
6
7
#ifndef mozilla_dom_Storage_h
8
#define mozilla_dom_Storage_h
9
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
18
class nsIPrincipal;
19
class nsPIDOMWindowInner;
20
21
namespace mozilla {
22
namespace dom {
23
24
class Storage : public nsISupports, public nsWrapperCache {
25
public:
26
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
27
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Storage)
28
29
Storage(nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal,
30
nsIPrincipal* aStoragePrincipal);
31
32
static bool StoragePrefIsEnabled();
33
34
enum StorageType {
35
eSessionStorage,
36
eLocalStorage,
37
ePartitionedLocalStorage,
38
};
39
40
virtual StorageType Type() const = 0;
41
42
virtual bool IsForkOf(const Storage* aStorage) const = 0;
43
44
virtual int64_t GetOriginQuotaUsage() const = 0;
45
46
nsIPrincipal* Principal() const { return mPrincipal; }
47
48
nsIPrincipal* StoragePrincipal() const { return mStoragePrincipal; }
49
50
// WebIDL
51
JSObject* WrapObject(JSContext* aCx,
52
JS::Handle<JSObject*> aGivenProto) override;
53
54
nsPIDOMWindowInner* GetParentObject() const { return mWindow; }
55
56
virtual uint32_t GetLength(nsIPrincipal& aSubjectPrincipal,
57
ErrorResult& aRv) = 0;
58
59
virtual void Key(uint32_t aIndex, nsAString& aResult,
60
nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) = 0;
61
62
virtual void GetItem(const nsAString& aKey, nsAString& aResult,
63
nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) = 0;
64
65
virtual void GetSupportedNames(nsTArray<nsString>& aKeys) = 0;
66
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();
71
}
72
73
virtual void SetItem(const nsAString& aKey, const nsAString& aValue,
74
nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) = 0;
75
76
void NamedSetter(const nsAString& aKey, const nsAString& aValue,
77
nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {
78
SetItem(aKey, aValue, aSubjectPrincipal, aRv);
79
}
80
81
virtual void RemoveItem(const nsAString& aKey,
82
nsIPrincipal& aSubjectPrincipal,
83
ErrorResult& aRv) = 0;
84
85
void NamedDeleter(const nsAString& aKey, bool& aFound,
86
nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {
87
RemoveItem(aKey, aSubjectPrincipal, aRv);
88
89
aFound = !aRv.ErrorCodeIs(NS_SUCCESS_DOM_NO_OPERATION);
90
}
91
92
virtual void Clear(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) = 0;
93
94
bool IsSessionOnly() const { return mIsSessionOnly; }
95
96
//////////////////////////////////////////////////////////////////////////////
97
// Testing Methods:
98
//
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.
102
103
virtual void Open(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {}
104
105
virtual void Close(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {}
106
107
virtual void BeginExplicitSnapshot(nsIPrincipal& aSubjectPrincipal,
108
ErrorResult& aRv) {}
109
110
virtual void EndExplicitSnapshot(nsIPrincipal& aSubjectPrincipal,
111
ErrorResult& aRv) {}
112
113
virtual bool GetHasActiveSnapshot(nsIPrincipal& aSubjectPrincipal,
114
ErrorResult& aRv) {
115
return false;
116
}
117
118
//////////////////////////////////////////////////////////////////////////////
119
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.
124
//
125
// aStorage can be null if this method is called by LocalStorageCacheChild.
126
//
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);
140
141
protected:
142
virtual ~Storage();
143
144
// The method checks whether the caller can use a storage.
145
bool CanUseStorage(nsIPrincipal& aSubjectPrincipal);
146
147
virtual void LastRelease() {}
148
149
private:
150
nsCOMPtr<nsPIDOMWindowInner> mWindow;
151
nsCOMPtr<nsIPrincipal> mPrincipal;
152
nsCOMPtr<nsIPrincipal> mStoragePrincipal;
153
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;
158
};
159
160
} // namespace dom
161
} // namespace mozilla
162
163
#endif // mozilla_dom_Storage_h