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

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: fix running with partial implementation 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)
20 {
21 // FIXME: Construct correct DOM error objects rather than returning strings.
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));
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;
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 {
122 // This management mechanism insures that we get the same Cache* object for every WebServiceWorkerCache object, so the
dominicc (has gone to gerrit) 2014/08/11 01:40:41 ensures
gavinp 2014/08/13 23:25:22 Done.
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.
125 if (Cache* cache = static_cast<Cache*>(webCache->proxyInterface()))
126 return cache;
28 return adoptRefWillBeNoop(new Cache(webCache)); 127 return adoptRefWillBeNoop(new Cache(webCache));
dominicc (has gone to gerrit) 2014/08/11 01:40:41 I'd add Cache::create; it's typical Blink, more re
gavinp 2014/08/13 23:25:22 Done.
29 } 128 }
30 129
31 // FIXME: Implement these methods. 130 Cache::~Cache()
32 ScriptPromise Cache::match(ScriptState* scriptState, Request* request, const Dic tionary& queryParams)
33 { 131 {
34 return rejectAsNotImplemented(scriptState); 132 m_webCache->notifyDone();
35 } 133 }
36 134
37 ScriptPromise Cache::match(ScriptState* scriptState, const String& requestString , const Dictionary& queryParams) 135 ScriptPromise Cache::match(ScriptState* scriptState, Request* originalRequest, c onst Dictionary& queryParamsDict)
38 { 136 {
39 return rejectAsNotImplemented(scriptState); 137 TrackExceptionState exceptionState;
138 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), originalRequest, exceptionState);
139 if (exceptionState.hadException()) {
140 // FIXME: We should throw the caught error.
dominicc (has gone to gerrit) 2014/08/11 01:40:41 Why? I think the spec just says to reject the Prom
dominicc (has gone to gerrit) 2014/08/14 02:35:12 Ping.
gavinp 2014/08/14 22:34:07 Yup, you're right. This is pretty tricky; matchAll
141 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate()));
142 }
143 return matchImpl(scriptState, request, queryParamsDict);
40 } 144 }
41 145
42 ScriptPromise Cache::matchAll(ScriptState* scriptState, Request* request, const Dictionary& queryParams) 146 ScriptPromise Cache::match(ScriptState* scriptState, const String& requestString , const Dictionary& queryParamsDict)
43 { 147 {
44 return rejectAsNotImplemented(scriptState); 148 TrackExceptionState exceptionState;
149 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), requestString, exceptionState);
150 if (exceptionState.hadException()) {
151 // FIXME: We should throw the caught error.
152 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate()));
153 }
154 return matchImpl(scriptState, request, queryParamsDict);
45 } 155 }
46 156
47 ScriptPromise Cache::matchAll(ScriptState* scriptState, const String& requestStr ing, const Dictionary& queryParams) 157 ScriptPromise Cache::matchAll(ScriptState* scriptState, Request* originalRequest , const Dictionary& queryParamsDict)
48 { 158 {
49 return rejectAsNotImplemented(scriptState); 159 TrackExceptionState exceptionState;
160 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), originalRequest, exceptionState);
161 if (exceptionState.hadException()) {
162 // FIXME: We should throw the caught error.
163 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate()));
164 }
165 return matchAllImpl(scriptState, request, queryParamsDict);
50 } 166 }
51 167
52 ScriptPromise Cache::add(ScriptState* scriptState, Request* request) 168 ScriptPromise Cache::matchAll(ScriptState* scriptState, const String& requestStr ing, const Dictionary& queryParamsDict)
53 { 169 {
54 return rejectAsNotImplemented(scriptState); 170 TrackExceptionState exceptionState;
171 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), requestString, exceptionState);
172 if (exceptionState.hadException()) {
173 // FIXME: We should throw the caught error.
174 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate()));
175 }
176 return matchAllImpl(scriptState, request, queryParamsDict);
177 }
178
179 ScriptPromise Cache::add(ScriptState* scriptState, Request* originalRequest)
180 {
181 TrackExceptionState exceptionState;
182 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), originalRequest, exceptionState);
183 if (exceptionState.hadException()) {
184 // FIXME: We should throw the caught error.
185 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate()));
186 }
187 return addImpl(scriptState, request);
55 } 188 }
56 189
57 ScriptPromise Cache::add(ScriptState* scriptState, const String& requestString) 190 ScriptPromise Cache::add(ScriptState* scriptState, const String& requestString)
58 { 191 {
59 return rejectAsNotImplemented(scriptState); 192 TrackExceptionState exceptionState;
193 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), requestString, exceptionState);
194 if (exceptionState.hadException()) {
195 // FIXME: We should throw the caught error.
196 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate()));
197 }
198 return addImpl(scriptState, request);
60 } 199 }
61 200
62 ScriptPromise Cache::addAll(ScriptState* scriptState, const WillBeHeapVector<Scr iptValue>& rawRequests) 201 ScriptPromise Cache::addAll(ScriptState* scriptState, const WillBeHeapVector<Scr iptValue>& rawRequests)
63 { 202 {
64 return rejectAsNotImplemented(scriptState); 203 return rejectAsNotImplemented(scriptState);
65 } 204 }
66 205
67 ScriptPromise Cache::deleteFunction(ScriptState* scriptState, Request* request, const Dictionary& queryParams) 206 ScriptPromise Cache::deleteFunction(ScriptState* scriptState, Request* originalR equest, const Dictionary& queryParamsDict)
68 { 207 {
69 return rejectAsNotImplemented(scriptState); 208 TrackExceptionState exceptionState;
209 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), originalRequest, exceptionState);
210 if (exceptionState.hadException()) {
211 // FIXME: We should throw the caught error.
212 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate()));
213 }
214 return deleteImpl(scriptState, request, queryParamsDict);
70 } 215 }
71 216
72 ScriptPromise Cache::deleteFunction(ScriptState* scriptState, const String& requ estString, const Dictionary& queryParams) 217 ScriptPromise Cache::deleteFunction(ScriptState* scriptState, const String& requ estString, const Dictionary& queryParamsDict)
73 { 218 {
74 return rejectAsNotImplemented(scriptState); 219 TrackExceptionState exceptionState;
220 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), requestString, exceptionState);
221 if (exceptionState.hadException()) {
222 // FIXME: We should throw the caught error.
223 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate()));
224 }
225 return deleteImpl(scriptState, request, queryParamsDict);
75 } 226 }
76 227
77 ScriptPromise Cache::put(ScriptState* scriptState, Request* request, Response*) 228 ScriptPromise Cache::put(ScriptState* scriptState, Request* originalRequest, Res ponse* response)
78 { 229 {
79 return rejectAsNotImplemented(scriptState); 230 TrackExceptionState exceptionState;
231 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), originalRequest, exceptionState);
232 if (exceptionState.hadException()) {
233 // FIXME: We should throw the caught error.
234 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate()));
235 }
236 return putImpl(scriptState, request, response);
80 } 237 }
81 238
82 ScriptPromise Cache::put(ScriptState* scriptState, const String& requestString, Response*) 239 ScriptPromise Cache::put(ScriptState* scriptState, const String& requestString, Response* response)
83 { 240 {
84 return rejectAsNotImplemented(scriptState); 241 TrackExceptionState exceptionState;
242 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), requestString, exceptionState);
243 if (exceptionState.hadException()) {
244 // FIXME: We should throw the caught error.
245 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate()));
246 }
247 return putImpl(scriptState, request, response);
85 } 248 }
86 249
87 ScriptPromise Cache::keys(ScriptState* scriptState) 250 ScriptPromise Cache::keys(ScriptState* scriptState)
88 { 251 {
89 return rejectAsNotImplemented(scriptState); 252 return keysImpl(scriptState, RefPtrWillBeRawPtr<Request>(), Dictionary());
90 } 253 }
91 254
92 ScriptPromise Cache::keys(ScriptState* scriptState, Request* request, const Dict ionary& queryParams) 255 ScriptPromise Cache::keys(ScriptState* scriptState, Request* originalRequest, co nst Dictionary& queryParamsDict)
93 { 256 {
94 return rejectAsNotImplemented(scriptState); 257 TrackExceptionState exceptionState;
258 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), originalRequest, exceptionState);
259 if (exceptionState.hadException()) {
260 // FIXME: We should throw the caught error.
261 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate()));
262 }
263 return keysImpl(scriptState);
95 } 264 }
96 265
97 ScriptPromise Cache::keys(ScriptState* scriptState, const String& requestString, const Dictionary& queryParams) 266 ScriptPromise Cache::keys(ScriptState* scriptState, const String& requestString, const Dictionary& queryParamsDict)
98 { 267 {
99 return rejectAsNotImplemented(scriptState); 268 TrackExceptionState exceptionState;
269 RefPtrWillBeRawPtr<Request> request = Request::create(scriptState->execution Context(), requestString, exceptionState);
270 if (exceptionState.hadException()) {
271 // FIXME: We should throw the caught error.
272 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr ror(exceptionState.message(), scriptState->isolate()));
273 }
274 return keysImpl(scriptState, request, queryParamsDict);
100 } 275 }
101 276
102 Cache::Cache(WebServiceWorkerCache* webCache) : m_webCache(webCache) 277 Cache::Cache(WebServiceWorkerCache* webCache) : m_webCache(webCache)
103 { 278 {
104 ScriptWrappable::init(this); 279 ScriptWrappable::init(this);
280 ASSERT(m_webCache->proxyInterface() == 0);
281 m_webCache->setProxyInterface(this);
dominicc (has gone to gerrit) 2014/08/11 01:40:41 Who owns m_webCache?
gavinp 2014/08/13 23:25:22 I've cleared this up a lot in the newest upload; I
dominicc (has gone to gerrit) 2014/08/14 02:35:12 I commented over there first. To me, the embedder
gavinp 2014/08/14 22:34:07 Yeah, that comment was bad, it's gone now. So reg
282 }
283
284 ScriptPromise Cache::matchImpl(ScriptState* scriptState, PassRefPtrWillBeRawPtr< Request> request, const Dictionary& queryParamsDict)
285 {
286 WebServiceWorkerRequest webRequest;
287 request->populateWebServiceWorkerRequest(webRequest);
288
289 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState);
290 const ScriptPromise promise = resolver->promise();
291 m_webCache->dispatchMatch(new CacheMatchCallbacks(resolver), webRequest, que ryParamsFromDictionary(queryParamsDict));
292 return promise;
293 }
294
295 ScriptPromise Cache::matchAllImpl(ScriptState* scriptState, PassRefPtrWillBeRawP tr<Request> request, const Dictionary& queryParamsDict)
296 {
297 WebServiceWorkerRequest webRequest;
298 request->populateWebServiceWorkerRequest(webRequest);
299
300 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState);
301 const ScriptPromise promise = resolver->promise();
302 m_webCache->dispatchMatch(new CacheMatchCallbacks(resolver), webRequest, que ryParamsFromDictionary(queryParamsDict));
dominicc (has gone to gerrit) 2014/08/11 01:40:41 Match all? Since the signatures are the same does
gavinp 2014/08/13 23:25:22 The return value is different, although kind of tr
dominicc (has gone to gerrit) 2014/08/14 02:35:12 +1, yes, separate is simpler; simpler is better.
gavinp 2014/08/14 22:34:07 That was a cut and paste mistake... I have now fix
303 return promise;
304 }
305
306 ScriptPromise Cache::addImpl(ScriptState* scriptState, PassRefPtrWillBeRawPtr<Re quest>)
307 {
308 return rejectAsNotImplemented(scriptState);
309 }
310
311 ScriptPromise Cache::addAllImpl(ScriptState* scriptState, Vector<RefPtrWillBeRaw Ptr<Request> >)
312 {
313 return rejectAsNotImplemented(scriptState);
314 }
315
316 ScriptPromise Cache::deleteImpl(ScriptState* scriptState, PassRefPtrWillBeRawPtr <Request> request, const Dictionary& queryParamsDict)
317 {
318 WebVector<WebServiceWorkerCache::BatchOperation> batchOperations(size_t(1));
319 batchOperations[0].operationType = WebServiceWorkerCache::WebServiceWorkerCa cheOperationTypeDelete;
320 request->populateWebServiceWorkerRequest(batchOperations[0].request);
321 batchOperations[0].matchParams = queryParamsFromDictionary(queryParamsDict);
322
323 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState);
324 const ScriptPromise promise = resolver->promise();
325 m_webCache->dispatchBatch(new CacheWithResponsesCallbacks(resolver), batchOp erations);
326 return promise;
327 }
328
329 ScriptPromise Cache::putImpl(ScriptState* scriptState, PassRefPtrWillBeRawPtr<Re quest> request, Response* response)
330 {
331 WebVector<WebServiceWorkerCache::BatchOperation> batchOperations(size_t(1));
332 batchOperations[0].operationType = WebServiceWorkerCache::WebServiceWorkerCa cheOperationTypePut;
333 request->populateWebServiceWorkerRequest(batchOperations[0].request);
334 response->populateWebServiceWorkerResponse(batchOperations[0].response);
335
336 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState);
337 const ScriptPromise promise = resolver->promise();
338 m_webCache->dispatchBatch(new CacheWithResponsesCallbacks(resolver), batchOp erations);
339 return promise;
340 }
341
342 ScriptPromise Cache::keysImpl(ScriptState* scriptState)
343 {
344 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState);
345 const ScriptPromise promise = resolver->promise();
346 m_webCache->dispatchKeys(new CacheWithRequestsCallbacks(resolver), 0, WebSer viceWorkerCache::QueryParams());
347 return promise;
348 }
349
350 ScriptPromise Cache::keysImpl(ScriptState* scriptState, PassRefPtrWillBeRawPtr<R equest> request, const Dictionary& queryParamsDict)
351 {
352 WebServiceWorkerRequest webRequest;
353 request->populateWebServiceWorkerRequest(webRequest);
354
355 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState);
356 const ScriptPromise promise = resolver->promise();
357 m_webCache->dispatchKeys(new CacheWithRequestsCallbacks(resolver), 0, WebSer viceWorkerCache::QueryParams());
358 return promise;
105 } 359 }
106 360
107 } // namespace blink 361 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698