Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(585)

Side by Side Diff: third_party/WebKit/Source/modules/serviceworkers/ServiceWorkerContainer.cpp

Issue 2564363002: ServiceWorker: Replace RegistrationCallback and GetRegistrationsCallback with CallbackPromiseAdapter (Closed)
Patch Set: base::MakeUnique Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 11 matching lines...) Expand all
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 #include "modules/serviceworkers/ServiceWorkerContainer.h" 30 #include "modules/serviceworkers/ServiceWorkerContainer.h"
31 31
32 #include "bindings/core/v8/CallbackPromiseAdapter.h"
32 #include "bindings/core/v8/ScriptPromise.h" 33 #include "bindings/core/v8/ScriptPromise.h"
33 #include "bindings/core/v8/ScriptPromiseResolver.h" 34 #include "bindings/core/v8/ScriptPromiseResolver.h"
34 #include "bindings/core/v8/ScriptState.h" 35 #include "bindings/core/v8/ScriptState.h"
35 #include "bindings/core/v8/SerializedScriptValue.h" 36 #include "bindings/core/v8/SerializedScriptValue.h"
36 #include "bindings/core/v8/SerializedScriptValueFactory.h" 37 #include "bindings/core/v8/SerializedScriptValueFactory.h"
37 #include "bindings/core/v8/V8ThrowException.h" 38 #include "bindings/core/v8/V8ThrowException.h"
38 #include "core/dom/DOMException.h" 39 #include "core/dom/DOMException.h"
39 #include "core/dom/Document.h" 40 #include "core/dom/Document.h"
40 #include "core/dom/ExceptionCode.h" 41 #include "core/dom/ExceptionCode.h"
41 #include "core/dom/ExecutionContext.h" 42 #include "core/dom/ExecutionContext.h"
(...skipping 13 matching lines...) Expand all
55 #include "public/platform/WebURL.h" 56 #include "public/platform/WebURL.h"
56 #include "public/platform/modules/serviceworker/WebServiceWorker.h" 57 #include "public/platform/modules/serviceworker/WebServiceWorker.h"
57 #include "public/platform/modules/serviceworker/WebServiceWorkerProvider.h" 58 #include "public/platform/modules/serviceworker/WebServiceWorkerProvider.h"
58 #include "public/platform/modules/serviceworker/WebServiceWorkerRegistration.h" 59 #include "public/platform/modules/serviceworker/WebServiceWorkerRegistration.h"
59 #include "wtf/PtrUtil.h" 60 #include "wtf/PtrUtil.h"
60 #include <memory> 61 #include <memory>
61 #include <utility> 62 #include <utility>
62 63
63 namespace blink { 64 namespace blink {
64 65
65 class RegistrationCallback
66 : public WebServiceWorkerProvider::WebServiceWorkerRegistrationCallbacks {
67 public:
68 explicit RegistrationCallback(ScriptPromiseResolver* resolver)
69 : m_resolver(resolver) {}
70 ~RegistrationCallback() override {}
71
72 void onSuccess(
73 std::unique_ptr<WebServiceWorkerRegistration::Handle> handle) override {
74 if (!m_resolver->getExecutionContext() ||
75 m_resolver->getExecutionContext()->isContextDestroyed())
76 return;
77 m_resolver->resolve(ServiceWorkerRegistration::getOrCreate(
78 m_resolver->getExecutionContext(), WTF::wrapUnique(handle.release())));
79 }
80
81 void onError(const WebServiceWorkerError& error) override {
82 if (!m_resolver->getExecutionContext() ||
83 m_resolver->getExecutionContext()->isContextDestroyed())
84 return;
85 ScriptState::Scope scope(m_resolver->getScriptState());
86 if (error.errorType == WebServiceWorkerError::ErrorTypeType) {
87 m_resolver->reject(V8ThrowException::createTypeError(
88 m_resolver->getScriptState()->isolate(), error.message));
89 } else {
90 m_resolver->reject(
91 ServiceWorkerErrorForUpdate::take(m_resolver.get(), error));
92 }
93 }
94
95 private:
96 Persistent<ScriptPromiseResolver> m_resolver;
97 WTF_MAKE_NONCOPYABLE(RegistrationCallback);
98 };
99
100 class GetRegistrationCallback : public WebServiceWorkerProvider:: 66 class GetRegistrationCallback : public WebServiceWorkerProvider::
101 WebServiceWorkerGetRegistrationCallbacks { 67 WebServiceWorkerGetRegistrationCallbacks {
102 public: 68 public:
103 explicit GetRegistrationCallback(ScriptPromiseResolver* resolver) 69 explicit GetRegistrationCallback(ScriptPromiseResolver* resolver)
104 : m_resolver(resolver) {} 70 : m_resolver(resolver) {}
105 ~GetRegistrationCallback() override {} 71 ~GetRegistrationCallback() override {}
106 72
107 void onSuccess(std::unique_ptr<WebServiceWorkerRegistration::Handle> 73 void onSuccess(std::unique_ptr<WebServiceWorkerRegistration::Handle>
108 webPassHandle) override { 74 webPassHandle) override {
109 std::unique_ptr<WebServiceWorkerRegistration::Handle> handle = 75 std::unique_ptr<WebServiceWorkerRegistration::Handle> handle =
(...skipping 15 matching lines...) Expand all
125 m_resolver->getExecutionContext()->isContextDestroyed()) 91 m_resolver->getExecutionContext()->isContextDestroyed())
126 return; 92 return;
127 m_resolver->reject(ServiceWorkerError::take(m_resolver.get(), error)); 93 m_resolver->reject(ServiceWorkerError::take(m_resolver.get(), error));
128 } 94 }
129 95
130 private: 96 private:
131 Persistent<ScriptPromiseResolver> m_resolver; 97 Persistent<ScriptPromiseResolver> m_resolver;
132 WTF_MAKE_NONCOPYABLE(GetRegistrationCallback); 98 WTF_MAKE_NONCOPYABLE(GetRegistrationCallback);
133 }; 99 };
134 100
135 class GetRegistrationsCallback : public WebServiceWorkerProvider::
136 WebServiceWorkerGetRegistrationsCallbacks {
137 public:
138 explicit GetRegistrationsCallback(ScriptPromiseResolver* resolver)
139 : m_resolver(resolver) {}
140 ~GetRegistrationsCallback() override {}
141
142 void onSuccess(
143 std::unique_ptr<WebVector<WebServiceWorkerRegistration::Handle*>>
144 webPassRegistrations) override {
145 Vector<std::unique_ptr<WebServiceWorkerRegistration::Handle>> handles;
146 std::unique_ptr<WebVector<WebServiceWorkerRegistration::Handle*>>
147 webRegistrations = WTF::wrapUnique(webPassRegistrations.release());
148 for (auto& handle : *webRegistrations) {
149 handles.append(WTF::wrapUnique(handle));
150 }
151
152 if (!m_resolver->getExecutionContext() ||
153 m_resolver->getExecutionContext()->isContextDestroyed())
154 return;
155 m_resolver->resolve(
156 ServiceWorkerRegistrationArray::take(m_resolver.get(), &handles));
157 }
158
159 void onError(const WebServiceWorkerError& error) override {
160 if (!m_resolver->getExecutionContext() ||
161 m_resolver->getExecutionContext()->isContextDestroyed())
162 return;
163 m_resolver->reject(ServiceWorkerError::take(m_resolver.get(), error));
164 }
165
166 private:
167 Persistent<ScriptPromiseResolver> m_resolver;
168 WTF_MAKE_NONCOPYABLE(GetRegistrationsCallback);
169 };
170
171 class ServiceWorkerContainer::GetRegistrationForReadyCallback 101 class ServiceWorkerContainer::GetRegistrationForReadyCallback
172 : public WebServiceWorkerProvider:: 102 : public WebServiceWorkerProvider::
173 WebServiceWorkerGetRegistrationForReadyCallbacks { 103 WebServiceWorkerGetRegistrationForReadyCallbacks {
174 public: 104 public:
175 explicit GetRegistrationForReadyCallback(ReadyProperty* ready) 105 explicit GetRegistrationForReadyCallback(ReadyProperty* ready)
176 : m_ready(ready) {} 106 : m_ready(ready) {}
177 ~GetRegistrationForReadyCallback() override {} 107 ~GetRegistrationForReadyCallback() override {}
178 108
179 void onSuccess( 109 void onSuccess(
180 std::unique_ptr<WebServiceWorkerRegistration::Handle> handle) override { 110 std::unique_ptr<WebServiceWorkerRegistration::Handle> handle) override {
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 enteredExecutionContext(scriptState->isolate())->completeURL(url); 281 enteredExecutionContext(scriptState->isolate())->completeURL(url);
352 scriptURL.removeFragmentIdentifier(); 282 scriptURL.removeFragmentIdentifier();
353 283
354 KURL patternURL; 284 KURL patternURL;
355 if (options.scope().isNull()) 285 if (options.scope().isNull())
356 patternURL = KURL(scriptURL, "./"); 286 patternURL = KURL(scriptURL, "./");
357 else 287 else
358 patternURL = enteredExecutionContext(scriptState->isolate()) 288 patternURL = enteredExecutionContext(scriptState->isolate())
359 ->completeURL(options.scope()); 289 ->completeURL(options.scope());
360 290
361 registerServiceWorkerImpl(executionContext, scriptURL, patternURL, 291 registerServiceWorkerImpl(
362 WTF::makeUnique<RegistrationCallback>(resolver)); 292 executionContext, scriptURL, patternURL,
293 WTF::makeUnique<CallbackPromiseAdapter<ServiceWorkerRegistration,
294 ServiceWorkerErrorForUpdate>>(
295 resolver));
363 296
364 return promise; 297 return promise;
365 } 298 }
366 299
367 ScriptPromise ServiceWorkerContainer::getRegistration( 300 ScriptPromise ServiceWorkerContainer::getRegistration(
368 ScriptState* scriptState, 301 ScriptState* scriptState,
369 const String& documentURL) { 302 const String& documentURL) {
370 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); 303 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
371 ScriptPromise promise = resolver->promise(); 304 ScriptPromise promise = resolver->promise();
372 305
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 pageURL.protocol())) { 381 pageURL.protocol())) {
449 resolver->reject(DOMException::create( 382 resolver->reject(DOMException::create(
450 SecurityError, 383 SecurityError,
451 "Failed to get ServiceWorkerRegistration objects: The URL protocol of " 384 "Failed to get ServiceWorkerRegistration objects: The URL protocol of "
452 "the current origin ('" + 385 "the current origin ('" +
453 documentOrigin->toString() + "') is not supported.")); 386 documentOrigin->toString() + "') is not supported."));
454 return promise; 387 return promise;
455 } 388 }
456 389
457 m_provider->getRegistrations( 390 m_provider->getRegistrations(
458 WTF::makeUnique<GetRegistrationsCallback>(resolver)); 391 WTF::makeUnique<CallbackPromiseAdapter<ServiceWorkerRegistrationArray,
392 ServiceWorkerError>>(resolver));
459 393
460 return promise; 394 return promise;
461 } 395 }
462 396
463 ServiceWorkerContainer::ReadyProperty* 397 ServiceWorkerContainer::ReadyProperty*
464 ServiceWorkerContainer::createReadyProperty() { 398 ServiceWorkerContainer::createReadyProperty() {
465 return new ReadyProperty(getExecutionContext(), this, ReadyProperty::Ready); 399 return new ReadyProperty(getExecutionContext(), this, ReadyProperty::Ready);
466 } 400 }
467 401
468 ScriptPromise ServiceWorkerContainer::ready(ScriptState* callerState) { 402 ScriptPromise ServiceWorkerContainer::ready(ScriptState* callerState) {
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
532 466
533 if (ServiceWorkerContainerClient* client = 467 if (ServiceWorkerContainerClient* client =
534 ServiceWorkerContainerClient::from(executionContext)) { 468 ServiceWorkerContainerClient::from(executionContext)) {
535 m_provider = client->provider(); 469 m_provider = client->provider();
536 if (m_provider) 470 if (m_provider)
537 m_provider->setClient(this); 471 m_provider->setClient(this);
538 } 472 }
539 } 473 }
540 474
541 } // namespace blink 475 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698