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/service_worker/service_worker_cache_storage.h" | 5 #include "content/browser/service_worker/service_worker_cache_storage.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/files/memory_mapped_file.h" | 10 #include "base/files/memory_mapped_file.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/sha1.h" | 12 #include "base/sha1.h" |
13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "content/browser/service_worker/service_worker_cache.h" | 16 #include "content/browser/service_worker/service_worker_cache.h" |
17 #include "content/browser/service_worker/service_worker_cache.pb.h" | 17 #include "content/browser/service_worker/service_worker_cache.pb.h" |
18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
19 #include "net/base/directory_lister.h" | 19 #include "net/base/directory_lister.h" |
20 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
21 #include "storage/browser/blob/blob_storage_context.h" | 21 #include "storage/browser/blob/blob_storage_context.h" |
| 22 #include "storage/browser/quota/quota_manager_proxy.h" |
22 | 23 |
23 namespace content { | 24 namespace content { |
24 | 25 |
25 const char ServiceWorkerCacheStorage::kIndexFileName[] = "index.txt"; | 26 const char ServiceWorkerCacheStorage::kIndexFileName[] = "index.txt"; |
26 | 27 |
27 // Handles the loading and clean up of ServiceWorkerCache objects. The | 28 // Handles the loading and clean up of ServiceWorkerCache objects. The |
28 // callback of every public method is guaranteed to be called. | 29 // callback of every public method is guaranteed to be called. |
29 class ServiceWorkerCacheStorage::CacheLoader { | 30 class ServiceWorkerCacheStorage::CacheLoader { |
30 public: | 31 public: |
31 typedef base::Callback<void(const scoped_refptr<ServiceWorkerCache>&)> | 32 typedef base::Callback<void(const scoped_refptr<ServiceWorkerCache>&)> |
32 CacheCallback; | 33 CacheCallback; |
33 typedef base::Callback<void(bool)> BoolCallback; | 34 typedef base::Callback<void(bool)> BoolCallback; |
34 typedef base::Callback<void(scoped_ptr<std::vector<std::string> >)> | 35 typedef base::Callback<void(scoped_ptr<std::vector<std::string> >)> |
35 StringVectorCallback; | 36 StringVectorCallback; |
36 | 37 |
37 CacheLoader(base::SequencedTaskRunner* cache_task_runner, | 38 CacheLoader( |
38 net::URLRequestContext* request_context, | 39 base::SequencedTaskRunner* cache_task_runner, |
39 base::WeakPtr<storage::BlobStorageContext> blob_context, | 40 net::URLRequestContext* request_context, |
40 const GURL& origin) | 41 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
| 42 base::WeakPtr<storage::BlobStorageContext> blob_context, |
| 43 const GURL& origin) |
41 : cache_task_runner_(cache_task_runner), | 44 : cache_task_runner_(cache_task_runner), |
42 request_context_(request_context), | 45 request_context_(request_context), |
| 46 quota_manager_proxy_(quota_manager_proxy), |
43 blob_context_(blob_context), | 47 blob_context_(blob_context), |
44 origin_(origin) { | 48 origin_(origin) { |
45 DCHECK(!origin_.is_empty()); | 49 DCHECK(!origin_.is_empty()); |
46 } | 50 } |
47 | 51 |
48 virtual ~CacheLoader() {} | 52 virtual ~CacheLoader() {} |
49 | 53 |
50 // Creates a ServiceWorkerCache with the given name. It does not attempt to | 54 // Creates a ServiceWorkerCache with the given name. It does not attempt to |
51 // load the backend, that happens lazily when the cache is used. | 55 // load the backend, that happens lazily when the cache is used. |
52 virtual scoped_refptr<ServiceWorkerCache> CreateServiceWorkerCache( | 56 virtual scoped_refptr<ServiceWorkerCache> CreateServiceWorkerCache( |
(...skipping 12 matching lines...) Expand all Loading... |
65 virtual void WriteIndex(const StringVector& cache_names, | 69 virtual void WriteIndex(const StringVector& cache_names, |
66 const BoolCallback& callback) = 0; | 70 const BoolCallback& callback) = 0; |
67 | 71 |
68 // Loads the cache names from disk if applicable. | 72 // Loads the cache names from disk if applicable. |
69 virtual void LoadIndex(scoped_ptr<std::vector<std::string> > cache_names, | 73 virtual void LoadIndex(scoped_ptr<std::vector<std::string> > cache_names, |
70 const StringVectorCallback& callback) = 0; | 74 const StringVectorCallback& callback) = 0; |
71 | 75 |
72 protected: | 76 protected: |
73 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; | 77 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; |
74 net::URLRequestContext* request_context_; | 78 net::URLRequestContext* request_context_; |
| 79 scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_; |
75 base::WeakPtr<storage::BlobStorageContext> blob_context_; | 80 base::WeakPtr<storage::BlobStorageContext> blob_context_; |
76 GURL origin_; | 81 GURL origin_; |
77 }; | 82 }; |
78 | 83 |
79 // Creates memory-only ServiceWorkerCaches. Because these caches have no | 84 // Creates memory-only ServiceWorkerCaches. Because these caches have no |
80 // persistent storage it is not safe to free them from memory if they might be | 85 // persistent storage it is not safe to free them from memory if they might be |
81 // used again. Therefore this class holds a reference to each cache until the | 86 // used again. Therefore this class holds a reference to each cache until the |
82 // cache is deleted. | 87 // cache is deleted. |
83 class ServiceWorkerCacheStorage::MemoryLoader | 88 class ServiceWorkerCacheStorage::MemoryLoader |
84 : public ServiceWorkerCacheStorage::CacheLoader { | 89 : public ServiceWorkerCacheStorage::CacheLoader { |
85 public: | 90 public: |
86 MemoryLoader(base::SequencedTaskRunner* cache_task_runner, | 91 MemoryLoader( |
87 net::URLRequestContext* request_context, | 92 base::SequencedTaskRunner* cache_task_runner, |
88 base::WeakPtr<storage::BlobStorageContext> blob_context, | 93 net::URLRequestContext* request_context, |
89 const GURL& origin) | 94 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
90 : CacheLoader(cache_task_runner, request_context, blob_context, origin) {} | 95 base::WeakPtr<storage::BlobStorageContext> blob_context, |
| 96 const GURL& origin) |
| 97 : CacheLoader(cache_task_runner, |
| 98 request_context, |
| 99 quota_manager_proxy, |
| 100 blob_context, |
| 101 origin) {} |
91 | 102 |
92 scoped_refptr<ServiceWorkerCache> CreateServiceWorkerCache( | 103 scoped_refptr<ServiceWorkerCache> CreateServiceWorkerCache( |
93 const std::string& cache_name) override { | 104 const std::string& cache_name) override { |
94 return ServiceWorkerCache::CreateMemoryCache(request_context_, | 105 return ServiceWorkerCache::CreateMemoryCache( |
95 blob_context_); | 106 origin_, request_context_, quota_manager_proxy_, blob_context_); |
96 } | 107 } |
97 | 108 |
98 void CreateCache(const std::string& cache_name, | 109 void CreateCache(const std::string& cache_name, |
99 const CacheCallback& callback) override { | 110 const CacheCallback& callback) override { |
100 scoped_refptr<ServiceWorkerCache> cache = | 111 scoped_refptr<ServiceWorkerCache> cache = |
101 ServiceWorkerCache::CreateMemoryCache(request_context_, blob_context_); | 112 CreateServiceWorkerCache(cache_name); |
102 cache_refs_.insert(std::make_pair(cache_name, cache)); | 113 cache_refs_.insert(std::make_pair(cache_name, cache)); |
103 callback.Run(cache); | 114 callback.Run(cache); |
104 } | 115 } |
105 | 116 |
106 void CleanUpDeletedCache(const std::string& cache_name, | 117 void CleanUpDeletedCache(const std::string& cache_name, |
107 const BoolCallback& callback) override { | 118 const BoolCallback& callback) override { |
108 CacheRefMap::iterator it = cache_refs_.find(cache_name); | 119 CacheRefMap::iterator it = cache_refs_.find(cache_name); |
109 DCHECK(it != cache_refs_.end()); | 120 DCHECK(it != cache_refs_.end()); |
110 cache_refs_.erase(it); | 121 cache_refs_.erase(it); |
111 callback.Run(true); | 122 callback.Run(true); |
(...skipping 15 matching lines...) Expand all Loading... |
127 | 138 |
128 // Keep a reference to each cache to ensure that it's not freed before the | 139 // Keep a reference to each cache to ensure that it's not freed before the |
129 // client calls ServiceWorkerCacheStorage::Delete or the CacheStorage is | 140 // client calls ServiceWorkerCacheStorage::Delete or the CacheStorage is |
130 // freed. | 141 // freed. |
131 CacheRefMap cache_refs_; | 142 CacheRefMap cache_refs_; |
132 }; | 143 }; |
133 | 144 |
134 class ServiceWorkerCacheStorage::SimpleCacheLoader | 145 class ServiceWorkerCacheStorage::SimpleCacheLoader |
135 : public ServiceWorkerCacheStorage::CacheLoader { | 146 : public ServiceWorkerCacheStorage::CacheLoader { |
136 public: | 147 public: |
137 SimpleCacheLoader(const base::FilePath& origin_path, | 148 SimpleCacheLoader( |
138 base::SequencedTaskRunner* cache_task_runner, | 149 const base::FilePath& origin_path, |
139 net::URLRequestContext* request_context, | 150 base::SequencedTaskRunner* cache_task_runner, |
140 base::WeakPtr<storage::BlobStorageContext> blob_context, | 151 net::URLRequestContext* request_context, |
141 const GURL& origin) | 152 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
142 : CacheLoader(cache_task_runner, request_context, blob_context, origin), | 153 base::WeakPtr<storage::BlobStorageContext> blob_context, |
| 154 const GURL& origin) |
| 155 : CacheLoader(cache_task_runner, |
| 156 request_context, |
| 157 quota_manager_proxy, |
| 158 blob_context, |
| 159 origin), |
143 origin_path_(origin_path), | 160 origin_path_(origin_path), |
144 weak_ptr_factory_(this) {} | 161 weak_ptr_factory_(this) {} |
145 | 162 |
146 scoped_refptr<ServiceWorkerCache> CreateServiceWorkerCache( | 163 scoped_refptr<ServiceWorkerCache> CreateServiceWorkerCache( |
147 const std::string& cache_name) override { | 164 const std::string& cache_name) override { |
148 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 165 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
149 | 166 |
150 return ServiceWorkerCache::CreatePersistentCache( | 167 return ServiceWorkerCache::CreatePersistentCache( |
| 168 origin_, |
151 CreatePersistentCachePath(origin_path_, cache_name), | 169 CreatePersistentCachePath(origin_path_, cache_name), |
152 request_context_, | 170 request_context_, |
| 171 quota_manager_proxy_, |
153 blob_context_); | 172 blob_context_); |
154 } | 173 } |
155 | 174 |
156 void CreateCache(const std::string& cache_name, | 175 void CreateCache(const std::string& cache_name, |
157 const CacheCallback& callback) override { | 176 const CacheCallback& callback) override { |
158 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 177 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
159 | 178 |
160 // 1. Delete the cache's directory if it exists. | 179 // 1. Delete the cache's directory if it exists. |
161 // (CreateCacheDeleteFilesInPool) | 180 // (CreateCacheDeleteFilesInPool) |
162 // 2. Load the cache. (LoadCreateDirectoryInPool) | 181 // 2. Load the cache. (LoadCreateDirectoryInPool) |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 const base::FilePath origin_path_; | 357 const base::FilePath origin_path_; |
339 | 358 |
340 base::WeakPtrFactory<SimpleCacheLoader> weak_ptr_factory_; | 359 base::WeakPtrFactory<SimpleCacheLoader> weak_ptr_factory_; |
341 }; | 360 }; |
342 | 361 |
343 ServiceWorkerCacheStorage::ServiceWorkerCacheStorage( | 362 ServiceWorkerCacheStorage::ServiceWorkerCacheStorage( |
344 const base::FilePath& path, | 363 const base::FilePath& path, |
345 bool memory_only, | 364 bool memory_only, |
346 base::SequencedTaskRunner* cache_task_runner, | 365 base::SequencedTaskRunner* cache_task_runner, |
347 net::URLRequestContext* request_context, | 366 net::URLRequestContext* request_context, |
| 367 const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy, |
348 base::WeakPtr<storage::BlobStorageContext> blob_context, | 368 base::WeakPtr<storage::BlobStorageContext> blob_context, |
349 const GURL& origin) | 369 const GURL& origin) |
350 : initialized_(false), | 370 : initialized_(false), |
351 origin_path_(path), | 371 origin_path_(path), |
352 cache_task_runner_(cache_task_runner), | 372 cache_task_runner_(cache_task_runner), |
353 memory_only_(memory_only), | 373 memory_only_(memory_only), |
354 weak_factory_(this) { | 374 weak_factory_(this) { |
355 if (memory_only) | 375 if (memory_only) |
356 cache_loader_.reset(new MemoryLoader( | 376 cache_loader_.reset(new MemoryLoader(cache_task_runner_.get(), |
357 cache_task_runner_.get(), request_context, blob_context, origin)); | 377 request_context, |
| 378 quota_manager_proxy, |
| 379 blob_context, |
| 380 origin)); |
358 else | 381 else |
359 cache_loader_.reset(new SimpleCacheLoader(origin_path_, | 382 cache_loader_.reset(new SimpleCacheLoader(origin_path_, |
360 cache_task_runner_.get(), | 383 cache_task_runner_.get(), |
361 request_context, | 384 request_context, |
| 385 quota_manager_proxy, |
362 blob_context, | 386 blob_context, |
363 origin)); | 387 origin)); |
364 } | 388 } |
365 | 389 |
366 ServiceWorkerCacheStorage::~ServiceWorkerCacheStorage() { | 390 ServiceWorkerCacheStorage::~ServiceWorkerCacheStorage() { |
367 } | 391 } |
368 | 392 |
369 void ServiceWorkerCacheStorage::OpenCache( | 393 void ServiceWorkerCacheStorage::OpenCache( |
370 const std::string& cache_name, | 394 const std::string& cache_name, |
371 const CacheAndErrorCallback& callback) { | 395 const CacheAndErrorCallback& callback) { |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
592 scoped_refptr<ServiceWorkerCache> new_cache = | 616 scoped_refptr<ServiceWorkerCache> new_cache = |
593 cache_loader_->CreateServiceWorkerCache(cache_name); | 617 cache_loader_->CreateServiceWorkerCache(cache_name); |
594 map_iter->second = new_cache->AsWeakPtr(); | 618 map_iter->second = new_cache->AsWeakPtr(); |
595 return new_cache; | 619 return new_cache; |
596 } | 620 } |
597 | 621 |
598 return make_scoped_refptr(cache.get()); | 622 return make_scoped_refptr(cache.get()); |
599 } | 623 } |
600 | 624 |
601 } // namespace content | 625 } // namespace content |
OLD | NEW |