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"
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
28 CacheCreator(const base::FilePath
& path
,
32 net::BackendType backend_type
,
34 const scoped_refptr
<base::SingleThreadTaskRunner
>& thread
,
36 scoped_ptr
<disk_cache::Backend
>* backend
,
37 const net::CompletionCallback
& callback
);
39 // Creates the backend.
45 void DoCallback(int result
);
47 void OnIOComplete(int result
);
49 const base::FilePath path_
;
54 net::BackendType backend_type_
;
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
,
70 net::BackendType backend_type
,
72 const scoped_refptr
<base::SingleThreadTaskRunner
>& thread
,
74 scoped_ptr
<disk_cache::Backend
>* backend
,
75 const net::CompletionCallback
& callback
)
79 max_bytes_(max_bytes
),
81 backend_type_(backend_type
),
89 CacheCreator::~CacheCreator() {
92 int CacheCreator::Run() {
93 #if defined(OS_ANDROID)
94 static const bool kSimpleBackendIsDefault
= true;
96 static const bool kSimpleBackendIsDefault
= false;
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
;
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
);
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();
133 new disk_cache::TracingCacheBackend(created_cache_
.Pass()));
136 LOG(ERROR
) << "Unable to create cache";
137 created_cache_
.reset();
139 callback_
.Run(result
);
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.
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.
159 DCHECK_EQ(net::ERR_IO_PENDING
, rv
);
164 namespace disk_cache
{
166 int CreateCacheBackend(
168 net::BackendType backend_type
,
169 const base::FilePath
& path
,
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
,
192 return creator
->Run();
195 } // namespace disk_cache