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 "RemoteServiceWorkerImpl.h"
8
9
#include "mozilla/dom/ClientInfo.h"
10
#include "mozilla/dom/ClientState.h"
11
#include "mozilla/ipc/BackgroundChild.h"
12
#include "mozilla/ipc/PBackgroundChild.h"
13
#include "ServiceWorkerChild.h"
14
#include "ServiceWorkerCloneData.h"
15
16
namespace mozilla {
17
namespace dom {
18
19
using mozilla::ipc::BackgroundChild;
20
using mozilla::ipc::PBackgroundChild;
21
22
RemoteServiceWorkerImpl::~RemoteServiceWorkerImpl() {
23
MOZ_DIAGNOSTIC_ASSERT(!mWorker);
24
Shutdown();
25
}
26
27
void RemoteServiceWorkerImpl::Shutdown() {
28
if (mShutdown) {
29
return;
30
}
31
mShutdown = true;
32
33
if (mActor) {
34
mActor->RevokeOwner(this);
35
mActor->MaybeStartTeardown();
36
mActor = nullptr;
37
}
38
}
39
40
void RemoteServiceWorkerImpl::AddServiceWorker(ServiceWorker* aWorker) {
41
NS_ASSERT_OWNINGTHREAD(RemoteServiceWorkerImpl);
42
MOZ_DIAGNOSTIC_ASSERT(!mWorker);
43
MOZ_DIAGNOSTIC_ASSERT(aWorker);
44
mWorker = aWorker;
45
}
46
47
void RemoteServiceWorkerImpl::RemoveServiceWorker(ServiceWorker* aWorker) {
48
NS_ASSERT_OWNINGTHREAD(RemoteServiceWorkerImpl);
49
MOZ_DIAGNOSTIC_ASSERT(mWorker);
50
MOZ_DIAGNOSTIC_ASSERT(aWorker == mWorker);
51
mWorker = nullptr;
52
}
53
54
void RemoteServiceWorkerImpl::GetRegistration(
55
ServiceWorkerRegistrationCallback&& aSuccessCB,
56
ServiceWorkerFailureCallback&& aFailureCB) {
57
// TODO
58
}
59
60
void RemoteServiceWorkerImpl::PostMessage(
61
RefPtr<ServiceWorkerCloneData>&& aData, const ClientInfo& aClientInfo,
62
const ClientState& aClientState) {
63
NS_ASSERT_OWNINGTHREAD(RemoteServiceWorkerImpl);
64
if (!mActor) {
65
return;
66
}
67
68
ClonedMessageData data;
69
if (!aData->BuildClonedMessageDataForBackgroundChild(mActor->Manager(),
70
data)) {
71
return;
72
}
73
74
mActor->SendPostMessage(
75
data, ClientInfoAndState(aClientInfo.ToIPC(), aClientState.ToIPC()));
76
}
77
78
RemoteServiceWorkerImpl::RemoteServiceWorkerImpl(
79
const ServiceWorkerDescriptor& aDescriptor)
80
: mActor(nullptr), mWorker(nullptr), mShutdown(false) {
81
PBackgroundChild* parentActor =
82
BackgroundChild::GetOrCreateForCurrentThread();
83
if (NS_WARN_IF(!parentActor)) {
84
Shutdown();
85
return;
86
}
87
88
ServiceWorkerChild* actor = ServiceWorkerChild::Create();
89
if (NS_WARN_IF(!actor)) {
90
Shutdown();
91
return;
92
}
93
94
PServiceWorkerChild* sentActor =
95
parentActor->SendPServiceWorkerConstructor(actor, aDescriptor.ToIPC());
96
if (NS_WARN_IF(!sentActor)) {
97
Shutdown();
98
return;
99
}
100
MOZ_DIAGNOSTIC_ASSERT(sentActor == actor);
101
102
mActor = actor;
103
mActor->SetOwner(this);
104
}
105
106
void RemoteServiceWorkerImpl::RevokeActor(ServiceWorkerChild* aActor) {
107
MOZ_DIAGNOSTIC_ASSERT(mActor);
108
MOZ_DIAGNOSTIC_ASSERT(mActor == aActor);
109
mActor->RevokeOwner(this);
110
mActor = nullptr;
111
112
mShutdown = true;
113
}
114
115
} // namespace dom
116
} // namespace mozilla