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
#include "Storage.h"
8
#include "StorageNotifierService.h"
9
10
#include "mozilla/dom/StorageBinding.h"
11
#include "mozilla/BasePrincipal.h"
12
#include "mozilla/StorageAccess.h"
13
#include "nsPIDOMWindow.h"
14
15
namespace mozilla {
16
namespace dom {
17
18
static const char kStorageEnabled[] = "dom.storage.enabled";
19
20
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Storage, mWindow, mPrincipal,
21
mStoragePrincipal)
22
23
NS_IMPL_CYCLE_COLLECTING_ADDREF(Storage)
24
NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_LAST_RELEASE(Storage, LastRelease())
25
26
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Storage)
27
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
28
NS_INTERFACE_MAP_ENTRY(nsISupports)
29
NS_INTERFACE_MAP_END
30
31
Storage::Storage(nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal,
32
nsIPrincipal* aStoragePrincipal)
33
: mWindow(aWindow),
34
mPrincipal(aPrincipal),
35
mStoragePrincipal(aStoragePrincipal),
36
mIsSessionOnly(false) {
37
MOZ_ASSERT(aPrincipal);
38
39
if (mPrincipal->IsSystemPrincipal()) {
40
mIsSessionOnly = false;
41
} else if (mWindow) {
42
uint32_t rejectedReason = 0;
43
StorageAccess access = StorageAllowedForWindow(mWindow, &rejectedReason);
44
45
mIsSessionOnly = access <= StorageAccess::eSessionScoped;
46
}
47
}
48
49
Storage::~Storage() = default;
50
51
/* static */
52
bool Storage::StoragePrefIsEnabled() {
53
return mozilla::Preferences::GetBool(kStorageEnabled);
54
}
55
56
bool Storage::CanUseStorage(nsIPrincipal& aSubjectPrincipal) {
57
if (!StoragePrefIsEnabled()) {
58
return false;
59
}
60
61
return aSubjectPrincipal.Subsumes(mPrincipal);
62
}
63
64
/* virtual */
65
JSObject* Storage::WrapObject(JSContext* aCx,
66
JS::Handle<JSObject*> aGivenProto) {
67
return Storage_Binding::Wrap(aCx, this, aGivenProto);
68
}
69
70
namespace {
71
72
class StorageNotifierRunnable : public Runnable {
73
public:
74
StorageNotifierRunnable(nsISupports* aSubject, const char16_t* aStorageType,
75
bool aPrivateBrowsing)
76
: Runnable("StorageNotifierRunnable"),
77
mSubject(aSubject),
78
mStorageType(aStorageType),
79
mPrivateBrowsing(aPrivateBrowsing) {}
80
81
NS_IMETHOD
82
Run() override {
83
nsCOMPtr<nsIObserverService> observerService =
84
mozilla::services::GetObserverService();
85
if (observerService) {
86
observerService->NotifyObservers(mSubject,
87
mPrivateBrowsing
88
? "dom-private-storage2-changed"
89
: "dom-storage2-changed",
90
mStorageType);
91
}
92
return NS_OK;
93
}
94
95
private:
96
nsCOMPtr<nsISupports> mSubject;
97
const char16_t* mStorageType;
98
const bool mPrivateBrowsing;
99
};
100
101
} // namespace
102
103
/* static */
104
void Storage::NotifyChange(Storage* aStorage, nsIPrincipal* aPrincipal,
105
const nsAString& aKey, const nsAString& aOldValue,
106
const nsAString& aNewValue,
107
const char16_t* aStorageType,
108
const nsAString& aDocumentURI, bool aIsPrivate,
109
bool aImmediateDispatch) {
110
StorageEventInit dict;
111
dict.mBubbles = false;
112
dict.mCancelable = false;
113
dict.mKey = aKey;
114
dict.mNewValue = aNewValue;
115
dict.mOldValue = aOldValue;
116
dict.mStorageArea = aStorage;
117
dict.mUrl = aDocumentURI;
118
119
// Note, this DOM event should never reach JS. It is cloned later in
120
// nsGlobalWindow.
121
RefPtr<StorageEvent> event =
122
StorageEvent::Constructor(nullptr, NS_LITERAL_STRING("storage"), dict);
123
124
event->SetPrincipal(aPrincipal);
125
126
// This will send the event to any registered window.
127
StorageNotifierService::Broadcast(event, aStorageType, aIsPrivate,
128
aImmediateDispatch);
129
130
// This runnable is mainly used by devtools. Windows receive notification by
131
// StorageNotifierService.
132
133
RefPtr<StorageNotifierRunnable> r =
134
new StorageNotifierRunnable(event, aStorageType, aIsPrivate);
135
136
if (aImmediateDispatch) {
137
Unused << r->Run();
138
} else {
139
SystemGroup::Dispatch(TaskCategory::Other, r.forget());
140
}
141
}
142
143
} // namespace dom
144
} // namespace mozilla