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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "SessionStorageService.h"
9 #include "MainThreadUtils.h"
10 #include "mozilla/ClearOnShutdown.h"
11 #include "mozilla/Components.h"
12 #include "mozilla/StaticPtr.h"
13 #include "mozilla/ipc/BackgroundChild.h"
14 #include "mozilla/ipc/PBackgroundChild.h"
16 namespace mozilla::dom
{
18 using namespace mozilla::ipc
;
22 StaticRefPtr
<SessionStorageService
> gSessionStorageService
;
24 bool gShutdown(false);
28 NS_IMPL_ISUPPORTS(SessionStorageService
, nsISessionStorageService
)
31 SessionStorageService::ClearStoragesForOrigin(nsIPrincipal
* aPrincipal
) {
32 AssertIsOnMainThread();
33 MOZ_ASSERT(aPrincipal
);
35 QM_TRY_INSPECT(const auto& originAttrs
,
36 MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(nsCString
, aPrincipal
,
39 QM_TRY_INSPECT(const auto& originKey
,
40 MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(nsCString
, aPrincipal
,
41 GetStorageOriginKey
));
43 QM_TRY(OkIf(SendClearStoragesForOrigin(originAttrs
, originKey
)),
49 SessionStorageService::SessionStorageService() { AssertIsOnMainThread(); }
51 SessionStorageService::~SessionStorageService() {
52 AssertIsOnMainThread();
53 MOZ_ASSERT_IF(mInitialized
, mActorDestroyed
);
57 Result
<RefPtr
<SessionStorageService
>, nsresult
> SessionStorageService::Acquire(
58 const CreateIfNonExistent
&) {
59 AssertIsOnMainThread();
61 QM_TRY(OkIf(!gShutdown
), Err(NS_ERROR_ILLEGAL_DURING_SHUTDOWN
));
63 if (gSessionStorageService
) {
64 return RefPtr
<SessionStorageService
>(gSessionStorageService
);
67 auto sessionStorageService
= MakeRefPtr
<SessionStorageService
>();
69 QM_TRY(sessionStorageService
->Init());
71 gSessionStorageService
= sessionStorageService
;
77 gSessionStorageService
->Shutdown();
79 gSessionStorageService
= nullptr;
81 ShutdownPhase::XPCOMShutdown
);
83 return sessionStorageService
;
87 RefPtr
<SessionStorageService
> SessionStorageService::Acquire() {
88 AssertIsOnMainThread();
90 return gSessionStorageService
;
93 Result
<Ok
, nsresult
> SessionStorageService::Init() {
94 AssertIsOnMainThread();
96 PBackgroundChild
* backgroundActor
=
97 BackgroundChild::GetOrCreateForCurrentThread();
98 QM_TRY(OkIf(backgroundActor
), Err(NS_ERROR_FAILURE
));
100 QM_TRY(OkIf(backgroundActor
->SendPBackgroundSessionStorageServiceConstructor(
102 Err(NS_ERROR_FAILURE
));
109 void SessionStorageService::Shutdown() {
110 AssertIsOnMainThread();
112 if (!mActorDestroyed
) {
113 QM_WARNONLY_TRY(OkIf(Send__delete__(this)));
117 void SessionStorageService::ActorDestroy(ActorDestroyReason
/* aWhy */) {
118 AssertIsOnMainThread();
120 mActorDestroyed
.Flip();
123 } // namespace mozilla::dom
125 NS_IMPL_COMPONENT_FACTORY(nsISessionStorageService
) {
126 mozilla::AssertIsOnMainThread();
128 QM_TRY_UNWRAP(auto sessionStorageService
,
129 mozilla::dom::SessionStorageService::Acquire(
130 mozilla::CreateIfNonExistent
{}),
133 return sessionStorageService
.forget().downcast
<nsISupports
>();