summaryrefslogtreecommitdiffstats
path: root/dom/storage/SessionStorageService.cpp
blob: fe42354c09fef07badd6121a855ada73a1b75699 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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<SessionStorageService> 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<RefPtr<SessionStorageService>, nsresult> SessionStorageService::Acquire(
    const CreateIfNonExistent&) {
  AssertIsOnMainThread();

  QM_TRY(OkIf(!gShutdown), Err(NS_ERROR_ILLEGAL_DURING_SHUTDOWN));

  if (gSessionStorageService) {
    return RefPtr<SessionStorageService>(gSessionStorageService);
  }

  auto sessionStorageService = MakeRefPtr<SessionStorageService>();

  QM_TRY(sessionStorageService->Init());

  gSessionStorageService = sessionStorageService;

  RunOnShutdown(
      [] {
        gShutdown = true;

        gSessionStorageService->Shutdown();

        gSessionStorageService = nullptr;
      },
      ShutdownPhase::XPCOMShutdown);

  return sessionStorageService;
}

// static
RefPtr<SessionStorageService> SessionStorageService::Acquire() {
  AssertIsOnMainThread();

  return gSessionStorageService;
}

Result<Ok, nsresult> 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<nsISupports>();
}