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 "StorageNotifierService.h"
8
#include "mozilla/ClearOnShutdown.h"
9
#include "mozilla/StaticPtr.h"
10
11
namespace mozilla {
12
namespace dom {
13
14
namespace {
15
16
// This boolean is used to avoid the creation of the service after been
17
// distroyed on shutdown.
18
bool gStorageShuttingDown = false;
19
20
StaticRefPtr<StorageNotifierService> gStorageNotifierService;
21
22
} // namespace
23
24
/* static */
25
StorageNotifierService* StorageNotifierService::GetOrCreate() {
26
MOZ_ASSERT(NS_IsMainThread());
27
if (!gStorageNotifierService && !gStorageShuttingDown) {
28
gStorageNotifierService = new StorageNotifierService();
29
ClearOnShutdown(&gStorageNotifierService);
30
}
31
32
return gStorageNotifierService;
33
}
34
35
StorageNotifierService::StorageNotifierService() {
36
MOZ_ASSERT(NS_IsMainThread());
37
MOZ_ASSERT(!gStorageNotifierService);
38
}
39
40
StorageNotifierService::~StorageNotifierService() {
41
MOZ_ASSERT(NS_IsMainThread());
42
MOZ_ASSERT(!gStorageNotifierService);
43
gStorageShuttingDown = true;
44
}
45
46
/* static */
47
void StorageNotifierService::Broadcast(StorageEvent* aEvent,
48
const char16_t* aStorageType,
49
bool aPrivateBrowsing,
50
bool aImmediateDispatch) {
51
MOZ_ASSERT(NS_IsMainThread());
52
53
RefPtr<StorageNotifierService> service = gStorageNotifierService;
54
if (!service) {
55
return;
56
}
57
58
RefPtr<StorageEvent> event = aEvent;
59
60
nsTObserverArray<RefPtr<StorageNotificationObserver>>::ForwardIterator iter(
61
service->mObservers);
62
63
while (iter.HasMore()) {
64
RefPtr<StorageNotificationObserver> observer = iter.GetNext();
65
66
// Enforce that the source storage area's private browsing state matches
67
// this window's state. These flag checks and their maintenance independent
68
// from the principal's OriginAttributes matter because chrome docshells
69
// that are part of private browsing windows can be private browsing without
70
// having their OriginAttributes set (because they have the system
71
// principal).
72
if (aPrivateBrowsing != observer->IsPrivateBrowsing()) {
73
continue;
74
}
75
76
// No reasons to continue if the principal of the event doesn't match with
77
// the window's one.
78
if (!StorageUtils::PrincipalsEqual(
79
aEvent->GetPrincipal(), observer->GetEffectiveStoragePrincipal())) {
80
continue;
81
}
82
83
RefPtr<Runnable> r = NS_NewRunnableFunction(
84
"StorageNotifierService::Broadcast",
85
[observer, event, aStorageType, aPrivateBrowsing]() {
86
observer->ObserveStorageNotification(event, aStorageType,
87
aPrivateBrowsing);
88
});
89
90
if (aImmediateDispatch) {
91
r->Run();
92
} else {
93
nsCOMPtr<nsIEventTarget> et = observer->GetEventTarget();
94
if (et) {
95
et->Dispatch(r.forget());
96
}
97
}
98
}
99
}
100
101
void StorageNotifierService::Register(StorageNotificationObserver* aObserver) {
102
MOZ_ASSERT(NS_IsMainThread());
103
MOZ_ASSERT(aObserver);
104
MOZ_ASSERT(!mObservers.Contains(aObserver));
105
106
mObservers.AppendElement(aObserver);
107
}
108
109
void StorageNotifierService::Unregister(
110
StorageNotificationObserver* aObserver) {
111
MOZ_ASSERT(NS_IsMainThread());
112
MOZ_ASSERT(aObserver);
113
114
// No assertion about mObservers containing aObserver because window calls
115
// this method multiple times.
116
117
mObservers.RemoveElement(aObserver);
118
}
119
120
} // namespace dom
121
} // namespace mozilla