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>(); | |
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() || 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.
| |
152 webRequests.resize(1); | |
153 | |
154 Request* request = nullptr; | |
155 if (requests.isRequest()) { | |
156 request = requests.getAsRequest(); | |
157 } else { | |
158 DCHECK(requests.isUSVString()); | |
159 request = Request::create(scriptState, requests.getAsUSVString(), | |
160 exceptionState); | |
161 if (exceptionState.hadException()) | |
162 return Vector<WebServiceWorkerRequest>(); | |
163 } | |
164 | |
165 DCHECK(request); | |
166 request->populateWebServiceWorkerRequest(webRequests[0]); | |
167 } else { | |
168 exceptionState.throwTypeError(kNullRequestErrorMessage); | |
169 return Vector<WebServiceWorkerRequest>(); | |
170 } | |
171 | |
172 return webRequests; | |
173 } | |
174 | |
94 void BackgroundFetchManager::didGetRegistration( | 175 void BackgroundFetchManager::didGetRegistration( |
95 ScriptPromiseResolver* resolver, | 176 ScriptPromiseResolver* resolver, |
96 mojom::blink::BackgroundFetchError error, | 177 mojom::blink::BackgroundFetchError error, |
97 BackgroundFetchRegistration* registration) { | 178 BackgroundFetchRegistration* registration) { |
98 switch (error) { | 179 switch (error) { |
99 case mojom::blink::BackgroundFetchError::NONE: | 180 case mojom::blink::BackgroundFetchError::NONE: |
100 resolver->resolve(registration); | 181 resolver->resolve(registration); |
101 return; | 182 return; |
102 case mojom::blink::BackgroundFetchError::DUPLICATED_TAG: | 183 case mojom::blink::BackgroundFetchError::DUPLICATED_TAG: |
103 case mojom::blink::BackgroundFetchError::INVALID_TAG: | 184 case mojom::blink::BackgroundFetchError::INVALID_TAG: |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
142 | 223 |
143 NOTREACHED(); | 224 NOTREACHED(); |
144 } | 225 } |
145 | 226 |
146 DEFINE_TRACE(BackgroundFetchManager) { | 227 DEFINE_TRACE(BackgroundFetchManager) { |
147 visitor->trace(m_registration); | 228 visitor->trace(m_registration); |
148 visitor->trace(m_bridge); | 229 visitor->trace(m_bridge); |
149 } | 230 } |
150 | 231 |
151 } // namespace blink | 232 } // namespace blink |
OLD | NEW |