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.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/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
13 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
14 #include "base/sha1.h" | 15 #include "base/sha1.h" |
| 16 #include "base/single_thread_task_runner.h" |
15 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
16 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
17 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 20 #include "base/thread_task_runner_handle.h" |
18 #include "content/browser/cache_storage/cache_storage.pb.h" | 21 #include "content/browser/cache_storage/cache_storage.pb.h" |
19 #include "content/browser/cache_storage/cache_storage_cache.h" | 22 #include "content/browser/cache_storage/cache_storage_cache.h" |
20 #include "content/browser/cache_storage/cache_storage_scheduler.h" | 23 #include "content/browser/cache_storage/cache_storage_scheduler.h" |
21 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
22 #include "net/base/directory_lister.h" | 25 #include "net/base/directory_lister.h" |
23 #include "net/base/net_errors.h" | 26 #include "net/base/net_errors.h" |
24 #include "storage/browser/blob/blob_storage_context.h" | 27 #include "storage/browser/blob/blob_storage_context.h" |
25 #include "storage/browser/quota/quota_manager_proxy.h" | 28 #include "storage/browser/quota/quota_manager_proxy.h" |
26 | 29 |
27 namespace content { | 30 namespace content { |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 const BoolCallback& callback) override { | 221 const BoolCallback& callback) override { |
219 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 222 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
220 | 223 |
221 // 1. Delete the cache's directory. (CleanUpDeleteCacheDirInPool) | 224 // 1. Delete the cache's directory. (CleanUpDeleteCacheDirInPool) |
222 | 225 |
223 base::FilePath cache_path = | 226 base::FilePath cache_path = |
224 CreatePersistentCachePath(origin_path_, cache_name); | 227 CreatePersistentCachePath(origin_path_, cache_name); |
225 cache_task_runner_->PostTask( | 228 cache_task_runner_->PostTask( |
226 FROM_HERE, | 229 FROM_HERE, |
227 base::Bind(&SimpleCacheLoader::CleanUpDeleteCacheDirInPool, cache_path, | 230 base::Bind(&SimpleCacheLoader::CleanUpDeleteCacheDirInPool, cache_path, |
228 callback, base::MessageLoopProxy::current())); | 231 callback, base::ThreadTaskRunnerHandle::Get())); |
229 } | 232 } |
230 | 233 |
231 static void CleanUpDeleteCacheDirInPool( | 234 static void CleanUpDeleteCacheDirInPool( |
232 const base::FilePath& cache_path, | 235 const base::FilePath& cache_path, |
233 const BoolCallback& callback, | 236 const BoolCallback& callback, |
234 const scoped_refptr<base::MessageLoopProxy>& original_loop) { | 237 const scoped_refptr<base::SingleThreadTaskRunner>& original_task_runner) { |
235 bool rv = base::DeleteFile(cache_path, true); | 238 bool rv = base::DeleteFile(cache_path, true); |
236 original_loop->PostTask(FROM_HERE, base::Bind(callback, rv)); | 239 original_task_runner->PostTask(FROM_HERE, base::Bind(callback, rv)); |
237 } | 240 } |
238 | 241 |
239 void WriteIndex(const StringVector& cache_names, | 242 void WriteIndex(const StringVector& cache_names, |
240 const BoolCallback& callback) override { | 243 const BoolCallback& callback) override { |
241 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 244 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
242 | 245 |
243 // 1. Create the index file as a string. (WriteIndex) | 246 // 1. Create the index file as a string. (WriteIndex) |
244 // 2. Write the file to disk. (WriteIndexWriteToFileInPool) | 247 // 2. Write the file to disk. (WriteIndexWriteToFileInPool) |
245 | 248 |
246 CacheStorageIndex index; | 249 CacheStorageIndex index; |
247 index.set_origin(origin_.spec()); | 250 index.set_origin(origin_.spec()); |
248 | 251 |
249 for (size_t i = 0u, max = cache_names.size(); i < max; ++i) { | 252 for (size_t i = 0u, max = cache_names.size(); i < max; ++i) { |
250 CacheStorageIndex::Cache* index_cache = index.add_cache(); | 253 CacheStorageIndex::Cache* index_cache = index.add_cache(); |
251 index_cache->set_name(cache_names[i]); | 254 index_cache->set_name(cache_names[i]); |
252 } | 255 } |
253 | 256 |
254 std::string serialized; | 257 std::string serialized; |
255 bool success = index.SerializeToString(&serialized); | 258 bool success = index.SerializeToString(&serialized); |
256 DCHECK(success); | 259 DCHECK(success); |
257 | 260 |
258 base::FilePath tmp_path = origin_path_.AppendASCII("index.txt.tmp"); | 261 base::FilePath tmp_path = origin_path_.AppendASCII("index.txt.tmp"); |
259 base::FilePath index_path = | 262 base::FilePath index_path = |
260 origin_path_.AppendASCII(CacheStorage::kIndexFileName); | 263 origin_path_.AppendASCII(CacheStorage::kIndexFileName); |
261 | 264 |
262 cache_task_runner_->PostTask( | 265 cache_task_runner_->PostTask( |
263 FROM_HERE, base::Bind(&SimpleCacheLoader::WriteIndexWriteToFileInPool, | 266 FROM_HERE, base::Bind(&SimpleCacheLoader::WriteIndexWriteToFileInPool, |
264 tmp_path, index_path, serialized, callback, | 267 tmp_path, index_path, serialized, callback, |
265 base::MessageLoopProxy::current())); | 268 base::ThreadTaskRunnerHandle::Get())); |
266 } | 269 } |
267 | 270 |
268 static void WriteIndexWriteToFileInPool( | 271 static void WriteIndexWriteToFileInPool( |
269 const base::FilePath& tmp_path, | 272 const base::FilePath& tmp_path, |
270 const base::FilePath& index_path, | 273 const base::FilePath& index_path, |
271 const std::string& data, | 274 const std::string& data, |
272 const BoolCallback& callback, | 275 const BoolCallback& callback, |
273 const scoped_refptr<base::MessageLoopProxy>& original_loop) { | 276 const scoped_refptr<base::SingleThreadTaskRunner>& original_task_runner) { |
274 int bytes_written = base::WriteFile(tmp_path, data.c_str(), data.size()); | 277 int bytes_written = base::WriteFile(tmp_path, data.c_str(), data.size()); |
275 if (bytes_written != implicit_cast<int>(data.size())) { | 278 if (bytes_written != implicit_cast<int>(data.size())) { |
276 base::DeleteFile(tmp_path, /* recursive */ false); | 279 base::DeleteFile(tmp_path, /* recursive */ false); |
277 original_loop->PostTask(FROM_HERE, base::Bind(callback, false)); | 280 original_task_runner->PostTask(FROM_HERE, base::Bind(callback, false)); |
278 } | 281 } |
279 | 282 |
280 // Atomically rename the temporary index file to become the real one. | 283 // Atomically rename the temporary index file to become the real one. |
281 bool rv = base::ReplaceFile(tmp_path, index_path, NULL); | 284 bool rv = base::ReplaceFile(tmp_path, index_path, NULL); |
282 original_loop->PostTask(FROM_HERE, base::Bind(callback, rv)); | 285 original_task_runner->PostTask(FROM_HERE, base::Bind(callback, rv)); |
283 } | 286 } |
284 | 287 |
285 void LoadIndex(scoped_ptr<std::vector<std::string>> names, | 288 void LoadIndex(scoped_ptr<std::vector<std::string>> names, |
286 const StringVectorCallback& callback) override { | 289 const StringVectorCallback& callback) override { |
287 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 290 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
288 | 291 |
289 // 1. Read the file from disk. (LoadIndexReadFileInPool) | 292 // 1. Read the file from disk. (LoadIndexReadFileInPool) |
290 // 2. Parse file and return the names of the caches (LoadIndexDidReadFile) | 293 // 2. Parse file and return the names of the caches (LoadIndexDidReadFile) |
291 | 294 |
292 base::FilePath index_path = | 295 base::FilePath index_path = |
293 origin_path_.AppendASCII(CacheStorage::kIndexFileName); | 296 origin_path_.AppendASCII(CacheStorage::kIndexFileName); |
294 | 297 |
295 cache_task_runner_->PostTask( | 298 cache_task_runner_->PostTask( |
296 FROM_HERE, base::Bind(&SimpleCacheLoader::LoadIndexReadFileInPool, | 299 FROM_HERE, base::Bind(&SimpleCacheLoader::LoadIndexReadFileInPool, |
297 index_path, base::Passed(names.Pass()), callback, | 300 index_path, base::Passed(names.Pass()), callback, |
298 base::MessageLoopProxy::current())); | 301 base::ThreadTaskRunnerHandle::Get())); |
299 } | 302 } |
300 | 303 |
301 static void LoadIndexReadFileInPool( | 304 static void LoadIndexReadFileInPool( |
302 const base::FilePath& index_path, | 305 const base::FilePath& index_path, |
303 scoped_ptr<std::vector<std::string>> names, | 306 scoped_ptr<std::vector<std::string>> names, |
304 const StringVectorCallback& callback, | 307 const StringVectorCallback& callback, |
305 const scoped_refptr<base::MessageLoopProxy>& original_loop) { | 308 const scoped_refptr<base::SingleThreadTaskRunner>& original_task_runner) { |
306 std::string body; | 309 std::string body; |
307 base::ReadFileToString(index_path, &body); | 310 base::ReadFileToString(index_path, &body); |
308 | 311 |
309 original_loop->PostTask( | 312 original_task_runner->PostTask( |
310 FROM_HERE, base::Bind(&SimpleCacheLoader::LoadIndexDidReadFile, | 313 FROM_HERE, base::Bind(&SimpleCacheLoader::LoadIndexDidReadFile, |
311 base::Passed(names.Pass()), callback, body)); | 314 base::Passed(names.Pass()), callback, body)); |
312 } | 315 } |
313 | 316 |
314 static void LoadIndexDidReadFile(scoped_ptr<std::vector<std::string>> names, | 317 static void LoadIndexDidReadFile(scoped_ptr<std::vector<std::string>> names, |
315 const StringVectorCallback& callback, | 318 const StringVectorCallback& callback, |
316 const std::string& serialized) { | 319 const std::string& serialized) { |
317 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 320 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
318 | 321 |
319 CacheStorageIndex index; | 322 CacheStorageIndex index; |
(...skipping 531 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
851 scoped_ptr<ServiceWorkerResponse> response, | 854 scoped_ptr<ServiceWorkerResponse> response, |
852 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { | 855 scoped_ptr<storage::BlobDataHandle> blob_data_handle) { |
853 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr(); | 856 base::WeakPtr<CacheStorage> cache_storage = weak_factory_.GetWeakPtr(); |
854 | 857 |
855 callback.Run(error, response.Pass(), blob_data_handle.Pass()); | 858 callback.Run(error, response.Pass(), blob_data_handle.Pass()); |
856 if (cache_storage) | 859 if (cache_storage) |
857 scheduler_->CompleteOperationAndRunNext(); | 860 scheduler_->CompleteOperationAndRunNext(); |
858 } | 861 } |
859 | 862 |
860 } // namespace content | 863 } // namespace content |
OLD | NEW |