OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "config.h" | 5 #include "config.h" |
6 #include "modules/serviceworkers/Cache.h" | 6 #include "modules/serviceworkers/Cache.h" |
7 | 7 |
8 #include "bindings/core/v8/Dictionary.h" | |
8 #include "bindings/core/v8/ScriptPromiseResolver.h" | 9 #include "bindings/core/v8/ScriptPromiseResolver.h" |
9 #include "bindings/core/v8/ScriptState.h" | 10 #include "bindings/core/v8/ScriptState.h" |
11 #include "bindings/core/v8/V8ThrowException.h" | |
12 #include "modules/serviceworkers/Request.h" | |
13 #include "modules/serviceworkers/Response.h" | |
14 #include "public/platform/WebServiceWorkerCache.h" | |
10 | 15 |
11 namespace blink { | 16 namespace blink { |
12 | 17 |
13 namespace { | 18 namespace { |
14 | 19 |
20 const char* cacheErrorToString(WebServiceWorkerCacheError reason) | |
21 { | |
22 // FIXME: Construct correct DOM error objects rather than returning strings. | |
dominicc (has gone to gerrit)
2014/08/21 06:37:56
One way to avoid the problem with cut-and-paste an
| |
23 switch (reason) { | |
24 case WebServiceWorkerCacheError::WebServiceWorkerCacheErrorNotImplemented: | |
25 return "not implemented"; | |
26 case WebServiceWorkerCacheError::WebServiceWorkerCacheErrorNotFound: | |
27 return "not found"; | |
28 case WebServiceWorkerCacheError::WebServiceWorkerCacheErrorExists: | |
29 return "entry already exists"; | |
30 default: | |
31 ASSERT_NOT_REACHED(); | |
32 return "unknown error"; | |
33 } | |
34 } | |
35 | |
36 WebServiceWorkerCache::QueryParams queryParamsFromDictionary(const Dictionary& d ictionary) | |
37 { | |
38 WebServiceWorkerCache::QueryParams queryParams; | |
39 DictionaryHelper::get(dictionary, "ignoreSearch", queryParams.ignoreSearch); | |
dominicc (has gone to gerrit)
2014/08/21 06:37:56
Where did we end up with the idea of making these
| |
40 DictionaryHelper::get(dictionary, "ignoreMethod", queryParams.ignoreMethod); | |
41 DictionaryHelper::get(dictionary, "ignoreVary", queryParams.ignoreVary); | |
42 DictionaryHelper::get(dictionary, "prefixMatch", queryParams.prefixMatch); | |
43 // FIXME: Add cacheName. | |
dominicc (has gone to gerrit)
2014/08/21 06:37:56
Given that these other parameters are not hooked u
| |
44 return queryParams; | |
45 } | |
46 | |
47 class CacheMatchCallbacks : public WebServiceWorkerCache::CacheMatchCallbacks { | |
48 WTF_MAKE_NONCOPYABLE(CacheMatchCallbacks); | |
49 public: | |
50 CacheMatchCallbacks(PassRefPtr<ScriptPromiseResolver> resolver) : m_resolver (resolver) { } | |
51 | |
52 virtual void onSuccess(WebServiceWorkerResponse* webResponse) OVERRIDE | |
53 { | |
54 m_resolver->resolve(Response::create(*webResponse)); | |
55 m_resolver.clear(); | |
56 } | |
57 | |
58 virtual void onError(WebServiceWorkerCacheError* reason) OVERRIDE | |
59 { | |
60 m_resolver->reject(cacheErrorToString(*reason)); | |
61 m_resolver.clear(); | |
62 } | |
63 | |
64 private: | |
65 RefPtr<ScriptPromiseResolver> m_resolver; | |
66 }; | |
67 | |
68 class CacheWithResponsesCallbacks : public WebServiceWorkerCache::CacheWithRespo nsesCallbacks { | |
69 WTF_MAKE_NONCOPYABLE(CacheWithResponsesCallbacks); | |
70 public: | |
71 CacheWithResponsesCallbacks(PassRefPtr<ScriptPromiseResolver> resolver) : m_ resolver(resolver) { } | |
72 | |
73 virtual void onSuccess(WebVector<WebServiceWorkerResponse>* webResponses) OV ERRIDE | |
74 { | |
75 Vector<RefPtrWillBeRawPtr<Response> > responses; | |
76 for (size_t i = 0; i < webResponses->size(); ++i) | |
77 responses.append(Response::create((*webResponses)[i])); | |
78 m_resolver->resolve(responses); | |
79 m_resolver.clear(); | |
80 } | |
81 | |
82 virtual void onError(WebServiceWorkerCacheError* reason) OVERRIDE | |
83 { | |
84 m_resolver->reject(cacheErrorToString(*reason)); | |
85 m_resolver.clear(); | |
86 } | |
87 | |
88 private: | |
89 RefPtr<ScriptPromiseResolver> m_resolver; | |
90 }; | |
91 | |
92 class CacheWithRequestsCallbacks : public WebServiceWorkerCache::CacheWithReques tsCallbacks { | |
93 WTF_MAKE_NONCOPYABLE(CacheWithRequestsCallbacks); | |
94 public: | |
95 CacheWithRequestsCallbacks(PassRefPtr<ScriptPromiseResolver> resolver) : m_r esolver(resolver) { } | |
96 | |
97 virtual void onSuccess(WebVector<WebServiceWorkerRequest>* webRequests) OVER RIDE | |
98 { | |
99 Vector<RefPtrWillBeRawPtr<Request> > requests; | |
100 for (size_t i = 0; i < webRequests->size(); ++i) | |
101 requests.append(Request::create((*webRequests)[i])); | |
102 m_resolver->resolve(requests); | |
103 m_resolver.clear(); | |
104 } | |
105 | |
106 virtual void onError(WebServiceWorkerCacheError* reason) OVERRIDE | |
107 { | |
108 m_resolver->reject(cacheErrorToString(*reason)); | |
109 m_resolver.clear(); | |
110 } | |
111 | |
112 private: | |
113 RefPtr<ScriptPromiseResolver> m_resolver; | |
114 }; | |
115 | |
15 ScriptPromise rejectAsNotImplemented(ScriptState* scriptState) | 116 ScriptPromise rejectAsNotImplemented(ScriptState* scriptState) |
16 { | 117 { |
17 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState); | 118 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState); |
18 const ScriptPromise promise = resolver->promise(); | 119 const ScriptPromise promise = resolver->promise(); |
19 resolver->reject("not implemented"); | 120 resolver->reject("not implemented"); |
20 | 121 |
21 return promise; | 122 return promise; |
22 } | 123 } |
23 | 124 |
24 } | 125 } // namespace |
25 | 126 |
26 PassRefPtrWillBeRawPtr<Cache> Cache::fromWebServiceWorkerCache(WebServiceWorkerC ache* webCache) | 127 PassRefPtrWillBeRawPtr<Cache> Cache::fromWebServiceWorkerCache(WebServiceWorkerC ache* webCache) |
27 { | 128 { |
28 return adoptRefWillBeNoop(new Cache(webCache)); | 129 if (Cache* cache = static_cast<Cache*>(webCache->proxyInterface())) |
130 return cache; | |
131 return create(webCache); | |
29 } | 132 } |
30 | 133 |
31 // FIXME: Implement these methods. | 134 Cache::~Cache() |
32 ScriptPromise Cache::match(ScriptState* scriptState, Request* request, const Dic tionary& queryParams) | |
33 { | 135 { |
34 return rejectAsNotImplemented(scriptState); | |
35 } | 136 } |
36 | 137 |
37 ScriptPromise Cache::match(ScriptState* scriptState, const String& requestString , const Dictionary& queryParams) | 138 ScriptPromise Cache::match(ScriptState* scriptState, Request* originalRequest, c onst Dictionary& queryParamsDict) |
38 { | 139 { |
39 return rejectAsNotImplemented(scriptState); | 140 TrackExceptionState exceptionState; |
dominicc (has gone to gerrit)
2014/08/21 06:37:56
I think you can extract two helpers, one for Strin
| |
141 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), originalRequest, exceptionState); | |
142 if (exceptionState.hadException()) | |
143 return ScriptPromise::reject(scriptState, DOMException::create(NotFoundE rror, "The specified Service Worker resource was not found.")->newLocalWrapper(s criptState->isolate())); | |
dominicc (has gone to gerrit)
2014/08/21 06:37:56
rejectWithDOMException
ditto below
| |
144 return matchImpl(scriptState, request, queryParamsDict); | |
40 } | 145 } |
41 | 146 |
42 ScriptPromise Cache::matchAll(ScriptState* scriptState, Request* request, const Dictionary& queryParams) | 147 ScriptPromise Cache::match(ScriptState* scriptState, const String& requestString , const Dictionary& queryParamsDict) |
43 { | 148 { |
44 return rejectAsNotImplemented(scriptState); | 149 TrackExceptionState exceptionState; |
150 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), requestString, exceptionState); | |
151 if (exceptionState.hadException()) | |
152 return ScriptPromise::reject(scriptState, DOMException::create(NotFoundE rror, "The specified Service Worker resource was not found.")->newLocalWrapper(s criptState->isolate())); | |
153 return matchImpl(scriptState, request, queryParamsDict); | |
45 } | 154 } |
46 | 155 |
47 ScriptPromise Cache::matchAll(ScriptState* scriptState, const String& requestStr ing, const Dictionary& queryParams) | 156 ScriptPromise Cache::matchAll(ScriptState* scriptState, Request* originalRequest , const Dictionary& queryParamsDict) |
48 { | 157 { |
49 return rejectAsNotImplemented(scriptState); | 158 TrackExceptionState exceptionState; |
159 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), originalRequest, exceptionState); | |
160 if (exceptionState.hadException()) { | |
161 // FIXME: We should throw the caught error. | |
dominicc (has gone to gerrit)
2014/08/21 06:37:56
Just do it?
Ditto below.
| |
162 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate())); | |
163 } | |
164 return matchAllImpl(scriptState, request, queryParamsDict); | |
50 } | 165 } |
51 | 166 |
52 ScriptPromise Cache::add(ScriptState* scriptState, Request* request) | 167 ScriptPromise Cache::matchAll(ScriptState* scriptState, const String& requestStr ing, const Dictionary& queryParamsDict) |
53 { | 168 { |
54 return rejectAsNotImplemented(scriptState); | 169 TrackExceptionState exceptionState; |
170 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), requestString, exceptionState); | |
171 if (exceptionState.hadException()) { | |
172 // FIXME: We should throw the caught error. | |
173 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate())); | |
174 } | |
175 return matchAllImpl(scriptState, request, queryParamsDict); | |
176 } | |
177 | |
178 ScriptPromise Cache::add(ScriptState* scriptState, Request* originalRequest) | |
179 { | |
180 TrackExceptionState exceptionState; | |
181 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), originalRequest, exceptionState); | |
182 if (exceptionState.hadException()) { | |
183 // FIXME: We should throw the caught error. | |
184 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate())); | |
185 } | |
186 return addImpl(scriptState, request); | |
55 } | 187 } |
56 | 188 |
57 ScriptPromise Cache::add(ScriptState* scriptState, const String& requestString) | 189 ScriptPromise Cache::add(ScriptState* scriptState, const String& requestString) |
58 { | 190 { |
59 return rejectAsNotImplemented(scriptState); | 191 TrackExceptionState exceptionState; |
192 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), requestString, exceptionState); | |
193 if (exceptionState.hadException()) { | |
194 // FIXME: We should throw the caught error. | |
195 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate())); | |
196 } | |
197 return addImpl(scriptState, request); | |
60 } | 198 } |
61 | 199 |
62 ScriptPromise Cache::addAll(ScriptState* scriptState, const Vector<ScriptValue>& rawRequests) | 200 ScriptPromise Cache::addAll(ScriptState* scriptState, const Vector<ScriptValue>& rawRequests) |
63 { | 201 { |
64 return rejectAsNotImplemented(scriptState); | 202 return rejectAsNotImplemented(scriptState); |
65 } | 203 } |
66 | 204 |
67 ScriptPromise Cache::deleteFunction(ScriptState* scriptState, Request* request, const Dictionary& queryParams) | 205 ScriptPromise Cache::deleteFunction(ScriptState* scriptState, Request* originalR equest, const Dictionary& queryParamsDict) |
206 { | |
207 TrackExceptionState exceptionState; | |
208 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), originalRequest, exceptionState); | |
209 if (exceptionState.hadException()) { | |
210 // FIXME: We should throw the caught error. | |
211 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate())); | |
212 } | |
213 return deleteImpl(scriptState, request, queryParamsDict); | |
214 } | |
215 | |
216 ScriptPromise Cache::deleteFunction(ScriptState* scriptState, const String& requ estString, const Dictionary& queryParamsDict) | |
217 { | |
218 TrackExceptionState exceptionState; | |
219 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), requestString, exceptionState); | |
220 if (exceptionState.hadException()) { | |
221 // FIXME: We should throw the caught error. | |
222 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate())); | |
223 } | |
224 return deleteImpl(scriptState, request, queryParamsDict); | |
225 } | |
226 | |
227 ScriptPromise Cache::put(ScriptState* scriptState, Request* originalRequest, Res ponse* response) | |
228 { | |
229 TrackExceptionState exceptionState; | |
230 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), originalRequest, exceptionState); | |
231 if (exceptionState.hadException()) { | |
232 // FIXME: We should throw the caught error. | |
233 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate())); | |
234 } | |
235 return putImpl(scriptState, request, response); | |
236 } | |
237 | |
238 ScriptPromise Cache::put(ScriptState* scriptState, const String& requestString, Response* response) | |
239 { | |
240 TrackExceptionState exceptionState; | |
241 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), requestString, exceptionState); | |
242 if (exceptionState.hadException()) { | |
243 // FIXME: We should throw the caught error. | |
244 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate())); | |
245 } | |
246 return putImpl(scriptState, request, response); | |
247 } | |
248 | |
249 ScriptPromise Cache::keys(ScriptState* scriptState) | |
250 { | |
251 return keysImpl(scriptState, RefPtrWillBeRawPtr<Request>(), Dictionary()); | |
dominicc (has gone to gerrit)
2014/08/21 06:37:56
If you have keysImpl(ScriptState) as line 262 indi
| |
252 } | |
253 | |
254 ScriptPromise Cache::keys(ScriptState* scriptState, Request* originalRequest, co nst Dictionary& queryParamsDict) | |
255 { | |
256 TrackExceptionState exceptionState; | |
257 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), originalRequest, exceptionState); | |
258 if (exceptionState.hadException()) { | |
259 // FIXME: We should throw the caught error. | |
260 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate())); | |
261 } | |
262 return keysImpl(scriptState); | |
263 } | |
264 | |
265 ScriptPromise Cache::keys(ScriptState* scriptState, const String& requestString, const Dictionary& queryParamsDict) | |
266 { | |
267 TrackExceptionState exceptionState; | |
268 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), requestString, exceptionState); | |
269 if (exceptionState.hadException()) { | |
270 // FIXME: We should throw the caught error. | |
271 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate())); | |
272 } | |
273 return keysImpl(scriptState, request, queryParamsDict); | |
274 } | |
275 | |
276 Cache::Cache(WebServiceWorkerCache* webCache) : m_webCache(adoptPtr(webCache)) | |
277 { | |
278 ScriptWrappable::init(this); | |
279 ASSERT(m_webCache->proxyInterface() == 0); | |
280 m_webCache->setProxyInterface(this); | |
281 } | |
282 | |
283 PassRefPtrWillBeRawPtr<Cache> Cache::create(WebServiceWorkerCache* webCache) | |
284 { | |
285 return adoptRefWillBeNoop(new Cache(webCache)); | |
286 } | |
287 | |
288 ScriptPromise Cache::matchImpl(ScriptState* scriptState, PassRefPtrWillBeRawPtr< Request> request, const Dictionary& queryParamsDict) | |
289 { | |
290 WebServiceWorkerRequest webRequest; | |
291 request->populateWebServiceWorkerRequest(webRequest); | |
292 | |
293 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState); | |
294 const ScriptPromise promise = resolver->promise(); | |
295 m_webCache->dispatchMatch(new CacheMatchCallbacks(resolver), webRequest, que ryParamsFromDictionary(queryParamsDict)); | |
296 return promise; | |
297 } | |
298 | |
299 ScriptPromise Cache::matchAllImpl(ScriptState* scriptState, PassRefPtrWillBeRawP tr<Request> request, const Dictionary& queryParamsDict) | |
300 { | |
301 WebServiceWorkerRequest webRequest; | |
302 request->populateWebServiceWorkerRequest(webRequest); | |
303 | |
304 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState); | |
305 const ScriptPromise promise = resolver->promise(); | |
306 m_webCache->dispatchMatchAll(new CacheWithResponsesCallbacks(resolver), webR equest, queryParamsFromDictionary(queryParamsDict)); | |
307 return promise; | |
308 } | |
309 | |
310 ScriptPromise Cache::addImpl(ScriptState* scriptState, PassRefPtrWillBeRawPtr<Re quest>) | |
68 { | 311 { |
69 return rejectAsNotImplemented(scriptState); | 312 return rejectAsNotImplemented(scriptState); |
70 } | 313 } |
71 | 314 |
72 ScriptPromise Cache::deleteFunction(ScriptState* scriptState, const String& requ estString, const Dictionary& queryParams) | 315 ScriptPromise Cache::addAllImpl(ScriptState* scriptState, Vector<RefPtrWillBeRaw Ptr<Request> >) |
73 { | 316 { |
74 return rejectAsNotImplemented(scriptState); | 317 return rejectAsNotImplemented(scriptState); |
75 } | 318 } |
76 | 319 |
77 ScriptPromise Cache::put(ScriptState* scriptState, Request* request, Response*) | 320 ScriptPromise Cache::deleteImpl(ScriptState* scriptState, PassRefPtrWillBeRawPtr <Request> request, const Dictionary& queryParamsDict) |
78 { | 321 { |
79 return rejectAsNotImplemented(scriptState); | 322 WebVector<WebServiceWorkerCache::BatchOperation> batchOperations(size_t(1)); |
323 batchOperations[0].operationType = WebServiceWorkerCache::WebServiceWorkerCa cheOperationTypeDelete; | |
324 request->populateWebServiceWorkerRequest(batchOperations[0].request); | |
325 batchOperations[0].matchParams = queryParamsFromDictionary(queryParamsDict); | |
326 | |
327 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState); | |
328 const ScriptPromise promise = resolver->promise(); | |
329 m_webCache->dispatchBatch(new CacheWithResponsesCallbacks(resolver), batchOp erations); | |
330 return promise; | |
80 } | 331 } |
81 | 332 |
82 ScriptPromise Cache::put(ScriptState* scriptState, const String& requestString, Response*) | 333 ScriptPromise Cache::putImpl(ScriptState* scriptState, PassRefPtrWillBeRawPtr<Re quest> request, Response* response) |
83 { | 334 { |
84 return rejectAsNotImplemented(scriptState); | 335 WebVector<WebServiceWorkerCache::BatchOperation> batchOperations(size_t(1)); |
336 batchOperations[0].operationType = WebServiceWorkerCache::WebServiceWorkerCa cheOperationTypePut; | |
337 request->populateWebServiceWorkerRequest(batchOperations[0].request); | |
338 response->populateWebServiceWorkerResponse(batchOperations[0].response); | |
339 | |
340 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState); | |
341 const ScriptPromise promise = resolver->promise(); | |
342 m_webCache->dispatchBatch(new CacheWithResponsesCallbacks(resolver), batchOp erations); | |
343 return promise; | |
85 } | 344 } |
86 | 345 |
87 ScriptPromise Cache::keys(ScriptState* scriptState) | 346 ScriptPromise Cache::keysImpl(ScriptState* scriptState) |
88 { | 347 { |
89 return rejectAsNotImplemented(scriptState); | 348 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState); |
349 const ScriptPromise promise = resolver->promise(); | |
350 m_webCache->dispatchKeys(new CacheWithRequestsCallbacks(resolver), 0, WebSer viceWorkerCache::QueryParams()); | |
351 return promise; | |
90 } | 352 } |
91 | 353 |
92 ScriptPromise Cache::keys(ScriptState* scriptState, Request* request, const Dict ionary& queryParams) | 354 ScriptPromise Cache::keysImpl(ScriptState* scriptState, PassRefPtrWillBeRawPtr<R equest> request, const Dictionary& queryParamsDict) |
93 { | 355 { |
94 return rejectAsNotImplemented(scriptState); | 356 WebServiceWorkerRequest webRequest; |
dominicc (has gone to gerrit)
2014/08/21 06:37:56
Why go to the trouble of creating the WebRequest a
| |
95 } | 357 request->populateWebServiceWorkerRequest(webRequest); |
96 | 358 |
97 ScriptPromise Cache::keys(ScriptState* scriptState, const String& requestString, const Dictionary& queryParams) | 359 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState); |
98 { | 360 const ScriptPromise promise = resolver->promise(); |
99 return rejectAsNotImplemented(scriptState); | 361 m_webCache->dispatchKeys(new CacheWithRequestsCallbacks(resolver), 0, WebSer viceWorkerCache::QueryParams()); |
100 } | 362 return promise; |
101 | |
102 Cache::Cache(WebServiceWorkerCache* webCache) : m_webCache(webCache) | |
103 { | |
104 ScriptWrappable::init(this); | |
105 } | 363 } |
106 | 364 |
107 } // namespace blink | 365 } // namespace blink |
OLD | NEW |