Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "modules/background_fetch/BackgroundFetchManager.h" | 5 #include "modules/background_fetch/BackgroundFetchManager.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ScriptPromiseResolver.h" | 7 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 8 #include "bindings/core/v8/ScriptState.h" | 8 #include "bindings/core/v8/ScriptState.h" |
| 9 #include "bindings/core/v8/V8ThrowException.h" | 9 #include "bindings/core/v8/V8ThrowException.h" |
| 10 #include "bindings/modules/v8/RequestOrUSVString.h" | |
| 11 #include "bindings/modules/v8/RequestOrUSVStringOrRequestOrUSVStringSequence.h" | |
| 10 #include "core/dom/DOMException.h" | 12 #include "core/dom/DOMException.h" |
| 11 #include "core/dom/ExceptionCode.h" | 13 #include "core/dom/ExceptionCode.h" |
| 12 #include "modules/background_fetch/BackgroundFetchBridge.h" | 14 #include "modules/background_fetch/BackgroundFetchBridge.h" |
| 13 #include "modules/background_fetch/BackgroundFetchOptions.h" | 15 #include "modules/background_fetch/BackgroundFetchOptions.h" |
| 14 #include "modules/background_fetch/BackgroundFetchRegistration.h" | 16 #include "modules/background_fetch/BackgroundFetchRegistration.h" |
| 17 #include "modules/fetch/Request.h" | |
| 15 #include "modules/serviceworkers/ServiceWorkerRegistration.h" | 18 #include "modules/serviceworkers/ServiceWorkerRegistration.h" |
| 19 #include "public/platform/modules/serviceworker/WebServiceWorkerRequest.h" | |
| 16 | 20 |
| 17 namespace blink { | 21 namespace blink { |
| 18 | 22 |
| 23 namespace { | |
| 24 | |
| 25 // Message for the TypeError thrown when an empty request sequence is seen. | |
| 26 const char kEmptyRequestSequenceErrorMessage[] = | |
| 27 "At least one request must be given."; | |
| 28 | |
| 29 // Message for the TypeError thrown when a null request is seen. | |
| 30 const char kNullRequestErrorMessage[] = "Requests must not be null."; | |
| 31 | |
| 32 } // namespace | |
| 33 | |
| 19 BackgroundFetchManager::BackgroundFetchManager( | 34 BackgroundFetchManager::BackgroundFetchManager( |
| 20 ServiceWorkerRegistration* registration) | 35 ServiceWorkerRegistration* registration) |
| 21 : m_registration(registration) { | 36 : m_registration(registration) { |
| 22 DCHECK(registration); | 37 DCHECK(registration); |
| 23 m_bridge = BackgroundFetchBridge::from(m_registration); | 38 m_bridge = BackgroundFetchBridge::from(m_registration); |
| 24 } | 39 } |
| 25 | 40 |
| 26 ScriptPromise BackgroundFetchManager::fetch( | 41 ScriptPromise BackgroundFetchManager::fetch( |
| 27 ScriptState* scriptState, | 42 ScriptState* scriptState, |
| 28 const String& tag, | 43 const String& tag, |
| 29 const RequestOrUSVStringOrRequestOrUSVStringSequence& requests, | 44 const RequestOrUSVStringOrRequestOrUSVStringSequence& requests, |
| 30 const BackgroundFetchOptions& options) { | 45 const BackgroundFetchOptions& options, |
| 46 ExceptionState& exceptionState) { | |
| 31 if (!m_registration->active()) { | 47 if (!m_registration->active()) { |
| 32 return ScriptPromise::reject( | 48 return ScriptPromise::reject( |
| 33 scriptState, | 49 scriptState, |
| 34 V8ThrowException::createTypeError(scriptState->isolate(), | 50 V8ThrowException::createTypeError(scriptState->isolate(), |
| 35 "No active registration available on " | 51 "No active registration available on " |
| 36 "the ServiceWorkerRegistration.")); | 52 "the ServiceWorkerRegistration.")); |
| 37 } | 53 } |
| 38 | 54 |
| 55 Vector<WebServiceWorkerRequest> webRequests = | |
| 56 createWebRequestVector(scriptState, requests, exceptionState); | |
| 57 if (exceptionState.hadException()) | |
| 58 return ScriptPromise(); | |
| 59 | |
| 39 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 60 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 40 ScriptPromise promise = resolver->promise(); | 61 ScriptPromise promise = resolver->promise(); |
| 41 | 62 |
| 42 // TODO(peter): Include the |requests| in the Mojo call. | 63 m_bridge->fetch(tag, std::move(webRequests), options, |
| 43 | |
| 44 m_bridge->fetch(tag, options, | |
| 45 WTF::bind(&BackgroundFetchManager::didFetch, | 64 WTF::bind(&BackgroundFetchManager::didFetch, |
| 46 wrapPersistent(this), wrapPersistent(resolver))); | 65 wrapPersistent(this), wrapPersistent(resolver))); |
| 47 | 66 |
| 48 return promise; | 67 return promise; |
| 49 } | 68 } |
| 50 | 69 |
| 51 void BackgroundFetchManager::didFetch( | 70 void BackgroundFetchManager::didFetch( |
| 52 ScriptPromiseResolver* resolver, | 71 ScriptPromiseResolver* resolver, |
| 53 mojom::blink::BackgroundFetchError error, | 72 mojom::blink::BackgroundFetchError error, |
| 54 BackgroundFetchRegistration* registration) { | 73 BackgroundFetchRegistration* registration) { |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 84 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 103 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 85 ScriptPromise promise = resolver->promise(); | 104 ScriptPromise promise = resolver->promise(); |
| 86 | 105 |
| 87 m_bridge->getRegistration( | 106 m_bridge->getRegistration( |
| 88 tag, WTF::bind(&BackgroundFetchManager::didGetRegistration, | 107 tag, WTF::bind(&BackgroundFetchManager::didGetRegistration, |
| 89 wrapPersistent(this), wrapPersistent(resolver))); | 108 wrapPersistent(this), wrapPersistent(resolver))); |
| 90 | 109 |
| 91 return promise; | 110 return promise; |
| 92 } | 111 } |
| 93 | 112 |
| 113 // static | |
| 114 Vector<WebServiceWorkerRequest> BackgroundFetchManager::createWebRequestVector( | |
| 115 ScriptState* scriptState, | |
| 116 const RequestOrUSVStringOrRequestOrUSVStringSequence& requests, | |
| 117 ExceptionState& exceptionState) { | |
| 118 Vector<WebServiceWorkerRequest> webRequests; | |
| 119 | |
| 120 if (requests.isRequestOrUSVStringSequence()) { | |
| 121 HeapVector<RequestOrUSVString> requestVector = | |
| 122 requests.getAsRequestOrUSVStringSequence(); | |
| 123 | |
| 124 // Throw a TypeError when the developer has passed an empty sequence. | |
| 125 if (!requestVector.size()) { | |
| 126 exceptionState.throwTypeError(kEmptyRequestSequenceErrorMessage); | |
| 127 return Vector<WebServiceWorkerRequest>(); | |
| 128 } | |
| 129 | |
| 130 webRequests.resize(requestVector.size()); | |
| 131 | |
| 132 for (size_t i = 0; i < requestVector.size(); ++i) { | |
| 133 const RequestOrUSVString& requestOrUrl = requestVector[i]; | |
| 134 | |
| 135 Request* request = nullptr; | |
| 136 if (requestOrUrl.isRequest()) { | |
| 137 request = requestOrUrl.getAsRequest(); | |
| 138 } else if (requestOrUrl.isUSVString()) { | |
| 139 request = Request::create(scriptState, requestOrUrl.getAsUSVString(), | |
| 140 exceptionState); | |
| 141 if (exceptionState.hadException()) | |
| 142 return Vector<WebServiceWorkerRequest>(); | |
|
haraken
2017/03/24 01:29:38
Don't we need to throw a type error?
Peter Beverloo
2017/03/24 14:41:20
No, because an exception already was thrown (throu
| |
| 143 } else { | |
| 144 exceptionState.throwTypeError(kNullRequestErrorMessage); | |
| 145 return Vector<WebServiceWorkerRequest>(); | |
| 146 } | |
| 147 | |
| 148 DCHECK(request); | |
| 149 request->populateWebServiceWorkerRequest(webRequests[i]); | |
| 150 } | |
| 151 } else if (requests.isRequest()) { | |
| 152 DCHECK(requests.getAsRequest()); | |
| 153 webRequests.resize(1); | |
| 154 requests.getAsRequest()->populateWebServiceWorkerRequest(webRequests[0]); | |
|
horo
2017/03/24 02:01:51
populateWebServiceWorkerRequest() doesn't support
Peter Beverloo
2017/03/24 14:41:20
Right now we're focusing on `URL` to get end-to-en
| |
| 155 } else if (requests.isUSVString()) { | |
| 156 Request* request = | |
| 157 Request::create(scriptState, requests.getAsUSVString(), exceptionState); | |
| 158 if (exceptionState.hadException()) | |
| 159 return Vector<WebServiceWorkerRequest>(); | |
|
haraken
2017/03/24 01:29:38
Ditto.
Peter Beverloo
2017/03/24 14:41:20
Acknowledged.
| |
| 160 | |
| 161 DCHECK(request); | |
| 162 webRequests.resize(1); | |
| 163 request->populateWebServiceWorkerRequest(webRequests[0]); | |
| 164 } else { | |
| 165 exceptionState.throwTypeError(kNullRequestErrorMessage); | |
| 166 return Vector<WebServiceWorkerRequest>(); | |
| 167 } | |
| 168 | |
| 169 return webRequests; | |
| 170 } | |
| 171 | |
| 94 void BackgroundFetchManager::didGetRegistration( | 172 void BackgroundFetchManager::didGetRegistration( |
| 95 ScriptPromiseResolver* resolver, | 173 ScriptPromiseResolver* resolver, |
| 96 mojom::blink::BackgroundFetchError error, | 174 mojom::blink::BackgroundFetchError error, |
| 97 BackgroundFetchRegistration* registration) { | 175 BackgroundFetchRegistration* registration) { |
| 98 switch (error) { | 176 switch (error) { |
| 99 case mojom::blink::BackgroundFetchError::NONE: | 177 case mojom::blink::BackgroundFetchError::NONE: |
| 100 resolver->resolve(registration); | 178 resolver->resolve(registration); |
| 101 return; | 179 return; |
| 102 case mojom::blink::BackgroundFetchError::DUPLICATED_TAG: | 180 case mojom::blink::BackgroundFetchError::DUPLICATED_TAG: |
| 103 case mojom::blink::BackgroundFetchError::INVALID_TAG: | 181 case mojom::blink::BackgroundFetchError::INVALID_TAG: |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 142 | 220 |
| 143 NOTREACHED(); | 221 NOTREACHED(); |
| 144 } | 222 } |
| 145 | 223 |
| 146 DEFINE_TRACE(BackgroundFetchManager) { | 224 DEFINE_TRACE(BackgroundFetchManager) { |
| 147 visitor->trace(m_registration); | 225 visitor->trace(m_registration); |
| 148 visitor->trace(m_bridge); | 226 visitor->trace(m_bridge); |
| 149 } | 227 } |
| 150 | 228 |
| 151 } // namespace blink | 229 } // namespace blink |
| OLD | NEW |