Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/serviceworkers/ServiceWorkerContainerImpl.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 "ServiceWorkerContainerImpl.h"
8
9
#include "ServiceWorkerRegistration.h"
10
11
namespace mozilla {
12
namespace dom {
13
14
ServiceWorkerContainerImpl::~ServiceWorkerContainerImpl()
15
0
{
16
0
  MOZ_DIAGNOSTIC_ASSERT(!mOuter);
17
0
}
18
19
ServiceWorkerContainerImpl::ServiceWorkerContainerImpl()
20
  : mOuter(nullptr)
21
0
{
22
0
}
23
24
void
25
ServiceWorkerContainerImpl::AddContainer(ServiceWorkerContainer* aOuter)
26
0
{
27
0
  MOZ_DIAGNOSTIC_ASSERT(aOuter);
28
0
  MOZ_DIAGNOSTIC_ASSERT(!mOuter);
29
0
  mOuter = aOuter;
30
0
}
31
32
void
33
ServiceWorkerContainerImpl::RemoveContainer(ServiceWorkerContainer* aOuter)
34
0
{
35
0
  MOZ_DIAGNOSTIC_ASSERT(aOuter);
36
0
  MOZ_DIAGNOSTIC_ASSERT(mOuter == aOuter);
37
0
  mOuter = nullptr;
38
0
}
39
40
void
41
ServiceWorkerContainerImpl::Register(const ClientInfo& aClientInfo,
42
                                     const nsACString& aScopeURL,
43
                                     const nsACString& aScriptURL,
44
                                     ServiceWorkerUpdateViaCache aUpdateViaCache,
45
                                     ServiceWorkerRegistrationCallback&& aSuccessCB,
46
                                     ServiceWorkerFailureCallback&& aFailureCB) const
47
0
{
48
0
  MOZ_DIAGNOSTIC_ASSERT(mOuter);
49
0
50
0
  nsIGlobalObject* global = mOuter->GetParentObject();
51
0
  if (NS_WARN_IF(!global)) {
52
0
    aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
53
0
    return;
54
0
  }
55
0
56
0
  RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
57
0
  if (NS_WARN_IF(!swm)) {
58
0
    aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
59
0
    return;
60
0
  }
61
0
62
0
  auto holder =
63
0
    MakeRefPtr<DOMMozPromiseRequestHolder<ServiceWorkerRegistrationPromise>>(global);
64
0
65
0
  swm->Register(aClientInfo, aScopeURL, aScriptURL, aUpdateViaCache)->Then(
66
0
    global->EventTargetFor(TaskCategory::Other), __func__,
67
0
    [successCB = std::move(aSuccessCB), holder] (const ServiceWorkerRegistrationDescriptor& aDescriptor) {
68
0
      holder->Complete();
69
0
      successCB(aDescriptor);
70
0
    }, [failureCB = std::move(aFailureCB), holder] (const CopyableErrorResult& aResult) {
71
0
      holder->Complete();
72
0
      failureCB(CopyableErrorResult(aResult));
73
0
    })->Track(*holder);
74
0
}
75
76
void
77
ServiceWorkerContainerImpl::GetRegistration(const ClientInfo& aClientInfo,
78
                                            const nsACString& aURL,
79
                                            ServiceWorkerRegistrationCallback&& aSuccessCB,
80
                                            ServiceWorkerFailureCallback&& aFailureCB) const
81
0
{
82
0
  MOZ_DIAGNOSTIC_ASSERT(mOuter);
83
0
84
0
  nsIGlobalObject* global = mOuter->GetParentObject();
85
0
  if (NS_WARN_IF(!global)) {
86
0
    aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
87
0
    return;
88
0
  }
89
0
90
0
  RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
91
0
  if (NS_WARN_IF(!swm)) {
92
0
    aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
93
0
    return;
94
0
  }
95
0
96
0
  auto holder =
97
0
    MakeRefPtr<DOMMozPromiseRequestHolder<ServiceWorkerRegistrationPromise>>(global);
98
0
99
0
  swm->GetRegistration(aClientInfo, aURL)->Then(
100
0
    global->EventTargetFor(TaskCategory::Other), __func__,
101
0
    [successCB = std::move(aSuccessCB), holder] (const ServiceWorkerRegistrationDescriptor& aDescriptor) {
102
0
      holder->Complete();
103
0
      successCB(aDescriptor);
104
0
    }, [failureCB = std::move(aFailureCB), holder] (const CopyableErrorResult& aResult) {
105
0
      holder->Complete();
106
0
      failureCB(CopyableErrorResult(aResult));
107
0
    })->Track(*holder);
108
0
}
109
110
void
111
ServiceWorkerContainerImpl::GetRegistrations(const ClientInfo& aClientInfo,
112
                                             ServiceWorkerRegistrationListCallback&& aSuccessCB,
113
                                             ServiceWorkerFailureCallback&& aFailureCB) const
114
0
{
115
0
  MOZ_DIAGNOSTIC_ASSERT(mOuter);
116
0
117
0
  nsIGlobalObject* global = mOuter->GetParentObject();
118
0
  if (NS_WARN_IF(!global)) {
119
0
    aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
120
0
    return;
121
0
  }
122
0
123
0
  RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
124
0
  if (NS_WARN_IF(!swm)) {
125
0
    aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
126
0
    return;
127
0
  }
128
0
129
0
  auto holder =
130
0
    MakeRefPtr<DOMMozPromiseRequestHolder<ServiceWorkerRegistrationListPromise>>(global);
131
0
132
0
  swm->GetRegistrations(aClientInfo)->Then(
133
0
    global->EventTargetFor(TaskCategory::Other), __func__,
134
0
    [successCB = std::move(aSuccessCB), holder] (const nsTArray<ServiceWorkerRegistrationDescriptor>& aList) {
135
0
      holder->Complete();
136
0
      successCB(aList);
137
0
    }, [failureCB = std::move(aFailureCB), holder] (const CopyableErrorResult& aResult) {
138
0
      holder->Complete();
139
0
      failureCB(CopyableErrorResult(aResult));
140
0
    })->Track(*holder);
141
0
}
142
143
void
144
ServiceWorkerContainerImpl::GetReady(const ClientInfo& aClientInfo,
145
                                     ServiceWorkerRegistrationCallback&& aSuccessCB,
146
                                     ServiceWorkerFailureCallback&& aFailureCB) const
147
0
{
148
0
  MOZ_DIAGNOSTIC_ASSERT(mOuter);
149
0
150
0
  nsIGlobalObject* global = mOuter->GetParentObject();
151
0
  if (NS_WARN_IF(!global)) {
152
0
    aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
153
0
    return;
154
0
  }
155
0
156
0
  RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
157
0
  if (NS_WARN_IF(!swm)) {
158
0
    aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
159
0
    return;
160
0
  }
161
0
162
0
  auto holder =
163
0
    MakeRefPtr<DOMMozPromiseRequestHolder<ServiceWorkerRegistrationPromise>>(global);
164
0
165
0
  swm->WhenReady(aClientInfo)->Then(
166
0
    global->EventTargetFor(TaskCategory::Other), __func__,
167
0
    [successCB = std::move(aSuccessCB), holder] (const ServiceWorkerRegistrationDescriptor& aDescriptor) {
168
0
      holder->Complete();
169
0
      successCB(aDescriptor);
170
0
    }, [failureCB = std::move(aFailureCB), holder] (const CopyableErrorResult& aResult) {
171
0
      holder->Complete();
172
0
      failureCB(CopyableErrorResult(aResult));
173
0
    })->Track(*holder);
174
0
}
175
176
} // namespace dom
177
} // namespace mozilla