Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/cache_storage/cache_storage_dispatcher_host.h" | 5 #include "content/browser/cache_storage/cache_storage_dispatcher_host.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/stl_util.h" | |
| 13 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" | |
| 17 #include "base/time/time.h" | |
| 15 #include "base/trace_event/trace_event.h" | 18 #include "base/trace_event/trace_event.h" |
| 16 #include "content/browser/bad_message.h" | 19 #include "content/browser/bad_message.h" |
| 17 #include "content/browser/cache_storage/cache_storage_cache.h" | 20 #include "content/browser/cache_storage/cache_storage_cache.h" |
| 18 #include "content/browser/cache_storage/cache_storage_cache_handle.h" | 21 #include "content/browser/cache_storage/cache_storage_cache_handle.h" |
| 19 #include "content/browser/cache_storage/cache_storage_context_impl.h" | 22 #include "content/browser/cache_storage/cache_storage_context_impl.h" |
| 20 #include "content/browser/cache_storage/cache_storage_manager.h" | 23 #include "content/browser/cache_storage/cache_storage_manager.h" |
| 21 #include "content/common/cache_storage/cache_storage_messages.h" | 24 #include "content/common/cache_storage/cache_storage_messages.h" |
| 22 #include "content/public/browser/content_browser_client.h" | 25 #include "content/public/browser/content_browser_client.h" |
| 23 #include "content/public/common/origin_util.h" | 26 #include "content/public/common/origin_util.h" |
| 24 #include "storage/browser/blob/blob_data_handle.h" | 27 #include "storage/browser/blob/blob_data_handle.h" |
| 25 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWor kerCacheError.h" | 28 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWor kerCacheError.h" |
| 26 #include "url/gurl.h" | 29 #include "url/gurl.h" |
| 27 #include "url/origin.h" | 30 #include "url/origin.h" |
| 28 | 31 |
| 29 namespace content { | 32 namespace content { |
| 30 | 33 |
| 31 namespace { | 34 namespace { |
| 32 | 35 |
| 33 const uint32_t kFilteredMessageClasses[] = {CacheStorageMsgStart}; | 36 const uint32_t kFilteredMessageClasses[] = {CacheStorageMsgStart}; |
| 37 const int32_t kCachePreservationSeconds = 5; | |
| 34 | 38 |
| 35 blink::WebServiceWorkerCacheError ToWebServiceWorkerCacheError( | 39 blink::WebServiceWorkerCacheError ToWebServiceWorkerCacheError( |
| 36 CacheStorageError err) { | 40 CacheStorageError err) { |
| 37 switch (err) { | 41 switch (err) { |
| 38 case CACHE_STORAGE_OK: | 42 case CACHE_STORAGE_OK: |
| 39 NOTREACHED(); | 43 NOTREACHED(); |
| 40 return blink::WebServiceWorkerCacheErrorNotImplemented; | 44 return blink::WebServiceWorkerCacheErrorNotImplemented; |
| 41 case CACHE_STORAGE_ERROR_EXISTS: | 45 case CACHE_STORAGE_ERROR_EXISTS: |
| 42 return blink::WebServiceWorkerCacheErrorExists; | 46 return blink::WebServiceWorkerCacheErrorExists; |
| 43 case CACHE_STORAGE_ERROR_STORAGE: | 47 case CACHE_STORAGE_ERROR_STORAGE: |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 56 } | 60 } |
| 57 | 61 |
| 58 bool OriginCanAccessCacheStorage(const url::Origin& origin) { | 62 bool OriginCanAccessCacheStorage(const url::Origin& origin) { |
| 59 return !origin.unique() && IsOriginSecure(GURL(origin.Serialize())); | 63 return !origin.unique() && IsOriginSecure(GURL(origin.Serialize())); |
| 60 } | 64 } |
| 61 | 65 |
| 62 } // namespace | 66 } // namespace |
| 63 | 67 |
| 64 CacheStorageDispatcherHost::CacheStorageDispatcherHost() | 68 CacheStorageDispatcherHost::CacheStorageDispatcherHost() |
| 65 : BrowserMessageFilter(kFilteredMessageClasses, | 69 : BrowserMessageFilter(kFilteredMessageClasses, |
| 66 arraysize(kFilteredMessageClasses)) { | 70 arraysize(kFilteredMessageClasses)) {} |
| 67 } | |
| 68 | 71 |
| 69 CacheStorageDispatcherHost::~CacheStorageDispatcherHost() { | 72 CacheStorageDispatcherHost::~CacheStorageDispatcherHost() { |
| 70 } | 73 } |
| 71 | 74 |
| 72 void CacheStorageDispatcherHost::Init(CacheStorageContextImpl* context) { | 75 void CacheStorageDispatcherHost::Init(CacheStorageContextImpl* context) { |
| 73 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 76 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 74 BrowserThread::PostTask( | 77 BrowserThread::PostTask( |
| 75 BrowserThread::IO, FROM_HERE, | 78 BrowserThread::IO, FROM_HERE, |
| 76 base::Bind(&CacheStorageDispatcherHost::CreateCacheListener, this, | 79 base::Bind(&CacheStorageDispatcherHost::CreateCacheListener, this, |
| 77 base::RetainedRef(context))); | 80 base::RetainedRef(context))); |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 347 void CacheStorageDispatcherHost::OnCacheStorageOpenCallback( | 350 void CacheStorageDispatcherHost::OnCacheStorageOpenCallback( |
| 348 int thread_id, | 351 int thread_id, |
| 349 int request_id, | 352 int request_id, |
| 350 std::unique_ptr<CacheStorageCacheHandle> cache_handle, | 353 std::unique_ptr<CacheStorageCacheHandle> cache_handle, |
| 351 CacheStorageError error) { | 354 CacheStorageError error) { |
| 352 if (error != CACHE_STORAGE_OK) { | 355 if (error != CACHE_STORAGE_OK) { |
| 353 Send(new CacheStorageMsg_CacheStorageOpenError( | 356 Send(new CacheStorageMsg_CacheStorageOpenError( |
| 354 thread_id, request_id, ToWebServiceWorkerCacheError(error))); | 357 thread_id, request_id, ToWebServiceWorkerCacheError(error))); |
| 355 return; | 358 return; |
| 356 } | 359 } |
| 360 | |
| 361 // Hang on to the cache for a few seconds (if we aren't already doing so). | |
| 362 // This way if the user quickly closes and reopens it the next open operation | |
| 363 // will be fast. | |
| 364 if (!ContainsKey(preserved_cache_handles_, cache_handle->value())) { | |
| 365 preserved_cache_handles_[cache_handle->value()] = cache_handle->Clone(); | |
| 366 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
| 367 FROM_HERE, base::Bind(&CacheStorageDispatcherHost::StopPreservingCache, | |
| 368 this, cache_handle->value()), | |
|
nhiroki
2016/06/29 09:15:39
This may extend the lifetime of MessageFilter. Alt
jkarlin
2016/06/29 12:52:53
Actually, there isn't even a need to run the callb
| |
| 369 base::TimeDelta::FromSeconds(kCachePreservationSeconds)); | |
| 370 } | |
| 371 | |
| 357 CacheID cache_id = StoreCacheReference(std::move(cache_handle)); | 372 CacheID cache_id = StoreCacheReference(std::move(cache_handle)); |
| 358 Send(new CacheStorageMsg_CacheStorageOpenSuccess(thread_id, request_id, | 373 Send(new CacheStorageMsg_CacheStorageOpenSuccess(thread_id, request_id, |
| 359 cache_id)); | 374 cache_id)); |
| 360 } | 375 } |
| 361 | 376 |
| 362 void CacheStorageDispatcherHost::OnCacheStorageDeleteCallback( | 377 void CacheStorageDispatcherHost::OnCacheStorageDeleteCallback( |
| 363 int thread_id, | 378 int thread_id, |
| 364 int request_id, | 379 int request_id, |
| 365 bool deleted, | 380 bool deleted, |
| 366 CacheStorageError error) { | 381 CacheStorageError error) { |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 501 CacheStorageError error) { | 516 CacheStorageError error) { |
| 502 if (error != CACHE_STORAGE_OK) { | 517 if (error != CACHE_STORAGE_OK) { |
| 503 Send(new CacheStorageMsg_CacheBatchError( | 518 Send(new CacheStorageMsg_CacheBatchError( |
| 504 thread_id, request_id, ToWebServiceWorkerCacheError(error))); | 519 thread_id, request_id, ToWebServiceWorkerCacheError(error))); |
| 505 return; | 520 return; |
| 506 } | 521 } |
| 507 | 522 |
| 508 Send(new CacheStorageMsg_CacheBatchSuccess(thread_id, request_id)); | 523 Send(new CacheStorageMsg_CacheBatchSuccess(thread_id, request_id)); |
| 509 } | 524 } |
| 510 | 525 |
| 526 void CacheStorageDispatcherHost::StopPreservingCache( | |
| 527 const CacheStorageCache* cache) { | |
| 528 DCHECK(ContainsKey(preserved_cache_handles_, cache)); | |
| 529 preserved_cache_handles_.erase(cache); | |
| 530 } | |
| 531 | |
| 511 CacheStorageDispatcherHost::CacheID | 532 CacheStorageDispatcherHost::CacheID |
| 512 CacheStorageDispatcherHost::StoreCacheReference( | 533 CacheStorageDispatcherHost::StoreCacheReference( |
| 513 std::unique_ptr<CacheStorageCacheHandle> cache_handle) { | 534 std::unique_ptr<CacheStorageCacheHandle> cache_handle) { |
| 514 int cache_id = next_cache_id_++; | 535 int cache_id = next_cache_id_++; |
| 515 id_to_cache_map_[cache_id] = std::move(cache_handle); | 536 id_to_cache_map_[cache_id] = std::move(cache_handle); |
| 516 return cache_id; | 537 return cache_id; |
| 517 } | 538 } |
| 518 | 539 |
| 519 void CacheStorageDispatcherHost::DropCacheReference(CacheID cache_id) { | 540 void CacheStorageDispatcherHost::DropCacheReference(CacheID cache_id) { |
| 520 id_to_cache_map_.erase(cache_id); | 541 id_to_cache_map_.erase(cache_id); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 532 UUIDToBlobDataHandleList::iterator it = blob_handle_store_.find(uuid); | 553 UUIDToBlobDataHandleList::iterator it = blob_handle_store_.find(uuid); |
| 533 if (it == blob_handle_store_.end()) | 554 if (it == blob_handle_store_.end()) |
| 534 return; | 555 return; |
| 535 DCHECK(!it->second.empty()); | 556 DCHECK(!it->second.empty()); |
| 536 it->second.pop_front(); | 557 it->second.pop_front(); |
| 537 if (it->second.empty()) | 558 if (it->second.empty()) |
| 538 blob_handle_store_.erase(it); | 559 blob_handle_store_.erase(it); |
| 539 } | 560 } |
| 540 | 561 |
| 541 } // namespace content | 562 } // namespace content |
| OLD | NEW |