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"
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/WebServiceWorkerCache.h"
22 #include "third_party/WebKit/public/platform/WebServiceWorkerRequest.h"
23 #include "third_party/WebKit/public/platform/WebServiceWorkerResponse.h"
25 using base::TimeTicks
;
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
;
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(
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();
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(
73 for (size_t i
= 0; i
< requests
.size(); ++i
)
74 PopulateWebRequestFromFetchRequest(requests
[i
], &(web_requests
[i
]));
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
;
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
;
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
);
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(
132 new blink::WebServiceWorkerCacheError(
133 blink::WebServiceWorkerCacheErrorNotFound
));
134 callbacks_map
->Remove(iter
.GetCurrentKey());
141 // The WebCache object is the Chromium side implementation of the Blink
142 // WebServiceWorkerCache API. Most of its methods delegate directly to the
143 // ServiceWorkerStorage object, which is able to assign unique IDs as well
144 // as have a lifetime longer than the requests.
145 class CacheStorageDispatcher::WebCache
: public blink::WebServiceWorkerCache
{
147 WebCache(base::WeakPtr
<CacheStorageDispatcher
> dispatcher
, int cache_id
)
148 : dispatcher_(dispatcher
), cache_id_(cache_id
) {}
150 virtual ~WebCache() {
152 dispatcher_
->OnWebCacheDestruction(cache_id_
);
155 // From blink::WebServiceWorkerCache:
156 virtual void dispatchMatch(CacheMatchCallbacks
* callbacks
,
157 const blink::WebServiceWorkerRequest
& request
,
158 const QueryParams
& query_params
) {
161 dispatcher_
->dispatchMatchForCache(cache_id_
, callbacks
, request
,
164 virtual void dispatchMatchAll(CacheWithResponsesCallbacks
* callbacks
,
165 const blink::WebServiceWorkerRequest
& request
,
166 const QueryParams
& query_params
) {
169 dispatcher_
->dispatchMatchAllForCache(cache_id_
, callbacks
, request
,
172 virtual void dispatchKeys(CacheWithRequestsCallbacks
* callbacks
,
173 const blink::WebServiceWorkerRequest
* request
,
174 const QueryParams
& query_params
) {
177 dispatcher_
->dispatchKeysForCache(cache_id_
, callbacks
, request
,
180 virtual void dispatchBatch(
181 CacheBatchCallbacks
* callbacks
,
182 const blink::WebVector
<BatchOperation
>& batch_operations
) {
185 dispatcher_
->dispatchBatchForCache(cache_id_
, callbacks
, batch_operations
);
189 const base::WeakPtr
<CacheStorageDispatcher
> dispatcher_
;
193 CacheStorageDispatcher::CacheStorageDispatcher(
194 ThreadSafeSender
* thread_safe_sender
)
195 : thread_safe_sender_(thread_safe_sender
), weak_factory_(this) {
196 g_cache_storage_dispatcher_tls
.Pointer()->Set(this);
199 CacheStorageDispatcher::~CacheStorageDispatcher() {
200 ClearCallbacksMapWithErrors(&has_callbacks_
);
201 ClearCallbacksMapWithErrors(&open_callbacks_
);
202 ClearCallbacksMapWithErrors(&delete_callbacks_
);
203 ClearCallbacksMapWithErrors(&keys_callbacks_
);
204 ClearCallbacksMapWithErrors(&match_callbacks_
);
206 ClearCallbacksMapWithErrors(&cache_match_callbacks_
);
207 ClearCallbacksMapWithErrors(&cache_match_all_callbacks_
);
208 ClearCallbacksMapWithErrors(&cache_keys_callbacks_
);
209 ClearCallbacksMapWithErrors(&cache_batch_callbacks_
);
211 g_cache_storage_dispatcher_tls
.Pointer()->Set(kHasBeenDeleted
);
214 CacheStorageDispatcher
* CacheStorageDispatcher::ThreadSpecificInstance(
215 ThreadSafeSender
* thread_safe_sender
) {
216 if (g_cache_storage_dispatcher_tls
.Pointer()->Get() == kHasBeenDeleted
) {
217 NOTREACHED() << "Re-instantiating TLS CacheStorageDispatcher.";
218 g_cache_storage_dispatcher_tls
.Pointer()->Set(NULL
);
220 if (g_cache_storage_dispatcher_tls
.Pointer()->Get())
221 return g_cache_storage_dispatcher_tls
.Pointer()->Get();
223 CacheStorageDispatcher
* dispatcher
=
224 new CacheStorageDispatcher(thread_safe_sender
);
225 if (WorkerTaskRunner::Instance()->CurrentWorkerId())
226 WorkerTaskRunner::Instance()->AddStopObserver(dispatcher
);
230 void CacheStorageDispatcher::OnWorkerRunLoopStopped() {
234 bool CacheStorageDispatcher::Send(IPC::Message
* msg
) {
235 return thread_safe_sender_
->Send(msg
);
238 bool CacheStorageDispatcher::OnMessageReceived(const IPC::Message
& message
) {
240 IPC_BEGIN_MESSAGE_MAP(CacheStorageDispatcher
, message
)
241 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheStorageHasSuccess
,
242 OnCacheStorageHasSuccess
)
243 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheStorageOpenSuccess
,
244 OnCacheStorageOpenSuccess
)
245 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheStorageDeleteSuccess
,
246 OnCacheStorageDeleteSuccess
)
247 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheStorageKeysSuccess
,
248 OnCacheStorageKeysSuccess
)
249 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheStorageMatchSuccess
,
250 OnCacheStorageMatchSuccess
)
251 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheStorageHasError
,
252 OnCacheStorageHasError
)
253 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheStorageOpenError
,
254 OnCacheStorageOpenError
)
255 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheStorageDeleteError
,
256 OnCacheStorageDeleteError
)
257 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheStorageKeysError
,
258 OnCacheStorageKeysError
)
259 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheStorageMatchError
,
260 OnCacheStorageMatchError
)
261 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheMatchSuccess
,
263 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheMatchAllSuccess
,
264 OnCacheMatchAllSuccess
)
265 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheKeysSuccess
, OnCacheKeysSuccess
)
266 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheBatchSuccess
,
268 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheMatchError
, OnCacheMatchError
)
269 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheMatchAllError
,
270 OnCacheMatchAllError
)
271 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheKeysError
, OnCacheKeysError
)
272 IPC_MESSAGE_HANDLER(CacheStorageMsg_CacheBatchError
, OnCacheBatchError
)
273 IPC_MESSAGE_UNHANDLED(handled
= false)
274 IPC_END_MESSAGE_MAP()
279 void CacheStorageDispatcher::OnCacheStorageHasSuccess(int thread_id
,
281 DCHECK_EQ(thread_id
, CurrentWorkerId());
282 UMA_HISTOGRAM_TIMES("ServiceWorkerCache.CacheStorage.Has",
283 TimeTicks::Now() - has_times_
[request_id
]);
284 WebServiceWorkerCacheStorage::CacheStorageCallbacks
* callbacks
=
285 has_callbacks_
.Lookup(request_id
);
286 callbacks
->onSuccess();
287 has_callbacks_
.Remove(request_id
);
288 has_times_
.erase(request_id
);
291 void CacheStorageDispatcher::OnCacheStorageOpenSuccess(int thread_id
,
294 DCHECK_EQ(thread_id
, CurrentWorkerId());
295 WebCache
* web_cache
= new WebCache(weak_factory_
.GetWeakPtr(), cache_id
);
296 web_caches_
.AddWithID(web_cache
, cache_id
);
297 UMA_HISTOGRAM_TIMES("ServiceWorkerCache.CacheStorage.Open",
298 TimeTicks::Now() - open_times_
[request_id
]);
299 WebServiceWorkerCacheStorage::CacheStorageWithCacheCallbacks
* callbacks
=
300 open_callbacks_
.Lookup(request_id
);
301 callbacks
->onSuccess(web_cache
);
302 open_callbacks_
.Remove(request_id
);
303 open_times_
.erase(request_id
);
306 void CacheStorageDispatcher::OnCacheStorageDeleteSuccess(int thread_id
,
308 DCHECK_EQ(thread_id
, CurrentWorkerId());
309 UMA_HISTOGRAM_TIMES("ServiceWorkerCache.CacheStorage.Delete",
310 TimeTicks::Now() - delete_times_
[request_id
]);
311 WebServiceWorkerCacheStorage::CacheStorageCallbacks
* callbacks
=
312 delete_callbacks_
.Lookup(request_id
);
313 callbacks
->onSuccess();
314 delete_callbacks_
.Remove(request_id
);
315 delete_times_
.erase(request_id
);
318 void CacheStorageDispatcher::OnCacheStorageKeysSuccess(
321 const std::vector
<base::string16
>& keys
) {
322 DCHECK_EQ(thread_id
, CurrentWorkerId());
323 blink::WebVector
<blink::WebString
> webKeys(keys
.size());
324 for (size_t i
= 0; i
< keys
.size(); ++i
)
325 webKeys
[i
] = keys
[i
];
327 UMA_HISTOGRAM_TIMES("ServiceWorkerCache.CacheStorage.Keys",
328 TimeTicks::Now() - keys_times_
[request_id
]);
329 WebServiceWorkerCacheStorage::CacheStorageKeysCallbacks
* callbacks
=
330 keys_callbacks_
.Lookup(request_id
);
331 callbacks
->onSuccess(&webKeys
);
332 keys_callbacks_
.Remove(request_id
);
333 keys_times_
.erase(request_id
);
336 void CacheStorageDispatcher::OnCacheStorageMatchSuccess(
339 const ServiceWorkerResponse
& response
) {
340 DCHECK_EQ(thread_id
, CurrentWorkerId());
341 blink::WebServiceWorkerResponse web_response
;
342 PopulateWebResponseFromResponse(response
, &web_response
);
344 UMA_HISTOGRAM_TIMES("ServiceWorkerCache.CacheStorage.Match",
345 TimeTicks::Now() - match_times_
[request_id
]);
346 WebServiceWorkerCacheStorage::CacheStorageMatchCallbacks
* callbacks
=
347 match_callbacks_
.Lookup(request_id
);
348 callbacks
->onSuccess(&web_response
);
349 match_callbacks_
.Remove(request_id
);
350 match_times_
.erase(request_id
);
353 void CacheStorageDispatcher::OnCacheStorageHasError(
356 blink::WebServiceWorkerCacheError reason
) {
357 DCHECK_EQ(thread_id
, CurrentWorkerId());
358 WebServiceWorkerCacheStorage::CacheStorageCallbacks
* callbacks
=
359 has_callbacks_
.Lookup(request_id
);
360 callbacks
->onError(new blink::WebServiceWorkerCacheError(reason
));
361 has_callbacks_
.Remove(request_id
);
362 has_times_
.erase(request_id
);
365 void CacheStorageDispatcher::OnCacheStorageOpenError(
368 blink::WebServiceWorkerCacheError reason
) {
369 DCHECK_EQ(thread_id
, CurrentWorkerId());
370 WebServiceWorkerCacheStorage::CacheStorageWithCacheCallbacks
* callbacks
=
371 open_callbacks_
.Lookup(request_id
);
372 callbacks
->onError(new blink::WebServiceWorkerCacheError(reason
));
373 open_callbacks_
.Remove(request_id
);
374 open_times_
.erase(request_id
);
377 void CacheStorageDispatcher::OnCacheStorageDeleteError(
380 blink::WebServiceWorkerCacheError reason
) {
381 DCHECK_EQ(thread_id
, CurrentWorkerId());
382 WebServiceWorkerCacheStorage::CacheStorageCallbacks
* callbacks
=
383 delete_callbacks_
.Lookup(request_id
);
384 callbacks
->onError(new blink::WebServiceWorkerCacheError(reason
));
385 delete_callbacks_
.Remove(request_id
);
386 delete_times_
.erase(request_id
);
389 void CacheStorageDispatcher::OnCacheStorageKeysError(
392 blink::WebServiceWorkerCacheError reason
) {
393 DCHECK_EQ(thread_id
, CurrentWorkerId());
394 WebServiceWorkerCacheStorage::CacheStorageKeysCallbacks
* callbacks
=
395 keys_callbacks_
.Lookup(request_id
);
396 callbacks
->onError(new blink::WebServiceWorkerCacheError(reason
));
397 keys_callbacks_
.Remove(request_id
);
398 keys_times_
.erase(request_id
);
401 void CacheStorageDispatcher::OnCacheStorageMatchError(
404 blink::WebServiceWorkerCacheError reason
) {
405 DCHECK_EQ(thread_id
, CurrentWorkerId());
406 WebServiceWorkerCacheStorage::CacheStorageMatchCallbacks
* callbacks
=
407 match_callbacks_
.Lookup(request_id
);
408 callbacks
->onError(new blink::WebServiceWorkerCacheError(reason
));
409 match_callbacks_
.Remove(request_id
);
410 match_times_
.erase(request_id
);
413 void CacheStorageDispatcher::OnCacheMatchSuccess(
416 const ServiceWorkerResponse
& response
) {
417 DCHECK_EQ(thread_id
, CurrentWorkerId());
418 blink::WebServiceWorkerResponse web_response
;
419 PopulateWebResponseFromResponse(response
, &web_response
);
421 UMA_HISTOGRAM_TIMES("ServiceWorkerCache.Cache.Match",
422 TimeTicks::Now() - cache_match_times_
[request_id
]);
423 blink::WebServiceWorkerCache::CacheMatchCallbacks
* callbacks
=
424 cache_match_callbacks_
.Lookup(request_id
);
425 callbacks
->onSuccess(&web_response
);
426 cache_match_callbacks_
.Remove(request_id
);
427 cache_match_times_
.erase(request_id
);
430 void CacheStorageDispatcher::OnCacheMatchAllSuccess(
433 const std::vector
<ServiceWorkerResponse
>& responses
) {
434 DCHECK_EQ(thread_id
, CurrentWorkerId());
435 blink::WebVector
<blink::WebServiceWorkerResponse
> web_responses
=
436 WebResponsesFromResponses(responses
);
438 UMA_HISTOGRAM_TIMES("ServiceWorkerCache.Cache.MatchAll",
439 TimeTicks::Now() - cache_match_all_times_
[request_id
]);
440 blink::WebServiceWorkerCache::CacheWithResponsesCallbacks
* callbacks
=
441 cache_match_all_callbacks_
.Lookup(request_id
);
442 callbacks
->onSuccess(&web_responses
);
443 cache_match_all_callbacks_
.Remove(request_id
);
444 cache_match_all_times_
.erase(request_id
);
447 void CacheStorageDispatcher::OnCacheKeysSuccess(
450 const std::vector
<ServiceWorkerFetchRequest
>& requests
) {
451 DCHECK_EQ(thread_id
, CurrentWorkerId());
452 blink::WebVector
<blink::WebServiceWorkerRequest
> web_requests
=
453 WebRequestsFromRequests(requests
);
455 UMA_HISTOGRAM_TIMES("ServiceWorkerCache.Cache.Keys",
456 TimeTicks::Now() - cache_keys_times_
[request_id
]);
457 blink::WebServiceWorkerCache::CacheWithRequestsCallbacks
* callbacks
=
458 cache_keys_callbacks_
.Lookup(request_id
);
459 callbacks
->onSuccess(&web_requests
);
460 cache_keys_callbacks_
.Remove(request_id
);
461 cache_keys_times_
.erase(request_id
);
464 void CacheStorageDispatcher::OnCacheBatchSuccess(
467 DCHECK_EQ(thread_id
, CurrentWorkerId());
469 UMA_HISTOGRAM_TIMES("ServiceWorkerCache.Cache.Batch",
470 TimeTicks::Now() - cache_batch_times_
[request_id
]);
471 blink::WebServiceWorkerCache::CacheBatchCallbacks
* callbacks
=
472 cache_batch_callbacks_
.Lookup(request_id
);
473 callbacks
->onSuccess();
474 cache_batch_callbacks_
.Remove(request_id
);
475 cache_batch_times_
.erase(request_id
);
478 void CacheStorageDispatcher::OnCacheMatchError(
481 blink::WebServiceWorkerCacheError reason
) {
482 DCHECK_EQ(thread_id
, CurrentWorkerId());
483 blink::WebServiceWorkerCache::CacheMatchCallbacks
* callbacks
=
484 cache_match_callbacks_
.Lookup(request_id
);
485 callbacks
->onError(new blink::WebServiceWorkerCacheError(reason
));
486 cache_match_callbacks_
.Remove(request_id
);
487 cache_match_times_
.erase(request_id
);
490 void CacheStorageDispatcher::OnCacheMatchAllError(
493 blink::WebServiceWorkerCacheError reason
) {
494 DCHECK_EQ(thread_id
, CurrentWorkerId());
495 blink::WebServiceWorkerCache::CacheWithResponsesCallbacks
* callbacks
=
496 cache_match_all_callbacks_
.Lookup(request_id
);
497 callbacks
->onError(new blink::WebServiceWorkerCacheError(reason
));
498 cache_match_all_callbacks_
.Remove(request_id
);
499 cache_match_all_times_
.erase(request_id
);
502 void CacheStorageDispatcher::OnCacheKeysError(
505 blink::WebServiceWorkerCacheError reason
) {
506 DCHECK_EQ(thread_id
, CurrentWorkerId());
507 blink::WebServiceWorkerCache::CacheWithRequestsCallbacks
* callbacks
=
508 cache_keys_callbacks_
.Lookup(request_id
);
509 callbacks
->onError(new blink::WebServiceWorkerCacheError(reason
));
510 cache_keys_callbacks_
.Remove(request_id
);
511 cache_keys_times_
.erase(request_id
);
514 void CacheStorageDispatcher::OnCacheBatchError(
517 blink::WebServiceWorkerCacheError reason
) {
518 DCHECK_EQ(thread_id
, CurrentWorkerId());
519 blink::WebServiceWorkerCache::CacheBatchCallbacks
* callbacks
=
520 cache_batch_callbacks_
.Lookup(request_id
);
521 callbacks
->onError(new blink::WebServiceWorkerCacheError(reason
));
522 cache_batch_callbacks_
.Remove(request_id
);
523 cache_batch_times_
.erase(request_id
);
526 void CacheStorageDispatcher::dispatchHas(
527 WebServiceWorkerCacheStorage::CacheStorageCallbacks
* callbacks
,
529 const blink::WebString
& cacheName
) {
530 int request_id
= has_callbacks_
.Add(callbacks
);
531 has_times_
[request_id
] = base::TimeTicks::Now();
532 Send(new CacheStorageHostMsg_CacheStorageHas(CurrentWorkerId(), request_id
,
536 void CacheStorageDispatcher::dispatchOpen(
537 WebServiceWorkerCacheStorage::CacheStorageWithCacheCallbacks
* callbacks
,
539 const blink::WebString
& cacheName
) {
540 int request_id
= open_callbacks_
.Add(callbacks
);
541 open_times_
[request_id
] = base::TimeTicks::Now();
542 Send(new CacheStorageHostMsg_CacheStorageOpen(CurrentWorkerId(), request_id
,
546 void CacheStorageDispatcher::dispatchDelete(
547 WebServiceWorkerCacheStorage::CacheStorageCallbacks
* callbacks
,
549 const blink::WebString
& cacheName
) {
550 int request_id
= delete_callbacks_
.Add(callbacks
);
551 delete_times_
[request_id
] = base::TimeTicks::Now();
552 Send(new CacheStorageHostMsg_CacheStorageDelete(CurrentWorkerId(), request_id
,
556 void CacheStorageDispatcher::dispatchKeys(
557 WebServiceWorkerCacheStorage::CacheStorageKeysCallbacks
* callbacks
,
558 const GURL
& origin
) {
559 int request_id
= keys_callbacks_
.Add(callbacks
);
560 keys_times_
[request_id
] = base::TimeTicks::Now();
561 Send(new CacheStorageHostMsg_CacheStorageKeys(CurrentWorkerId(), request_id
,
565 void CacheStorageDispatcher::dispatchMatch(
566 WebServiceWorkerCacheStorage::CacheStorageMatchCallbacks
* callbacks
,
568 const blink::WebServiceWorkerRequest
& request
,
569 const blink::WebServiceWorkerCache::QueryParams
& query_params
) {
570 int request_id
= match_callbacks_
.Add(callbacks
);
571 match_times_
[request_id
] = base::TimeTicks::Now();
572 Send(new CacheStorageHostMsg_CacheStorageMatch(
573 CurrentWorkerId(), request_id
, origin
,
574 FetchRequestFromWebRequest(request
),
575 QueryParamsFromWebQueryParams(query_params
)));
578 void CacheStorageDispatcher::dispatchMatchForCache(
580 blink::WebServiceWorkerCache::CacheMatchCallbacks
* callbacks
,
581 const blink::WebServiceWorkerRequest
& request
,
582 const blink::WebServiceWorkerCache::QueryParams
& query_params
) {
583 int request_id
= cache_match_callbacks_
.Add(callbacks
);
584 cache_match_times_
[request_id
] = base::TimeTicks::Now();
586 Send(new CacheStorageHostMsg_CacheMatch(
587 CurrentWorkerId(), request_id
, cache_id
,
588 FetchRequestFromWebRequest(request
),
589 QueryParamsFromWebQueryParams(query_params
)));
592 void CacheStorageDispatcher::dispatchMatchAllForCache(
594 blink::WebServiceWorkerCache::CacheWithResponsesCallbacks
* callbacks
,
595 const blink::WebServiceWorkerRequest
& request
,
596 const blink::WebServiceWorkerCache::QueryParams
& query_params
) {
597 int request_id
= cache_match_all_callbacks_
.Add(callbacks
);
598 cache_match_all_times_
[request_id
] = base::TimeTicks::Now();
600 Send(new CacheStorageHostMsg_CacheMatchAll(
601 CurrentWorkerId(), request_id
, cache_id
,
602 FetchRequestFromWebRequest(request
),
603 QueryParamsFromWebQueryParams(query_params
)));
606 void CacheStorageDispatcher::dispatchKeysForCache(
608 blink::WebServiceWorkerCache::CacheWithRequestsCallbacks
* callbacks
,
609 const blink::WebServiceWorkerRequest
* request
,
610 const blink::WebServiceWorkerCache::QueryParams
& query_params
) {
611 int request_id
= cache_keys_callbacks_
.Add(callbacks
);
612 cache_keys_times_
[request_id
] = base::TimeTicks::Now();
614 Send(new CacheStorageHostMsg_CacheKeys(
615 CurrentWorkerId(), request_id
, cache_id
,
616 request
? FetchRequestFromWebRequest(*request
)
617 : ServiceWorkerFetchRequest(),
618 QueryParamsFromWebQueryParams(query_params
)));
621 void CacheStorageDispatcher::dispatchBatchForCache(
623 blink::WebServiceWorkerCache::CacheBatchCallbacks
* callbacks
,
624 const blink::WebVector
<blink::WebServiceWorkerCache::BatchOperation
>&
626 int request_id
= cache_batch_callbacks_
.Add(callbacks
);
627 cache_batch_times_
[request_id
] = base::TimeTicks::Now();
629 std::vector
<CacheStorageBatchOperation
> operations
;
630 operations
.reserve(web_operations
.size());
631 for (size_t i
= 0; i
< web_operations
.size(); ++i
) {
632 operations
.push_back(
633 BatchOperationFromWebBatchOperation(web_operations
[i
]));
636 Send(new CacheStorageHostMsg_CacheBatch(CurrentWorkerId(), request_id
,
637 cache_id
, operations
));
640 void CacheStorageDispatcher::OnWebCacheDestruction(int cache_id
) {
641 web_caches_
.Remove(cache_id
);
642 Send(new CacheStorageHostMsg_CacheClosed(cache_id
));
645 void CacheStorageDispatcher::PopulateWebResponseFromResponse(
646 const ServiceWorkerResponse
& response
,
647 blink::WebServiceWorkerResponse
* web_response
) {
648 web_response
->setURL(response
.url
);
649 web_response
->setStatus(response
.status_code
);
650 web_response
->setStatusText(base::ASCIIToUTF16(response
.status_text
));
651 web_response
->setResponseType(response
.response_type
);
653 for (const auto& i
: response
.headers
) {
654 web_response
->setHeader(base::ASCIIToUTF16(i
.first
),
655 base::ASCIIToUTF16(i
.second
));
658 if (!response
.blob_uuid
.empty()) {
659 web_response
->setBlob(blink::WebString::fromUTF8(response
.blob_uuid
),
661 // Let the host know that it can release its reference to the blob.
662 Send(new CacheStorageHostMsg_BlobDataHandled(response
.blob_uuid
));
666 blink::WebVector
<blink::WebServiceWorkerResponse
>
667 CacheStorageDispatcher::WebResponsesFromResponses(
668 const std::vector
<ServiceWorkerResponse
>& responses
) {
669 blink::WebVector
<blink::WebServiceWorkerResponse
> web_responses(
671 for (size_t i
= 0; i
< responses
.size(); ++i
)
672 PopulateWebResponseFromResponse(responses
[i
], &(web_responses
[i
]));
673 return web_responses
;
676 } // namespace content