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