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 "ServiceWorkerProxy.h"
8
9
#include "mozilla/ipc/BackgroundParent.h"
10
#include "ServiceWorkerInfo.h"
11
12
namespace mozilla {
13
namespace dom {
14
15
using mozilla::ipc::AssertIsOnBackgroundThread;
16
17
ServiceWorkerProxy::~ServiceWorkerProxy() {
18
// Any thread
19
MOZ_DIAGNOSTIC_ASSERT(!mActor);
20
MOZ_DIAGNOSTIC_ASSERT(!mInfo);
21
}
22
23
void ServiceWorkerProxy::MaybeShutdownOnBGThread() {
24
AssertIsOnBackgroundThread();
25
if (!mActor) {
26
return;
27
}
28
mActor->MaybeSendDelete();
29
}
30
31
void ServiceWorkerProxy::InitOnMainThread() {
32
AssertIsOnMainThread();
33
34
auto scopeExit = MakeScopeExit([&] { MaybeShutdownOnMainThread(); });
35
36
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
37
NS_ENSURE_TRUE_VOID(swm);
38
39
RefPtr<ServiceWorkerRegistrationInfo> reg =
40
swm->GetRegistration(mDescriptor.PrincipalInfo(), mDescriptor.Scope());
41
NS_ENSURE_TRUE_VOID(reg);
42
43
RefPtr<ServiceWorkerInfo> info = reg->GetByDescriptor(mDescriptor);
44
NS_ENSURE_TRUE_VOID(info);
45
46
scopeExit.release();
47
48
mInfo = new nsMainThreadPtrHolder<ServiceWorkerInfo>(
49
"ServiceWorkerProxy::mInfo", info);
50
}
51
52
void ServiceWorkerProxy::MaybeShutdownOnMainThread() {
53
AssertIsOnMainThread();
54
55
nsCOMPtr<nsIRunnable> r = NewRunnableMethod(
56
__func__, this, &ServiceWorkerProxy::MaybeShutdownOnBGThread);
57
58
MOZ_ALWAYS_SUCCEEDS(mEventTarget->Dispatch(r.forget(), NS_DISPATCH_NORMAL));
59
}
60
61
void ServiceWorkerProxy::StopListeningOnMainThread() {
62
AssertIsOnMainThread();
63
mInfo = nullptr;
64
}
65
66
ServiceWorkerProxy::ServiceWorkerProxy(
67
const ServiceWorkerDescriptor& aDescriptor)
68
: mActor(nullptr),
69
mEventTarget(GetCurrentThreadSerialEventTarget()),
70
mDescriptor(aDescriptor) {}
71
72
void ServiceWorkerProxy::Init(ServiceWorkerParent* aActor) {
73
AssertIsOnBackgroundThread();
74
MOZ_DIAGNOSTIC_ASSERT(aActor);
75
MOZ_DIAGNOSTIC_ASSERT(!mActor);
76
MOZ_DIAGNOSTIC_ASSERT(mEventTarget);
77
78
mActor = aActor;
79
80
// Note, this must be done from a separate Init() method and not in
81
// the constructor. If done from the constructor the runnable can
82
// execute, complete, and release its reference before the constructor
83
// returns.
84
nsCOMPtr<nsIRunnable> r = NewRunnableMethod(
85
"ServiceWorkerProxy::Init", this, &ServiceWorkerProxy::InitOnMainThread);
86
MOZ_ALWAYS_SUCCEEDS(SystemGroup::Dispatch(TaskCategory::Other, r.forget()));
87
}
88
89
void ServiceWorkerProxy::RevokeActor(ServiceWorkerParent* aActor) {
90
AssertIsOnBackgroundThread();
91
MOZ_DIAGNOSTIC_ASSERT(mActor);
92
MOZ_DIAGNOSTIC_ASSERT(mActor == aActor);
93
mActor = nullptr;
94
95
nsCOMPtr<nsIRunnable> r = NewRunnableMethod(
96
__func__, this, &ServiceWorkerProxy::StopListeningOnMainThread);
97
MOZ_ALWAYS_SUCCEEDS(SystemGroup::Dispatch(TaskCategory::Other, r.forget()));
98
}
99
100
void ServiceWorkerProxy::PostMessage(RefPtr<ServiceWorkerCloneData>&& aData,
101
const ClientInfo& aClientInfo,
102
const ClientState& aClientState) {
103
AssertIsOnBackgroundThread();
104
RefPtr<ServiceWorkerProxy> self = this;
105
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
106
__func__,
107
[self, data = std::move(aData), aClientInfo, aClientState]() mutable {
108
if (!self->mInfo) {
109
return;
110
}
111
self->mInfo->PostMessage(std::move(data), aClientInfo, aClientState);
112
});
113
MOZ_ALWAYS_SUCCEEDS(SystemGroup::Dispatch(TaskCategory::Other, r.forget()));
114
}
115
116
} // namespace dom
117
} // namespace mozilla