Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / disk_cache / cache_creator.cc
blobd91c201398155738fb022db4f12a684e836b17ac
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/files/file_path.h"
6 #include "base/metrics/field_trial.h"
7 #include "base/single_thread_task_runner.h"
8 #include "base/strings/stringprintf.h"
9 #include "net/base/cache_type.h"
10 #include "net/base/net_errors.h"
11 #include "net/disk_cache/blockfile/backend_impl.h"
12 #include "net/disk_cache/cache_util.h"
13 #include "net/disk_cache/disk_cache.h"
14 #include "net/disk_cache/memory/mem_backend_impl.h"
15 #include "net/disk_cache/simple/simple_backend_impl.h"
17 #ifdef USE_TRACING_CACHE_BACKEND
18 #include "net/disk_cache/tracing_cache_backend.h"
19 #endif
21 namespace {
23 // Builds an instance of the backend depending on platform, type, experiments
24 // etc. Takes care of the retry state. This object will self-destroy when
25 // finished.
26 class CacheCreator {
27 public:
28 CacheCreator(const base::FilePath& path,
29 bool force,
30 int max_bytes,
31 net::CacheType type,
32 net::BackendType backend_type,
33 uint32 flags,
34 const scoped_refptr<base::SingleThreadTaskRunner>& thread,
35 net::NetLog* net_log,
36 scoped_ptr<disk_cache::Backend>* backend,
37 const net::CompletionCallback& callback);
39 // Creates the backend.
40 int Run();
42 private:
43 ~CacheCreator();
45 void DoCallback(int result);
47 void OnIOComplete(int result);
49 const base::FilePath path_;
50 bool force_;
51 bool retry_;
52 int max_bytes_;
53 net::CacheType type_;
54 net::BackendType backend_type_;
55 uint32 flags_;
56 scoped_refptr<base::SingleThreadTaskRunner> thread_;
57 scoped_ptr<disk_cache::Backend>* backend_;
58 net::CompletionCallback callback_;
59 scoped_ptr<disk_cache::Backend> created_cache_;
60 net::NetLog* net_log_;
62 DISALLOW_COPY_AND_ASSIGN(CacheCreator);
65 CacheCreator::CacheCreator(
66 const base::FilePath& path,
67 bool force,
68 int max_bytes,
69 net::CacheType type,
70 net::BackendType backend_type,
71 uint32 flags,
72 const scoped_refptr<base::SingleThreadTaskRunner>& thread,
73 net::NetLog* net_log,
74 scoped_ptr<disk_cache::Backend>* backend,
75 const net::CompletionCallback& callback)
76 : path_(path),
77 force_(force),
78 retry_(false),
79 max_bytes_(max_bytes),
80 type_(type),
81 backend_type_(backend_type),
82 flags_(flags),
83 thread_(thread),
84 backend_(backend),
85 callback_(callback),
86 net_log_(net_log) {
89 CacheCreator::~CacheCreator() {
92 int CacheCreator::Run() {
93 #if defined(OS_ANDROID)
94 static const bool kSimpleBackendIsDefault = true;
95 #else
96 static const bool kSimpleBackendIsDefault = false;
97 #endif
98 if (backend_type_ == net::CACHE_BACKEND_SIMPLE ||
99 (backend_type_ == net::CACHE_BACKEND_DEFAULT &&
100 kSimpleBackendIsDefault)) {
101 disk_cache::SimpleBackendImpl* simple_cache =
102 new disk_cache::SimpleBackendImpl(
103 path_, max_bytes_, type_, thread_, net_log_);
104 created_cache_.reset(simple_cache);
105 return simple_cache->Init(
106 base::Bind(&CacheCreator::OnIOComplete, base::Unretained(this)));
109 // Avoid references to blockfile functions on Android to reduce binary size.
110 #if defined(OS_ANDROID)
111 return net::ERR_FAILED;
112 #else
113 disk_cache::BackendImpl* new_cache =
114 new disk_cache::BackendImpl(path_, thread_, net_log_);
115 created_cache_.reset(new_cache);
116 new_cache->SetMaxSize(max_bytes_);
117 new_cache->SetType(type_);
118 new_cache->SetFlags(flags_);
119 int rv = new_cache->Init(
120 base::Bind(&CacheCreator::OnIOComplete, base::Unretained(this)));
121 DCHECK_EQ(net::ERR_IO_PENDING, rv);
122 return rv;
123 #endif
126 void CacheCreator::DoCallback(int result) {
127 DCHECK_NE(net::ERR_IO_PENDING, result);
128 if (result == net::OK) {
129 #ifndef USE_TRACING_CACHE_BACKEND
130 *backend_ = created_cache_.Pass();
131 #else
132 *backend_.reset(
133 new disk_cache::TracingCacheBackend(created_cache_.Pass()));
134 #endif
135 } else {
136 LOG(ERROR) << "Unable to create cache";
137 created_cache_.reset();
139 callback_.Run(result);
140 delete this;
143 // If the initialization of the cache fails, and |force| is true, we will
144 // discard the whole cache and create a new one.
145 void CacheCreator::OnIOComplete(int result) {
146 if (result == net::OK || !force_ || retry_)
147 return DoCallback(result);
149 // This is a failure and we are supposed to try again, so delete the object,
150 // delete all the files, and try again.
151 retry_ = true;
152 created_cache_.reset();
153 if (!disk_cache::DelayedCacheCleanup(path_))
154 return DoCallback(result);
156 // The worker thread will start deleting files soon, but the original folder
157 // is not there anymore... let's create a new set of files.
158 int rv = Run();
159 DCHECK_EQ(net::ERR_IO_PENDING, rv);
162 } // namespace
164 namespace disk_cache {
166 int CreateCacheBackend(
167 net::CacheType type,
168 net::BackendType backend_type,
169 const base::FilePath& path,
170 int max_bytes,
171 bool force,
172 const scoped_refptr<base::SingleThreadTaskRunner>& thread,
173 net::NetLog* net_log,
174 scoped_ptr<Backend>* backend,
175 const net::CompletionCallback& callback) {
176 DCHECK(!callback.is_null());
177 if (type == net::MEMORY_CACHE) {
178 *backend = disk_cache::MemBackendImpl::CreateBackend(max_bytes, net_log);
179 return *backend ? net::OK : net::ERR_FAILED;
181 DCHECK(thread.get());
182 CacheCreator* creator = new CacheCreator(path,
183 force,
184 max_bytes,
185 type,
186 backend_type,
187 kNone,
188 thread,
189 net_log,
190 backend,
191 callback);
192 return creator->Run();
195 } // namespace disk_cache