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 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
258 scoped_ptr<storage::BlobDataHandle> blob_data_handle, | 259 scoped_ptr<storage::BlobDataHandle> blob_data_handle, |
259 const CacheStorageCache::ErrorCallback& callback, | 260 const CacheStorageCache::ErrorCallback& callback, |
260 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, | 261 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
261 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy) | 262 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy) |
262 : origin(origin), | 263 : origin(origin), |
263 request(std::move(request)), | 264 request(std::move(request)), |
264 response(std::move(response)), | 265 response(std::move(response)), |
265 blob_data_handle(std::move(blob_data_handle)), | 266 blob_data_handle(std::move(blob_data_handle)), |
266 callback(callback), | 267 callback(callback), |
267 request_context_getter(request_context_getter), | 268 request_context_getter(request_context_getter), |
268 quota_manager_proxy(quota_manager_proxy) {} | 269 quota_manager_proxy(quota_manager_proxy), |
270 available_bytes(0) {} | |
269 | 271 |
270 // Input parameters to the Put function. | 272 // Input parameters to the Put function. |
271 GURL origin; | 273 GURL origin; |
272 scoped_ptr<ServiceWorkerFetchRequest> request; | 274 scoped_ptr<ServiceWorkerFetchRequest> request; |
273 scoped_ptr<ServiceWorkerResponse> response; | 275 scoped_ptr<ServiceWorkerResponse> response; |
274 scoped_ptr<storage::BlobDataHandle> blob_data_handle; | 276 scoped_ptr<storage::BlobDataHandle> blob_data_handle; |
275 CacheStorageCache::ErrorCallback callback; | 277 CacheStorageCache::ErrorCallback callback; |
276 scoped_refptr<net::URLRequestContextGetter> request_context_getter; | 278 scoped_refptr<net::URLRequestContextGetter> request_context_getter; |
277 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy; | 279 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy; |
278 disk_cache::ScopedEntryPtr cache_entry; | 280 disk_cache::ScopedEntryPtr cache_entry; |
281 int64_t available_bytes; | |
michaeln
2016/01/26 00:16:08
could use = 0 here instead of adding the the initi
jkarlin
2016/01/26 12:08:35
Done.
| |
279 | 282 |
280 private: | 283 private: |
281 DISALLOW_COPY_AND_ASSIGN(PutContext); | 284 DISALLOW_COPY_AND_ASSIGN(PutContext); |
282 }; | 285 }; |
283 | 286 |
284 // static | 287 // static |
285 scoped_refptr<CacheStorageCache> CacheStorageCache::CreateMemoryCache( | 288 scoped_refptr<CacheStorageCache> CacheStorageCache::CreateMemoryCache( |
286 const GURL& origin, | 289 const GURL& origin, |
287 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, | 290 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter, |
288 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, | 291 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)))); | 790 base::Passed(std::move(put_context)))); |
788 } | 791 } |
789 | 792 |
790 void CacheStorageCache::PutDidDelete(scoped_ptr<PutContext> put_context, | 793 void CacheStorageCache::PutDidDelete(scoped_ptr<PutContext> put_context, |
791 CacheStorageError delete_error) { | 794 CacheStorageError delete_error) { |
792 if (backend_state_ != BACKEND_OPEN) { | 795 if (backend_state_ != BACKEND_OPEN) { |
793 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); | 796 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); |
794 return; | 797 return; |
795 } | 798 } |
796 | 799 |
800 quota_manager_proxy_->GetUsageAndQuota( | |
801 base::ThreadTaskRunnerHandle::Get().get(), put_context->origin, | |
802 storage::kStorageTypeTemporary, | |
803 base::Bind(&CacheStorageCache::PutDidGetUsageAndQuota, | |
804 weak_ptr_factory_.GetWeakPtr(), | |
805 base::Passed(std::move(put_context)))); | |
806 } | |
807 | |
808 void CacheStorageCache::PutDidGetUsageAndQuota( | |
809 scoped_ptr<PutContext> put_context, | |
810 storage::QuotaStatusCode status_code, | |
811 int64_t usage, | |
812 int64_t quota) { | |
813 if (backend_state_ != BACKEND_OPEN) { | |
814 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); | |
815 return; | |
816 } | |
817 | |
818 if (status_code != storage::kQuotaStatusOk) { | |
819 put_context->callback.Run(CACHE_STORAGE_ERROR_QUOTA_EXCEEDED); | |
820 return; | |
821 } | |
822 | |
823 put_context->available_bytes = quota - usage; | |
824 | |
797 scoped_ptr<disk_cache::Entry*> scoped_entry_ptr(new disk_cache::Entry*()); | 825 scoped_ptr<disk_cache::Entry*> scoped_entry_ptr(new disk_cache::Entry*()); |
798 disk_cache::Entry** entry_ptr = scoped_entry_ptr.get(); | 826 disk_cache::Entry** entry_ptr = scoped_entry_ptr.get(); |
799 ServiceWorkerFetchRequest* request_ptr = put_context->request.get(); | 827 ServiceWorkerFetchRequest* request_ptr = put_context->request.get(); |
800 disk_cache::Backend* backend_ptr = backend_.get(); | 828 disk_cache::Backend* backend_ptr = backend_.get(); |
801 | 829 |
802 net::CompletionCallback create_entry_callback = base::Bind( | 830 net::CompletionCallback create_entry_callback = base::Bind( |
803 &CacheStorageCache::PutDidCreateEntry, weak_ptr_factory_.GetWeakPtr(), | 831 &CacheStorageCache::PutDidCreateEntry, weak_ptr_factory_.GetWeakPtr(), |
804 base::Passed(std::move(scoped_entry_ptr)), | 832 base::Passed(std::move(scoped_entry_ptr)), |
805 base::Passed(std::move(put_context))); | 833 base::Passed(std::move(put_context))); |
806 | 834 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
853 | 881 |
854 scoped_ptr<std::string> serialized(new std::string()); | 882 scoped_ptr<std::string> serialized(new std::string()); |
855 if (!metadata.SerializeToString(serialized.get())) { | 883 if (!metadata.SerializeToString(serialized.get())) { |
856 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); | 884 put_context->callback.Run(CACHE_STORAGE_ERROR_STORAGE); |
857 return; | 885 return; |
858 } | 886 } |
859 | 887 |
860 scoped_refptr<net::StringIOBuffer> buffer( | 888 scoped_refptr<net::StringIOBuffer> buffer( |
861 new net::StringIOBuffer(std::move(serialized))); | 889 new net::StringIOBuffer(std::move(serialized))); |
862 | 890 |
891 int64_t bytes_to_write = buffer->size() + put_context->response->blob_size; | |
892 if (put_context->available_bytes < bytes_to_write) { | |
893 put_context->callback.Run(CACHE_STORAGE_ERROR_QUOTA_EXCEEDED); | |
894 return; | |
895 } | |
896 | |
863 // Get a temporary copy of the entry pointer before passing it in base::Bind. | 897 // 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(); | 898 disk_cache::Entry* temp_entry_ptr = put_context->cache_entry.get(); |
865 | 899 |
866 net::CompletionCallback write_headers_callback = base::Bind( | 900 net::CompletionCallback write_headers_callback = base::Bind( |
867 &CacheStorageCache::PutDidWriteHeaders, weak_ptr_factory_.GetWeakPtr(), | 901 &CacheStorageCache::PutDidWriteHeaders, weak_ptr_factory_.GetWeakPtr(), |
868 base::Passed(std::move(put_context)), buffer->size()); | 902 base::Passed(std::move(put_context)), buffer->size()); |
869 | 903 |
870 rv = temp_entry_ptr->WriteData(INDEX_HEADERS, 0 /* offset */, buffer.get(), | 904 rv = temp_entry_ptr->WriteData(INDEX_HEADERS, 0 /* offset */, buffer.get(), |
871 buffer->size(), write_headers_callback, | 905 buffer->size(), write_headers_callback, |
872 true /* truncate */); | 906 true /* truncate */); |
(...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1256 storage::BlobDataBuilder blob_data(response->blob_uuid); | 1290 storage::BlobDataBuilder blob_data(response->blob_uuid); |
1257 | 1291 |
1258 disk_cache::Entry* temp_entry = entry.get(); | 1292 disk_cache::Entry* temp_entry = entry.get(); |
1259 blob_data.AppendDiskCacheEntry( | 1293 blob_data.AppendDiskCacheEntry( |
1260 new CacheStorageCacheDataHandle(this, std::move(entry)), temp_entry, | 1294 new CacheStorageCacheDataHandle(this, std::move(entry)), temp_entry, |
1261 INDEX_RESPONSE_BODY); | 1295 INDEX_RESPONSE_BODY); |
1262 return blob_storage_context_->AddFinishedBlob(&blob_data); | 1296 return blob_storage_context_->AddFinishedBlob(&blob_data); |
1263 } | 1297 } |
1264 | 1298 |
1265 } // namespace content | 1299 } // namespace content |
OLD | NEW |