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

Side by Side Diff: content/browser/service_worker/service_worker_cache_listener.cc

Issue 1039763002: Cache Storage: Move files to content/*/cache_storage, rename classes (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: GN fix Created 5 years, 8 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "content/browser/service_worker/service_worker_cache_listener.h"
6
7 #include "base/bind.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/trace_event/trace_event.h"
10 #include "content/browser/service_worker/cache_storage_context_impl.h"
11 #include "content/browser/service_worker/service_worker_cache.h"
12 #include "content/browser/service_worker/service_worker_cache_storage_manager.h"
13 #include "content/common/service_worker/cache_storage_messages.h"
14 #include "storage/browser/blob/blob_data_handle.h"
15 #include "third_party/WebKit/public/platform/WebServiceWorkerCacheError.h"
16
17 namespace content {
18
19 using blink::WebServiceWorkerCacheError;
20
21 namespace {
22
23 WebServiceWorkerCacheError ToWebServiceWorkerCacheError(
24 ServiceWorkerCacheStorage::CacheStorageError err) {
25 switch (err) {
26 case ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR:
27 NOTREACHED();
28 return blink::WebServiceWorkerCacheErrorNotImplemented;
29 case ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NOT_IMPLEMENTED:
30 return blink::WebServiceWorkerCacheErrorNotImplemented;
31 case ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NOT_FOUND:
32 return blink::WebServiceWorkerCacheErrorNotFound;
33 case ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_EXISTS:
34 return blink::WebServiceWorkerCacheErrorExists;
35 case ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_STORAGE:
36 // TODO(jkarlin): Change this to CACHE_STORAGE_ERROR_STORAGE once that's
37 // added.
38 return blink::WebServiceWorkerCacheErrorNotFound;
39 case ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_CLOSING:
40 // TODO(jkarlin): Update this to CACHE_STORAGE_ERROR_CLOSING once that's
41 // added.
42 return blink::WebServiceWorkerCacheErrorNotFound;
43 }
44 NOTREACHED();
45 return blink::WebServiceWorkerCacheErrorNotImplemented;
46 }
47
48 // TODO(jkarlin): ServiceWorkerCache and ServiceWorkerCacheStorage should share
49 // an error enum type.
50 WebServiceWorkerCacheError CacheErrorToWebServiceWorkerCacheError(
51 ServiceWorkerCache::ErrorType err) {
52 switch (err) {
53 case ServiceWorkerCache::ERROR_TYPE_OK:
54 NOTREACHED();
55 return blink::WebServiceWorkerCacheErrorNotImplemented;
56 case ServiceWorkerCache::ERROR_TYPE_EXISTS:
57 return blink::WebServiceWorkerCacheErrorExists;
58 case ServiceWorkerCache::ERROR_TYPE_STORAGE:
59 // TODO(jkarlin): Change this to CACHE_STORAGE_ERROR_STORAGE once that's
60 // added.
61 return blink::WebServiceWorkerCacheErrorNotFound;
62 case ServiceWorkerCache::ERROR_TYPE_NOT_FOUND:
63 return blink::WebServiceWorkerCacheErrorNotFound;
64 }
65 NOTREACHED();
66 return blink::WebServiceWorkerCacheErrorNotImplemented;
67 }
68
69 } // namespace
70
71 ServiceWorkerCacheListener::ServiceWorkerCacheListener(
72 CacheStorageDispatcherHost* dispatcher,
73 CacheStorageContextImpl* context)
74 : dispatcher_(dispatcher), context_(context), weak_factory_(this) {
75 }
76
77 ServiceWorkerCacheListener::~ServiceWorkerCacheListener() {
78 }
79
80 bool ServiceWorkerCacheListener::OnMessageReceived(
81 const IPC::Message& message) {
82 bool handled = true;
83 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerCacheListener, message)
84 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageHas,
85 OnCacheStorageHas)
86 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageOpen,
87 OnCacheStorageOpen)
88 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageDelete,
89 OnCacheStorageDelete)
90 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageKeys,
91 OnCacheStorageKeys)
92 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheStorageMatch,
93 OnCacheStorageMatch)
94 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheMatch,
95 OnCacheMatch)
96 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheMatchAll,
97 OnCacheMatchAll)
98 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheKeys,
99 OnCacheKeys)
100 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheBatch,
101 OnCacheBatch)
102 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CacheClosed,
103 OnCacheClosed)
104 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_BlobDataHandled, OnBlobDataHandled)
105 IPC_MESSAGE_UNHANDLED(handled = false)
106 IPC_END_MESSAGE_MAP()
107
108 return handled;
109 }
110
111 void ServiceWorkerCacheListener::OnCacheStorageHas(
112 int thread_id,
113 int request_id,
114 const GURL& origin,
115 const base::string16& cache_name) {
116 TRACE_EVENT0("ServiceWorker",
117 "ServiceWorkerCacheListener::OnCacheStorageHas");
118 context_->cache_manager()->HasCache(
119 origin, base::UTF16ToUTF8(cache_name),
120 base::Bind(&ServiceWorkerCacheListener::OnCacheStorageHasCallback,
121 weak_factory_.GetWeakPtr(), thread_id, request_id));
122 }
123
124 void ServiceWorkerCacheListener::OnCacheStorageOpen(
125 int thread_id,
126 int request_id,
127 const GURL& origin,
128 const base::string16& cache_name) {
129 TRACE_EVENT0("ServiceWorker",
130 "ServiceWorkerCacheListener::OnCacheStorageOpen");
131 context_->cache_manager()->OpenCache(
132 origin, base::UTF16ToUTF8(cache_name),
133 base::Bind(&ServiceWorkerCacheListener::OnCacheStorageOpenCallback,
134 weak_factory_.GetWeakPtr(), thread_id, request_id));
135 }
136
137 void ServiceWorkerCacheListener::OnCacheStorageDelete(
138 int thread_id,
139 int request_id,
140 const GURL& origin,
141 const base::string16& cache_name) {
142 TRACE_EVENT0("ServiceWorker",
143 "ServiceWorkerCacheListener::OnCacheStorageDelete");
144 context_->cache_manager()->DeleteCache(
145 origin, base::UTF16ToUTF8(cache_name),
146 base::Bind(&ServiceWorkerCacheListener::OnCacheStorageDeleteCallback,
147 weak_factory_.GetWeakPtr(), thread_id, request_id));
148 }
149
150 void ServiceWorkerCacheListener::OnCacheStorageKeys(int thread_id,
151 int request_id,
152 const GURL& origin) {
153 TRACE_EVENT0("ServiceWorker",
154 "ServiceWorkerCacheListener::OnCacheStorageKeys");
155 context_->cache_manager()->EnumerateCaches(
156 origin,
157 base::Bind(&ServiceWorkerCacheListener::OnCacheStorageKeysCallback,
158 weak_factory_.GetWeakPtr(), thread_id, request_id));
159 }
160
161 void ServiceWorkerCacheListener::OnCacheStorageMatch(
162 int thread_id,
163 int request_id,
164 const GURL& origin,
165 const ServiceWorkerFetchRequest& request,
166 const ServiceWorkerCacheQueryParams& match_params) {
167 TRACE_EVENT0("ServiceWorker",
168 "ServiceWorkerCacheListener::OnCacheStorageMatch");
169
170 scoped_ptr<ServiceWorkerFetchRequest> scoped_request(
171 new ServiceWorkerFetchRequest(request.url, request.method,
172 request.headers, request.referrer,
173 request.is_reload));
174
175 if (match_params.cache_name.empty()) {
176 context_->cache_manager()->MatchAllCaches(
177 origin, scoped_request.Pass(),
178 base::Bind(&ServiceWorkerCacheListener::OnCacheStorageMatchCallback,
179 weak_factory_.GetWeakPtr(), thread_id, request_id));
180 return;
181 }
182 context_->cache_manager()->MatchCache(
183 origin, base::UTF16ToUTF8(match_params.cache_name), scoped_request.Pass(),
184 base::Bind(&ServiceWorkerCacheListener::OnCacheStorageMatchCallback,
185 weak_factory_.GetWeakPtr(), thread_id, request_id));
186 }
187
188 void ServiceWorkerCacheListener::OnCacheMatch(
189 int thread_id,
190 int request_id,
191 int cache_id,
192 const ServiceWorkerFetchRequest& request,
193 const ServiceWorkerCacheQueryParams& match_params) {
194 IDToCacheMap::iterator it = id_to_cache_map_.find(cache_id);
195 if (it == id_to_cache_map_.end()) {
196 Send(new ServiceWorkerMsg_CacheMatchError(
197 thread_id, request_id, blink::WebServiceWorkerCacheErrorNotFound));
198 return;
199 }
200
201 scoped_refptr<ServiceWorkerCache> cache = it->second;
202 scoped_ptr<ServiceWorkerFetchRequest> scoped_request(
203 new ServiceWorkerFetchRequest(request.url,
204 request.method,
205 request.headers,
206 request.referrer,
207 request.is_reload));
208 cache->Match(
209 scoped_request.Pass(),
210 base::Bind(&ServiceWorkerCacheListener::OnCacheMatchCallback,
211 weak_factory_.GetWeakPtr(), thread_id, request_id, cache));
212 }
213
214 void ServiceWorkerCacheListener::OnCacheMatchAll(
215 int thread_id,
216 int request_id,
217 int cache_id,
218 const ServiceWorkerFetchRequest& request,
219 const ServiceWorkerCacheQueryParams& match_params) {
220 // TODO(gavinp,jkarlin): Implement this method.
221 Send(new ServiceWorkerMsg_CacheMatchAllError(
222 thread_id, request_id, blink::WebServiceWorkerCacheErrorNotImplemented));
223 }
224
225 void ServiceWorkerCacheListener::OnCacheKeys(
226 int thread_id,
227 int request_id,
228 int cache_id,
229 const ServiceWorkerFetchRequest& request,
230 const ServiceWorkerCacheQueryParams& match_params) {
231 IDToCacheMap::iterator it = id_to_cache_map_.find(cache_id);
232 if (it == id_to_cache_map_.end()) {
233 Send(new ServiceWorkerMsg_CacheKeysError(
234 thread_id, request_id, blink::WebServiceWorkerCacheErrorNotFound));
235 return;
236 }
237
238 scoped_refptr<ServiceWorkerCache> cache = it->second;
239
240 cache->Keys(base::Bind(&ServiceWorkerCacheListener::OnCacheKeysCallback,
241 weak_factory_.GetWeakPtr(), thread_id, request_id,
242 cache));
243 }
244
245 void ServiceWorkerCacheListener::OnCacheBatch(
246 int thread_id,
247 int request_id,
248 int cache_id,
249 const std::vector<ServiceWorkerBatchOperation>& operations) {
250 if (operations.size() != 1u) {
251 Send(new ServiceWorkerMsg_CacheBatchError(
252 thread_id, request_id,
253 blink::WebServiceWorkerCacheErrorNotImplemented));
254 return;
255 }
256
257 IDToCacheMap::iterator it = id_to_cache_map_.find(cache_id);
258 if (it == id_to_cache_map_.end()) {
259 Send(new ServiceWorkerMsg_CacheBatchError(
260 thread_id, request_id, blink::WebServiceWorkerCacheErrorNotFound));
261 return;
262 }
263
264 const ServiceWorkerBatchOperation& operation = operations[0];
265
266 scoped_refptr<ServiceWorkerCache> cache = it->second;
267 scoped_ptr<ServiceWorkerFetchRequest> scoped_request(
268 new ServiceWorkerFetchRequest(operation.request.url,
269 operation.request.method,
270 operation.request.headers,
271 operation.request.referrer,
272 operation.request.is_reload));
273
274 if (operation.operation_type == SERVICE_WORKER_CACHE_OPERATION_TYPE_DELETE) {
275 cache->Delete(
276 scoped_request.Pass(),
277 base::Bind(&ServiceWorkerCacheListener::OnCacheDeleteCallback,
278 weak_factory_.GetWeakPtr(), thread_id, request_id, cache));
279 return;
280 }
281
282 if (operation.operation_type == SERVICE_WORKER_CACHE_OPERATION_TYPE_PUT) {
283 // We don't support streaming for cache.
284 DCHECK(operation.response.stream_url.is_empty());
285 scoped_ptr<ServiceWorkerResponse> scoped_response(
286 new ServiceWorkerResponse(operation.response.url,
287 operation.response.status_code,
288 operation.response.status_text,
289 operation.response.response_type,
290 operation.response.headers,
291 operation.response.blob_uuid,
292 operation.response.blob_size,
293 operation.response.stream_url));
294 cache->Put(
295 scoped_request.Pass(), scoped_response.Pass(),
296 base::Bind(&ServiceWorkerCacheListener::OnCachePutCallback,
297 weak_factory_.GetWeakPtr(), thread_id, request_id, cache));
298
299 return;
300 }
301
302 Send(new ServiceWorkerMsg_CacheBatchError(
303 thread_id, request_id, blink::WebServiceWorkerCacheErrorNotImplemented));
304 }
305
306 void ServiceWorkerCacheListener::OnCacheClosed(int cache_id) {
307 DropCacheReference(cache_id);
308 }
309
310 void ServiceWorkerCacheListener::OnBlobDataHandled(const std::string& uuid) {
311 DropBlobDataHandle(uuid);
312 }
313
314 void ServiceWorkerCacheListener::Send(IPC::Message* message) {
315 dispatcher_->Send(message);
316 }
317
318 void ServiceWorkerCacheListener::OnCacheStorageHasCallback(
319 int thread_id,
320 int request_id,
321 bool has_cache,
322 ServiceWorkerCacheStorage::CacheStorageError error) {
323 if (error != ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR) {
324 Send(new ServiceWorkerMsg_CacheStorageHasError(
325 thread_id, request_id, ToWebServiceWorkerCacheError(error)));
326 return;
327 }
328 if (!has_cache) {
329 Send(new ServiceWorkerMsg_CacheStorageHasError(
330 thread_id, request_id, blink::WebServiceWorkerCacheErrorNotFound));
331 return;
332 }
333 Send(new ServiceWorkerMsg_CacheStorageHasSuccess(thread_id, request_id));
334 }
335
336 void ServiceWorkerCacheListener::OnCacheStorageOpenCallback(
337 int thread_id,
338 int request_id,
339 const scoped_refptr<ServiceWorkerCache>& cache,
340 ServiceWorkerCacheStorage::CacheStorageError error) {
341 if (error != ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR) {
342 Send(new ServiceWorkerMsg_CacheStorageOpenError(
343 thread_id, request_id, ToWebServiceWorkerCacheError(error)));
344 return;
345 }
346 CacheID cache_id = StoreCacheReference(cache);
347 Send(new ServiceWorkerMsg_CacheStorageOpenSuccess(thread_id, request_id,
348 cache_id));
349 }
350
351 void ServiceWorkerCacheListener::OnCacheStorageDeleteCallback(
352 int thread_id,
353 int request_id,
354 bool deleted,
355 ServiceWorkerCacheStorage::CacheStorageError error) {
356 if (!deleted ||
357 error != ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR) {
358 Send(new ServiceWorkerMsg_CacheStorageDeleteError(
359 thread_id, request_id, ToWebServiceWorkerCacheError(error)));
360 return;
361 }
362 Send(new ServiceWorkerMsg_CacheStorageDeleteSuccess(thread_id, request_id));
363 }
364
365 void ServiceWorkerCacheListener::OnCacheStorageKeysCallback(
366 int thread_id,
367 int request_id,
368 const std::vector<std::string>& strings,
369 ServiceWorkerCacheStorage::CacheStorageError error) {
370 if (error != ServiceWorkerCacheStorage::CACHE_STORAGE_ERROR_NO_ERROR) {
371 Send(new ServiceWorkerMsg_CacheStorageKeysError(
372 thread_id, request_id, ToWebServiceWorkerCacheError(error)));
373 return;
374 }
375
376 std::vector<base::string16> string16s;
377 for (size_t i = 0, max = strings.size(); i < max; ++i) {
378 string16s.push_back(base::UTF8ToUTF16(strings[i]));
379 }
380 Send(new ServiceWorkerMsg_CacheStorageKeysSuccess(thread_id, request_id,
381 string16s));
382 }
383
384 void ServiceWorkerCacheListener::OnCacheStorageMatchCallback(
385 int thread_id,
386 int request_id,
387 ServiceWorkerCache::ErrorType error,
388 scoped_ptr<ServiceWorkerResponse> response,
389 scoped_ptr<storage::BlobDataHandle> blob_data_handle) {
390 if (error != ServiceWorkerCache::ERROR_TYPE_OK) {
391 Send(new ServiceWorkerMsg_CacheStorageMatchError(
392 thread_id, request_id, CacheErrorToWebServiceWorkerCacheError(error)));
393 return;
394 }
395
396 if (blob_data_handle)
397 StoreBlobDataHandle(blob_data_handle.Pass());
398
399 Send(new ServiceWorkerMsg_CacheStorageMatchSuccess(thread_id, request_id,
400 *response));
401 }
402
403 void ServiceWorkerCacheListener::OnCacheMatchCallback(
404 int thread_id,
405 int request_id,
406 const scoped_refptr<ServiceWorkerCache>& cache,
407 ServiceWorkerCache::ErrorType error,
408 scoped_ptr<ServiceWorkerResponse> response,
409 scoped_ptr<storage::BlobDataHandle> blob_data_handle) {
410 if (error != ServiceWorkerCache::ERROR_TYPE_OK) {
411 Send(new ServiceWorkerMsg_CacheMatchError(
412 thread_id, request_id, CacheErrorToWebServiceWorkerCacheError(error)));
413 return;
414 }
415
416 if (blob_data_handle)
417 StoreBlobDataHandle(blob_data_handle.Pass());
418
419 Send(
420 new ServiceWorkerMsg_CacheMatchSuccess(thread_id, request_id, *response));
421 }
422
423 void ServiceWorkerCacheListener::OnCacheKeysCallback(
424 int thread_id,
425 int request_id,
426 const scoped_refptr<ServiceWorkerCache>& cache,
427 ServiceWorkerCache::ErrorType error,
428 scoped_ptr<ServiceWorkerCache::Requests> requests) {
429 if (error != ServiceWorkerCache::ERROR_TYPE_OK) {
430 Send(new ServiceWorkerMsg_CacheKeysError(
431 thread_id, request_id, CacheErrorToWebServiceWorkerCacheError(error)));
432 return;
433 }
434
435 ServiceWorkerCache::Requests out;
436
437 for (ServiceWorkerCache::Requests::const_iterator it = requests->begin();
438 it != requests->end();
439 ++it) {
440 ServiceWorkerFetchRequest request(
441 it->url, it->method, it->headers, it->referrer, it->is_reload);
442 out.push_back(request);
443 }
444
445 Send(new ServiceWorkerMsg_CacheKeysSuccess(thread_id, request_id, out));
446 }
447
448 void ServiceWorkerCacheListener::OnCacheDeleteCallback(
449 int thread_id,
450 int request_id,
451 const scoped_refptr<ServiceWorkerCache>& cache,
452 ServiceWorkerCache::ErrorType error) {
453 if (error != ServiceWorkerCache::ERROR_TYPE_OK) {
454 Send(new ServiceWorkerMsg_CacheBatchError(
455 thread_id, request_id, CacheErrorToWebServiceWorkerCacheError(error)));
456 return;
457 }
458
459 Send(new ServiceWorkerMsg_CacheBatchSuccess(
460 thread_id, request_id, std::vector<ServiceWorkerResponse>()));
461 }
462
463 void ServiceWorkerCacheListener::OnCachePutCallback(
464 int thread_id,
465 int request_id,
466 const scoped_refptr<ServiceWorkerCache>& cache,
467 ServiceWorkerCache::ErrorType error,
468 scoped_ptr<ServiceWorkerResponse> response,
469 scoped_ptr<storage::BlobDataHandle> blob_data_handle) {
470 if (error != ServiceWorkerCache::ERROR_TYPE_OK) {
471 Send(new ServiceWorkerMsg_CacheBatchError(
472 thread_id, request_id, CacheErrorToWebServiceWorkerCacheError(error)));
473 return;
474 }
475
476 if (blob_data_handle)
477 StoreBlobDataHandle(blob_data_handle.Pass());
478
479 std::vector<ServiceWorkerResponse> responses;
480 responses.push_back(*response);
481 Send(
482 new ServiceWorkerMsg_CacheBatchSuccess(thread_id, request_id, responses));
483 }
484
485 ServiceWorkerCacheListener::CacheID
486 ServiceWorkerCacheListener::StoreCacheReference(
487 const scoped_refptr<ServiceWorkerCache>& cache) {
488 int cache_id = next_cache_id_++;
489 id_to_cache_map_[cache_id] = cache;
490 return cache_id;
491 }
492
493 void ServiceWorkerCacheListener::DropCacheReference(CacheID cache_id) {
494 id_to_cache_map_.erase(cache_id);
495 }
496
497 void ServiceWorkerCacheListener::StoreBlobDataHandle(
498 scoped_ptr<storage::BlobDataHandle> blob_data_handle) {
499 DCHECK(blob_data_handle);
500 std::pair<UUIDToBlobDataHandleList::iterator, bool> rv =
501 blob_handle_store_.insert(std::make_pair(
502 blob_data_handle->uuid(), std::list<storage::BlobDataHandle>()));
503 rv.first->second.push_front(storage::BlobDataHandle(*blob_data_handle));
504 }
505
506 void ServiceWorkerCacheListener::DropBlobDataHandle(std::string uuid) {
507 UUIDToBlobDataHandleList::iterator it = blob_handle_store_.find(uuid);
508 if (it == blob_handle_store_.end())
509 return;
510 DCHECK(!it->second.empty());
511 it->second.pop_front();
512 if (it->second.empty())
513 blob_handle_store_.erase(it);
514 }
515
516 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698