Chromium Code Reviews| 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/file_util.h" | 9 #include "base/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/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "content/browser/service_worker/service_worker_cache.h" | 15 #include "content/browser/service_worker/service_worker_cache.h" |
| 16 #include "content/browser/service_worker/service_worker_cache.pb.h" | 16 #include "content/browser/service_worker/service_worker_cache.pb.h" |
| 17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "net/base/directory_lister.h" | 18 #include "net/base/directory_lister.h" |
| 19 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| 20 #include "webkit/browser/blob/blob_storage_context.h" | |
| 20 | 21 |
| 21 namespace content { | 22 namespace content { |
| 22 | 23 |
| 23 // Handles the loading and clean up of ServiceWorkerCache objects. | 24 // Handles the loading and clean up of ServiceWorkerCache objects. |
| 24 class ServiceWorkerCacheStorage::CacheLoader | 25 class ServiceWorkerCacheStorage::CacheLoader |
| 25 : public base::RefCountedThreadSafe< | 26 : public base::RefCountedThreadSafe< |
| 26 ServiceWorkerCacheStorage::CacheLoader> { | 27 ServiceWorkerCacheStorage::CacheLoader> { |
| 27 public: | 28 public: |
| 28 typedef base::Callback<void(scoped_ptr<ServiceWorkerCache>)> CacheCallback; | 29 typedef base::Callback<void(scoped_ptr<ServiceWorkerCache>)> CacheCallback; |
| 29 typedef base::Callback<void(bool)> BoolCallback; | 30 typedef base::Callback<void(bool)> BoolCallback; |
| 30 typedef base::Callback<void(scoped_ptr<std::vector<std::string> >)> | 31 typedef base::Callback<void(scoped_ptr<std::vector<std::string> >)> |
| 31 StringsCallback; | 32 StringsCallback; |
| 32 | 33 |
| 33 explicit CacheLoader(base::SequencedTaskRunner* cache_task_runner) | 34 explicit CacheLoader( |
| 34 : cache_task_runner_(cache_task_runner) {} | 35 base::SequencedTaskRunner* cache_task_runner, |
| 36 net::URLRequestContext* request_context, | |
| 37 const base::WeakPtr<webkit_blob::BlobStorageContext>& blob_context) | |
|
michaeln
2014/08/13 00:11:28
either pass by value or const ref throughout to be
jkarlin
2014/08/13 00:34:40
Done.
| |
| 38 : cache_task_runner_(cache_task_runner), | |
| 39 request_context_(request_context), | |
| 40 blob_context_(blob_context) {} | |
| 35 | 41 |
| 36 // Loads the given cache_name, the cache is NULL if it fails. If the cache | 42 // Loads the given cache_name, the cache is NULL if it fails. If the cache |
| 37 // doesn't exist a new one is created. | 43 // doesn't exist a new one is created. |
| 38 virtual void LoadCache(const std::string& cache_name, | 44 virtual void LoadCache(const std::string& cache_name, |
| 39 const CacheCallback& callback) = 0; | 45 const CacheCallback& callback) = 0; |
| 40 | 46 |
| 41 // Deletes any pre-existing cache of the same name and then loads it. | 47 // Deletes any pre-existing cache of the same name and then loads it. |
| 42 virtual void CreateCache(const std::string& cache_name, | 48 virtual void CreateCache(const std::string& cache_name, |
| 43 const CacheCallback& callback) = 0; | 49 const CacheCallback& callback) = 0; |
| 44 | 50 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 55 const StringsCallback& callback) = 0; | 61 const StringsCallback& callback) = 0; |
| 56 | 62 |
| 57 protected: | 63 protected: |
| 58 friend class base::RefCountedThreadSafe< | 64 friend class base::RefCountedThreadSafe< |
| 59 ServiceWorkerCacheStorage::CacheLoader>; | 65 ServiceWorkerCacheStorage::CacheLoader>; |
| 60 | 66 |
| 61 virtual ~CacheLoader() {}; | 67 virtual ~CacheLoader() {}; |
| 62 virtual void LoadCacheImpl(const std::string&) {} | 68 virtual void LoadCacheImpl(const std::string&) {} |
| 63 | 69 |
| 64 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; | 70 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; |
| 71 net::URLRequestContext* request_context_; | |
| 72 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context_; | |
| 65 }; | 73 }; |
| 66 | 74 |
| 67 class ServiceWorkerCacheStorage::MemoryLoader | 75 class ServiceWorkerCacheStorage::MemoryLoader |
| 68 : public ServiceWorkerCacheStorage::CacheLoader { | 76 : public ServiceWorkerCacheStorage::CacheLoader { |
| 69 public: | 77 public: |
| 70 explicit MemoryLoader(base::SequencedTaskRunner* cache_task_runner) | 78 explicit MemoryLoader( |
| 71 : CacheLoader(cache_task_runner) {} | 79 base::SequencedTaskRunner* cache_task_runner, |
| 80 net::URLRequestContext* request_context, | |
| 81 const base::WeakPtr<webkit_blob::BlobStorageContext>& blob_context) | |
| 82 : CacheLoader(cache_task_runner, request_context, blob_context) {} | |
| 72 virtual void LoadCache(const std::string& cache_name, | 83 virtual void LoadCache(const std::string& cache_name, |
| 73 const CacheCallback& callback) OVERRIDE { | 84 const CacheCallback& callback) OVERRIDE { |
| 74 NOTREACHED(); | 85 NOTREACHED(); |
| 75 } | 86 } |
| 76 | 87 |
| 77 virtual void CreateCache(const std::string& cache_name, | 88 virtual void CreateCache(const std::string& cache_name, |
| 78 const CacheCallback& callback) OVERRIDE { | 89 const CacheCallback& callback) OVERRIDE { |
| 79 scoped_ptr<ServiceWorkerCache> cache = | 90 scoped_ptr<ServiceWorkerCache> cache = |
| 80 ServiceWorkerCache::CreateMemoryCache(cache_name); | 91 ServiceWorkerCache::CreateMemoryCache( |
| 92 cache_name, request_context_, blob_context_); | |
| 81 callback.Run(cache.Pass()); | 93 callback.Run(cache.Pass()); |
| 82 } | 94 } |
| 83 | 95 |
| 84 virtual void CleanUpDeletedCache(const std::string& cache_name, | 96 virtual void CleanUpDeletedCache(const std::string& cache_name, |
| 85 const BoolCallback& callback) OVERRIDE { | 97 const BoolCallback& callback) OVERRIDE { |
| 86 callback.Run(true); | 98 callback.Run(true); |
| 87 } | 99 } |
| 88 | 100 |
| 89 virtual void WriteIndex(CacheMap* caches, | 101 virtual void WriteIndex(CacheMap* caches, |
| 90 const BoolCallback& callback) OVERRIDE { | 102 const BoolCallback& callback) OVERRIDE { |
| 91 callback.Run(false); | 103 callback.Run(false); |
| 92 } | 104 } |
| 93 | 105 |
| 94 virtual void LoadIndex(scoped_ptr<std::vector<std::string> > cache_names, | 106 virtual void LoadIndex(scoped_ptr<std::vector<std::string> > cache_names, |
| 95 const StringsCallback& callback) OVERRIDE { | 107 const StringsCallback& callback) OVERRIDE { |
| 96 callback.Run(cache_names.Pass()); | 108 callback.Run(cache_names.Pass()); |
| 97 } | 109 } |
| 98 | 110 |
| 99 private: | 111 private: |
| 100 virtual ~MemoryLoader() {} | 112 virtual ~MemoryLoader() {} |
| 101 }; | 113 }; |
| 102 | 114 |
| 103 class ServiceWorkerCacheStorage::SimpleCacheLoader | 115 class ServiceWorkerCacheStorage::SimpleCacheLoader |
| 104 : public ServiceWorkerCacheStorage::CacheLoader { | 116 : public ServiceWorkerCacheStorage::CacheLoader { |
| 105 public: | 117 public: |
| 106 SimpleCacheLoader(const base::FilePath& origin_path, | 118 SimpleCacheLoader(const base::FilePath& origin_path, |
| 107 base::SequencedTaskRunner* cache_task_runner) | 119 base::SequencedTaskRunner* cache_task_runner, |
| 108 : CacheLoader(cache_task_runner), origin_path_(origin_path) {} | 120 net::URLRequestContext* request_context, |
| 121 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context) | |
| 122 : CacheLoader(cache_task_runner, request_context, blob_context), | |
| 123 origin_path_(origin_path) {} | |
| 109 | 124 |
| 110 virtual void LoadCache(const std::string& cache_name, | 125 virtual void LoadCache(const std::string& cache_name, |
| 111 const CacheCallback& callback) OVERRIDE { | 126 const CacheCallback& callback) OVERRIDE { |
| 112 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 127 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 113 | 128 |
| 114 // 1. Create the cache's directory if necessary. (LoadCreateDirectoryInPool) | 129 // 1. Create the cache's directory if necessary. (LoadCreateDirectoryInPool) |
| 115 // 2. Create the cache object. (LoadDidCreateDirectory) | 130 // 2. Create the cache object. (LoadDidCreateDirectory) |
| 116 | 131 |
| 117 cache_task_runner_->PostTask( | 132 cache_task_runner_->PostTask( |
| 118 FROM_HERE, | 133 FROM_HERE, |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 143 | 158 |
| 144 void LoadDidCreateDirectory(const std::string& cache_name, | 159 void LoadDidCreateDirectory(const std::string& cache_name, |
| 145 const CacheCallback& callback, | 160 const CacheCallback& callback, |
| 146 bool dir_rv) { | 161 bool dir_rv) { |
| 147 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 162 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 148 | 163 |
| 149 if (!dir_rv) { | 164 if (!dir_rv) { |
| 150 callback.Run(scoped_ptr<ServiceWorkerCache>()); | 165 callback.Run(scoped_ptr<ServiceWorkerCache>()); |
| 151 return; | 166 return; |
| 152 } | 167 } |
| 153 base::FilePath cache_path = | 168 |
| 154 CreatePersistentCachePath(origin_path_, cache_name); | 169 scoped_ptr<ServiceWorkerCache> cache = |
| 155 scoped_ptr<ServiceWorkerCache> cache( | 170 ServiceWorkerCache::CreatePersistentCache( |
| 156 ServiceWorkerCache::CreatePersistentCache(cache_path, cache_name)); | 171 CreatePersistentCachePath(origin_path_, cache_name), |
| 172 cache_name, | |
| 173 request_context_, | |
| 174 blob_context_); | |
| 157 callback.Run(cache.Pass()); | 175 callback.Run(cache.Pass()); |
| 158 } | 176 } |
| 159 | 177 |
| 160 virtual void CreateCache(const std::string& cache_name, | 178 virtual void CreateCache(const std::string& cache_name, |
| 161 const CacheCallback& callback) OVERRIDE { | 179 const CacheCallback& callback) OVERRIDE { |
| 162 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 180 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 163 | 181 |
| 164 // 1. Delete the cache's directory if it exists. | 182 // 1. Delete the cache's directory if it exists. |
| 165 // (CreateCacheDeleteFilesInPool) | 183 // (CreateCacheDeleteFilesInPool) |
| 166 // 2. Load the cache. (LoadCreateDirectoryInPool) | 184 // 2. Load the cache. (LoadCreateDirectoryInPool) |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 352 const std::string& cache_name) { | 370 const std::string& cache_name) { |
| 353 return origin_path.AppendASCII(HexedHash(cache_name)); | 371 return origin_path.AppendASCII(HexedHash(cache_name)); |
| 354 } | 372 } |
| 355 | 373 |
| 356 const base::FilePath origin_path_; | 374 const base::FilePath origin_path_; |
| 357 }; | 375 }; |
| 358 | 376 |
| 359 ServiceWorkerCacheStorage::ServiceWorkerCacheStorage( | 377 ServiceWorkerCacheStorage::ServiceWorkerCacheStorage( |
| 360 const base::FilePath& path, | 378 const base::FilePath& path, |
| 361 bool memory_only, | 379 bool memory_only, |
| 362 base::SequencedTaskRunner* cache_task_runner) | 380 base::SequencedTaskRunner* cache_task_runner, |
| 381 net::URLRequestContext* request_context, | |
| 382 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context) | |
| 363 : initialized_(false), | 383 : initialized_(false), |
| 364 origin_path_(path), | 384 origin_path_(path), |
| 365 cache_task_runner_(cache_task_runner), | 385 cache_task_runner_(cache_task_runner), |
| 366 weak_factory_(this) { | 386 weak_factory_(this) { |
| 367 if (memory_only) | 387 if (memory_only) |
| 368 cache_loader_ = new MemoryLoader(cache_task_runner_); | 388 cache_loader_ = |
| 389 new MemoryLoader(cache_task_runner_, request_context, blob_context); | |
| 369 else | 390 else |
| 370 cache_loader_ = new SimpleCacheLoader(origin_path_, cache_task_runner_); | 391 cache_loader_ = new SimpleCacheLoader( |
| 392 origin_path_, cache_task_runner_, request_context, blob_context); | |
| 371 } | 393 } |
| 372 | 394 |
| 373 ServiceWorkerCacheStorage::~ServiceWorkerCacheStorage() { | 395 ServiceWorkerCacheStorage::~ServiceWorkerCacheStorage() { |
| 374 } | 396 } |
| 375 | 397 |
| 376 void ServiceWorkerCacheStorage::CreateCache( | 398 void ServiceWorkerCacheStorage::CreateCache( |
| 377 const std::string& cache_name, | 399 const std::string& cache_name, |
| 378 const CacheAndErrorCallback& callback) { | 400 const CacheAndErrorCallback& callback) { |
| 379 if (!initialized_) { | 401 if (!initialized_) { |
| 380 LazyInit(base::Bind(&ServiceWorkerCacheStorage::CreateCache, | 402 LazyInit(base::Bind(&ServiceWorkerCacheStorage::CreateCache, |
| (...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 687 NameMap::const_iterator it = name_map_.find(cache_name); | 709 NameMap::const_iterator it = name_map_.find(cache_name); |
| 688 if (it == name_map_.end()) | 710 if (it == name_map_.end()) |
| 689 return NULL; | 711 return NULL; |
| 690 | 712 |
| 691 ServiceWorkerCache* cache = cache_map_.Lookup(it->second); | 713 ServiceWorkerCache* cache = cache_map_.Lookup(it->second); |
| 692 DCHECK(cache); | 714 DCHECK(cache); |
| 693 return cache; | 715 return cache; |
| 694 } | 716 } |
| 695 | 717 |
| 696 } // namespace content | 718 } // namespace content |
| OLD | NEW |