Implement "UpdateWorkerState" and use it
[WebKit-https.git] / Source / WebCore / workers / service / ServiceWorkerRegistration.cpp
1 /*
2  * Copyright (C) 2017 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "ServiceWorkerRegistration.h"
28
29 #if ENABLE(SERVICE_WORKER)
30 #include "DOMWindow.h"
31 #include "Document.h"
32 #include "Logging.h"
33 #include "ServiceWorker.h"
34 #include "ServiceWorkerContainer.h"
35 #include "ServiceWorkerTypes.h"
36 #include "WorkerGlobalScope.h"
37
38 namespace WebCore {
39
40 ServiceWorkerRegistration::ServiceWorkerRegistration(ScriptExecutionContext& context, Ref<ServiceWorkerContainer>&& container, ServiceWorkerRegistrationData&& registrationData)
41     : ActiveDOMObject(&context)
42     , m_registrationData(WTFMove(registrationData))
43     , m_container(WTFMove(container))
44 {
45     LOG(ServiceWorker, "Creating registration %p for registration key %s", this, m_registrationData.key.loggingString().utf8().data());
46     suspendIfNeeded();
47     m_container->addRegistration(*this);
48 }
49
50 ServiceWorkerRegistration::~ServiceWorkerRegistration()
51 {
52     LOG(ServiceWorker, "Deleting registration %p for registration key %s", this, m_registrationData.key.loggingString().utf8().data());
53
54     m_container->removeRegistration(*this);
55 }
56
57 ServiceWorker* ServiceWorkerRegistration::installing()
58 {
59     return m_installingWorker.get();
60 }
61
62 ServiceWorker* ServiceWorkerRegistration::waiting()
63 {
64     return m_waitingWorker.get();
65 }
66
67 ServiceWorker* ServiceWorkerRegistration::active()
68 {
69     return m_activeWorker.get();
70 }
71
72 void ServiceWorkerRegistration::setInstallingWorker(RefPtr<ServiceWorker>&& serviceWorker)
73 {
74     m_installingWorker = WTFMove(serviceWorker);
75 }
76
77 void ServiceWorkerRegistration::setWaitingWorker(RefPtr<ServiceWorker>&& serviceWorker)
78 {
79     m_waitingWorker = WTFMove(serviceWorker);
80 }
81
82 void ServiceWorkerRegistration::setActiveWorker(RefPtr<ServiceWorker>&& serviceWorker)
83 {
84     m_activeWorker = WTFMove(serviceWorker);
85 }
86
87 ServiceWorker* ServiceWorkerRegistration::getNewestWorker()
88 {
89     if (m_installingWorker)
90         return m_installingWorker.get();
91     if (m_waitingWorker)
92         return m_waitingWorker.get();
93
94     return m_activeWorker.get();
95 }
96
97 const String& ServiceWorkerRegistration::scope() const
98 {
99     return m_registrationData.scopeURL;
100 }
101
102 ServiceWorkerUpdateViaCache ServiceWorkerRegistration::updateViaCache() const
103 {
104     return m_registrationData.updateViaCache;
105 }
106
107 void ServiceWorkerRegistration::update(Ref<DeferredPromise>&& promise)
108 {
109     auto* context = scriptExecutionContext();
110     if (!context) {
111         ASSERT_NOT_REACHED();
112         promise->reject(Exception(InvalidStateError));
113         return;
114     }
115
116     auto* container = context->serviceWorkerContainer();
117     if (!container) {
118         promise->reject(Exception(InvalidStateError));
119         return;
120     }
121
122     auto* newestWorker = getNewestWorker();
123     if (!newestWorker) {
124         promise->reject(Exception(InvalidStateError, ASCIILiteral("newestWorker is null")));
125         return;
126     }
127
128     // FIXME: Support worker types.
129     container->updateRegistration(m_registrationData.scopeURL, newestWorker->scriptURL(), WorkerType::Classic, WTFMove(promise));
130 }
131
132 void ServiceWorkerRegistration::unregister(Ref<DeferredPromise>&& promise)
133 {
134     auto* context = scriptExecutionContext();
135     if (!context) {
136         ASSERT_NOT_REACHED();
137         promise->reject(Exception(InvalidStateError));
138         return;
139     }
140
141     auto* container = context->serviceWorkerContainer();
142     if (!container) {
143         promise->reject(Exception(InvalidStateError));
144         return;
145     }
146
147     container->removeRegistration(m_registrationData.scopeURL, WTFMove(promise));
148 }
149
150 void ServiceWorkerRegistration::updateStateFromServer(ServiceWorkerRegistrationState state, std::optional<ServiceWorkerIdentifier> serviceWorkerIdentifier)
151 {
152     auto* context = scriptExecutionContext();
153     if (!context)
154         return;
155
156     RefPtr<ServiceWorker> worker;
157     if (serviceWorkerIdentifier)
158         worker = ServiceWorker::create(*context, *serviceWorkerIdentifier, m_registrationData.scriptURL);
159
160     switch (state) {
161     case ServiceWorkerRegistrationState::Installing:
162         m_installingWorker = WTFMove(worker);
163         break;
164     case ServiceWorkerRegistrationState::Waiting:
165         m_waitingWorker = WTFMove(worker);
166         break;
167     case ServiceWorkerRegistrationState::Active:
168         m_activeWorker = WTFMove(worker);
169         break;
170     }
171 }
172
173 EventTargetInterface ServiceWorkerRegistration::eventTargetInterface() const
174 {
175     return ServiceWorkerRegistrationEventTargetInterfaceType;
176 }
177
178 ScriptExecutionContext* ServiceWorkerRegistration::scriptExecutionContext() const
179 {
180     return ActiveDOMObject::scriptExecutionContext();
181 }
182
183 const char* ServiceWorkerRegistration::activeDOMObjectName() const
184 {
185     return "ServiceWorkerRegistration";
186 }
187
188 bool ServiceWorkerRegistration::canSuspendForDocumentSuspension() const
189 {
190     return !hasPendingActivity();
191 }
192
193 } // namespace WebCore
194
195 #endif // ENABLE(SERVICE_WORKER)