Source code

Revision control

Other Tools

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
#include "SessionStorageService.h"
6
7
#include "mozilla/Services.h"
8
#include "mozilla/Unused.h"
9
#include "nsIObserverService.h"
10
#include "nsXULAppAPI.h"
11
12
#include <cstring>
13
14
namespace mozilla {
15
namespace dom {
16
17
namespace {
18
19
const char* const kContentProcessShutdownTopic = "content-child-will-shutdown";
20
21
}
22
23
RefPtr<SessionStorageService> SessionStorageService::sService = nullptr;
24
bool SessionStorageService::sShutdown = false;
25
26
NS_IMPL_ISUPPORTS(SessionStorageService, nsIObserver)
27
28
SessionStorageService::SessionStorageService() {
29
if (const nsCOMPtr<nsIObserverService> observerService =
30
services::GetObserverService()) {
31
Unused << observerService->AddObserver(this, kContentProcessShutdownTopic,
32
false);
33
}
34
}
35
36
SessionStorageService::~SessionStorageService() {
37
if (const nsCOMPtr<nsIObserverService> observerService =
38
services::GetObserverService()) {
39
Unused << observerService->RemoveObserver(this,
40
kContentProcessShutdownTopic);
41
}
42
}
43
44
SessionStorageService* SessionStorageService::Get() {
45
if (sShutdown) {
46
return nullptr;
47
}
48
49
if (XRE_IsParentProcess()) {
50
ShutDown();
51
return nullptr;
52
}
53
54
if (!sService) {
55
sService = new SessionStorageService();
56
}
57
58
return sService;
59
}
60
61
NS_IMETHODIMP SessionStorageService::Observe(nsISupports* const aSubject,
62
const char* const aTopic,
63
const char16_t* const aData) {
64
if (std::strcmp(aTopic, kContentProcessShutdownTopic) == 0) {
65
SendSessionStorageDataToParentProcess();
66
ShutDown();
67
}
68
return NS_OK;
69
}
70
71
void SessionStorageService::RegisterSessionStorageManager(
72
SessionStorageManager* aManager) {
73
mManagers.PutEntry(aManager);
74
}
75
76
void SessionStorageService::UnregisterSessionStorageManager(
77
SessionStorageManager* aManager) {
78
if (const auto entry = mManagers.GetEntry(aManager)) {
79
mManagers.RemoveEntry(entry);
80
}
81
}
82
83
void SessionStorageService::SendSessionStorageDataToParentProcess() {
84
for (auto iter = mManagers.Iter(); !iter.Done(); iter.Next()) {
85
iter.Get()->GetKey()->SendSessionStorageDataToParentProcess();
86
}
87
}
88
89
void SessionStorageService::ShutDown() {
90
sShutdown = true;
91
sService = nullptr;
92
}
93
94
} // namespace dom
95
} // namespace mozilla