Bug 1610775 [wpt PR 21336] - Update urllib3 to 1.25.8, a=testonly
[gecko.git] / dom / storage / StorageNotifierService.cpp
bloba95746bf644cd7eada1b2f85df27657458ca7eb0
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"
11 namespace mozilla {
12 namespace dom {
14 namespace {
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;
22 } // namespace
24 /* static */
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;
46 /* static */
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;
54 if (!service) {
55 return;
58 RefPtr<StorageEvent> event = aEvent;
60 nsTObserverArray<RefPtr<StorageNotificationObserver>>::ForwardIterator iter(
61 service->mObservers);
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
71 // principal).
72 if (aPrivateBrowsing != observer->IsPrivateBrowsing()) {
73 continue;
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;
83 RefPtr<Runnable> r = NS_NewRunnableFunction(
84 "StorageNotifierService::Broadcast",
85 [observer, event, aStorageType, aPrivateBrowsing]() {
86 observer->ObserveStorageNotification(event, aStorageType,
87 aPrivateBrowsing);
88 });
90 if (aImmediateDispatch) {
91 r->Run();
92 } else {
93 nsCOMPtr<nsIEventTarget> et = observer->GetEventTarget();
94 if (et) {
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);
120 } // namespace dom
121 } // namespace mozilla