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 #include "StorageNotifierService.h"
8 #include "mozilla/ClearOnShutdown.h"
9 #include "mozilla/StaticPtr.h"
16 // This boolean is used to avoid the creation of the service after been
17 // distroyed on shutdown.
18 bool gStorageShuttingDown
= false;
20 StaticRefPtr
<StorageNotifierService
> gStorageNotifierService
;
25 StorageNotifierService
* StorageNotifierService::GetOrCreate() {
26 MOZ_ASSERT(NS_IsMainThread());
27 if (!gStorageNotifierService
&& !gStorageShuttingDown
) {
28 gStorageNotifierService
= new StorageNotifierService();
29 ClearOnShutdown(&gStorageNotifierService
);
32 return gStorageNotifierService
;
35 StorageNotifierService::StorageNotifierService() {
36 MOZ_ASSERT(NS_IsMainThread());
37 MOZ_ASSERT(!gStorageNotifierService
);
40 StorageNotifierService::~StorageNotifierService() {
41 MOZ_ASSERT(NS_IsMainThread());
42 MOZ_ASSERT(!gStorageNotifierService
);
43 gStorageShuttingDown
= true;
47 void StorageNotifierService::Broadcast(StorageEvent
* aEvent
,
48 const char16_t
* aStorageType
,
49 bool aPrivateBrowsing
,
50 bool aImmediateDispatch
) {
51 MOZ_ASSERT(NS_IsMainThread());
53 RefPtr
<StorageNotifierService
> service
= gStorageNotifierService
;
58 RefPtr
<StorageEvent
> event
= aEvent
;
60 nsTObserverArray
<RefPtr
<StorageNotificationObserver
>>::ForwardIterator
iter(
63 while (iter
.HasMore()) {
64 RefPtr
<StorageNotificationObserver
> observer
= iter
.GetNext();
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
72 if (aPrivateBrowsing
!= observer
->IsPrivateBrowsing()) {
76 // No reasons to continue if the principal of the event doesn't match with
78 if (!StorageUtils::PrincipalsEqual(
79 aEvent
->GetPrincipal(), observer
->GetEffectiveStoragePrincipal())) {
83 RefPtr
<Runnable
> r
= NS_NewRunnableFunction(
84 "StorageNotifierService::Broadcast",
85 [observer
, event
, aStorageType
, aPrivateBrowsing
]() {
86 observer
->ObserveStorageNotification(event
, aStorageType
,
90 if (aImmediateDispatch
) {
93 nsCOMPtr
<nsIEventTarget
> et
= observer
->GetEventTarget();
95 et
->Dispatch(r
.forget());
101 void StorageNotifierService::Register(StorageNotificationObserver
* aObserver
) {
102 MOZ_ASSERT(NS_IsMainThread());
103 MOZ_ASSERT(aObserver
);
104 MOZ_ASSERT(!mObservers
.Contains(aObserver
));
106 mObservers
.AppendElement(aObserver
);
109 void StorageNotifierService::Unregister(
110 StorageNotificationObserver
* aObserver
) {
111 MOZ_ASSERT(NS_IsMainThread());
112 MOZ_ASSERT(aObserver
);
114 // No assertion about mObservers containing aObserver because window calls
115 // this method multiple times.
117 mObservers
.RemoveElement(aObserver
);
121 } // namespace mozilla