Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: content/browser/cache_storage/cache_storage.cc

Issue 1174943004: [CacheStorage] Use URLRequestContextGetter instead of URLRequestContext (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments from PS4 Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.h" 5 #include "content/browser/cache_storage/cache_storage.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/barrier_closure.h" 9 #include "base/barrier_closure.h"
10 #include "base/files/file_util.h" 10 #include "base/files/file_util.h"
11 #include "base/files/memory_mapped_file.h" 11 #include "base/files/memory_mapped_file.h"
12 #include "base/location.h" 12 #include "base/location.h"
13 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
14 #include "base/metrics/histogram_macros.h" 14 #include "base/metrics/histogram_macros.h"
15 #include "base/sha1.h" 15 #include "base/sha1.h"
16 #include "base/single_thread_task_runner.h" 16 #include "base/single_thread_task_runner.h"
17 #include "base/stl_util.h" 17 #include "base/stl_util.h"
18 #include "base/strings/string_number_conversions.h" 18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_util.h" 19 #include "base/strings/string_util.h"
20 #include "base/thread_task_runner_handle.h" 20 #include "base/thread_task_runner_handle.h"
21 #include "content/browser/cache_storage/cache_storage.pb.h" 21 #include "content/browser/cache_storage/cache_storage.pb.h"
22 #include "content/browser/cache_storage/cache_storage_cache.h" 22 #include "content/browser/cache_storage/cache_storage_cache.h"
23 #include "content/browser/cache_storage/cache_storage_scheduler.h" 23 #include "content/browser/cache_storage/cache_storage_scheduler.h"
24 #include "content/public/browser/browser_thread.h" 24 #include "content/public/browser/browser_thread.h"
25 #include "net/base/directory_lister.h" 25 #include "net/base/directory_lister.h"
26 #include "net/base/net_errors.h" 26 #include "net/base/net_errors.h"
27 #include "net/url_request/url_request_context_getter.h"
27 #include "storage/browser/blob/blob_storage_context.h" 28 #include "storage/browser/blob/blob_storage_context.h"
28 #include "storage/browser/quota/quota_manager_proxy.h" 29 #include "storage/browser/quota/quota_manager_proxy.h"
29 30
30 namespace content { 31 namespace content {
31 32
32 namespace { 33 namespace {
33 34
34 void CloseAllCachesDidCloseCache(const scoped_refptr<CacheStorageCache>& cache, 35 void CloseAllCachesDidCloseCache(const scoped_refptr<CacheStorageCache>& cache,
35 const base::Closure& barrier_closure) { 36 const base::Closure& barrier_closure) {
36 barrier_closure.Run(); 37 barrier_closure.Run();
37 } 38 }
38 39
39 } // namespace 40 } // namespace
40 41
41 const char CacheStorage::kIndexFileName[] = "index.txt"; 42 const char CacheStorage::kIndexFileName[] = "index.txt";
42 43
43 // Handles the loading and clean up of CacheStorageCache objects. The 44 // Handles the loading and clean up of CacheStorageCache objects. The
44 // callback of every public method is guaranteed to be called. 45 // callback of every public method is guaranteed to be called.
45 class CacheStorage::CacheLoader { 46 class CacheStorage::CacheLoader {
46 public: 47 public:
47 typedef base::Callback<void(const scoped_refptr<CacheStorageCache>&)> 48 typedef base::Callback<void(const scoped_refptr<CacheStorageCache>&)>
48 CacheCallback; 49 CacheCallback;
49 typedef base::Callback<void(bool)> BoolCallback; 50 typedef base::Callback<void(bool)> BoolCallback;
50 typedef base::Callback<void(scoped_ptr<std::vector<std::string>>)> 51 typedef base::Callback<void(scoped_ptr<std::vector<std::string>>)>
51 StringVectorCallback; 52 StringVectorCallback;
52 53
53 CacheLoader( 54 CacheLoader(
54 base::SequencedTaskRunner* cache_task_runner, 55 base::SequencedTaskRunner* cache_task_runner,
55 net::URLRequestContext* request_context, 56 const scoped_refptr<net::URLRequestContextGetter>& request_context_getter,
56 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, 57 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
57 base::WeakPtr<storage::BlobStorageContext> blob_context, 58 base::WeakPtr<storage::BlobStorageContext> blob_context,
58 const GURL& origin) 59 const GURL& origin)
59 : cache_task_runner_(cache_task_runner), 60 : cache_task_runner_(cache_task_runner),
60 request_context_(request_context), 61 request_context_getter_(request_context_getter),
61 quota_manager_proxy_(quota_manager_proxy), 62 quota_manager_proxy_(quota_manager_proxy),
62 blob_context_(blob_context), 63 blob_context_(blob_context),
63 origin_(origin) { 64 origin_(origin) {
64 DCHECK(!origin_.is_empty()); 65 DCHECK(!origin_.is_empty());
65 } 66 }
66 67
67 virtual ~CacheLoader() {} 68 virtual ~CacheLoader() {}
68 69
69 // Creates a CacheStorageCache with the given name. It does not attempt to 70 // Creates a CacheStorageCache with the given name. It does not attempt to
70 // load the backend, that happens lazily when the cache is used. 71 // load the backend, that happens lazily when the cache is used.
(...skipping 12 matching lines...) Expand all
83 // Writes the cache names (and sizes) to disk if applicable. 84 // Writes the cache names (and sizes) to disk if applicable.
84 virtual void WriteIndex(const StringVector& cache_names, 85 virtual void WriteIndex(const StringVector& cache_names,
85 const BoolCallback& callback) = 0; 86 const BoolCallback& callback) = 0;
86 87
87 // Loads the cache names from disk if applicable. 88 // Loads the cache names from disk if applicable.
88 virtual void LoadIndex(scoped_ptr<std::vector<std::string>> cache_names, 89 virtual void LoadIndex(scoped_ptr<std::vector<std::string>> cache_names,
89 const StringVectorCallback& callback) = 0; 90 const StringVectorCallback& callback) = 0;
90 91
91 protected: 92 protected:
92 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; 93 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_;
93 net::URLRequestContext* request_context_; 94 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
94 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; 95 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_;
95 base::WeakPtr<storage::BlobStorageContext> blob_context_; 96 base::WeakPtr<storage::BlobStorageContext> blob_context_;
96 GURL origin_; 97 GURL origin_;
97 }; 98 };
98 99
99 // Creates memory-only ServiceWorkerCaches. Because these caches have no 100 // Creates memory-only ServiceWorkerCaches. Because these caches have no
100 // persistent storage it is not safe to free them from memory if they might be 101 // persistent storage it is not safe to free them from memory if they might be
101 // used again. Therefore this class holds a reference to each cache until the 102 // used again. Therefore this class holds a reference to each cache until the
102 // cache is deleted. 103 // cache is deleted.
103 class CacheStorage::MemoryLoader : public CacheStorage::CacheLoader { 104 class CacheStorage::MemoryLoader : public CacheStorage::CacheLoader {
104 public: 105 public:
105 MemoryLoader( 106 MemoryLoader(
106 base::SequencedTaskRunner* cache_task_runner, 107 base::SequencedTaskRunner* cache_task_runner,
107 net::URLRequestContext* request_context, 108 const scoped_refptr<net::URLRequestContextGetter>& request_context,
108 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, 109 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
109 base::WeakPtr<storage::BlobStorageContext> blob_context, 110 base::WeakPtr<storage::BlobStorageContext> blob_context,
110 const GURL& origin) 111 const GURL& origin)
111 : CacheLoader(cache_task_runner, 112 : CacheLoader(cache_task_runner,
112 request_context, 113 request_context,
113 quota_manager_proxy, 114 quota_manager_proxy,
114 blob_context, 115 blob_context,
115 origin) {} 116 origin) {}
116 117
117 scoped_refptr<CacheStorageCache> CreateCache( 118 scoped_refptr<CacheStorageCache> CreateCache(
118 const std::string& cache_name) override { 119 const std::string& cache_name) override {
119 return CacheStorageCache::CreateMemoryCache( 120 return CacheStorageCache::CreateMemoryCache(
120 origin_, request_context_, quota_manager_proxy_, blob_context_); 121 origin_, request_context_getter_, quota_manager_proxy_, blob_context_);
121 } 122 }
122 123
123 void CreateCache(const std::string& cache_name, 124 void CreateCache(const std::string& cache_name,
124 const CacheCallback& callback) override { 125 const CacheCallback& callback) override {
125 scoped_refptr<CacheStorageCache> cache = CreateCache(cache_name); 126 scoped_refptr<CacheStorageCache> cache = CreateCache(cache_name);
126 cache_refs_.insert(std::make_pair(cache_name, cache)); 127 cache_refs_.insert(std::make_pair(cache_name, cache));
127 callback.Run(cache); 128 callback.Run(cache);
128 } 129 }
129 130
130 void CleanUpDeletedCache(const std::string& cache_name, 131 void CleanUpDeletedCache(const std::string& cache_name,
(...skipping 22 matching lines...) Expand all
153 // client calls CacheStorage::Delete or the CacheStorage is 154 // client calls CacheStorage::Delete or the CacheStorage is
154 // freed. 155 // freed.
155 CacheRefMap cache_refs_; 156 CacheRefMap cache_refs_;
156 }; 157 };
157 158
158 class CacheStorage::SimpleCacheLoader : public CacheStorage::CacheLoader { 159 class CacheStorage::SimpleCacheLoader : public CacheStorage::CacheLoader {
159 public: 160 public:
160 SimpleCacheLoader( 161 SimpleCacheLoader(
161 const base::FilePath& origin_path, 162 const base::FilePath& origin_path,
162 base::SequencedTaskRunner* cache_task_runner, 163 base::SequencedTaskRunner* cache_task_runner,
163 net::URLRequestContext* request_context, 164 const scoped_refptr<net::URLRequestContextGetter>& request_context,
164 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, 165 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
165 base::WeakPtr<storage::BlobStorageContext> blob_context, 166 base::WeakPtr<storage::BlobStorageContext> blob_context,
166 const GURL& origin) 167 const GURL& origin)
167 : CacheLoader(cache_task_runner, 168 : CacheLoader(cache_task_runner,
168 request_context, 169 request_context,
169 quota_manager_proxy, 170 quota_manager_proxy,
170 blob_context, 171 blob_context,
171 origin), 172 origin),
172 origin_path_(origin_path), 173 origin_path_(origin_path),
173 weak_ptr_factory_(this) {} 174 weak_ptr_factory_(this) {}
174 175
175 scoped_refptr<CacheStorageCache> CreateCache( 176 scoped_refptr<CacheStorageCache> CreateCache(
176 const std::string& cache_name) override { 177 const std::string& cache_name) override {
177 DCHECK_CURRENTLY_ON(BrowserThread::IO); 178 DCHECK_CURRENTLY_ON(BrowserThread::IO);
178 179
179 return CacheStorageCache::CreatePersistentCache( 180 return CacheStorageCache::CreatePersistentCache(
180 origin_, CreatePersistentCachePath(origin_path_, cache_name), 181 origin_, CreatePersistentCachePath(origin_path_, cache_name),
181 request_context_, quota_manager_proxy_, blob_context_); 182 request_context_getter_, quota_manager_proxy_, blob_context_);
182 } 183 }
183 184
184 void CreateCache(const std::string& cache_name, 185 void CreateCache(const std::string& cache_name,
185 const CacheCallback& callback) override { 186 const CacheCallback& callback) override {
186 DCHECK_CURRENTLY_ON(BrowserThread::IO); 187 DCHECK_CURRENTLY_ON(BrowserThread::IO);
187 188
188 // 1. Delete the cache's directory if it exists. 189 // 1. Delete the cache's directory if it exists.
189 // (CreateCacheDeleteFilesInPool) 190 // (CreateCacheDeleteFilesInPool)
190 // 2. Load the cache. (LoadCreateDirectoryInPool) 191 // 2. Load the cache. (LoadCreateDirectoryInPool)
191 192
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
350 351
351 const base::FilePath origin_path_; 352 const base::FilePath origin_path_;
352 353
353 base::WeakPtrFactory<SimpleCacheLoader> weak_ptr_factory_; 354 base::WeakPtrFactory<SimpleCacheLoader> weak_ptr_factory_;
354 }; 355 };
355 356
356 CacheStorage::CacheStorage( 357 CacheStorage::CacheStorage(
357 const base::FilePath& path, 358 const base::FilePath& path,
358 bool memory_only, 359 bool memory_only,
359 base::SequencedTaskRunner* cache_task_runner, 360 base::SequencedTaskRunner* cache_task_runner,
360 net::URLRequestContext* request_context, 361 const scoped_refptr<net::URLRequestContextGetter>& request_context,
361 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, 362 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy,
362 base::WeakPtr<storage::BlobStorageContext> blob_context, 363 base::WeakPtr<storage::BlobStorageContext> blob_context,
363 const GURL& origin) 364 const GURL& origin)
364 : initialized_(false), 365 : initialized_(false),
365 initializing_(false), 366 initializing_(false),
366 scheduler_(new CacheStorageScheduler()), 367 scheduler_(new CacheStorageScheduler()),
367 origin_path_(path), 368 origin_path_(path),
368 cache_task_runner_(cache_task_runner), 369 cache_task_runner_(cache_task_runner),
369 memory_only_(memory_only), 370 memory_only_(memory_only),
370 weak_factory_(this) { 371 weak_factory_(this) {
(...skipping 483 matching lines...) Expand 10 before | Expand all | Expand 10 after
854 scoped_ptr<ServiceWorkerResponse> response, 855 scoped_ptr<ServiceWorkerResponse> response,
855 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { 856 scoped_ptr<storage::BlobDataHandle> blob_data_handle) {
856 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr(); 857 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr();
857 858
858 callback.Run(error, response.Pass(), blob_data_handle.Pass()); 859 callback.Run(error, response.Pass(), blob_data_handle.Pass());
859 if (cache_storage) 860 if (cache_storage)
860 scheduler_->CompleteOperationAndRunNext(); 861 scheduler_->CompleteOperationAndRunNext();
861 } 862 }
862 863
863 } // namespace content 864 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/cache_storage/cache_storage.h ('k') | content/browser/cache_storage/cache_storage_cache.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698