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

Side by Side Diff: content/browser/service_worker/service_worker_cache_storage.cc

Issue 459003002: Plumbs URLRequestContext and CacheBlobStorageContext down to cache. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@cache1
Patch Set: Rebase Created 6 years, 4 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/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/fileapi/chrome_blob_storage_context.h"
15 #include "content/browser/service_worker/service_worker_cache.h" 16 #include "content/browser/service_worker/service_worker_cache.h"
16 #include "content/browser/service_worker/service_worker_cache.pb.h" 17 #include "content/browser/service_worker/service_worker_cache.pb.h"
17 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
18 #include "net/base/directory_lister.h" 19 #include "net/base/directory_lister.h"
19 #include "net/base/net_errors.h" 20 #include "net/base/net_errors.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(base::SequencedTaskRunner* cache_task_runner,
34 : cache_task_runner_(cache_task_runner) {} 35 net::URLRequestContext* request_context,
36 ChromeBlobStorageContext* blob_context)
37 : cache_task_runner_(cache_task_runner),
38 request_context_(request_context),
39 blob_context_(blob_context) {}
35 40
36 // Loads the given cache_name, the cache is NULL if it fails. If the cache 41 // Loads the given cache_name, the cache is NULL if it fails. If the cache
37 // doesn't exist a new one is created. 42 // doesn't exist a new one is created.
38 virtual void LoadCache(const std::string& cache_name, 43 virtual void LoadCache(const std::string& cache_name,
39 const CacheCallback& callback) = 0; 44 const CacheCallback& callback) = 0;
40 45
41 // Deletes any pre-existing cache of the same name and then loads it. 46 // Deletes any pre-existing cache of the same name and then loads it.
42 virtual void CreateCache(const std::string& cache_name, 47 virtual void CreateCache(const std::string& cache_name,
43 const CacheCallback& callback) = 0; 48 const CacheCallback& callback) = 0;
44 49
(...skipping 10 matching lines...) Expand all
55 const StringsCallback& callback) = 0; 60 const StringsCallback& callback) = 0;
56 61
57 protected: 62 protected:
58 friend class base::RefCountedThreadSafe< 63 friend class base::RefCountedThreadSafe<
59 ServiceWorkerCacheStorage::CacheLoader>; 64 ServiceWorkerCacheStorage::CacheLoader>;
60 65
61 virtual ~CacheLoader() {}; 66 virtual ~CacheLoader() {};
62 virtual void LoadCacheImpl(const std::string&) {} 67 virtual void LoadCacheImpl(const std::string&) {}
63 68
64 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_; 69 scoped_refptr<base::SequencedTaskRunner> cache_task_runner_;
65 70 net::URLRequestContext* request_context_;
71 ChromeBlobStorageContext* blob_context_;
66 }; 72 };
67 73
68 class ServiceWorkerCacheStorage::MemoryLoader 74 class ServiceWorkerCacheStorage::MemoryLoader
69 : public ServiceWorkerCacheStorage::CacheLoader { 75 : public ServiceWorkerCacheStorage::CacheLoader {
70 public: 76 public:
71 explicit MemoryLoader(base::SequencedTaskRunner* cache_task_runner) 77 explicit MemoryLoader(base::SequencedTaskRunner* cache_task_runner,
72 : CacheLoader(cache_task_runner) {} 78 net::URLRequestContext* request_context,
79 ChromeBlobStorageContext* blob_context)
80 : CacheLoader(cache_task_runner, request_context, blob_context) {}
73 virtual void LoadCache(const std::string& cache_name, 81 virtual void LoadCache(const std::string& cache_name,
74 const CacheCallback& callback) OVERRIDE { 82 const CacheCallback& callback) OVERRIDE {
75 NOTREACHED(); 83 NOTREACHED();
76 } 84 }
77 85
78 virtual void CreateCache(const std::string& cache_name, 86 virtual void CreateCache(const std::string& cache_name,
79 const CacheCallback& callback) OVERRIDE { 87 const CacheCallback& callback) OVERRIDE {
80 scoped_ptr<ServiceWorkerCache> cache = 88 scoped_ptr<ServiceWorkerCache> cache =
81 ServiceWorkerCache::CreateMemoryCache(cache_name); 89 ServiceWorkerCache::CreateMemoryCache(
90 cache_name, request_context_, blob_context_);
82 callback.Run(cache.Pass()); 91 callback.Run(cache.Pass());
83 } 92 }
84 93
85 virtual void CleanUpDeletedCache(const std::string& cache_name, 94 virtual void CleanUpDeletedCache(const std::string& cache_name,
86 const BoolCallback& callback) OVERRIDE { 95 const BoolCallback& callback) OVERRIDE {
87 callback.Run(true); 96 callback.Run(true);
88 } 97 }
89 98
90 virtual void WriteIndex(CacheMap* caches, 99 virtual void WriteIndex(CacheMap* caches,
91 const BoolCallback& callback) OVERRIDE { 100 const BoolCallback& callback) OVERRIDE {
92 callback.Run(false); 101 callback.Run(false);
93 } 102 }
94 103
95 virtual void LoadIndex(scoped_ptr<std::vector<std::string> > cache_names, 104 virtual void LoadIndex(scoped_ptr<std::vector<std::string> > cache_names,
96 const StringsCallback& callback) OVERRIDE { 105 const StringsCallback& callback) OVERRIDE {
97 callback.Run(cache_names.Pass()); 106 callback.Run(cache_names.Pass());
98 } 107 }
99 108
100 private: 109 private:
101 virtual ~MemoryLoader() {} 110 virtual ~MemoryLoader() {}
102 }; 111 };
103 112
104 class ServiceWorkerCacheStorage::SimpleCacheLoader 113 class ServiceWorkerCacheStorage::SimpleCacheLoader
105 : public ServiceWorkerCacheStorage::CacheLoader { 114 : public ServiceWorkerCacheStorage::CacheLoader {
106 public: 115 public:
107 SimpleCacheLoader(const base::FilePath& origin_path, 116 SimpleCacheLoader(const base::FilePath& origin_path,
108 base::SequencedTaskRunner* cache_task_runner) 117 base::SequencedTaskRunner* cache_task_runner,
109 : CacheLoader(cache_task_runner), origin_path_(origin_path) {} 118 net::URLRequestContext* request_context,
119 ChromeBlobStorageContext* blob_context)
120 : CacheLoader(cache_task_runner, request_context, blob_context),
121 origin_path_(origin_path) {}
110 122
111 virtual void LoadCache(const std::string& cache_name, 123 virtual void LoadCache(const std::string& cache_name,
112 const CacheCallback& callback) OVERRIDE { 124 const CacheCallback& callback) OVERRIDE {
113 DCHECK_CURRENTLY_ON(BrowserThread::IO); 125 DCHECK_CURRENTLY_ON(BrowserThread::IO);
114 126
115 // 1. Create the cache's directory if necessary. (LoadCreateDirectoryInPool) 127 // 1. Create the cache's directory if necessary. (LoadCreateDirectoryInPool)
116 // 2. Create the cache object. (LoadDidCreateDirectory) 128 // 2. Create the cache object. (LoadDidCreateDirectory)
117 129
118 cache_task_runner_->PostTask( 130 cache_task_runner_->PostTask(
119 FROM_HERE, 131 FROM_HERE,
(...skipping 24 matching lines...) Expand all
144 156
145 void LoadDidCreateDirectory(const std::string& cache_name, 157 void LoadDidCreateDirectory(const std::string& cache_name,
146 const CacheCallback& callback, 158 const CacheCallback& callback,
147 bool dir_rv) { 159 bool dir_rv) {
148 DCHECK_CURRENTLY_ON(BrowserThread::IO); 160 DCHECK_CURRENTLY_ON(BrowserThread::IO);
149 161
150 if (!dir_rv) { 162 if (!dir_rv) {
151 callback.Run(scoped_ptr<ServiceWorkerCache>()); 163 callback.Run(scoped_ptr<ServiceWorkerCache>());
152 return; 164 return;
153 } 165 }
154 base::FilePath cache_path = 166
155 CreatePersistentCachePath(origin_path_, cache_name); 167 scoped_ptr<ServiceWorkerCache> cache =
156 scoped_ptr<ServiceWorkerCache> cache( 168 ServiceWorkerCache::CreatePersistentCache(
157 ServiceWorkerCache::CreatePersistentCache(cache_path, cache_name)); 169 CreatePersistentCachePath(origin_path_, cache_name),
170 cache_name,
171 request_context_,
172 blob_context_);
158 callback.Run(cache.Pass()); 173 callback.Run(cache.Pass());
159 } 174 }
160 175
161 virtual void CreateCache(const std::string& cache_name, 176 virtual void CreateCache(const std::string& cache_name,
162 const CacheCallback& callback) OVERRIDE { 177 const CacheCallback& callback) OVERRIDE {
163 DCHECK_CURRENTLY_ON(BrowserThread::IO); 178 DCHECK_CURRENTLY_ON(BrowserThread::IO);
164 179
165 // 1. Delete the cache's directory if it exists. 180 // 1. Delete the cache's directory if it exists.
166 // (CreateCacheDeleteFilesInPool) 181 // (CreateCacheDeleteFilesInPool)
167 // 2. Load the cache. (LoadCreateDirectoryInPool) 182 // 2. Load the cache. (LoadCreateDirectoryInPool)
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
353 const std::string& cache_name) { 368 const std::string& cache_name) {
354 return origin_path.AppendASCII(HexedHash(cache_name)); 369 return origin_path.AppendASCII(HexedHash(cache_name));
355 } 370 }
356 371
357 const base::FilePath origin_path_; 372 const base::FilePath origin_path_;
358 }; 373 };
359 374
360 ServiceWorkerCacheStorage::ServiceWorkerCacheStorage( 375 ServiceWorkerCacheStorage::ServiceWorkerCacheStorage(
361 const base::FilePath& path, 376 const base::FilePath& path,
362 bool memory_only, 377 bool memory_only,
363 base::SequencedTaskRunner* cache_task_runner) 378 base::SequencedTaskRunner* cache_task_runner,
379 net::URLRequestContext* request_context,
380 ChromeBlobStorageContext* blob_context)
364 : initialized_(false), 381 : initialized_(false),
365 origin_path_(path), 382 origin_path_(path),
366 cache_task_runner_(cache_task_runner), 383 cache_task_runner_(cache_task_runner),
367 weak_factory_(this) { 384 weak_factory_(this) {
368 if (memory_only) 385 if (memory_only)
369 cache_loader_ = new MemoryLoader(cache_task_runner_); 386 cache_loader_ =
387 new MemoryLoader(cache_task_runner_, request_context, blob_context);
370 else 388 else
371 cache_loader_ = new SimpleCacheLoader(origin_path_, cache_task_runner_); 389 cache_loader_ = new SimpleCacheLoader(
390 origin_path_, cache_task_runner_, request_context, blob_context);
372 } 391 }
373 392
374 ServiceWorkerCacheStorage::~ServiceWorkerCacheStorage() { 393 ServiceWorkerCacheStorage::~ServiceWorkerCacheStorage() {
375 } 394 }
376 395
377 void ServiceWorkerCacheStorage::CreateCache( 396 void ServiceWorkerCacheStorage::CreateCache(
378 const std::string& cache_name, 397 const std::string& cache_name,
379 const CacheAndErrorCallback& callback) { 398 const CacheAndErrorCallback& callback) {
380 if (!initialized_) { 399 if (!initialized_) {
381 LazyInit(base::Bind(&ServiceWorkerCacheStorage::CreateCache, 400 LazyInit(base::Bind(&ServiceWorkerCacheStorage::CreateCache,
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after
688 NameMap::const_iterator it = name_map_.find(cache_name); 707 NameMap::const_iterator it = name_map_.find(cache_name);
689 if (it == name_map_.end()) 708 if (it == name_map_.end())
690 return NULL; 709 return NULL;
691 710
692 ServiceWorkerCache* cache = cache_map_.Lookup(it->second); 711 ServiceWorkerCache* cache = cache_map_.Lookup(it->second);
693 DCHECK(cache); 712 DCHECK(cache);
694 return cache; 713 return cache;
695 } 714 }
696 715
697 } // namespace content 716 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698