Index: net/disk_cache/cache_creator.cc |
diff --git a/net/disk_cache/cache_creator.cc b/net/disk_cache/cache_creator.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..387c7b6c920735574f6d3562c4b5cd913f09104e |
--- /dev/null |
+++ b/net/disk_cache/cache_creator.cc |
@@ -0,0 +1,236 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
rvargas (doing something else)
2013/03/13 19:36:01
Could you copy this file from backend_impl.cc? (sv
pasko-google - do not use
2013/03/18 15:47:07
Done.
|
+#include "base/command_line.h" |
+#include "base/file_util.h" |
+#include "base/stringprintf.h" |
+#include "base/string_util.h" |
+#include "base/threading/thread_restrictions.h" |
+#include "base/threading/worker_pool.h" |
+#include "net/base/net_errors.h" |
+#include "net/disk_cache/backend_impl.h" |
+#include "net/disk_cache/cache_util.h" |
+#include "net/disk_cache/disk_cache.h" |
+#include "net/disk_cache/mem_backend_impl.h" |
+#include "net/disk_cache/simple/simple_backend_impl.h" |
+ |
+namespace { |
+ |
+const int kMaxOldFolders = 100; |
+const char kUseSimpleCacheBackend[] = "use-simple-cache-backend"; |
gavinp
2013/03/13 18:36:12
I'm a bit uncomfortable with the command line opti
pasko-google - do not use
2013/03/18 15:47:07
Switches are committed in the previous CL, about:f
|
+ |
+// Returns a fully qualified name from path and name, using a given name prefix |
+// and index number. For instance, if the arguments are "/foo", "bar" and 5, it |
+// will return "/foo/old_bar_005". |
+base::FilePath GetPrefixedName(const base::FilePath& path, |
+ const std::string& name, |
+ int index) { |
+ std::string tmp = base::StringPrintf("%s%s_%03d", "old_", |
+ name.c_str(), index); |
+ return path.AppendASCII(tmp); |
+} |
+ |
+// This is a simple callback to cleanup old caches. |
+void CleanupCallback(const base::FilePath& path, const std::string& name) { |
+ for (int i = 0; i < kMaxOldFolders; i++) { |
+ base::FilePath to_delete = GetPrefixedName(path, name, i); |
+ disk_cache::DeleteCache(to_delete, true); |
+ } |
+} |
+ |
+base::FilePath GetTempCacheName(const base::FilePath& path, |
gavinp
2013/03/13 18:36:12
The comment for this function has been orphaned ov
pasko-google - do not use
2013/03/18 15:47:07
Oops. Done.
|
+ const std::string& name) { |
+ // We'll attempt to have up to kMaxOldFolders directories for deletion. |
+ for (int i = 0; i < kMaxOldFolders; i++) { |
+ base::FilePath to_delete = GetPrefixedName(path, name, i); |
+ if (!file_util::PathExists(to_delete)) |
+ return to_delete; |
+ } |
+ return base::FilePath(); |
+} |
+ |
+// Builds the instance of the backend depending on command-line option. Takes |
gavinp
2013/03/13 18:36:12
"Builds an instance of the backend depending on co
pasko-google - do not use
2013/03/18 15:47:07
Done.
|
+// care of the retry state. This object will self-destroy when finished. |
+class CacheCreator { |
+ public: |
+ CacheCreator(const base::FilePath& path, bool force, int max_bytes, |
gavinp
2013/03/13 18:36:12
This declaration is formally a violation of our co
pasko-google - do not use
2013/03/18 15:47:07
Can you be more specific about the violation?
|
+ net::CacheType type, uint32 flags, |
+ base::MessageLoopProxy* thread, net::NetLog* net_log, |
+ disk_cache::Backend** backend, |
+ const net::CompletionCallback& callback); |
+ |
+ // Creates the backend. |
+ int Run(); |
+ |
+ private: |
+ ~CacheCreator(); |
+ |
+ void DoCallback(int result); |
+ |
+ // Callback implementation. |
+ void OnIOComplete(int result); |
+ |
+ const base::FilePath& path_; |
+ bool force_; |
+ bool retry_; |
+ int max_bytes_; |
+ net::CacheType type_; |
+ uint32 flags_; |
+ scoped_refptr<base::MessageLoopProxy> thread_; |
+ disk_cache::Backend** backend_; |
+ net::CompletionCallback callback_; |
+ disk_cache::Backend* created_cache_; |
+ net::NetLog* net_log_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(CacheCreator); |
+}; |
+ |
+CacheCreator::CacheCreator( |
+ const base::FilePath& path, bool force, int max_bytes, |
+ net::CacheType type, uint32 flags, |
+ base::MessageLoopProxy* thread, net::NetLog* net_log, |
+ disk_cache::Backend** backend, |
+ const net::CompletionCallback& callback) |
+ : path_(path), |
+ force_(force), |
+ retry_(false), |
+ max_bytes_(max_bytes), |
+ type_(type), |
+ flags_(flags), |
+ thread_(thread), |
+ backend_(backend), |
+ callback_(callback), |
+ created_cache_(NULL), |
+ net_log_(net_log) { |
+ } |
+ |
+CacheCreator::~CacheCreator() { |
+} |
+ |
+int CacheCreator::Run() { |
+ if (type_ == net::MEMORY_CACHE) { |
+ *backend_ = disk_cache::MemBackendImpl::CreateBackend(max_bytes_, net_log_); |
+ return *backend_ ? net::OK : net::ERR_FAILED; |
+ } |
+#if defined(USE_SIMPLE_CACHE_BACKEND) |
gavinp
2013/03/13 18:36:12
This #define probably needs to be renamed now to B
pasko-google - do not use
2013/03/18 15:47:07
Removed altogether as per other discussions.
|
+ if (CommandLine::ForCurrentProcess()->HasSwitch(kUseSimpleCacheBackend)) { |
+ // TODO(gavinp,pasko): While simple backend development proceeds, we're only |
+ // testing it against net::DISK_CACHE. Turn it on for more cache types as |
+ // appropriate. |
+ if (type_ == net::DISK_CACHE) { |
+ VLOG(1) << "Using the Simple Cache Backend."; |
+ return disk_cache::SimpleBackendImpl::CreateBackend(path_, max_bytes_, |
+ type_, disk_cache::kNone, thread_, net_log_, backend_, callback_); |
+ } |
+ } |
+#endif |
+ disk_cache::BackendImpl* new_cache = new disk_cache::BackendImpl( |
gavinp
2013/03/13 18:36:12
Why the automatic? Why not:
created_cache_ = new
pasko-google - do not use
2013/03/18 15:47:07
We use a few methods that are not present in Backe
|
+ path_, thread_, net_log_); |
+ created_cache_ = new_cache; |
+ new_cache->SetMaxSize(max_bytes_); |
+ new_cache->SetType(type_); |
+ new_cache->SetFlags(flags_); |
+ int rv = new_cache->Init( |
+ base::Bind(&CacheCreator::OnIOComplete, base::Unretained(this))); |
+ DCHECK_EQ(net::ERR_IO_PENDING, rv); |
+ return rv; |
+} |
+ |
+void CacheCreator::DoCallback(int result) { |
+ DCHECK_NE(net::ERR_IO_PENDING, result); |
+ if (result == net::OK) { |
+ *backend_ = created_cache_; |
+ } else { |
+ LOG(ERROR) << "Unable to create cache"; |
+ *backend_ = NULL; |
+ delete created_cache_; |
+ } |
+ callback_.Run(result); |
+ delete this; |
+} |
+ |
+void CacheCreator::OnIOComplete(int result) { |
+ if (result == net::OK || !force_ || retry_) |
+ return DoCallback(result); |
+ |
+ // This is a failure and we are supposed to try again, so delete the object, |
+ // delete all the files, and try again. |
+ retry_ = true; |
+ delete created_cache_; |
+ created_cache_ = NULL; |
+ if (!disk_cache::DelayedCacheCleanup(path_)) |
+ return DoCallback(result); |
+ |
+ // The worker thread will start deleting files soon, but the original |
+ // directory is not there anymore... let's create a new set of files. |
+ int rv = Run(); |
+ DCHECK_EQ(net::ERR_IO_PENDING, rv); |
+} |
+ |
+} // namespace |
+ |
+namespace disk_cache { |
+ |
+// If the initialization of the cache fails, and |force| is true, we will |
+// discard the whole cache and create a new one. In order to process a |
+// potentially large number of files, we'll rename the cache directory to old_ + |
+// original_name + number, (located on the same parent directory), and spawn a |
+// worker thread to delete all the files on all the stale cache directories. The |
+// whole process can still fail if we are not able to rename the cache directory |
+// (for instance due to a sharing violation), and in that case a cache for this |
+// profile (on the desired path) cannot be created. |
+int CreateCacheBackendWithFlags(net::CacheType type, const base::FilePath& path, |
+ int max_bytes, bool force, uint32 flags, base::MessageLoopProxy* thread, |
+ net::NetLog* net_log, Backend** backend, |
+ const net::CompletionCallback& callback) { |
+ DCHECK(!callback.is_null()); |
+ DCHECK(thread || type == net::MEMORY_CACHE); |
+ CacheCreator* creator = new CacheCreator(path, force, max_bytes, type, flags, |
+ thread, net_log, backend, callback); |
+ return creator->Run(); |
+} |
+ |
+int CreateCacheBackend(net::CacheType type, const base::FilePath& path, |
+ int max_bytes, bool force, base::MessageLoopProxy* thread, |
+ net::NetLog* net_log, Backend** backend, |
+ const net::CompletionCallback& callback) { |
+ return CreateCacheBackendWithFlags(type, path, max_bytes, force, kNone, |
+ thread, net_log, backend, callback); |
+} |
+ |
+// Moves the cache files to a new directory and creates a task to delete them. |
+bool DelayedCacheCleanup(const base::FilePath& full_path) { |
+ // GetTempCacheName() and MoveCache() use synchronous file |
+ // operations. |
+ base::ThreadRestrictions::ScopedAllowIO allow_io; |
+ |
+ base::FilePath current_path = full_path.StripTrailingSeparators(); |
+ |
+ base::FilePath path = current_path.DirName(); |
+ base::FilePath name = current_path.BaseName(); |
+#if defined(OS_POSIX) |
+ std::string name_str = name.value(); |
+#elif defined(OS_WIN) |
+ // We created this file so it should only contain ASCII. |
+ std::string name_str = WideToASCII(name.value()); |
+#endif |
+ |
+ base::FilePath to_delete = GetTempCacheName(path, name_str); |
+ if (to_delete.empty()) { |
+ LOG(ERROR) << "Unable to get another cache folder"; |
+ return false; |
+ } |
+ |
+ if (!disk_cache::MoveCache(full_path, to_delete)) { |
+ LOG(ERROR) << "Unable to move cache folder " << full_path.value() << " to " |
+ << to_delete.value(); |
+ return false; |
+ } |
+ |
+ base::WorkerPool::PostTask( |
+ FROM_HERE, base::Bind(&CleanupCallback, path, name_str), true); |
+ return true; |
+} |
+ |
+} // namespace disk_cache |