Index: third_party/WebKit/Source/modules/background_fetch/BackgroundFetchManager.cpp |
diff --git a/third_party/WebKit/Source/modules/background_fetch/BackgroundFetchManager.cpp b/third_party/WebKit/Source/modules/background_fetch/BackgroundFetchManager.cpp |
index d1018f8b240c4e73c7b5495d3bdc352066f93e24..d3862959b2752595b15e22a1ddaad90b30d0e473 100644 |
--- a/third_party/WebKit/Source/modules/background_fetch/BackgroundFetchManager.cpp |
+++ b/third_party/WebKit/Source/modules/background_fetch/BackgroundFetchManager.cpp |
@@ -7,15 +7,30 @@ |
#include "bindings/core/v8/ScriptPromiseResolver.h" |
#include "bindings/core/v8/ScriptState.h" |
#include "bindings/core/v8/V8ThrowException.h" |
+#include "bindings/modules/v8/RequestOrUSVString.h" |
+#include "bindings/modules/v8/RequestOrUSVStringOrRequestOrUSVStringSequence.h" |
#include "core/dom/DOMException.h" |
#include "core/dom/ExceptionCode.h" |
#include "modules/background_fetch/BackgroundFetchBridge.h" |
#include "modules/background_fetch/BackgroundFetchOptions.h" |
#include "modules/background_fetch/BackgroundFetchRegistration.h" |
+#include "modules/fetch/Request.h" |
#include "modules/serviceworkers/ServiceWorkerRegistration.h" |
+#include "public/platform/modules/serviceworker/WebServiceWorkerRequest.h" |
namespace blink { |
+namespace { |
+ |
+// Message for the TypeError thrown when an empty request sequence is seen. |
+const char kEmptyRequestSequenceErrorMessage[] = |
+ "At least one request must be given."; |
+ |
+// Message for the TypeError thrown when a null request is seen. |
+const char kNullRequestErrorMessage[] = "Requests must not be null."; |
+ |
+} // namespace |
+ |
BackgroundFetchManager::BackgroundFetchManager( |
ServiceWorkerRegistration* registration) |
: m_registration(registration) { |
@@ -27,7 +42,8 @@ ScriptPromise BackgroundFetchManager::fetch( |
ScriptState* scriptState, |
const String& tag, |
const RequestOrUSVStringOrRequestOrUSVStringSequence& requests, |
- const BackgroundFetchOptions& options) { |
+ const BackgroundFetchOptions& options, |
+ ExceptionState& exceptionState) { |
if (!m_registration->active()) { |
return ScriptPromise::reject( |
scriptState, |
@@ -36,12 +52,15 @@ ScriptPromise BackgroundFetchManager::fetch( |
"the ServiceWorkerRegistration.")); |
} |
+ Vector<WebServiceWorkerRequest> webRequests = |
+ createWebRequestVector(scriptState, requests, exceptionState); |
+ if (exceptionState.hadException()) |
+ return ScriptPromise(); |
+ |
ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
ScriptPromise promise = resolver->promise(); |
- // TODO(peter): Include the |requests| in the Mojo call. |
- |
- m_bridge->fetch(tag, options, |
+ m_bridge->fetch(tag, std::move(webRequests), options, |
WTF::bind(&BackgroundFetchManager::didFetch, |
wrapPersistent(this), wrapPersistent(resolver))); |
@@ -91,6 +110,68 @@ ScriptPromise BackgroundFetchManager::get(ScriptState* scriptState, |
return promise; |
} |
+// static |
+Vector<WebServiceWorkerRequest> BackgroundFetchManager::createWebRequestVector( |
+ ScriptState* scriptState, |
+ const RequestOrUSVStringOrRequestOrUSVStringSequence& requests, |
+ ExceptionState& exceptionState) { |
+ Vector<WebServiceWorkerRequest> webRequests; |
+ |
+ if (requests.isRequestOrUSVStringSequence()) { |
+ HeapVector<RequestOrUSVString> requestVector = |
+ requests.getAsRequestOrUSVStringSequence(); |
+ |
+ // Throw a TypeError when the developer has passed an empty sequence. |
+ if (!requestVector.size()) { |
+ exceptionState.throwTypeError(kEmptyRequestSequenceErrorMessage); |
+ return Vector<WebServiceWorkerRequest>(); |
+ } |
+ |
+ webRequests.resize(requestVector.size()); |
+ |
+ for (size_t i = 0; i < requestVector.size(); ++i) { |
+ const RequestOrUSVString& requestOrUrl = requestVector[i]; |
+ |
+ Request* request = nullptr; |
+ if (requestOrUrl.isRequest()) { |
+ request = requestOrUrl.getAsRequest(); |
+ } else if (requestOrUrl.isUSVString()) { |
+ request = Request::create(scriptState, requestOrUrl.getAsUSVString(), |
+ exceptionState); |
+ if (exceptionState.hadException()) |
+ return Vector<WebServiceWorkerRequest>(); |
+ } else { |
+ exceptionState.throwTypeError(kNullRequestErrorMessage); |
+ return Vector<WebServiceWorkerRequest>(); |
+ } |
+ |
+ DCHECK(request); |
+ request->populateWebServiceWorkerRequest(webRequests[i]); |
+ } |
+ } else if (requests.isRequest() || requests.isUSVString()) { |
harkness
2017/03/22 17:47:30
optional: personally, this would read better for m
Peter Beverloo
2017/03/23 19:57:43
Done.
|
+ webRequests.resize(1); |
+ |
+ Request* request = nullptr; |
+ if (requests.isRequest()) { |
+ request = requests.getAsRequest(); |
+ } else { |
+ DCHECK(requests.isUSVString()); |
+ request = Request::create(scriptState, requests.getAsUSVString(), |
+ exceptionState); |
+ if (exceptionState.hadException()) |
+ return Vector<WebServiceWorkerRequest>(); |
+ } |
+ |
+ DCHECK(request); |
+ request->populateWebServiceWorkerRequest(webRequests[0]); |
+ } else { |
+ exceptionState.throwTypeError(kNullRequestErrorMessage); |
+ return Vector<WebServiceWorkerRequest>(); |
+ } |
+ |
+ return webRequests; |
+} |
+ |
void BackgroundFetchManager::didGetRegistration( |
ScriptPromiseResolver* resolver, |
mojom::blink::BackgroundFetchError error, |