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"
19 // Builds an instance of the backend depending on platform, type, experiments
20 // etc. Takes care of the retry state. This object will self-destroy when
24 CacheCreator(const base::FilePath
& path
,
28 net::BackendType backend_type
,
30 const scoped_refptr
<base::SingleThreadTaskRunner
>& thread
,
32 scoped_ptr
<disk_cache::Backend
>* backend
,
33 const net::CompletionCallback
& callback
);
35 // Creates the backend.
41 void DoCallback(int result
);
43 void OnIOComplete(int result
);
45 const base::FilePath path_
;
50 net::BackendType backend_type_
;
52 scoped_refptr
<base::SingleThreadTaskRunner
> thread_
;
53 scoped_ptr
<disk_cache::Backend
>* backend_
;
54 net::CompletionCallback callback_
;
55 scoped_ptr
<disk_cache::Backend
> created_cache_
;
56 net::NetLog
* net_log_
;
58 DISALLOW_COPY_AND_ASSIGN(CacheCreator
);
61 CacheCreator::CacheCreator(
62 const base::FilePath
& path
,
66 net::BackendType backend_type
,
68 const scoped_refptr
<base::SingleThreadTaskRunner
>& thread
,
70 scoped_ptr
<disk_cache::Backend
>* backend
,
71 const net::CompletionCallback
& callback
)
75 max_bytes_(max_bytes
),
77 backend_type_(backend_type
),
85 CacheCreator::~CacheCreator() {
88 int CacheCreator::Run() {
89 #if defined(OS_ANDROID)
90 static const bool kSimpleBackendIsDefault
= true;
92 static const bool kSimpleBackendIsDefault
= false;
94 if (backend_type_
== net::CACHE_BACKEND_SIMPLE
||
95 (backend_type_
== net::CACHE_BACKEND_DEFAULT
&&
96 kSimpleBackendIsDefault
)) {
97 disk_cache::SimpleBackendImpl
* simple_cache
=
98 new disk_cache::SimpleBackendImpl(
99 path_
, max_bytes_
, type_
, thread_
, net_log_
);
100 created_cache_
.reset(simple_cache
);
101 return simple_cache
->Init(
102 base::Bind(&CacheCreator::OnIOComplete
, base::Unretained(this)));
105 // Avoid references to blockfile functions on Android to reduce binary size.
106 #if defined(OS_ANDROID)
107 return net::ERR_FAILED
;
109 disk_cache::BackendImpl
* new_cache
=
110 new disk_cache::BackendImpl(path_
, thread_
, net_log_
);
111 created_cache_
.reset(new_cache
);
112 new_cache
->SetMaxSize(max_bytes_
);
113 new_cache
->SetType(type_
);
114 new_cache
->SetFlags(flags_
);
115 int rv
= new_cache
->Init(
116 base::Bind(&CacheCreator::OnIOComplete
, base::Unretained(this)));
117 DCHECK_EQ(net::ERR_IO_PENDING
, rv
);
122 void CacheCreator::DoCallback(int result
) {
123 DCHECK_NE(net::ERR_IO_PENDING
, result
);
124 if (result
== net::OK
) {
125 #ifndef USE_TRACING_CACHE_BACKEND
126 *backend_
= created_cache_
.Pass();
129 new disk_cache::TracingCacheBackend(created_cache_
.Pass()));
132 LOG(ERROR
) << "Unable to create cache";
133 created_cache_
.reset();
135 callback_
.Run(result
);
139 // If the initialization of the cache fails, and |force| is true, we will
140 // discard the whole cache and create a new one.
141 void CacheCreator::OnIOComplete(int result
) {
142 if (result
== net::OK
|| !force_
|| retry_
)
143 return DoCallback(result
);
145 // This is a failure and we are supposed to try again, so delete the object,
146 // delete all the files, and try again.
148 created_cache_
.reset();
149 if (!disk_cache::DelayedCacheCleanup(path_
))
150 return DoCallback(result
);
152 // The worker thread will start deleting files soon, but the original folder
153 // is not there anymore... let's create a new set of files.
155 DCHECK_EQ(net::ERR_IO_PENDING
, rv
);
160 namespace disk_cache
{
162 int CreateCacheBackend(
164 net::BackendType backend_type
,
165 const base::FilePath
& path
,
168 const scoped_refptr
<base::SingleThreadTaskRunner
>& thread
,
169 net::NetLog
* net_log
,
170 scoped_ptr
<Backend
>* backend
,
171 const net::CompletionCallback
& callback
) {
172 DCHECK(!callback
.is_null());
173 if (type
== net::MEMORY_CACHE
) {
174 *backend
= disk_cache::MemBackendImpl::CreateBackend(max_bytes
, net_log
);
175 return *backend
? net::OK
: net::ERR_FAILED
;
177 DCHECK(thread
.get());
178 CacheCreator
* creator
= new CacheCreator(path
,
188 return creator
->Run();
191 } // namespace disk_cache