Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: net/disk_cache/cache_creator.cc

Issue 2626173003: Calculate the size of all cache entries between two points in time. (Closed)
Patch Set: revert cache_storage_cache_unittest.cc Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/disk_cache/backend_unittest.cc ('k') | net/disk_cache/disk_cache.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include <utility>
6
7 #include "base/files/file_path.h"
8 #include "base/macros.h"
9 #include "base/metrics/field_trial.h"
10 #include "base/single_thread_task_runner.h"
11 #include "net/base/cache_type.h"
12 #include "net/base/net_errors.h"
13 #include "net/disk_cache/blockfile/backend_impl.h"
14 #include "net/disk_cache/cache_util.h"
15 #include "net/disk_cache/disk_cache.h"
16 #include "net/disk_cache/memory/mem_backend_impl.h"
17 #include "net/disk_cache/simple/simple_backend_impl.h"
18
19 namespace {
20
21 // Builds an instance of the backend depending on platform, type, experiments
22 // etc. Takes care of the retry state. This object will self-destroy when
23 // finished.
24 class CacheCreator {
25 public:
26 CacheCreator(const base::FilePath& path,
27 bool force,
28 int max_bytes,
29 net::CacheType type,
30 net::BackendType backend_type,
31 uint32_t flags,
32 const scoped_refptr<base::SingleThreadTaskRunner>& thread,
33 net::NetLog* net_log,
34 std::unique_ptr<disk_cache::Backend>* backend,
35 const net::CompletionCallback& callback);
36
37 // Creates the backend.
38 int Run();
39
40 private:
41 ~CacheCreator();
42
43 void DoCallback(int result);
44
45 void OnIOComplete(int result);
46
47 const base::FilePath path_;
48 bool force_;
49 bool retry_;
50 int max_bytes_;
51 net::CacheType type_;
52 net::BackendType backend_type_;
53 #if !defined(OS_ANDROID)
54 uint32_t flags_;
55 #endif
56 scoped_refptr<base::SingleThreadTaskRunner> thread_;
57 std::unique_ptr<disk_cache::Backend>* backend_;
58 net::CompletionCallback callback_;
59 std::unique_ptr<disk_cache::Backend> created_cache_;
60 net::NetLog* net_log_;
61
62 DISALLOW_COPY_AND_ASSIGN(CacheCreator);
63 };
64
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_t flags,
72 const scoped_refptr<base::SingleThreadTaskRunner>& thread,
73 net::NetLog* net_log,
74 std::unique_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 #if !defined(OS_ANDROID)
83 flags_(flags),
84 #endif
85 thread_(thread),
86 backend_(backend),
87 callback_(callback),
88 net_log_(net_log) {
89 }
90
91 CacheCreator::~CacheCreator() {
92 }
93
94 int CacheCreator::Run() {
95 #if defined(OS_ANDROID)
96 static const bool kSimpleBackendIsDefault = true;
97 #else
98 static const bool kSimpleBackendIsDefault = false;
99 #endif
100 if (backend_type_ == net::CACHE_BACKEND_SIMPLE ||
101 (backend_type_ == net::CACHE_BACKEND_DEFAULT &&
102 kSimpleBackendIsDefault)) {
103 disk_cache::SimpleBackendImpl* simple_cache =
104 new disk_cache::SimpleBackendImpl(
105 path_, max_bytes_, type_, thread_, net_log_);
106 created_cache_.reset(simple_cache);
107 return simple_cache->Init(
108 base::Bind(&CacheCreator::OnIOComplete, base::Unretained(this)));
109 }
110
111 // Avoid references to blockfile functions on Android to reduce binary size.
112 #if defined(OS_ANDROID)
113 return net::ERR_FAILED;
114 #else
115 disk_cache::BackendImpl* new_cache =
116 new disk_cache::BackendImpl(path_, thread_, net_log_);
117 created_cache_.reset(new_cache);
118 new_cache->SetMaxSize(max_bytes_);
119 new_cache->SetType(type_);
120 new_cache->SetFlags(flags_);
121 int rv = new_cache->Init(
122 base::Bind(&CacheCreator::OnIOComplete, base::Unretained(this)));
123 DCHECK_EQ(net::ERR_IO_PENDING, rv);
124 return rv;
125 #endif
126 }
127
128 void CacheCreator::DoCallback(int result) {
129 DCHECK_NE(net::ERR_IO_PENDING, result);
130 if (result == net::OK) {
131 *backend_ = std::move(created_cache_);
132 } else {
133 LOG(ERROR) << "Unable to create cache";
134 created_cache_.reset();
135 }
136 callback_.Run(result);
137 delete this;
138 }
139
140 // If the initialization of the cache fails, and |force| is true, we will
141 // discard the whole cache and create a new one.
142 void CacheCreator::OnIOComplete(int result) {
143 if (result == net::OK || !force_ || retry_)
144 return DoCallback(result);
145
146 // This is a failure and we are supposed to try again, so delete the object,
147 // delete all the files, and try again.
148 retry_ = true;
149 created_cache_.reset();
150 if (!disk_cache::DelayedCacheCleanup(path_))
151 return DoCallback(result);
152
153 // The worker thread will start deleting files soon, but the original folder
154 // is not there anymore... let's create a new set of files.
155 int rv = Run();
156 DCHECK_EQ(net::ERR_IO_PENDING, rv);
157 }
158
159 } // namespace
160
161 namespace disk_cache {
162
163 int CreateCacheBackend(
164 net::CacheType type,
165 net::BackendType backend_type,
166 const base::FilePath& path,
167 int max_bytes,
168 bool force,
169 const scoped_refptr<base::SingleThreadTaskRunner>& thread,
170 net::NetLog* net_log,
171 std::unique_ptr<Backend>* backend,
172 const net::CompletionCallback& callback) {
173 DCHECK(!callback.is_null());
174 if (type == net::MEMORY_CACHE) {
175 *backend = disk_cache::MemBackendImpl::CreateBackend(max_bytes, net_log);
176 return *backend ? net::OK : net::ERR_FAILED;
177 }
178 DCHECK(thread.get());
179 CacheCreator* creator = new CacheCreator(path,
180 force,
181 max_bytes,
182 type,
183 backend_type,
184 kNone,
185 thread,
186 net_log,
187 backend,
188 callback);
189 return creator->Run();
190 }
191
192 } // namespace disk_cache
OLDNEW
« no previous file with comments | « net/disk_cache/backend_unittest.cc ('k') | net/disk_cache/disk_cache.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698