OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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_cache.h" | 5 #include "content/browser/cache_storage/cache_storage_cache.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/barrier_closure.h" | 11 #include "base/barrier_closure.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/guid.h" | 13 #include "base/guid.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
16 #include "base/strings/string_split.h" | 16 #include "base/strings/string_split.h" |
17 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| 18 #include "base/thread_task_runner_handle.h" |
18 #include "content/browser/cache_storage/cache_storage.pb.h" | 19 #include "content/browser/cache_storage/cache_storage.pb.h" |
19 #include "content/browser/cache_storage/cache_storage_blob_to_disk_cache.h" | 20 #include "content/browser/cache_storage/cache_storage_blob_to_disk_cache.h" |
20 #include "content/browser/cache_storage/cache_storage_scheduler.h" | 21 #include "content/browser/cache_storage/cache_storage_scheduler.h" |
21 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
22 #include "content/public/common/referrer.h" | 23 #include "content/public/common/referrer.h" |
23 #include "net/base/completion_callback.h" | 24 #include "net/base/completion_callback.h" |
24 #include "net/base/io_buffer.h" | 25 #include "net/base/io_buffer.h" |
25 #include "net/base/net_errors.h" | 26 #include "net/base/net_errors.h" |
26 #include "net/disk_cache/disk_cache.h" | 27 #include "net/disk_cache/disk_cache.h" |
27 #include "net/url_request/url_request_context_getter.h" | 28 #include "net/url_request/url_request_context_getter.h" |
(...skipping 23 matching lines...) Expand all Loading... |
51 scoped_refptr<CacheStorageCache> cache_; | 52 scoped_refptr<CacheStorageCache> cache_; |
52 disk_cache::ScopedEntryPtr entry_; | 53 disk_cache::ScopedEntryPtr entry_; |
53 | 54 |
54 DISALLOW_COPY_AND_ASSIGN(CacheStorageCacheDataHandle); | 55 DISALLOW_COPY_AND_ASSIGN(CacheStorageCacheDataHandle); |
55 }; | 56 }; |
56 | 57 |
57 typedef base::Callback<void(scoped_ptr<CacheMetadata>)> MetadataCallback; | 58 typedef base::Callback<void(scoped_ptr<CacheMetadata>)> MetadataCallback; |
58 | 59 |
59 enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY }; | 60 enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY }; |
60 | 61 |
61 // The maximum size of an individual cache. Ultimately cache size is controlled | 62 // The maximum size of each cache. Ultimately, cache size |
62 // per-origin. | 63 // is controlled per-origin by the QuotaManager. |
63 const int kMaxCacheBytes = 512 * 1024 * 1024; | 64 const int kMaxCacheBytes = std::numeric_limits<int>::max(); |
64 | 65 |
65 void NotReachedCompletionCallback(int rv) { | 66 void NotReachedCompletionCallback(int rv) { |
66 NOTREACHED(); | 67 NOTREACHED(); |
67 } | 68 } |
68 | 69 |
69 blink::WebServiceWorkerResponseType ProtoResponseTypeToWebResponseType( | 70 blink::WebServiceWorkerResponseType ProtoResponseTypeToWebResponseType( |
70 CacheResponse::ResponseType response_type) { | 71 CacheResponse::ResponseType response_type) { |
71 switch (response_type) { | 72 switch (response_type) { |
72 case CacheResponse::BASIC_TYPE: | 73 case CacheResponse::BASIC_TYPE: |
73 return blink::WebServiceWorkerResponseTypeBasic; | 74 return blink::WebServiceWorkerResponseTypeBasic; |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 | 270 |
270 // Input parameters to the Put function. | 271 // Input parameters to the Put function. |
271 GURL origin; | 272 GURL origin; |
272 scoped_ptr<ServiceWorkerFetchRequest> request; | 273 scoped_ptr<ServiceWorkerFetchRequest> request; |
273 scoped_ptr<ServiceWorkerResponse> response; | 274 scoped_ptr<ServiceWorkerResponse> response; |
274 scoped_ptr<storage::BlobDataHandle> blob_data_handle; | 275 scoped_ptr<storage::BlobDataHandle> blob_data_handle; |
275 CacheStorageCache::ErrorCallback callback; | 276 CacheStorageCache::ErrorCallback callback; |
276 scoped_refptr<net::URLRequestContextGetter> request_context_getter; | 277 scoped_refptr<net::URLRequestContextGetter> request_context_getter; |
277 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy; | 278 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy; |
278 disk_cache::ScopedEntryPtr cache_entry; | 279 disk_cache::ScopedEntryPtr cache_entry; |
| 280 int64_t available_bytes = 0; |
279 | 281 |
280 private: | 282 private: |
281 DISALLOW_COPY_AND_ASSIGN(PutContext); | 283 DISALLOW_COPY_AND_ASSIGN(PutContext); |
282 }; | 284 }; |
283 | 285 |
284 // static | 286 // static |
285 scoped_refptr<CacheStorageCache> CacheStorageCache::CreateMemoryCache( | 287 scoped_refptr<CacheStorageCache> CacheStorageCache::CreateMemoryCache( |
286 const GURL& origin, | 288 const GURL& origin, |
287 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, | 289 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
288 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 290 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
(...skipping 498 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
787 base::Passed(std::move(put_context)))); | 789 base::Passed(std::move(put_context)))); |
788 } | 790 } |
789 | 791 |
790 void CacheStorageCache::PutDidDelete(scoped_ptr<PutContext> put_context, | 792 void CacheStorageCache::PutDidDelete(scoped_ptr<PutContext> put_context, |
791 CacheStorageError delete_error) { | 793 CacheStorageError delete_error) { |
792 if (backend_state_ != BACKEND_OPEN) { | 794 if (backend_state_ != BACKEND_OPEN) { |
793 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); | 795 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); |
794 return; | 796 return; |
795 } | 797 } |
796 | 798 |
| 799 quota_manager_proxy_->GetUsageAndQuota( |
| 800 base::ThreadTaskRunnerHandle::Get().get(), origin_, |
| 801 storage::kStorageTypeTemporary, |
| 802 base::Bind(&CacheStorageCache::PutDidGetUsageAndQuota, |
| 803 weak_ptr_factory_.GetWeakPtr(), |
| 804 base::Passed(std::move(put_context)))); |
| 805 } |
| 806 |
| 807 void CacheStorageCache::PutDidGetUsageAndQuota( |
| 808 scoped_ptr<PutContext> put_context, |
| 809 storage::QuotaStatusCode status_code, |
| 810 int64_t usage, |
| 811 int64_t quota) { |
| 812 if (backend_state_ != BACKEND_OPEN) { |
| 813 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); |
| 814 return; |
| 815 } |
| 816 |
| 817 if (status_code != storage::kQuotaStatusOk) { |
| 818 put_context->callback.Run(CACHE_STORAGE_ERROR_QUOTA_EXCEEDED); |
| 819 return; |
| 820 } |
| 821 |
| 822 put_context->available_bytes = quota - usage; |
| 823 |
797 scoped_ptr<disk_cache::Entry*> scoped_entry_ptr(new disk_cache::Entry*()); | 824 scoped_ptr<disk_cache::Entry*> scoped_entry_ptr(new disk_cache::Entry*()); |
798 disk_cache::Entry** entry_ptr = scoped_entry_ptr.get(); | 825 disk_cache::Entry** entry_ptr = scoped_entry_ptr.get(); |
799 ServiceWorkerFetchRequest* request_ptr = put_context->request.get(); | 826 ServiceWorkerFetchRequest* request_ptr = put_context->request.get(); |
800 disk_cache::Backend* backend_ptr = backend_.get(); | 827 disk_cache::Backend* backend_ptr = backend_.get(); |
801 | 828 |
802 net::CompletionCallback create_entry_callback = base::Bind( | 829 net::CompletionCallback create_entry_callback = base::Bind( |
803 &CacheStorageCache::PutDidCreateEntry, weak_ptr_factory_.GetWeakPtr(), | 830 &CacheStorageCache::PutDidCreateEntry, weak_ptr_factory_.GetWeakPtr(), |
804 base::Passed(std::move(scoped_entry_ptr)), | 831 base::Passed(std::move(scoped_entry_ptr)), |
805 base::Passed(std::move(put_context))); | 832 base::Passed(std::move(put_context))); |
806 | 833 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
853 | 880 |
854 scoped_ptr<std::string> serialized(new std::string()); | 881 scoped_ptr<std::string> serialized(new std::string()); |
855 if (!metadata.SerializeToString(serialized.get())) { | 882 if (!metadata.SerializeToString(serialized.get())) { |
856 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); | 883 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); |
857 return; | 884 return; |
858 } | 885 } |
859 | 886 |
860 scoped_refptr<net::StringIOBuffer> buffer( | 887 scoped_refptr<net::StringIOBuffer> buffer( |
861 new net::StringIOBuffer(std::move(serialized))); | 888 new net::StringIOBuffer(std::move(serialized))); |
862 | 889 |
| 890 int64_t bytes_to_write = buffer->size() + put_context->response->blob_size; |
| 891 if (put_context->available_bytes < bytes_to_write) { |
| 892 put_context->callback.Run(CACHE_STORAGE_ERROR_QUOTA_EXCEEDED); |
| 893 return; |
| 894 } |
| 895 |
863 // Get a temporary copy of the entry pointer before passing it in base::Bind. | 896 // Get a temporary copy of the entry pointer before passing it in base::Bind. |
864 disk_cache::Entry* temp_entry_ptr = put_context->cache_entry.get(); | 897 disk_cache::Entry* temp_entry_ptr = put_context->cache_entry.get(); |
865 | 898 |
866 net::CompletionCallback write_headers_callback = base::Bind( | 899 net::CompletionCallback write_headers_callback = base::Bind( |
867 &CacheStorageCache::PutDidWriteHeaders, weak_ptr_factory_.GetWeakPtr(), | 900 &CacheStorageCache::PutDidWriteHeaders, weak_ptr_factory_.GetWeakPtr(), |
868 base::Passed(std::move(put_context)), buffer->size()); | 901 base::Passed(std::move(put_context)), buffer->size()); |
869 | 902 |
870 rv = temp_entry_ptr->WriteData(INDEX_HEADERS, 0 /* offset */, buffer.get(), | 903 rv = temp_entry_ptr->WriteData(INDEX_HEADERS, 0 /* offset */, buffer.get(), |
871 buffer->size(), write_headers_callback, | 904 buffer->size(), write_headers_callback, |
872 true /* truncate */); | 905 true /* truncate */); |
(...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1256 storage::BlobDataBuilder blob_data(response->blob_uuid); | 1289 storage::BlobDataBuilder blob_data(response->blob_uuid); |
1257 | 1290 |
1258 disk_cache::Entry* temp_entry = entry.get(); | 1291 disk_cache::Entry* temp_entry = entry.get(); |
1259 blob_data.AppendDiskCacheEntry( | 1292 blob_data.AppendDiskCacheEntry( |
1260 new CacheStorageCacheDataHandle(this, std::move(entry)), temp_entry, | 1293 new CacheStorageCacheDataHandle(this, std::move(entry)), temp_entry, |
1261 INDEX_RESPONSE_BODY); | 1294 INDEX_RESPONSE_BODY); |
1262 return blob_storage_context_->AddFinishedBlob(&blob_data); | 1295 return blob_storage_context_->AddFinishedBlob(&blob_data); |
1263 } | 1296 } |
1264 | 1297 |
1265 } // namespace content | 1298 } // namespace content |
OLD | NEW |