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