summaryrefslogtreecommitdiffstats
path: root/dom/storage/StorageNotifierService.cpp
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /dom/storage/StorageNotifierService.cpp
parentInitial commit. (diff)
downloadfirefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz
firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/storage/StorageNotifierService.cpp')
-rw-r--r--dom/storage/StorageNotifierService.cpp131
1 files changed, 131 insertions, 0 deletions
diff --git a/dom/storage/StorageNotifierService.cpp b/dom/storage/StorageNotifierService.cpp
new file mode 100644
index 0000000000..e09a05f8fb
--- /dev/null
+++ b/dom/storage/StorageNotifierService.cpp
@@ -0,0 +1,131 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "StorageNotifierService.h"
+#include "StorageUtils.h"
+#include "mozilla/dom/StorageEvent.h"
+#include "mozilla/ClearOnShutdown.h"
+#include "mozilla/StaticPtr.h"
+#include "nsThreadUtils.h"
+
+namespace mozilla {
+namespace dom {
+
+namespace {
+
+// This boolean is used to avoid the creation of the service after been
+// distroyed on shutdown.
+bool gStorageShuttingDown = false;
+
+StaticRefPtr<StorageNotifierService> gStorageNotifierService;
+
+} // namespace
+
+/* static */
+StorageNotifierService* StorageNotifierService::GetOrCreate() {
+ MOZ_ASSERT(NS_IsMainThread());
+ if (!gStorageNotifierService && !gStorageShuttingDown) {
+ gStorageNotifierService = new StorageNotifierService();
+ ClearOnShutdown(&gStorageNotifierService);
+ }
+
+ return gStorageNotifierService;
+}
+
+StorageNotifierService::StorageNotifierService() {
+ MOZ_ASSERT(NS_IsMainThread());
+ MOZ_ASSERT(!gStorageNotifierService);
+}
+
+StorageNotifierService::~StorageNotifierService() {
+ MOZ_ASSERT(NS_IsMainThread());
+ MOZ_ASSERT(!gStorageNotifierService);
+ gStorageShuttingDown = true;
+}
+
+/* static */
+void StorageNotifierService::Broadcast(StorageEvent* aEvent,
+ const char16_t* aStorageType,
+ bool aPrivateBrowsing,
+ bool aImmediateDispatch) {
+ MOZ_ASSERT(NS_IsMainThread());
+
+ RefPtr<StorageNotifierService> service = gStorageNotifierService;
+ if (!service) {
+ return;
+ }
+
+ RefPtr<StorageEvent> event = aEvent;
+
+ for (const auto& observer : service->mObservers.ForwardRange()) {
+ // Enforce that the source storage area's private browsing state matches
+ // this window's state. These flag checks and their maintenance independent
+ // from the principal's OriginAttributes matter because chrome docshells
+ // that are part of private browsing windows can be private browsing without
+ // having their OriginAttributes set (because they have the system
+ // principal).
+ if (aPrivateBrowsing != observer->IsPrivateBrowsing()) {
+ continue;
+ }
+
+ // No reasons to continue if the principal of the event doesn't match with
+ // the window's one.
+ if (!StorageUtils::PrincipalsEqual(
+ aEvent->GetPrincipal(), observer->GetEffectiveStoragePrincipal())) {
+ continue;
+ }
+
+ const auto pinnedObserver = observer;
+
+ RefPtr<Runnable> r = NS_NewRunnableFunction(
+ "StorageNotifierService::Broadcast",
+ [pinnedObserver, event, aStorageType, aPrivateBrowsing,
+ aImmediateDispatch]() {
+ // Check principals again. EffectiveStoragePrincipal may be changed
+ // when relaxed.
+ if (!aImmediateDispatch &&
+ !StorageUtils::PrincipalsEqual(
+ event->GetPrincipal(),
+ pinnedObserver->GetEffectiveStoragePrincipal())) {
+ return;
+ }
+
+ pinnedObserver->ObserveStorageNotification(event, aStorageType,
+ aPrivateBrowsing);
+ });
+
+ if (aImmediateDispatch) {
+ r->Run();
+ } else {
+ nsCOMPtr<nsIEventTarget> et = pinnedObserver->GetEventTarget();
+ if (et) {
+ et->Dispatch(r.forget());
+ }
+ }
+ }
+}
+
+void StorageNotifierService::Register(StorageNotificationObserver* aObserver) {
+ MOZ_ASSERT(NS_IsMainThread());
+ MOZ_ASSERT(aObserver);
+ MOZ_ASSERT(!mObservers.Contains(aObserver));
+
+ mObservers.AppendElement(aObserver);
+}
+
+void StorageNotifierService::Unregister(
+ StorageNotificationObserver* aObserver) {
+ MOZ_ASSERT(NS_IsMainThread());
+ MOZ_ASSERT(aObserver);
+
+ // No assertion about mObservers containing aObserver because window calls
+ // this method multiple times.
+
+ mObservers.RemoveElement(aObserver);
+}
+
+} // namespace dom
+} // namespace mozilla