Source code

Revision control

Other Tools

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
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "ServiceWorkerContainerProxy.h"
8
9
#include "mozilla/ipc/BackgroundParent.h"
10
11
namespace mozilla {
12
namespace dom {
13
14
using mozilla::ipc::AssertIsOnBackgroundThread;
15
16
ServiceWorkerContainerProxy::~ServiceWorkerContainerProxy() {
17
// Any thread
18
MOZ_DIAGNOSTIC_ASSERT(!mActor);
19
}
20
21
ServiceWorkerContainerProxy::ServiceWorkerContainerProxy(
22
ServiceWorkerContainerParent* aActor)
23
: mActor(aActor) {
24
AssertIsOnBackgroundThread();
25
MOZ_DIAGNOSTIC_ASSERT(mActor);
26
27
// The container does not directly listen for updates, so we don't need
28
// to immediately initialize. The controllerchange event comes via the
29
// ClientSource associated with the ServiceWorkerContainer's bound global.
30
}
31
32
void ServiceWorkerContainerProxy::RevokeActor(
33
ServiceWorkerContainerParent* aActor) {
34
AssertIsOnBackgroundThread();
35
MOZ_DIAGNOSTIC_ASSERT(mActor);
36
MOZ_DIAGNOSTIC_ASSERT(mActor == aActor);
37
mActor = nullptr;
38
}
39
40
RefPtr<ServiceWorkerRegistrationPromise> ServiceWorkerContainerProxy::Register(
41
const ClientInfo& aClientInfo, const nsCString& aScopeURL,
42
const nsCString& aScriptURL, ServiceWorkerUpdateViaCache aUpdateViaCache) {
43
AssertIsOnBackgroundThread();
44
45
RefPtr<ServiceWorkerRegistrationPromise::Private> promise =
46
new ServiceWorkerRegistrationPromise::Private(__func__);
47
48
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
49
__func__,
50
[aClientInfo, aScopeURL, aScriptURL, aUpdateViaCache, promise]() mutable {
51
auto scopeExit = MakeScopeExit(
52
[&] { promise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__); });
53
54
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
55
NS_ENSURE_TRUE_VOID(swm);
56
57
swm->Register(aClientInfo, aScopeURL, aScriptURL, aUpdateViaCache)
58
->ChainTo(promise.forget(), __func__);
59
60
scopeExit.release();
61
});
62
63
MOZ_ALWAYS_SUCCEEDS(SystemGroup::Dispatch(TaskCategory::Other, r.forget()));
64
65
return promise;
66
}
67
68
RefPtr<ServiceWorkerRegistrationPromise>
69
ServiceWorkerContainerProxy::GetRegistration(const ClientInfo& aClientInfo,
70
const nsCString& aURL) {
71
AssertIsOnBackgroundThread();
72
73
RefPtr<ServiceWorkerRegistrationPromise::Private> promise =
74
new ServiceWorkerRegistrationPromise::Private(__func__);
75
76
nsCOMPtr<nsIRunnable> r =
77
NS_NewRunnableFunction(__func__, [aClientInfo, aURL, promise]() mutable {
78
auto scopeExit = MakeScopeExit(
79
[&] { promise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__); });
80
81
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
82
NS_ENSURE_TRUE_VOID(swm);
83
84
swm->GetRegistration(aClientInfo, aURL)
85
->ChainTo(promise.forget(), __func__);
86
87
scopeExit.release();
88
});
89
90
MOZ_ALWAYS_SUCCEEDS(SystemGroup::Dispatch(TaskCategory::Other, r.forget()));
91
92
return promise;
93
}
94
95
RefPtr<ServiceWorkerRegistrationListPromise>
96
ServiceWorkerContainerProxy::GetRegistrations(const ClientInfo& aClientInfo) {
97
AssertIsOnBackgroundThread();
98
99
RefPtr<ServiceWorkerRegistrationListPromise::Private> promise =
100
new ServiceWorkerRegistrationListPromise::Private(__func__);
101
102
nsCOMPtr<nsIRunnable> r =
103
NS_NewRunnableFunction(__func__, [aClientInfo, promise]() mutable {
104
auto scopeExit = MakeScopeExit(
105
[&] { promise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__); });
106
107
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
108
NS_ENSURE_TRUE_VOID(swm);
109
110
swm->GetRegistrations(aClientInfo)->ChainTo(promise.forget(), __func__);
111
112
scopeExit.release();
113
});
114
115
MOZ_ALWAYS_SUCCEEDS(SystemGroup::Dispatch(TaskCategory::Other, r.forget()));
116
117
return promise;
118
}
119
120
RefPtr<ServiceWorkerRegistrationPromise> ServiceWorkerContainerProxy::GetReady(
121
const ClientInfo& aClientInfo) {
122
AssertIsOnBackgroundThread();
123
124
RefPtr<ServiceWorkerRegistrationPromise::Private> promise =
125
new ServiceWorkerRegistrationPromise::Private(__func__);
126
127
nsCOMPtr<nsIRunnable> r =
128
NS_NewRunnableFunction(__func__, [aClientInfo, promise]() mutable {
129
auto scopeExit = MakeScopeExit(
130
[&] { promise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__); });
131
132
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
133
NS_ENSURE_TRUE_VOID(swm);
134
135
swm->WhenReady(aClientInfo)->ChainTo(promise.forget(), __func__);
136
137
scopeExit.release();
138
});
139
140
MOZ_ALWAYS_SUCCEEDS(SystemGroup::Dispatch(TaskCategory::Other, r.forget()));
141
142
return promise;
143
}
144
145
} // namespace dom
146
} // namespace mozilla