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 "ServiceWorkerImpl.h"
8
9
#include "ServiceWorkerInfo.h"
10
11
namespace mozilla {
12
namespace dom {
13
14
ServiceWorkerImpl::~ServiceWorkerImpl() { MOZ_DIAGNOSTIC_ASSERT(!mOuter); }
15
16
void ServiceWorkerImpl::AddServiceWorker(ServiceWorker* aWorker) {
17
MOZ_DIAGNOSTIC_ASSERT(!mOuter);
18
MOZ_DIAGNOSTIC_ASSERT(aWorker);
19
mOuter = aWorker;
20
}
21
22
void ServiceWorkerImpl::RemoveServiceWorker(ServiceWorker* aWorker) {
23
MOZ_DIAGNOSTIC_ASSERT(mOuter);
24
MOZ_DIAGNOSTIC_ASSERT(mOuter == aWorker);
25
mOuter = nullptr;
26
}
27
28
void ServiceWorkerImpl::GetRegistration(
29
ServiceWorkerRegistrationCallback&& aSuccessCB,
30
ServiceWorkerFailureCallback&& aFailureCB) {
31
// While we could immediate call success with our registration descriptor
32
// we instead queue a runnable to do this. This ensures that
33
// GetRegistration() is always async to match how the IPC implementation will
34
// work. It also ensure that if any events are triggered from providing the
35
// registration that they are fired from a runnable on the correct global's
36
// event target.
37
38
if (!mOuter) {
39
aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
40
return;
41
}
42
43
nsIGlobalObject* global = mOuter->GetParentObject();
44
if (!global) {
45
aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
46
return;
47
}
48
49
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
50
"ServiceWorkerImpl::GetRegistration",
51
[reg = mReg, successCB = std::move(aSuccessCB)]() mutable {
52
successCB(reg->Descriptor());
53
});
54
55
nsresult rv = global->EventTargetFor(TaskCategory::Other)
56
->Dispatch(r.forget(), NS_DISPATCH_NORMAL);
57
if (NS_FAILED(rv)) {
58
aFailureCB(CopyableErrorResult(rv));
59
return;
60
}
61
}
62
63
void ServiceWorkerImpl::PostMessage(RefPtr<ServiceWorkerCloneData>&& aData,
64
const ClientInfo& aClientInfo,
65
const ClientState& aClientState) {
66
mInfo->PostMessage(std::move(aData), aClientInfo, aClientState);
67
}
68
69
ServiceWorkerImpl::ServiceWorkerImpl(ServiceWorkerInfo* aInfo,
70
ServiceWorkerRegistrationInfo* aReg)
71
: mInfo(aInfo), mReg(aReg), mOuter(nullptr) {
72
MOZ_DIAGNOSTIC_ASSERT(mInfo);
73
MOZ_DIAGNOSTIC_ASSERT(mReg);
74
}
75
76
} // namespace dom
77
} // namespace mozilla