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

Side by Side Diff: Source/modules/serviceworkers/Cache.cpp

Issue 433793002: Introducing the WebServiceWorkerCache. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: partial remediation Created 6 years, 4 months 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 | Annotate | Revision Log
OLDNEW
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 "modules/serviceworkers/Request.h"
12 #include "modules/serviceworkers/Response.h"
13 #include "public/platform/WebServiceWorkerCache.h"
10 14
11 namespace blink { 15 namespace blink {
12 16
13 namespace { 17 namespace {
14 18
19 const char* cacheErrorToString(WebServiceWorkerCacheError reason)
dominicc (has gone to gerrit) 2014/08/15 06:20:23 Why do all of this in the blank namespace?
gavinp 2014/08/19 21:09:36 It really should be moved out to a util module, to
20 {
21 // FIXME: Construct correct DOM error objects rather than returning strings.
dominicc (has gone to gerrit) 2014/08/15 06:20:23 Do this. The spec says error objects and not strin
gavinp 2014/08/19 21:09:36 This is a copy of a method currently used in Cache
22 switch (reason) {
23 case WebServiceWorkerCacheError::WebServiceWorkerCacheErrorNotImplemented:
24 return "not implemented";
25 case WebServiceWorkerCacheError::WebServiceWorkerCacheErrorNotFound:
26 return "not found";
27 case WebServiceWorkerCacheError::WebServiceWorkerCacheErrorExists:
28 return "entry already exists";
29 default:
30 ASSERT_NOT_REACHED();
31 return "unknown error";
32 }
33 }
34
35 WebServiceWorkerCache::QueryParams queryParamsFromDictionary(const Dictionary& d ictionary)
36 {
37 WebServiceWorkerCache::QueryParams queryParams;
38 DictionaryHelper::get(dictionary, "ignoreSearch", queryParams.ignoreSearch);
39 DictionaryHelper::get(dictionary, "ignoreMethod", queryParams.ignoreMethod);
40 DictionaryHelper::get(dictionary, "ignoreVary", queryParams.ignoreVary);
41 DictionaryHelper::get(dictionary, "prefixMatch", queryParams.prefixMatch);
42 // FIXME: Add cacheName.
43 return queryParams;
44 }
45
46 class CacheMatchCallbacks : public WebServiceWorkerCache::CacheMatchCallbacks {
47 WTF_MAKE_NONCOPYABLE(CacheMatchCallbacks);
48 public:
49 CacheMatchCallbacks(PassRefPtr<ScriptPromiseResolver> resolver) : m_resolver (resolver) { }
50
51 virtual void onSuccess(WebServiceWorkerResponse* webResponse) OVERRIDE
52 {
53 m_resolver->resolve(Response::create(*webResponse));
dominicc (has gone to gerrit) 2014/08/15 06:20:23 Why not reset the pointer after this? It will dete
gavinp 2014/08/19 21:09:36 Good idea.
54 }
55
56 virtual void onError(WebServiceWorkerCacheError* reason) OVERRIDE
57 {
58 m_resolver->reject(cacheErrorToString(*reason));
59 }
60
61 private:
62 const RefPtr<ScriptPromiseResolver> m_resolver;
dominicc (has gone to gerrit) 2014/08/15 06:20:23 Why const?
gavinp 2014/08/19 21:09:36 Up until we reset, it was never modified during th
63 };
64
65 class CacheWithResponsesCallbacks : public WebServiceWorkerCache::CacheWithRespo nsesCallbacks {
66 WTF_MAKE_NONCOPYABLE(CacheWithResponsesCallbacks);
67 public:
68 CacheWithResponsesCallbacks(PassRefPtr<ScriptPromiseResolver> resolver) : m_ resolver(resolver) { }
69
70 virtual void onSuccess(WebVector<WebServiceWorkerResponse>* webResponses) OV ERRIDE
71 {
72 Vector<RefPtrWillBeRawPtr<Response> > responses;
73 for (size_t i = 0; i < webResponses->size(); ++i)
74 responses.append(Response::create((*webResponses)[i]));
75 m_resolver->resolve(responses);
76 }
77
78 virtual void onError(WebServiceWorkerCacheError* reason) OVERRIDE
79 {
80 m_resolver->reject(cacheErrorToString(*reason));
81 }
82
83 private:
84 const RefPtr<ScriptPromiseResolver> m_resolver;
85 };
86
87 class CacheWithRequestsCallbacks : public WebServiceWorkerCache::CacheWithReques tsCallbacks {
88 WTF_MAKE_NONCOPYABLE(CacheWithRequestsCallbacks);
89 public:
90 CacheWithRequestsCallbacks(PassRefPtr<ScriptPromiseResolver> resolver) : m_r esolver(resolver) { }
91
92 virtual void onSuccess(WebVector<WebServiceWorkerRequest>* webRequests) OVER RIDE
93 {
94 Vector<RefPtrWillBeRawPtr<Request> > requests;
95 for (size_t i = 0; i < webRequests->size(); ++i)
96 requests.append(Request::create((*webRequests)[i]));
97 m_resolver->resolve(requests);
98 }
99
100 virtual void onError(WebServiceWorkerCacheError* reason) OVERRIDE
101 {
102 m_resolver->reject(cacheErrorToString(*reason));
103 }
104
105 private:
106 const RefPtr<ScriptPromiseResolver> m_resolver;
107 };
108
15 ScriptPromise rejectAsNotImplemented(ScriptState* scriptState) 109 ScriptPromise rejectAsNotImplemented(ScriptState* scriptState)
16 { 110 {
17 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState); 111 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState);
18 const ScriptPromise promise = resolver->promise(); 112 const ScriptPromise promise = resolver->promise();
19 resolver->reject("not implemented"); 113 resolver->reject("not implemented");
20 114
21 return promise; 115 return promise;
22 } 116 }
23 117
24 } 118 } // namespace
25 119
26 PassRefPtrWillBeRawPtr<Cache> Cache::fromWebServiceWorkerCache(WebServiceWorkerC ache* webCache) 120 PassRefPtrWillBeRawPtr<Cache> Cache::fromWebServiceWorkerCache(WebServiceWorkerC ache* webCache)
27 { 121 {
28 return adoptRefWillBeNoop(new Cache(webCache)); 122 // This management mechanism ensures that we get the same Cache* object for every WebServiceWorkerCache object, so the
dominicc (has gone to gerrit) 2014/08/15 06:20:23 Can this be tightened up? Something like: Ensure
gavinp 2014/08/19 21:09:37 I've just removed this comment, it was recapitulat
123 // == operator in Javascript will work correctly. The lifetime is safe despi te this WebServiceWorkerCache not adding a ref, since
124 // in our destructor we call WebServiceWorkerCache::notifyDone(), registerin g our destruction.
dominicc (has gone to gerrit) 2014/08/15 06:20:23 I'm worried about how the object equality works.
gavinp 2014/08/19 21:09:36 It turns out, in the content side version of this
125 if (Cache* cache = static_cast<Cache*>(webCache->proxyInterface()))
126 return cache;
127 return create(webCache);
29 } 128 }
30 129
31 // FIXME: Implement these methods. 130 Cache::~Cache()
dominicc (has gone to gerrit) 2014/08/15 06:20:23 Line 124 suggests this calls notifyDone; what am I
gavinp 2014/08/19 21:09:36 Vestigal comment, removed. It was just a recapitul
32 ScriptPromise Cache::match(ScriptState* scriptState, Request* request, const Dic tionary& queryParams)
33 { 131 {
34 return rejectAsNotImplemented(scriptState);
35 } 132 }
36 133
37 ScriptPromise Cache::match(ScriptState* scriptState, const String& requestString , const Dictionary& queryParams) 134 ScriptPromise Cache::match(ScriptState* scriptState, Request* originalRequest, c onst Dictionary& queryParamsDict)
38 { 135 {
39 return rejectAsNotImplemented(scriptState); 136 TrackExceptionState exceptionState;
137 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), originalRequest, exceptionState);
138 if (exceptionState.hadException()) {
139 // FIXME: We should throw the caught error.
dominicc (has gone to gerrit) 2014/08/15 06:20:23 Why should you throw it? I don't see anywhere it s
gavinp 2014/08/19 21:09:36 Good catch, that's true for match(). Almost all o
140 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate()));
dominicc (has gone to gerrit) 2014/08/15 06:20:23 Why type error?
gavinp 2014/08/19 21:09:36 Earlier revision of the spec perhaps? Now fixed.
141 }
142 return matchImpl(scriptState, request, queryParamsDict);
40 } 143 }
41 144
42 ScriptPromise Cache::matchAll(ScriptState* scriptState, Request* request, const Dictionary& queryParams) 145 ScriptPromise Cache::match(ScriptState* scriptState, const String& requestString , const Dictionary& queryParamsDict)
43 { 146 {
44 return rejectAsNotImplemented(scriptState); 147 TrackExceptionState exceptionState;
148 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), requestString, exceptionState);
149 if (exceptionState.hadException()) {
150 // FIXME: We should throw the caught error.
151 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate()));
152 }
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.
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 WillBeHeapVector<Scr iptValue>& rawRequests) 200 ScriptPromise Cache::addAll(ScriptState* scriptState, const WillBeHeapVector<Scr iptValue>& 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());
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;
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698