Roll src/third_party/WebKit a452221:9ff6d11 (svn 202117:202119)
[chromium-blink-merge.git] / content / renderer / cache_storage / cache_storage_dispatcher.cc
blobc2612ef2504a99ec5a08ff16ec34597cd48c1da0
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.
5 #include "content/renderer/cache_storage/cache_storage_dispatcher.h"
7 #include <map>
8 #include <string>
9 #include <utility>
11 #include "base/lazy_instance.h"
12 #include "base/logging.h"
13 #include "base/metrics/histogram_macros.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/threading/thread_local.h"
16 #include "content/child/thread_safe_sender.h"
17 #include "content/common/cache_storage/cache_storage_messages.h"
18 #include "content/public/common/referrer.h"
19 #include "content/public/renderer/render_thread.h"
20 #include "content/renderer/service_worker/service_worker_type_util.h"
21 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerCache.h"
22 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerRequest.h"
23 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerResponse.h"
25 using base::TimeTicks;
27 namespace content {
29 using blink::WebServiceWorkerCacheStorage;
30 using blink::WebServiceWorkerCacheError;
31 using blink::WebServiceWorkerRequest;
33 static base::LazyInstance<base::ThreadLocalPointer<CacheStorageDispatcher>>::
34 Leaky g_cache_storage_dispatcher_tls = LAZY_INSTANCE_INITIALIZER;
36 namespace {
38 CacheStorageDispatcher* const kHasBeenDeleted =
39 reinterpret_cast<CacheStorageDispatcher*>(0x1);
41 ServiceWorkerFetchRequest FetchRequestFromWebRequest(
42 const blink::WebServiceWorkerRequest& web_request) {
43 ServiceWorkerHeaderMap headers;
44 GetServiceWorkerHeaderMapFromWebRequest(web_request, &headers);
46 return ServiceWorkerFetchRequest(
47 web_request.url(),
48 base::UTF16ToASCII(base::StringPiece16(web_request.method())), headers,
49 Referrer(web_request.referrerUrl(), web_request.referrerPolicy()),
50 web_request.isReload());
53 void PopulateWebRequestFromFetchRequest(
54 const ServiceWorkerFetchRequest& request,
55 blink::WebServiceWorkerRequest* web_request) {
56 web_request->setURL(request.url);
57 web_request->setMethod(base::ASCIIToUTF16(request.method));
58 for (ServiceWorkerHeaderMap::const_iterator i = request.headers.begin(),
59 end = request.headers.end();
60 i != end; ++i) {
61 web_request->setHeader(base::ASCIIToUTF16(i->first),
62 base::ASCIIToUTF16(i->second));
64 web_request->setReferrer(base::ASCIIToUTF16(request.referrer.url.spec()),
65 request.referrer.policy);
66 web_request->setIsReload(request.is_reload);
69 blink::WebVector<blink::WebServiceWorkerRequest> WebRequestsFromRequests(
70 const std::vector<ServiceWorkerFetchRequest>& requests) {
71 blink::WebVector<blink::WebServiceWorkerRequest> web_requests(
72 requests.size());
73 for (size_t i = 0; i < requests.size(); ++i)
74 PopulateWebRequestFromFetchRequest(requests[i], &(web_requests[i]));
75 return web_requests;
78 ServiceWorkerResponse ResponseFromWebResponse(
79 const blink::WebServiceWorkerResponse& web_response) {
80 ServiceWorkerHeaderMap headers;
81 GetServiceWorkerHeaderMapFromWebResponse(web_response, &headers);
82 // We don't support streaming for cache.
83 DCHECK(web_response.streamURL().isEmpty());
84 return ServiceWorkerResponse(
85 web_response.url(), web_response.status(),
86 base::UTF16ToASCII(base::StringPiece16(web_response.statusText())),
87 web_response.responseType(), headers,
88 base::UTF16ToASCII(base::StringPiece16(web_response.blobUUID())),
89 web_response.blobSize(), web_response.streamURL(),
90 blink::WebServiceWorkerResponseErrorUnknown);
93 CacheStorageCacheQueryParams QueryParamsFromWebQueryParams(
94 const blink::WebServiceWorkerCache::QueryParams& web_query_params) {
95 CacheStorageCacheQueryParams query_params;
96 query_params.ignore_search = web_query_params.ignoreSearch;
97 query_params.ignore_method = web_query_params.ignoreMethod;
98 query_params.ignore_vary = web_query_params.ignoreVary;
99 query_params.cache_name = web_query_params.cacheName;
100 return query_params;
103 CacheStorageCacheOperationType CacheOperationTypeFromWebCacheOperationType(
104 blink::WebServiceWorkerCache::OperationType operation_type) {
105 switch (operation_type) {
106 case blink::WebServiceWorkerCache::OperationTypePut:
107 return CACHE_STORAGE_CACHE_OPERATION_TYPE_PUT;
108 case blink::WebServiceWorkerCache::OperationTypeDelete:
109 return CACHE_STORAGE_CACHE_OPERATION_TYPE_DELETE;
110 default:
111 return CACHE_STORAGE_CACHE_OPERATION_TYPE_UNDEFINED;
115 CacheStorageBatchOperation BatchOperationFromWebBatchOperation(
116 const blink::WebServiceWorkerCache::BatchOperation& web_operation) {
117 CacheStorageBatchOperation operation;
118 operation.operation_type =
119 CacheOperationTypeFromWebCacheOperationType(web_operation.operationType);
120 operation.request = FetchRequestFromWebRequest(web_operation.request);
121 operation.response = ResponseFromWebResponse(web_operation.response);
122 operation.match_params =
123 QueryParamsFromWebQueryParams(web_operation.matchParams);
124 return operation;
127 template <typename T>
128 void ClearCallbacksMapWithErrors(T* callbacks_map) {
129 typename T::iterator iter(callbacks_map);
130 while (!iter.IsAtEnd()) {
131 iter.GetCurrentValue()->onError(blink::WebServiceWorkerCacheErrorNotFound);
132 callbacks_map->Remove(iter.GetCurrentKey());
133 iter.Advance();
137 } // namespace
139 // The WebCache object is the Chromium side implementation of the Blink
140 // WebServiceWorkerCache API. Most of its methods delegate directly to the
141 // ServiceWorkerStorage object, which is able to assign unique IDs as well
142 // as have a lifetime longer than the requests.
143 class CacheStorageDispatcher::WebCache : public blink::WebServiceWorkerCache {
144 public:
145 WebCache(base::WeakPtr<CacheStorageDispatcher> dispatcher, int cache_id)
146 : dispatcher_(dispatcher), cache_id_(cache_id) {}
148 virtual ~WebCache() {
149 if (dispatcher_)
150 dispatcher_->OnWebCacheDestruction(cache_id_);
153 // From blink::WebServiceWorkerCache:
154 virtual void dispatchMatch(CacheMatchCallbacks* callbacks,
155 const blink::WebServiceWorkerRequest& request,
156 const QueryParams& query_params) {
157 if (!dispatcher_)
158 return;
159 dispatcher_->dispatchMatchForCache(cache_id_, callbacks, request,
160 query_params);
162 virtual void dispatchMatchAll(CacheWithResponsesCallbacks* callbacks,
163 const blink::WebServiceWorkerRequest& request,
164 const QueryParams& query_params) {
165 if (!dispatcher_)
166 return;
167 dispatcher_->dispatchMatchAllForCache(cache_id_, callbacks, request,
168 query_params);
170 virtual void dispatchKeys(CacheWithRequestsCallbacks* callbacks,
171 const blink::WebServiceWorkerRequest* request,
172 const QueryParams& query_params) {
173 if (!dispatcher_)
174 return;
175 dispatcher_->dispatchKeysForCache(cache_id_, callbacks, request,
176 query_params);
178 virtual void dispatchBatch(
179 CacheBatchCallbacks* callbacks,
180 const blink::WebVector<BatchOperation>& batch_operations) {
181 if (!dispatcher_)
182 return;
183 dispatcher_->dispatchBatchForCache(cache_id_, callbacks, batch_operations);
186 private:
187 const base::WeakPtr<CacheStorageDispatcher> dispatcher_;
188 const int cache_id_;
191 CacheStorageDispatcher::CacheStorageDispatcher(
192 ThreadSafeSender* thread_safe_sender)
193 : thread_safe_sender_(thread_safe_sender), weak_factory_(this) {
194 g_cache_storage_dispatcher_tls.Pointer()->Set(this);
197 CacheStorageDispatcher::~CacheStorageDispatcher() {
198 ClearCallbacksMapWithErrors(&has_callbacks_);
199 ClearCallbacksMapWithErrors(&open_callbacks_);
200 ClearCallbacksMapWithErrors(&delete_callbacks_);
201 ClearCallbacksMapWithErrors(&keys_callbacks_);
202 ClearCallbacksMapWithErrors(&match_callbacks_);
204 ClearCallbacksMapWithErrors(&cache_match_callbacks_);
205 ClearCallbacksMapWithErrors(&cache_match_all_callbacks_);
206 ClearCallbacksMapWithErrors(&cache_keys_callbacks_);
207 ClearCallbacksMapWithErrors(&cache_batch_callbacks_);
209 g_cache_storage_dispatcher_tls.Pointer()->Set(kHasBeenDeleted);
212 CacheStorageDispatcher* CacheStorageDispatcher::ThreadSpecificInstance(
213 ThreadSafeSender* thread_safe_sender) {
214 if (g_cache_storage_dispatcher_tls.Pointer()->Get() == kHasBeenDeleted) {
215 NOTREACHED() << "Re-instantiating TLS CacheStorageDispatcher.";
216 g_cache_storage_dispatcher_tls.Pointer()->Set(NULL);
218 if (g_cache_storage_dispatcher_tls.Pointer()->Get())
219 return g_cache_storage_dispatcher_tls.Pointer()->Get();
221 CacheStorageDispatcher* dispatcher =
222 new CacheStorageDispatcher(thread_safe_sender);
223 if (WorkerThread::GetCurrentId())
224 WorkerThread::AddObserver(dispatcher);
225 return dispatcher;
228 void CacheStorageDispatcher::WillStopCurrentWorkerThread() {
229 delete this;
232 bool CacheStorageDispatcher::Send(IPC::Message* msg) {
233 return thread_safe_sender_->Send(msg);
236 bool CacheStorageDispatcher::OnMessageReceived(const IPC::Message& message) {
237 bool handled = true;
238 IPC_BEGIN_MESSAGE_MAP(CacheStorageDispatcher, message)
239 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheStorageHasSuccess,
240 OnCacheStorageHasSuccess)
241 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheStorageOpenSuccess,
242 OnCacheStorageOpenSuccess)
243 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheStorageDeleteSuccess,
244 OnCacheStorageDeleteSuccess)
245 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheStorageKeysSuccess,
246 OnCacheStorageKeysSuccess)
247 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheStorageMatchSuccess,
248 OnCacheStorageMatchSuccess)
249 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheStorageHasError,
250 OnCacheStorageHasError)
251 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheStorageOpenError,
252 OnCacheStorageOpenError)
253 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheStorageDeleteError,
254 OnCacheStorageDeleteError)
255 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheStorageKeysError,
256 OnCacheStorageKeysError)
257 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheStorageMatchError,
258 OnCacheStorageMatchError)
259 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheMatchSuccess,
260 OnCacheMatchSuccess)
261 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheMatchAllSuccess,
262 OnCacheMatchAllSuccess)
263 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheKeysSuccess, OnCacheKeysSuccess)
264 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheBatchSuccess,
265 OnCacheBatchSuccess)
266 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheMatchError, OnCacheMatchError)
267 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheMatchAllError,
268 OnCacheMatchAllError)
269 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheKeysError, OnCacheKeysError)
270 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheBatchError, OnCacheBatchError)
271 IPC_MESSAGE_UNHANDLED(handled = false)
272 IPC_END_MESSAGE_MAP()
274 return handled;
277 void CacheStorageDispatcher::OnCacheStorageHasSuccess(int thread_id,
278 int request_id) {
279 DCHECK_EQ(thread_id, CurrentWorkerId());
280 UMA_HISTOGRAM_TIMES("ServiceWorkerCache.CacheStorage.Has",
281 TimeTicks::Now() - has_times_[request_id]);
282 WebServiceWorkerCacheStorage::CacheStorageCallbacks* callbacks =
283 has_callbacks_.Lookup(request_id);
284 callbacks->onSuccess();
285 has_callbacks_.Remove(request_id);
286 has_times_.erase(request_id);
289 void CacheStorageDispatcher::OnCacheStorageOpenSuccess(int thread_id,
290 int request_id,
291 int cache_id) {
292 DCHECK_EQ(thread_id, CurrentWorkerId());
293 scoped_ptr<WebCache> web_cache(
294 new WebCache(weak_factory_.GetWeakPtr(), cache_id));
295 web_caches_.AddWithID(web_cache.get(), cache_id);
296 UMA_HISTOGRAM_TIMES("ServiceWorkerCache.CacheStorage.Open",
297 TimeTicks::Now() - open_times_[request_id]);
298 WebServiceWorkerCacheStorage::CacheStorageWithCacheCallbacks* callbacks =
299 open_callbacks_.Lookup(request_id);
300 callbacks->onSuccess(blink::adoptWebPtr(web_cache.release()));
301 open_callbacks_.Remove(request_id);
302 open_times_.erase(request_id);
305 void CacheStorageDispatcher::OnCacheStorageDeleteSuccess(int thread_id,
306 int request_id) {
307 DCHECK_EQ(thread_id, CurrentWorkerId());
308 UMA_HISTOGRAM_TIMES("ServiceWorkerCache.CacheStorage.Delete",
309 TimeTicks::Now() - delete_times_[request_id]);
310 WebServiceWorkerCacheStorage::CacheStorageCallbacks* callbacks =
311 delete_callbacks_.Lookup(request_id);
312 callbacks->onSuccess();
313 delete_callbacks_.Remove(request_id);
314 delete_times_.erase(request_id);
317 void CacheStorageDispatcher::OnCacheStorageKeysSuccess(
318 int thread_id,
319 int request_id,
320 const std::vector<base::string16>& keys) {
321 DCHECK_EQ(thread_id, CurrentWorkerId());
322 blink::WebVector<blink::WebString> webKeys(keys.size());
323 for (size_t i = 0; i < keys.size(); ++i)
324 webKeys[i] = keys[i];
326 UMA_HISTOGRAM_TIMES("ServiceWorkerCache.CacheStorage.Keys",
327 TimeTicks::Now() - keys_times_[request_id]);
328 WebServiceWorkerCacheStorage::CacheStorageKeysCallbacks* callbacks =
329 keys_callbacks_.Lookup(request_id);
330 callbacks->onSuccess(webKeys);
331 keys_callbacks_.Remove(request_id);
332 keys_times_.erase(request_id);
335 void CacheStorageDispatcher::OnCacheStorageMatchSuccess(
336 int thread_id,
337 int request_id,
338 const ServiceWorkerResponse& response) {
339 DCHECK_EQ(thread_id, CurrentWorkerId());
340 blink::WebServiceWorkerResponse web_response;
341 PopulateWebResponseFromResponse(response, &web_response);
343 UMA_HISTOGRAM_TIMES("ServiceWorkerCache.CacheStorage.Match",
344 TimeTicks::Now() - match_times_[request_id]);
345 WebServiceWorkerCacheStorage::CacheStorageMatchCallbacks* callbacks =
346 match_callbacks_.Lookup(request_id);
347 callbacks->onSuccess(web_response);
348 match_callbacks_.Remove(request_id);
349 match_times_.erase(request_id);
352 void CacheStorageDispatcher::OnCacheStorageHasError(
353 int thread_id,
354 int request_id,
355 blink::WebServiceWorkerCacheError reason) {
356 DCHECK_EQ(thread_id, CurrentWorkerId());
357 WebServiceWorkerCacheStorage::CacheStorageCallbacks* callbacks =
358 has_callbacks_.Lookup(request_id);
359 callbacks->onError(reason);
360 has_callbacks_.Remove(request_id);
361 has_times_.erase(request_id);
364 void CacheStorageDispatcher::OnCacheStorageOpenError(
365 int thread_id,
366 int request_id,
367 blink::WebServiceWorkerCacheError reason) {
368 DCHECK_EQ(thread_id, CurrentWorkerId());
369 WebServiceWorkerCacheStorage::CacheStorageWithCacheCallbacks* callbacks =
370 open_callbacks_.Lookup(request_id);
371 callbacks->onError(reason);
372 open_callbacks_.Remove(request_id);
373 open_times_.erase(request_id);
376 void CacheStorageDispatcher::OnCacheStorageDeleteError(
377 int thread_id,
378 int request_id,
379 blink::WebServiceWorkerCacheError reason) {
380 DCHECK_EQ(thread_id, CurrentWorkerId());
381 WebServiceWorkerCacheStorage::CacheStorageCallbacks* callbacks =
382 delete_callbacks_.Lookup(request_id);
383 callbacks->onError(reason);
384 delete_callbacks_.Remove(request_id);
385 delete_times_.erase(request_id);
388 void CacheStorageDispatcher::OnCacheStorageKeysError(
389 int thread_id,
390 int request_id,
391 blink::WebServiceWorkerCacheError reason) {
392 DCHECK_EQ(thread_id, CurrentWorkerId());
393 WebServiceWorkerCacheStorage::CacheStorageKeysCallbacks* callbacks =
394 keys_callbacks_.Lookup(request_id);
395 callbacks->onError(reason);
396 keys_callbacks_.Remove(request_id);
397 keys_times_.erase(request_id);
400 void CacheStorageDispatcher::OnCacheStorageMatchError(
401 int thread_id,
402 int request_id,
403 blink::WebServiceWorkerCacheError reason) {
404 DCHECK_EQ(thread_id, CurrentWorkerId());
405 WebServiceWorkerCacheStorage::CacheStorageMatchCallbacks* callbacks =
406 match_callbacks_.Lookup(request_id);
407 callbacks->onError(reason);
408 match_callbacks_.Remove(request_id);
409 match_times_.erase(request_id);
412 void CacheStorageDispatcher::OnCacheMatchSuccess(
413 int thread_id,
414 int request_id,
415 const ServiceWorkerResponse& response) {
416 DCHECK_EQ(thread_id, CurrentWorkerId());
417 blink::WebServiceWorkerResponse web_response;
418 PopulateWebResponseFromResponse(response, &web_response);
420 UMA_HISTOGRAM_TIMES("ServiceWorkerCache.Cache.Match",
421 TimeTicks::Now() - cache_match_times_[request_id]);
422 blink::WebServiceWorkerCache::CacheMatchCallbacks* callbacks =
423 cache_match_callbacks_.Lookup(request_id);
424 callbacks->onSuccess(web_response);
425 cache_match_callbacks_.Remove(request_id);
426 cache_match_times_.erase(request_id);
429 void CacheStorageDispatcher::OnCacheMatchAllSuccess(
430 int thread_id,
431 int request_id,
432 const std::vector<ServiceWorkerResponse>& responses) {
433 DCHECK_EQ(thread_id, CurrentWorkerId());
435 UMA_HISTOGRAM_TIMES("ServiceWorkerCache.Cache.MatchAll",
436 TimeTicks::Now() - cache_match_all_times_[request_id]);
437 blink::WebServiceWorkerCache::CacheWithResponsesCallbacks* callbacks =
438 cache_match_all_callbacks_.Lookup(request_id);
439 callbacks->onSuccess(WebResponsesFromResponses(responses));
440 cache_match_all_callbacks_.Remove(request_id);
441 cache_match_all_times_.erase(request_id);
444 void CacheStorageDispatcher::OnCacheKeysSuccess(
445 int thread_id,
446 int request_id,
447 const std::vector<ServiceWorkerFetchRequest>& requests) {
448 DCHECK_EQ(thread_id, CurrentWorkerId());
450 UMA_HISTOGRAM_TIMES("ServiceWorkerCache.Cache.Keys",
451 TimeTicks::Now() - cache_keys_times_[request_id]);
452 blink::WebServiceWorkerCache::CacheWithRequestsCallbacks* callbacks =
453 cache_keys_callbacks_.Lookup(request_id);
454 callbacks->onSuccess(WebRequestsFromRequests(requests));
455 cache_keys_callbacks_.Remove(request_id);
456 cache_keys_times_.erase(request_id);
459 void CacheStorageDispatcher::OnCacheBatchSuccess(
460 int thread_id,
461 int request_id) {
462 DCHECK_EQ(thread_id, CurrentWorkerId());
464 UMA_HISTOGRAM_TIMES("ServiceWorkerCache.Cache.Batch",
465 TimeTicks::Now() - cache_batch_times_[request_id]);
466 blink::WebServiceWorkerCache::CacheBatchCallbacks* callbacks =
467 cache_batch_callbacks_.Lookup(request_id);
468 callbacks->onSuccess();
469 cache_batch_callbacks_.Remove(request_id);
470 cache_batch_times_.erase(request_id);
473 void CacheStorageDispatcher::OnCacheMatchError(
474 int thread_id,
475 int request_id,
476 blink::WebServiceWorkerCacheError reason) {
477 DCHECK_EQ(thread_id, CurrentWorkerId());
478 blink::WebServiceWorkerCache::CacheMatchCallbacks* callbacks =
479 cache_match_callbacks_.Lookup(request_id);
480 callbacks->onError(reason);
481 cache_match_callbacks_.Remove(request_id);
482 cache_match_times_.erase(request_id);
485 void CacheStorageDispatcher::OnCacheMatchAllError(
486 int thread_id,
487 int request_id,
488 blink::WebServiceWorkerCacheError reason) {
489 DCHECK_EQ(thread_id, CurrentWorkerId());
490 blink::WebServiceWorkerCache::CacheWithResponsesCallbacks* callbacks =
491 cache_match_all_callbacks_.Lookup(request_id);
492 callbacks->onError(reason);
493 cache_match_all_callbacks_.Remove(request_id);
494 cache_match_all_times_.erase(request_id);
497 void CacheStorageDispatcher::OnCacheKeysError(
498 int thread_id,
499 int request_id,
500 blink::WebServiceWorkerCacheError reason) {
501 DCHECK_EQ(thread_id, CurrentWorkerId());
502 blink::WebServiceWorkerCache::CacheWithRequestsCallbacks* callbacks =
503 cache_keys_callbacks_.Lookup(request_id);
504 callbacks->onError(reason);
505 cache_keys_callbacks_.Remove(request_id);
506 cache_keys_times_.erase(request_id);
509 void CacheStorageDispatcher::OnCacheBatchError(
510 int thread_id,
511 int request_id,
512 blink::WebServiceWorkerCacheError reason) {
513 DCHECK_EQ(thread_id, CurrentWorkerId());
514 blink::WebServiceWorkerCache::CacheBatchCallbacks* callbacks =
515 cache_batch_callbacks_.Lookup(request_id);
516 callbacks->onError(blink::WebServiceWorkerCacheError(reason));
517 cache_batch_callbacks_.Remove(request_id);
518 cache_batch_times_.erase(request_id);
521 void CacheStorageDispatcher::dispatchHas(
522 WebServiceWorkerCacheStorage::CacheStorageCallbacks* callbacks,
523 const GURL& origin,
524 const blink::WebString& cacheName) {
525 int request_id = has_callbacks_.Add(callbacks);
526 has_times_[request_id] = base::TimeTicks::Now();
527 Send(new CacheStorageHostMsg_CacheStorageHas(CurrentWorkerId(), request_id,
528 origin, cacheName));
531 void CacheStorageDispatcher::dispatchOpen(
532 WebServiceWorkerCacheStorage::CacheStorageWithCacheCallbacks* callbacks,
533 const GURL& origin,
534 const blink::WebString& cacheName) {
535 int request_id = open_callbacks_.Add(callbacks);
536 open_times_[request_id] = base::TimeTicks::Now();
537 Send(new CacheStorageHostMsg_CacheStorageOpen(CurrentWorkerId(), request_id,
538 origin, cacheName));
541 void CacheStorageDispatcher::dispatchDelete(
542 WebServiceWorkerCacheStorage::CacheStorageCallbacks* callbacks,
543 const GURL& origin,
544 const blink::WebString& cacheName) {
545 int request_id = delete_callbacks_.Add(callbacks);
546 delete_times_[request_id] = base::TimeTicks::Now();
547 Send(new CacheStorageHostMsg_CacheStorageDelete(CurrentWorkerId(), request_id,
548 origin, cacheName));
551 void CacheStorageDispatcher::dispatchKeys(
552 WebServiceWorkerCacheStorage::CacheStorageKeysCallbacks* callbacks,
553 const GURL& origin) {
554 int request_id = keys_callbacks_.Add(callbacks);
555 keys_times_[request_id] = base::TimeTicks::Now();
556 Send(new CacheStorageHostMsg_CacheStorageKeys(CurrentWorkerId(), request_id,
557 origin));
560 void CacheStorageDispatcher::dispatchMatch(
561 WebServiceWorkerCacheStorage::CacheStorageMatchCallbacks* callbacks,
562 const GURL& origin,
563 const blink::WebServiceWorkerRequest& request,
564 const blink::WebServiceWorkerCache::QueryParams& query_params) {
565 int request_id = match_callbacks_.Add(callbacks);
566 match_times_[request_id] = base::TimeTicks::Now();
567 Send(new CacheStorageHostMsg_CacheStorageMatch(
568 CurrentWorkerId(), request_id, origin,
569 FetchRequestFromWebRequest(request),
570 QueryParamsFromWebQueryParams(query_params)));
573 void CacheStorageDispatcher::dispatchMatchForCache(
574 int cache_id,
575 blink::WebServiceWorkerCache::CacheMatchCallbacks* callbacks,
576 const blink::WebServiceWorkerRequest& request,
577 const blink::WebServiceWorkerCache::QueryParams& query_params) {
578 int request_id = cache_match_callbacks_.Add(callbacks);
579 cache_match_times_[request_id] = base::TimeTicks::Now();
581 Send(new CacheStorageHostMsg_CacheMatch(
582 CurrentWorkerId(), request_id, cache_id,
583 FetchRequestFromWebRequest(request),
584 QueryParamsFromWebQueryParams(query_params)));
587 void CacheStorageDispatcher::dispatchMatchAllForCache(
588 int cache_id,
589 blink::WebServiceWorkerCache::CacheWithResponsesCallbacks* callbacks,
590 const blink::WebServiceWorkerRequest& request,
591 const blink::WebServiceWorkerCache::QueryParams& query_params) {
592 int request_id = cache_match_all_callbacks_.Add(callbacks);
593 cache_match_all_times_[request_id] = base::TimeTicks::Now();
595 Send(new CacheStorageHostMsg_CacheMatchAll(
596 CurrentWorkerId(), request_id, cache_id,
597 FetchRequestFromWebRequest(request),
598 QueryParamsFromWebQueryParams(query_params)));
601 void CacheStorageDispatcher::dispatchKeysForCache(
602 int cache_id,
603 blink::WebServiceWorkerCache::CacheWithRequestsCallbacks* callbacks,
604 const blink::WebServiceWorkerRequest* request,
605 const blink::WebServiceWorkerCache::QueryParams& query_params) {
606 int request_id = cache_keys_callbacks_.Add(callbacks);
607 cache_keys_times_[request_id] = base::TimeTicks::Now();
609 Send(new CacheStorageHostMsg_CacheKeys(
610 CurrentWorkerId(), request_id, cache_id,
611 request ? FetchRequestFromWebRequest(*request)
612 : ServiceWorkerFetchRequest(),
613 QueryParamsFromWebQueryParams(query_params)));
616 void CacheStorageDispatcher::dispatchBatchForCache(
617 int cache_id,
618 blink::WebServiceWorkerCache::CacheBatchCallbacks* callbacks,
619 const blink::WebVector<blink::WebServiceWorkerCache::BatchOperation>&
620 web_operations) {
621 int request_id = cache_batch_callbacks_.Add(callbacks);
622 cache_batch_times_[request_id] = base::TimeTicks::Now();
624 std::vector<CacheStorageBatchOperation> operations;
625 operations.reserve(web_operations.size());
626 for (size_t i = 0; i < web_operations.size(); ++i) {
627 operations.push_back(
628 BatchOperationFromWebBatchOperation(web_operations[i]));
631 Send(new CacheStorageHostMsg_CacheBatch(CurrentWorkerId(), request_id,
632 cache_id, operations));
635 void CacheStorageDispatcher::OnWebCacheDestruction(int cache_id) {
636 web_caches_.Remove(cache_id);
637 Send(new CacheStorageHostMsg_CacheClosed(cache_id));
640 void CacheStorageDispatcher::PopulateWebResponseFromResponse(
641 const ServiceWorkerResponse& response,
642 blink::WebServiceWorkerResponse* web_response) {
643 web_response->setURL(response.url);
644 web_response->setStatus(response.status_code);
645 web_response->setStatusText(base::ASCIIToUTF16(response.status_text));
646 web_response->setResponseType(response.response_type);
648 for (const auto& i : response.headers) {
649 web_response->setHeader(base::ASCIIToUTF16(i.first),
650 base::ASCIIToUTF16(i.second));
653 if (!response.blob_uuid.empty()) {
654 web_response->setBlob(blink::WebString::fromUTF8(response.blob_uuid),
655 response.blob_size);
656 // Let the host know that it can release its reference to the blob.
657 Send(new CacheStorageHostMsg_BlobDataHandled(response.blob_uuid));
661 blink::WebVector<blink::WebServiceWorkerResponse>
662 CacheStorageDispatcher::WebResponsesFromResponses(
663 const std::vector<ServiceWorkerResponse>& responses) {
664 blink::WebVector<blink::WebServiceWorkerResponse> web_responses(
665 responses.size());
666 for (size_t i = 0; i < responses.size(); ++i)
667 PopulateWebResponseFromResponse(responses[i], &(web_responses[i]));
668 return web_responses;
671 } // namespace content