Bug 1874684 - Part 6: Limit day length calculations to safe integers. r=mgaudet
[gecko.git] / dom / storage / StorageNotifierService.cpp
blob57b1ef6bc37609f33d0a1574293d387b24fd84b7
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 "StorageUtils.h"
9 #include "mozilla/dom/StorageEvent.h"
10 #include "mozilla/ClearOnShutdown.h"
11 #include "mozilla/StaticPtr.h"
12 #include "nsThreadUtils.h"
14 namespace mozilla::dom {
16 namespace {
18 // This boolean is used to avoid the creation of the service after been
19 // distroyed on shutdown.
20 bool gStorageShuttingDown = false;
22 StaticRefPtr<StorageNotifierService> gStorageNotifierService;
24 } // namespace
26 /* static */
27 StorageNotifierService* StorageNotifierService::GetOrCreate() {
28 MOZ_ASSERT(NS_IsMainThread());
29 if (!gStorageNotifierService && !gStorageShuttingDown) {
30 gStorageNotifierService = new StorageNotifierService();
31 ClearOnShutdown(&gStorageNotifierService);
34 return gStorageNotifierService;
37 StorageNotifierService::StorageNotifierService() {
38 MOZ_ASSERT(NS_IsMainThread());
39 MOZ_ASSERT(!gStorageNotifierService);
42 StorageNotifierService::~StorageNotifierService() {
43 MOZ_ASSERT(NS_IsMainThread());
44 MOZ_ASSERT(!gStorageNotifierService);
45 gStorageShuttingDown = true;
48 /* static */
49 void StorageNotifierService::Broadcast(StorageEvent* aEvent,
50 const char16_t* aStorageType,
51 bool aPrivateBrowsing,
52 bool aImmediateDispatch) {
53 MOZ_ASSERT(NS_IsMainThread());
55 RefPtr<StorageNotifierService> service = gStorageNotifierService;
56 if (!service) {
57 return;
60 RefPtr<StorageEvent> event = aEvent;
62 for (const auto& observer : service->mObservers.ForwardRange()) {
63 // Enforce that the source storage area's private browsing state matches
64 // this window's state. These flag checks and their maintenance independent
65 // from the principal's OriginAttributes matter because chrome docshells
66 // that are part of private browsing windows can be private browsing without
67 // having their OriginAttributes set (because they have the system
68 // principal).
69 if (aPrivateBrowsing != observer->IsPrivateBrowsing()) {
70 continue;
73 // No reasons to continue if the principal of the event doesn't match with
74 // the window's one.
75 if (!StorageUtils::PrincipalsEqual(
76 aEvent->GetPrincipal(), observer->GetEffectiveStoragePrincipal())) {
77 continue;
80 const auto pinnedObserver = observer;
82 RefPtr<Runnable> r = NS_NewRunnableFunction(
83 "StorageNotifierService::Broadcast",
84 [pinnedObserver, event, aStorageType, aPrivateBrowsing,
85 aImmediateDispatch]() {
86 // Check principals again. EffectiveStoragePrincipal may be changed
87 // when relaxed.
88 if (!aImmediateDispatch &&
89 !StorageUtils::PrincipalsEqual(
90 event->GetPrincipal(),
91 pinnedObserver->GetEffectiveStoragePrincipal())) {
92 return;
95 pinnedObserver->ObserveStorageNotification(event, aStorageType,
96 aPrivateBrowsing);
97 });
99 if (aImmediateDispatch) {
100 r->Run();
101 } else {
102 nsCOMPtr<nsIEventTarget> et = pinnedObserver->GetEventTarget();
103 if (et) {
104 et->Dispatch(r.forget());
110 void StorageNotifierService::Register(StorageNotificationObserver* aObserver) {
111 MOZ_ASSERT(NS_IsMainThread());
112 MOZ_ASSERT(aObserver);
113 MOZ_ASSERT(!mObservers.Contains(aObserver));
115 mObservers.AppendElement(aObserver);
118 void StorageNotifierService::Unregister(
119 StorageNotificationObserver* aObserver) {
120 MOZ_ASSERT(NS_IsMainThread());
121 MOZ_ASSERT(aObserver);
123 // No assertion about mObservers containing aObserver because window calls
124 // this method multiple times.
126 mObservers.RemoveElement(aObserver);
129 } // namespace mozilla::dom