From 0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 03:47:29 +0200 Subject: Adding upstream version 115.8.0esr. Signed-off-by: Daniel Baumann --- dom/storage/SessionStorageService.cpp | 134 ++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 dom/storage/SessionStorageService.cpp (limited to 'dom/storage/SessionStorageService.cpp') diff --git a/dom/storage/SessionStorageService.cpp b/dom/storage/SessionStorageService.cpp new file mode 100644 index 0000000000..fe42354c09 --- /dev/null +++ b/dom/storage/SessionStorageService.cpp @@ -0,0 +1,134 @@ +/* -*- 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 "SessionStorageService.h" + +#include "MainThreadUtils.h" +#include "mozilla/ClearOnShutdown.h" +#include "mozilla/Components.h" +#include "mozilla/StaticPtr.h" +#include "mozilla/ipc/BackgroundChild.h" +#include "mozilla/ipc/PBackgroundChild.h" + +namespace mozilla::dom { + +using namespace mozilla::ipc; + +namespace { + +StaticRefPtr gSessionStorageService; + +bool gShutdown(false); + +} // namespace + +NS_IMPL_ISUPPORTS(SessionStorageService, nsISessionStorageService) + +NS_IMETHODIMP +SessionStorageService::ClearStoragesForOrigin(nsIPrincipal* aPrincipal) { + AssertIsOnMainThread(); + MOZ_ASSERT(aPrincipal); + + QM_TRY_INSPECT(const auto& originAttrs, + MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(nsCString, aPrincipal, + GetOriginSuffix)); + + QM_TRY_INSPECT(const auto& originKey, + MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(nsCString, aPrincipal, + GetStorageOriginKey)); + + QM_TRY(OkIf(SendClearStoragesForOrigin(originAttrs, originKey)), + NS_ERROR_FAILURE); + + return NS_OK; +} + +SessionStorageService::SessionStorageService() { AssertIsOnMainThread(); } + +SessionStorageService::~SessionStorageService() { + AssertIsOnMainThread(); + MOZ_ASSERT_IF(mInitialized, mActorDestroyed); +} + +// static +Result, nsresult> SessionStorageService::Acquire( + const CreateIfNonExistent&) { + AssertIsOnMainThread(); + + QM_TRY(OkIf(!gShutdown), Err(NS_ERROR_ILLEGAL_DURING_SHUTDOWN)); + + if (gSessionStorageService) { + return RefPtr(gSessionStorageService); + } + + auto sessionStorageService = MakeRefPtr(); + + QM_TRY(sessionStorageService->Init()); + + gSessionStorageService = sessionStorageService; + + RunOnShutdown( + [] { + gShutdown = true; + + gSessionStorageService->Shutdown(); + + gSessionStorageService = nullptr; + }, + ShutdownPhase::XPCOMShutdown); + + return sessionStorageService; +} + +// static +RefPtr SessionStorageService::Acquire() { + AssertIsOnMainThread(); + + return gSessionStorageService; +} + +Result SessionStorageService::Init() { + AssertIsOnMainThread(); + + PBackgroundChild* backgroundActor = + BackgroundChild::GetOrCreateForCurrentThread(); + QM_TRY(OkIf(backgroundActor), Err(NS_ERROR_FAILURE)); + + QM_TRY(OkIf(backgroundActor->SendPBackgroundSessionStorageServiceConstructor( + this)), + Err(NS_ERROR_FAILURE)); + + mInitialized.Flip(); + + return Ok{}; +} + +void SessionStorageService::Shutdown() { + AssertIsOnMainThread(); + + if (!mActorDestroyed) { + QM_WARNONLY_TRY(OkIf(Send__delete__(this))); + } +} + +void SessionStorageService::ActorDestroy(ActorDestroyReason /* aWhy */) { + AssertIsOnMainThread(); + + mActorDestroyed.Flip(); +} + +} // namespace mozilla::dom + +NS_IMPL_COMPONENT_FACTORY(nsISessionStorageService) { + mozilla::AssertIsOnMainThread(); + + QM_TRY_UNWRAP(auto sessionStorageService, + mozilla::dom::SessionStorageService::Acquire( + mozilla::CreateIfNonExistent{}), + nullptr); + + return sessionStorageService.forget().downcast(); +} -- cgit v1.2.3