Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/serviceworkers/ServiceWorkerContainerProxy.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 "ServiceWorkerContainerProxy.h"
8
9
#include "mozilla/ipc/BackgroundParent.h"
10
11
namespace mozilla {
12
namespace dom {
13
14
using mozilla::ipc::AssertIsOnBackgroundThread;
15
16
ServiceWorkerContainerProxy::~ServiceWorkerContainerProxy()
17
0
{
18
0
  // Any thread
19
0
  MOZ_DIAGNOSTIC_ASSERT(!mActor);
20
0
}
21
22
ServiceWorkerContainerProxy::ServiceWorkerContainerProxy(ServiceWorkerContainerParent* aActor)
23
  : mActor(aActor)
24
0
{
25
0
  AssertIsOnBackgroundThread();
26
0
  MOZ_DIAGNOSTIC_ASSERT(mActor);
27
0
28
0
  // The container does not directly listen for updates, so we don't need
29
0
  // to immediately initialize.  The controllerchange event comes via the
30
0
  // ClientSource associated with the ServiceWorkerContainer's bound global.
31
0
}
32
33
void
34
ServiceWorkerContainerProxy::RevokeActor(ServiceWorkerContainerParent* aActor)
35
0
{
36
0
  AssertIsOnBackgroundThread();
37
0
  MOZ_DIAGNOSTIC_ASSERT(mActor);
38
0
  MOZ_DIAGNOSTIC_ASSERT(mActor == aActor);
39
0
  mActor = nullptr;
40
0
}
41
42
RefPtr<ServiceWorkerRegistrationPromise>
43
ServiceWorkerContainerProxy::Register(const ClientInfo& aClientInfo,
44
                                      const nsCString& aScopeURL,
45
                                      const nsCString& aScriptURL,
46
                                      ServiceWorkerUpdateViaCache aUpdateViaCache)
47
0
{
48
0
  AssertIsOnBackgroundThread();
49
0
50
0
  RefPtr<ServiceWorkerRegistrationPromise::Private> promise =
51
0
    new ServiceWorkerRegistrationPromise::Private(__func__);
52
0
53
0
  nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(__func__,
54
0
    [aClientInfo, aScopeURL, aScriptURL, aUpdateViaCache, promise] () mutable {
55
0
      auto scopeExit = MakeScopeExit([&] {
56
0
        promise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__);
57
0
      });
58
0
59
0
      RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
60
0
      NS_ENSURE_TRUE_VOID(swm);
61
0
62
0
      swm->Register(aClientInfo, aScopeURL, aScriptURL, aUpdateViaCache)
63
0
         ->ChainTo(promise.forget(), __func__);
64
0
65
0
      scopeExit.release();
66
0
    });
67
0
68
0
  MOZ_ALWAYS_SUCCEEDS(SystemGroup::Dispatch(TaskCategory::Other, r.forget()));
69
0
70
0
  return promise;
71
0
}
72
73
RefPtr<ServiceWorkerRegistrationPromise>
74
ServiceWorkerContainerProxy::GetRegistration(const ClientInfo& aClientInfo,
75
                                             const nsCString& aURL)
76
0
{
77
0
  AssertIsOnBackgroundThread();
78
0
79
0
  RefPtr<ServiceWorkerRegistrationPromise::Private> promise =
80
0
    new ServiceWorkerRegistrationPromise::Private(__func__);
81
0
82
0
  nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(__func__,
83
0
    [aClientInfo, aURL, promise] () mutable {
84
0
      auto scopeExit = MakeScopeExit([&] {
85
0
        promise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__);
86
0
      });
87
0
88
0
      RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
89
0
      NS_ENSURE_TRUE_VOID(swm);
90
0
91
0
      swm->GetRegistration(aClientInfo, aURL)->ChainTo(promise.forget(),
92
0
                                                       __func__);
93
0
94
0
      scopeExit.release();
95
0
    });
96
0
97
0
  MOZ_ALWAYS_SUCCEEDS(SystemGroup::Dispatch(TaskCategory::Other, r.forget()));
98
0
99
0
  return promise;
100
0
}
101
102
RefPtr<ServiceWorkerRegistrationListPromise>
103
ServiceWorkerContainerProxy::GetRegistrations(const ClientInfo& aClientInfo)
104
0
{
105
0
  AssertIsOnBackgroundThread();
106
0
107
0
  RefPtr<ServiceWorkerRegistrationListPromise::Private> promise =
108
0
    new ServiceWorkerRegistrationListPromise::Private(__func__);
109
0
110
0
  nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(__func__,
111
0
    [aClientInfo, promise] () mutable {
112
0
      auto scopeExit = MakeScopeExit([&] {
113
0
        promise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__);
114
0
      });
115
0
116
0
      RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
117
0
      NS_ENSURE_TRUE_VOID(swm);
118
0
119
0
      swm->GetRegistrations(aClientInfo)->ChainTo(promise.forget(), __func__);
120
0
121
0
      scopeExit.release();
122
0
    });
123
0
124
0
  MOZ_ALWAYS_SUCCEEDS(SystemGroup::Dispatch(TaskCategory::Other, r.forget()));
125
0
126
0
  return promise;
127
0
}
128
129
RefPtr<ServiceWorkerRegistrationPromise>
130
ServiceWorkerContainerProxy::GetReady(const ClientInfo& aClientInfo)
131
0
{
132
0
  AssertIsOnBackgroundThread();
133
0
134
0
  RefPtr<ServiceWorkerRegistrationPromise::Private> promise =
135
0
    new ServiceWorkerRegistrationPromise::Private(__func__);
136
0
137
0
  nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(__func__,
138
0
    [aClientInfo, promise] () mutable {
139
0
      auto scopeExit = MakeScopeExit([&] {
140
0
        promise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__);
141
0
      });
142
0
143
0
      RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
144
0
      NS_ENSURE_TRUE_VOID(swm);
145
0
146
0
      swm->WhenReady(aClientInfo)->ChainTo(promise.forget(), __func__);
147
0
148
0
      scopeExit.release();
149
0
    });
150
0
151
0
  MOZ_ALWAYS_SUCCEEDS(SystemGroup::Dispatch(TaskCategory::Other, r.forget()));
152
0
153
0
  return promise;
154
0
}
155
156
} // namespace dom
157
} // namespace mozilla