| OLD | NEW |
| (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/renderer/service_worker/service_worker_cache_storage_dispatche
r.h" | |
| 6 | |
| 7 #include <map> | |
| 8 #include <string> | |
| 9 #include <utility> | |
| 10 | |
| 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/service_worker/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" | |
| 24 | |
| 25 using base::TimeTicks; | |
| 26 | |
| 27 namespace content { | |
| 28 | |
| 29 using blink::WebServiceWorkerCacheStorage; | |
| 30 using blink::WebServiceWorkerCacheError; | |
| 31 using blink::WebServiceWorkerRequest; | |
| 32 | |
| 33 static base::LazyInstance< | |
| 34 base::ThreadLocalPointer<ServiceWorkerCacheStorageDispatcher>>::Leaky | |
| 35 g_cache_storage_dispatcher_tls = LAZY_INSTANCE_INITIALIZER; | |
| 36 | |
| 37 namespace { | |
| 38 | |
| 39 ServiceWorkerCacheStorageDispatcher* const kHasBeenDeleted = | |
| 40 reinterpret_cast<ServiceWorkerCacheStorageDispatcher*>(0x1); | |
| 41 | |
| 42 ServiceWorkerFetchRequest FetchRequestFromWebRequest( | |
| 43 const blink::WebServiceWorkerRequest& web_request) { | |
| 44 ServiceWorkerHeaderMap headers; | |
| 45 GetServiceWorkerHeaderMapFromWebRequest(web_request, &headers); | |
| 46 | |
| 47 return ServiceWorkerFetchRequest( | |
| 48 web_request.url(), base::UTF16ToASCII(web_request.method()), headers, | |
| 49 Referrer(web_request.referrerUrl(), web_request.referrerPolicy()), | |
| 50 web_request.isReload()); | |
| 51 } | |
| 52 | |
| 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)); | |
| 63 } | |
| 64 web_request->setReferrer(base::ASCIIToUTF16(request.referrer.url.spec()), | |
| 65 request.referrer.policy); | |
| 66 web_request->setIsReload(request.is_reload); | |
| 67 } | |
| 68 | |
| 69 blink::WebVector<blink::WebServiceWorkerRequest> WebRequestsFromRequests( | |
| 70 const std::vector<ServiceWorkerFetchRequest>& requests) { | |
| 71 blink::WebVector<blink::WebServiceWorkerRequest> | |
| 72 web_requests(requests.size()); | |
| 73 for (size_t i = 0; i < requests.size(); ++i) | |
| 74 PopulateWebRequestFromFetchRequest(requests[i], &(web_requests[i])); | |
| 75 return web_requests; | |
| 76 } | |
| 77 | |
| 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(web_response.url(), | |
| 85 web_response.status(), | |
| 86 base::UTF16ToASCII(web_response.statusText()), | |
| 87 web_response.responseType(), | |
| 88 headers, | |
| 89 base::UTF16ToASCII(web_response.blobUUID()), | |
| 90 web_response.blobSize(), | |
| 91 web_response.streamURL()); | |
| 92 } | |
| 93 | |
| 94 ServiceWorkerCacheQueryParams QueryParamsFromWebQueryParams( | |
| 95 const blink::WebServiceWorkerCache::QueryParams& web_query_params) { | |
| 96 ServiceWorkerCacheQueryParams query_params; | |
| 97 query_params.ignore_search = web_query_params.ignoreSearch; | |
| 98 query_params.ignore_method = web_query_params.ignoreMethod; | |
| 99 query_params.ignore_vary = web_query_params.ignoreVary; | |
| 100 query_params.cache_name = web_query_params.cacheName; | |
| 101 return query_params; | |
| 102 } | |
| 103 | |
| 104 ServiceWorkerCacheOperationType CacheOperationTypeFromWebCacheOperationType( | |
| 105 blink::WebServiceWorkerCache::OperationType operation_type) { | |
| 106 switch (operation_type) { | |
| 107 case blink::WebServiceWorkerCache::OperationTypePut: | |
| 108 return SERVICE_WORKER_CACHE_OPERATION_TYPE_PUT; | |
| 109 case blink::WebServiceWorkerCache::OperationTypeDelete: | |
| 110 return SERVICE_WORKER_CACHE_OPERATION_TYPE_DELETE; | |
| 111 default: | |
| 112 return SERVICE_WORKER_CACHE_OPERATION_TYPE_UNDEFINED; | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 ServiceWorkerBatchOperation BatchOperationFromWebBatchOperation( | |
| 117 const blink::WebServiceWorkerCache::BatchOperation& web_operation) { | |
| 118 ServiceWorkerBatchOperation operation; | |
| 119 operation.operation_type = | |
| 120 CacheOperationTypeFromWebCacheOperationType(web_operation.operationType); | |
| 121 operation.request = FetchRequestFromWebRequest(web_operation.request); | |
| 122 operation.response = ResponseFromWebResponse(web_operation.response); | |
| 123 operation.match_params = | |
| 124 QueryParamsFromWebQueryParams(web_operation.matchParams); | |
| 125 return operation; | |
| 126 } | |
| 127 | |
| 128 template<typename T> | |
| 129 void ClearCallbacksMapWithErrors(T* callbacks_map) { | |
| 130 typename T::iterator iter(callbacks_map); | |
| 131 while (!iter.IsAtEnd()) { | |
| 132 blink::WebServiceWorkerCacheError reason = | |
| 133 blink::WebServiceWorkerCacheErrorNotFound; | |
| 134 iter.GetCurrentValue()->onError(&reason); | |
| 135 callbacks_map->Remove(iter.GetCurrentKey()); | |
| 136 iter.Advance(); | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 } // namespace | |
| 141 | |
| 142 // The WebCache object is the Chromium side implementation of the Blink | |
| 143 // WebServiceWorkerCache API. Most of its methods delegate directly to the | |
| 144 // ServiceWorkerStorage object, which is able to assign unique IDs as well | |
| 145 // as have a lifetime longer than the requests. | |
| 146 class ServiceWorkerCacheStorageDispatcher::WebCache | |
| 147 : public blink::WebServiceWorkerCache { | |
| 148 public: | |
| 149 WebCache(base::WeakPtr<ServiceWorkerCacheStorageDispatcher> dispatcher, | |
| 150 int cache_id) | |
| 151 : dispatcher_(dispatcher), | |
| 152 cache_id_(cache_id) {} | |
| 153 | |
| 154 virtual ~WebCache() { | |
| 155 if (dispatcher_) | |
| 156 dispatcher_->OnWebCacheDestruction(cache_id_); | |
| 157 } | |
| 158 | |
| 159 // From blink::WebServiceWorkerCache: | |
| 160 virtual void dispatchMatch(CacheMatchCallbacks* callbacks, | |
| 161 const blink::WebServiceWorkerRequest& request, | |
| 162 const QueryParams& query_params) { | |
| 163 if (!dispatcher_) | |
| 164 return; | |
| 165 dispatcher_->dispatchMatchForCache(cache_id_, callbacks, request, | |
| 166 query_params); | |
| 167 } | |
| 168 virtual void dispatchMatchAll(CacheWithResponsesCallbacks* callbacks, | |
| 169 const blink::WebServiceWorkerRequest& request, | |
| 170 const QueryParams& query_params) { | |
| 171 if (!dispatcher_) | |
| 172 return; | |
| 173 dispatcher_->dispatchMatchAllForCache(cache_id_, callbacks, request, | |
| 174 query_params); | |
| 175 } | |
| 176 virtual void dispatchKeys(CacheWithRequestsCallbacks* callbacks, | |
| 177 const blink::WebServiceWorkerRequest* request, | |
| 178 const QueryParams& query_params) { | |
| 179 if (!dispatcher_) | |
| 180 return; | |
| 181 dispatcher_->dispatchKeysForCache(cache_id_, callbacks, request, | |
| 182 query_params); | |
| 183 } | |
| 184 virtual void dispatchBatch( | |
| 185 CacheWithResponsesCallbacks* callbacks, | |
| 186 const blink::WebVector<BatchOperation>& batch_operations) { | |
| 187 if (!dispatcher_) | |
| 188 return; | |
| 189 dispatcher_->dispatchBatchForCache(cache_id_, callbacks, batch_operations); | |
| 190 } | |
| 191 | |
| 192 private: | |
| 193 const base::WeakPtr<ServiceWorkerCacheStorageDispatcher> dispatcher_; | |
| 194 const int cache_id_; | |
| 195 }; | |
| 196 | |
| 197 ServiceWorkerCacheStorageDispatcher::ServiceWorkerCacheStorageDispatcher( | |
| 198 ThreadSafeSender* thread_safe_sender) | |
| 199 : thread_safe_sender_(thread_safe_sender), weak_factory_(this) { | |
| 200 g_cache_storage_dispatcher_tls.Pointer()->Set(this); | |
| 201 } | |
| 202 | |
| 203 ServiceWorkerCacheStorageDispatcher::~ServiceWorkerCacheStorageDispatcher() { | |
| 204 ClearCallbacksMapWithErrors(&has_callbacks_); | |
| 205 ClearCallbacksMapWithErrors(&open_callbacks_); | |
| 206 ClearCallbacksMapWithErrors(&delete_callbacks_); | |
| 207 ClearCallbacksMapWithErrors(&keys_callbacks_); | |
| 208 ClearCallbacksMapWithErrors(&match_callbacks_); | |
| 209 | |
| 210 ClearCallbacksMapWithErrors(&cache_match_callbacks_); | |
| 211 ClearCallbacksMapWithErrors(&cache_match_all_callbacks_); | |
| 212 ClearCallbacksMapWithErrors(&cache_keys_callbacks_); | |
| 213 ClearCallbacksMapWithErrors(&cache_batch_callbacks_); | |
| 214 | |
| 215 g_cache_storage_dispatcher_tls.Pointer()->Set(kHasBeenDeleted); | |
| 216 } | |
| 217 | |
| 218 ServiceWorkerCacheStorageDispatcher* | |
| 219 ServiceWorkerCacheStorageDispatcher::ThreadSpecificInstance( | |
| 220 ThreadSafeSender* thread_safe_sender) { | |
| 221 if (g_cache_storage_dispatcher_tls.Pointer()->Get() == kHasBeenDeleted) { | |
| 222 NOTREACHED() << "Re-instantiating TLS ServiceWorkerCacheStorageDispatcher."; | |
| 223 g_cache_storage_dispatcher_tls.Pointer()->Set(NULL); | |
| 224 } | |
| 225 if (g_cache_storage_dispatcher_tls.Pointer()->Get()) | |
| 226 return g_cache_storage_dispatcher_tls.Pointer()->Get(); | |
| 227 | |
| 228 ServiceWorkerCacheStorageDispatcher* dispatcher = | |
| 229 new ServiceWorkerCacheStorageDispatcher(thread_safe_sender); | |
| 230 if (WorkerTaskRunner::Instance()->CurrentWorkerId()) | |
| 231 WorkerTaskRunner::Instance()->AddStopObserver(dispatcher); | |
| 232 return dispatcher; | |
| 233 } | |
| 234 | |
| 235 void ServiceWorkerCacheStorageDispatcher::OnWorkerRunLoopStopped() { | |
| 236 delete this; | |
| 237 } | |
| 238 | |
| 239 bool ServiceWorkerCacheStorageDispatcher::Send(IPC::Message* msg) { | |
| 240 return thread_safe_sender_->Send(msg); | |
| 241 } | |
| 242 | |
| 243 bool ServiceWorkerCacheStorageDispatcher::OnMessageReceived( | |
| 244 const IPC::Message& message) { | |
| 245 bool handled = true; | |
| 246 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerCacheStorageDispatcher, message) | |
| 247 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CacheStorageHasSuccess, | |
| 248 OnCacheStorageHasSuccess) | |
| 249 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CacheStorageOpenSuccess, | |
| 250 OnCacheStorageOpenSuccess) | |
| 251 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CacheStorageDeleteSuccess, | |
| 252 OnCacheStorageDeleteSuccess) | |
| 253 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CacheStorageKeysSuccess, | |
| 254 OnCacheStorageKeysSuccess) | |
| 255 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CacheStorageMatchSuccess, | |
| 256 OnCacheStorageMatchSuccess) | |
| 257 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CacheStorageHasError, | |
| 258 OnCacheStorageHasError) | |
| 259 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CacheStorageOpenError, | |
| 260 OnCacheStorageOpenError) | |
| 261 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CacheStorageDeleteError, | |
| 262 OnCacheStorageDeleteError) | |
| 263 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CacheStorageKeysError, | |
| 264 OnCacheStorageKeysError) | |
| 265 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CacheStorageMatchError, | |
| 266 OnCacheStorageMatchError) | |
| 267 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CacheMatchSuccess, | |
| 268 OnCacheMatchSuccess) | |
| 269 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CacheMatchAllSuccess, | |
| 270 OnCacheMatchAllSuccess) | |
| 271 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CacheKeysSuccess, | |
| 272 OnCacheKeysSuccess) | |
| 273 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CacheBatchSuccess, | |
| 274 OnCacheBatchSuccess) | |
| 275 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CacheMatchError, | |
| 276 OnCacheMatchError) | |
| 277 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CacheMatchAllError, | |
| 278 OnCacheMatchAllError) | |
| 279 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CacheKeysError, | |
| 280 OnCacheKeysError) | |
| 281 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CacheBatchError, | |
| 282 OnCacheBatchError) | |
| 283 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 284 IPC_END_MESSAGE_MAP() | |
| 285 | |
| 286 return handled; | |
| 287 } | |
| 288 | |
| 289 void ServiceWorkerCacheStorageDispatcher::OnCacheStorageHasSuccess( | |
| 290 int thread_id, | |
| 291 int request_id) { | |
| 292 DCHECK_EQ(thread_id, CurrentWorkerId()); | |
| 293 UMA_HISTOGRAM_TIMES("ServiceWorkerCache.CacheStorage.Has", | |
| 294 TimeTicks::Now() - has_times_[request_id]); | |
| 295 WebServiceWorkerCacheStorage::CacheStorageCallbacks* callbacks = | |
| 296 has_callbacks_.Lookup(request_id); | |
| 297 callbacks->onSuccess(); | |
| 298 has_callbacks_.Remove(request_id); | |
| 299 has_times_.erase(request_id); | |
| 300 } | |
| 301 | |
| 302 void ServiceWorkerCacheStorageDispatcher::OnCacheStorageOpenSuccess( | |
| 303 int thread_id, | |
| 304 int request_id, | |
| 305 int cache_id) { | |
| 306 DCHECK_EQ(thread_id, CurrentWorkerId()); | |
| 307 WebCache* web_cache = new WebCache(weak_factory_.GetWeakPtr(), cache_id); | |
| 308 web_caches_.AddWithID(web_cache, cache_id); | |
| 309 UMA_HISTOGRAM_TIMES("ServiceWorkerCache.CacheStorage.Open", | |
| 310 TimeTicks::Now() - open_times_[request_id]); | |
| 311 WebServiceWorkerCacheStorage::CacheStorageWithCacheCallbacks* callbacks = | |
| 312 open_callbacks_.Lookup(request_id); | |
| 313 callbacks->onSuccess(web_cache); | |
| 314 open_callbacks_.Remove(request_id); | |
| 315 open_times_.erase(request_id); | |
| 316 } | |
| 317 | |
| 318 void ServiceWorkerCacheStorageDispatcher::OnCacheStorageDeleteSuccess( | |
| 319 int thread_id, | |
| 320 int request_id) { | |
| 321 DCHECK_EQ(thread_id, CurrentWorkerId()); | |
| 322 UMA_HISTOGRAM_TIMES("ServiceWorkerCache.CacheStorage.Delete", | |
| 323 TimeTicks::Now() - delete_times_[request_id]); | |
| 324 WebServiceWorkerCacheStorage::CacheStorageCallbacks* callbacks = | |
| 325 delete_callbacks_.Lookup(request_id); | |
| 326 callbacks->onSuccess(); | |
| 327 delete_callbacks_.Remove(request_id); | |
| 328 delete_times_.erase(request_id); | |
| 329 } | |
| 330 | |
| 331 void ServiceWorkerCacheStorageDispatcher::OnCacheStorageKeysSuccess( | |
| 332 int thread_id, | |
| 333 int request_id, | |
| 334 const std::vector<base::string16>& keys) { | |
| 335 DCHECK_EQ(thread_id, CurrentWorkerId()); | |
| 336 blink::WebVector<blink::WebString> webKeys(keys.size()); | |
| 337 for (size_t i = 0; i < keys.size(); ++i) | |
| 338 webKeys[i] = keys[i]; | |
| 339 | |
| 340 UMA_HISTOGRAM_TIMES("ServiceWorkerCache.CacheStorage.Keys", | |
| 341 TimeTicks::Now() - keys_times_[request_id]); | |
| 342 WebServiceWorkerCacheStorage::CacheStorageKeysCallbacks* callbacks = | |
| 343 keys_callbacks_.Lookup(request_id); | |
| 344 callbacks->onSuccess(&webKeys); | |
| 345 keys_callbacks_.Remove(request_id); | |
| 346 keys_times_.erase(request_id); | |
| 347 } | |
| 348 | |
| 349 void ServiceWorkerCacheStorageDispatcher::OnCacheStorageMatchSuccess( | |
| 350 int thread_id, | |
| 351 int request_id, | |
| 352 const ServiceWorkerResponse& response) { | |
| 353 DCHECK_EQ(thread_id, CurrentWorkerId()); | |
| 354 blink::WebServiceWorkerResponse web_response; | |
| 355 PopulateWebResponseFromResponse(response, &web_response); | |
| 356 | |
| 357 UMA_HISTOGRAM_TIMES("ServiceWorkerCache.CacheStorage.Match", | |
| 358 TimeTicks::Now() - match_times_[request_id]); | |
| 359 WebServiceWorkerCacheStorage::CacheStorageMatchCallbacks* callbacks = | |
| 360 match_callbacks_.Lookup(request_id); | |
| 361 callbacks->onSuccess(&web_response); | |
| 362 match_callbacks_.Remove(request_id); | |
| 363 match_times_.erase(request_id); | |
| 364 } | |
| 365 | |
| 366 void ServiceWorkerCacheStorageDispatcher::OnCacheStorageHasError( | |
| 367 int thread_id, | |
| 368 int request_id, | |
| 369 blink::WebServiceWorkerCacheError reason) { | |
| 370 DCHECK_EQ(thread_id, CurrentWorkerId()); | |
| 371 WebServiceWorkerCacheStorage::CacheStorageCallbacks* callbacks = | |
| 372 has_callbacks_.Lookup(request_id); | |
| 373 callbacks->onError(&reason); | |
| 374 has_callbacks_.Remove(request_id); | |
| 375 has_times_.erase(request_id); | |
| 376 } | |
| 377 | |
| 378 void ServiceWorkerCacheStorageDispatcher::OnCacheStorageOpenError( | |
| 379 int thread_id, | |
| 380 int request_id, | |
| 381 blink::WebServiceWorkerCacheError reason) { | |
| 382 DCHECK_EQ(thread_id, CurrentWorkerId()); | |
| 383 WebServiceWorkerCacheStorage::CacheStorageWithCacheCallbacks* callbacks = | |
| 384 open_callbacks_.Lookup(request_id); | |
| 385 callbacks->onError(&reason); | |
| 386 open_callbacks_.Remove(request_id); | |
| 387 open_times_.erase(request_id); | |
| 388 } | |
| 389 | |
| 390 void ServiceWorkerCacheStorageDispatcher::OnCacheStorageDeleteError( | |
| 391 int thread_id, | |
| 392 int request_id, | |
| 393 blink::WebServiceWorkerCacheError reason) { | |
| 394 DCHECK_EQ(thread_id, CurrentWorkerId()); | |
| 395 WebServiceWorkerCacheStorage::CacheStorageCallbacks* callbacks = | |
| 396 delete_callbacks_.Lookup(request_id); | |
| 397 callbacks->onError(&reason); | |
| 398 delete_callbacks_.Remove(request_id); | |
| 399 delete_times_.erase(request_id); | |
| 400 } | |
| 401 | |
| 402 void ServiceWorkerCacheStorageDispatcher::OnCacheStorageKeysError( | |
| 403 int thread_id, | |
| 404 int request_id, | |
| 405 blink::WebServiceWorkerCacheError reason) { | |
| 406 DCHECK_EQ(thread_id, CurrentWorkerId()); | |
| 407 WebServiceWorkerCacheStorage::CacheStorageKeysCallbacks* callbacks = | |
| 408 keys_callbacks_.Lookup(request_id); | |
| 409 callbacks->onError(&reason); | |
| 410 keys_callbacks_.Remove(request_id); | |
| 411 keys_times_.erase(request_id); | |
| 412 } | |
| 413 | |
| 414 void ServiceWorkerCacheStorageDispatcher::OnCacheStorageMatchError( | |
| 415 int thread_id, | |
| 416 int request_id, | |
| 417 blink::WebServiceWorkerCacheError reason) { | |
| 418 DCHECK_EQ(thread_id, CurrentWorkerId()); | |
| 419 WebServiceWorkerCacheStorage::CacheStorageMatchCallbacks* callbacks = | |
| 420 match_callbacks_.Lookup(request_id); | |
| 421 callbacks->onError(&reason); | |
| 422 match_callbacks_.Remove(request_id); | |
| 423 match_times_.erase(request_id); | |
| 424 } | |
| 425 | |
| 426 void ServiceWorkerCacheStorageDispatcher::OnCacheMatchSuccess( | |
| 427 int thread_id, | |
| 428 int request_id, | |
| 429 const ServiceWorkerResponse& response) { | |
| 430 DCHECK_EQ(thread_id, CurrentWorkerId()); | |
| 431 blink::WebServiceWorkerResponse web_response; | |
| 432 PopulateWebResponseFromResponse(response, &web_response); | |
| 433 | |
| 434 UMA_HISTOGRAM_TIMES("ServiceWorkerCache.Cache.Match", | |
| 435 TimeTicks::Now() - cache_match_times_[request_id]); | |
| 436 blink::WebServiceWorkerCache::CacheMatchCallbacks* callbacks = | |
| 437 cache_match_callbacks_.Lookup(request_id); | |
| 438 callbacks->onSuccess(&web_response); | |
| 439 cache_match_callbacks_.Remove(request_id); | |
| 440 cache_match_times_.erase(request_id); | |
| 441 } | |
| 442 | |
| 443 void ServiceWorkerCacheStorageDispatcher::OnCacheMatchAllSuccess( | |
| 444 int thread_id, | |
| 445 int request_id, | |
| 446 const std::vector<ServiceWorkerResponse>& responses) { | |
| 447 DCHECK_EQ(thread_id, CurrentWorkerId()); | |
| 448 blink::WebVector<blink::WebServiceWorkerResponse> | |
| 449 web_responses = WebResponsesFromResponses(responses); | |
| 450 | |
| 451 UMA_HISTOGRAM_TIMES("ServiceWorkerCache.Cache.MatchAll", | |
| 452 TimeTicks::Now() - cache_match_all_times_[request_id]); | |
| 453 blink::WebServiceWorkerCache::CacheWithResponsesCallbacks* callbacks = | |
| 454 cache_match_all_callbacks_.Lookup(request_id); | |
| 455 callbacks->onSuccess(&web_responses); | |
| 456 cache_match_all_callbacks_.Remove(request_id); | |
| 457 cache_match_all_times_.erase(request_id); | |
| 458 } | |
| 459 | |
| 460 void ServiceWorkerCacheStorageDispatcher::OnCacheKeysSuccess( | |
| 461 int thread_id, | |
| 462 int request_id, | |
| 463 const std::vector<ServiceWorkerFetchRequest>& requests) { | |
| 464 DCHECK_EQ(thread_id, CurrentWorkerId()); | |
| 465 blink::WebVector<blink::WebServiceWorkerRequest> | |
| 466 web_requests = WebRequestsFromRequests(requests); | |
| 467 | |
| 468 UMA_HISTOGRAM_TIMES("ServiceWorkerCache.Cache.Keys", | |
| 469 TimeTicks::Now() - cache_keys_times_[request_id]); | |
| 470 blink::WebServiceWorkerCache::CacheWithRequestsCallbacks* callbacks = | |
| 471 cache_keys_callbacks_.Lookup(request_id); | |
| 472 callbacks->onSuccess(&web_requests); | |
| 473 cache_keys_callbacks_.Remove(request_id); | |
| 474 cache_keys_times_.erase(request_id); | |
| 475 } | |
| 476 | |
| 477 void ServiceWorkerCacheStorageDispatcher::OnCacheBatchSuccess( | |
| 478 int thread_id, | |
| 479 int request_id, | |
| 480 const std::vector<ServiceWorkerResponse>& responses) { | |
| 481 DCHECK_EQ(thread_id, CurrentWorkerId()); | |
| 482 blink::WebVector<blink::WebServiceWorkerResponse> | |
| 483 web_responses = WebResponsesFromResponses(responses); | |
| 484 | |
| 485 UMA_HISTOGRAM_TIMES("ServiceWorkerCache.Cache.Batch", | |
| 486 TimeTicks::Now() - cache_batch_times_[request_id]); | |
| 487 blink::WebServiceWorkerCache::CacheWithResponsesCallbacks* callbacks = | |
| 488 cache_batch_callbacks_.Lookup(request_id); | |
| 489 callbacks->onSuccess(&web_responses); | |
| 490 cache_batch_callbacks_.Remove(request_id); | |
| 491 cache_batch_times_.erase(request_id); | |
| 492 } | |
| 493 | |
| 494 void ServiceWorkerCacheStorageDispatcher::OnCacheMatchError( | |
| 495 int thread_id, | |
| 496 int request_id, | |
| 497 blink::WebServiceWorkerCacheError reason) { | |
| 498 DCHECK_EQ(thread_id, CurrentWorkerId()); | |
| 499 blink::WebServiceWorkerCache::CacheMatchCallbacks* callbacks = | |
| 500 cache_match_callbacks_.Lookup(request_id); | |
| 501 callbacks->onError(&reason); | |
| 502 cache_match_callbacks_.Remove(request_id); | |
| 503 cache_match_times_.erase(request_id); | |
| 504 } | |
| 505 | |
| 506 void ServiceWorkerCacheStorageDispatcher::OnCacheMatchAllError( | |
| 507 int thread_id, | |
| 508 int request_id, | |
| 509 blink::WebServiceWorkerCacheError reason) { | |
| 510 DCHECK_EQ(thread_id, CurrentWorkerId()); | |
| 511 blink::WebServiceWorkerCache::CacheWithResponsesCallbacks* callbacks = | |
| 512 cache_match_all_callbacks_.Lookup(request_id); | |
| 513 callbacks->onError(&reason); | |
| 514 cache_match_all_callbacks_.Remove(request_id); | |
| 515 cache_match_all_times_.erase(request_id); | |
| 516 } | |
| 517 | |
| 518 void ServiceWorkerCacheStorageDispatcher::OnCacheKeysError( | |
| 519 int thread_id, | |
| 520 int request_id, | |
| 521 blink::WebServiceWorkerCacheError reason) { | |
| 522 DCHECK_EQ(thread_id, CurrentWorkerId()); | |
| 523 blink::WebServiceWorkerCache::CacheWithRequestsCallbacks* callbacks = | |
| 524 cache_keys_callbacks_.Lookup(request_id); | |
| 525 callbacks->onError(&reason); | |
| 526 cache_keys_callbacks_.Remove(request_id); | |
| 527 cache_keys_times_.erase(request_id); | |
| 528 } | |
| 529 | |
| 530 void ServiceWorkerCacheStorageDispatcher::OnCacheBatchError( | |
| 531 int thread_id, | |
| 532 int request_id, | |
| 533 blink::WebServiceWorkerCacheError reason) { | |
| 534 DCHECK_EQ(thread_id, CurrentWorkerId()); | |
| 535 blink::WebServiceWorkerCache::CacheWithResponsesCallbacks* callbacks = | |
| 536 cache_batch_callbacks_.Lookup(request_id); | |
| 537 callbacks->onError(&reason); | |
| 538 cache_batch_callbacks_.Remove(request_id); | |
| 539 cache_batch_times_.erase(request_id); | |
| 540 } | |
| 541 | |
| 542 void ServiceWorkerCacheStorageDispatcher::dispatchHas( | |
| 543 WebServiceWorkerCacheStorage::CacheStorageCallbacks* callbacks, | |
| 544 const GURL& origin, | |
| 545 const blink::WebString& cacheName) { | |
| 546 int request_id = has_callbacks_.Add(callbacks); | |
| 547 has_times_[request_id] = base::TimeTicks::Now(); | |
| 548 Send(new ServiceWorkerHostMsg_CacheStorageHas(CurrentWorkerId(), request_id, | |
| 549 origin, cacheName)); | |
| 550 } | |
| 551 | |
| 552 void ServiceWorkerCacheStorageDispatcher::dispatchOpen( | |
| 553 WebServiceWorkerCacheStorage::CacheStorageWithCacheCallbacks* callbacks, | |
| 554 const GURL& origin, | |
| 555 const blink::WebString& cacheName) { | |
| 556 int request_id = open_callbacks_.Add(callbacks); | |
| 557 open_times_[request_id] = base::TimeTicks::Now(); | |
| 558 Send(new ServiceWorkerHostMsg_CacheStorageOpen(CurrentWorkerId(), request_id, | |
| 559 origin, cacheName)); | |
| 560 } | |
| 561 | |
| 562 void ServiceWorkerCacheStorageDispatcher::dispatchDelete( | |
| 563 WebServiceWorkerCacheStorage::CacheStorageCallbacks* callbacks, | |
| 564 const GURL& origin, | |
| 565 const blink::WebString& cacheName) { | |
| 566 int request_id = delete_callbacks_.Add(callbacks); | |
| 567 delete_times_[request_id] = base::TimeTicks::Now(); | |
| 568 Send(new ServiceWorkerHostMsg_CacheStorageDelete( | |
| 569 CurrentWorkerId(), request_id, origin, cacheName)); | |
| 570 } | |
| 571 | |
| 572 void ServiceWorkerCacheStorageDispatcher::dispatchKeys( | |
| 573 WebServiceWorkerCacheStorage::CacheStorageKeysCallbacks* callbacks, | |
| 574 const GURL& origin) { | |
| 575 int request_id = keys_callbacks_.Add(callbacks); | |
| 576 keys_times_[request_id] = base::TimeTicks::Now(); | |
| 577 Send(new ServiceWorkerHostMsg_CacheStorageKeys(CurrentWorkerId(), request_id, | |
| 578 origin)); | |
| 579 } | |
| 580 | |
| 581 void ServiceWorkerCacheStorageDispatcher::dispatchMatch( | |
| 582 WebServiceWorkerCacheStorage::CacheStorageMatchCallbacks* callbacks, | |
| 583 const GURL& origin, | |
| 584 const blink::WebServiceWorkerRequest& request, | |
| 585 const blink::WebServiceWorkerCache::QueryParams& query_params) { | |
| 586 int request_id = match_callbacks_.Add(callbacks); | |
| 587 match_times_[request_id] = base::TimeTicks::Now(); | |
| 588 Send(new ServiceWorkerHostMsg_CacheStorageMatch( | |
| 589 CurrentWorkerId(), request_id, origin, | |
| 590 FetchRequestFromWebRequest(request), | |
| 591 QueryParamsFromWebQueryParams(query_params))); | |
| 592 } | |
| 593 | |
| 594 void ServiceWorkerCacheStorageDispatcher::dispatchMatchForCache( | |
| 595 int cache_id, | |
| 596 blink::WebServiceWorkerCache::CacheMatchCallbacks* callbacks, | |
| 597 const blink::WebServiceWorkerRequest& request, | |
| 598 const blink::WebServiceWorkerCache::QueryParams& query_params) { | |
| 599 int request_id = cache_match_callbacks_.Add(callbacks); | |
| 600 cache_match_times_[request_id] = base::TimeTicks::Now(); | |
| 601 | |
| 602 Send(new ServiceWorkerHostMsg_CacheMatch( | |
| 603 CurrentWorkerId(), request_id, cache_id, | |
| 604 FetchRequestFromWebRequest(request), | |
| 605 QueryParamsFromWebQueryParams(query_params))); | |
| 606 } | |
| 607 | |
| 608 void ServiceWorkerCacheStorageDispatcher::dispatchMatchAllForCache( | |
| 609 int cache_id, | |
| 610 blink::WebServiceWorkerCache::CacheWithResponsesCallbacks* callbacks, | |
| 611 const blink::WebServiceWorkerRequest& request, | |
| 612 const blink::WebServiceWorkerCache::QueryParams& query_params) { | |
| 613 int request_id = cache_match_all_callbacks_.Add(callbacks); | |
| 614 cache_match_all_times_[request_id] = base::TimeTicks::Now(); | |
| 615 | |
| 616 Send(new ServiceWorkerHostMsg_CacheMatchAll( | |
| 617 CurrentWorkerId(), request_id, cache_id, | |
| 618 FetchRequestFromWebRequest(request), | |
| 619 QueryParamsFromWebQueryParams(query_params))); | |
| 620 } | |
| 621 | |
| 622 void ServiceWorkerCacheStorageDispatcher::dispatchKeysForCache( | |
| 623 int cache_id, | |
| 624 blink::WebServiceWorkerCache::CacheWithRequestsCallbacks* callbacks, | |
| 625 const blink::WebServiceWorkerRequest* request, | |
| 626 const blink::WebServiceWorkerCache::QueryParams& query_params) { | |
| 627 int request_id = cache_keys_callbacks_.Add(callbacks); | |
| 628 cache_keys_times_[request_id] = base::TimeTicks::Now(); | |
| 629 | |
| 630 Send(new ServiceWorkerHostMsg_CacheKeys( | |
| 631 CurrentWorkerId(), request_id, cache_id, | |
| 632 request ? FetchRequestFromWebRequest(*request) | |
| 633 : ServiceWorkerFetchRequest(), | |
| 634 QueryParamsFromWebQueryParams(query_params))); | |
| 635 } | |
| 636 | |
| 637 void ServiceWorkerCacheStorageDispatcher::dispatchBatchForCache( | |
| 638 int cache_id, | |
| 639 blink::WebServiceWorkerCache::CacheWithResponsesCallbacks* callbacks, | |
| 640 const blink::WebVector< | |
| 641 blink::WebServiceWorkerCache::BatchOperation>& web_operations) { | |
| 642 int request_id = cache_batch_callbacks_.Add(callbacks); | |
| 643 cache_batch_times_[request_id] = base::TimeTicks::Now(); | |
| 644 | |
| 645 std::vector<ServiceWorkerBatchOperation> operations; | |
| 646 operations.reserve(web_operations.size()); | |
| 647 for (size_t i = 0; i < web_operations.size(); ++i) { | |
| 648 operations.push_back( | |
| 649 BatchOperationFromWebBatchOperation(web_operations[i])); | |
| 650 } | |
| 651 | |
| 652 Send(new ServiceWorkerHostMsg_CacheBatch(CurrentWorkerId(), request_id, | |
| 653 cache_id, operations)); | |
| 654 } | |
| 655 | |
| 656 void ServiceWorkerCacheStorageDispatcher::OnWebCacheDestruction(int cache_id) { | |
| 657 web_caches_.Remove(cache_id); | |
| 658 Send(new ServiceWorkerHostMsg_CacheClosed(cache_id)); | |
| 659 } | |
| 660 | |
| 661 void ServiceWorkerCacheStorageDispatcher::PopulateWebResponseFromResponse( | |
| 662 const ServiceWorkerResponse& response, | |
| 663 blink::WebServiceWorkerResponse* web_response) { | |
| 664 web_response->setURL(response.url); | |
| 665 web_response->setStatus(response.status_code); | |
| 666 web_response->setStatusText(base::ASCIIToUTF16(response.status_text)); | |
| 667 web_response->setResponseType(response.response_type); | |
| 668 | |
| 669 for (const auto& i : response.headers) { | |
| 670 web_response->setHeader(base::ASCIIToUTF16(i.first), | |
| 671 base::ASCIIToUTF16(i.second)); | |
| 672 } | |
| 673 | |
| 674 if (!response.blob_uuid.empty()) { | |
| 675 web_response->setBlob(blink::WebString::fromUTF8(response.blob_uuid), | |
| 676 response.blob_size); | |
| 677 // Let the host know that it can release its reference to the blob. | |
| 678 Send(new ServiceWorkerHostMsg_BlobDataHandled(response.blob_uuid)); | |
| 679 } | |
| 680 } | |
| 681 | |
| 682 blink::WebVector<blink::WebServiceWorkerResponse> | |
| 683 ServiceWorkerCacheStorageDispatcher::WebResponsesFromResponses( | |
| 684 const std::vector<ServiceWorkerResponse>& responses) { | |
| 685 blink::WebVector<blink::WebServiceWorkerResponse> web_responses( | |
| 686 responses.size()); | |
| 687 for (size_t i = 0; i < responses.size(); ++i) | |
| 688 PopulateWebResponseFromResponse(responses[i], &(web_responses[i])); | |
| 689 return web_responses; | |
| 690 } | |
| 691 | |
| 692 } // namespace content | |
| OLD | NEW |