Remove IsHighDPIEnabled, move EnableHighDPISupport to only place it's used
[chromium-blink-merge.git] / net / disk_cache / blockfile / in_flight_backend_io.cc
blob9bb011e7b0823f437cc5454d000d4ec7bcf3a1fb
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 "net/disk_cache/blockfile/in_flight_backend_io.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/compiler_specific.h"
10 #include "base/logging.h"
11 #include "base/profiler/scoped_profile.h"
12 #include "base/single_thread_task_runner.h"
13 #include "net/base/net_errors.h"
14 #include "net/disk_cache/blockfile/backend_impl.h"
15 #include "net/disk_cache/blockfile/entry_impl.h"
16 #include "net/disk_cache/blockfile/histogram_macros.h"
18 // Provide a BackendImpl object to macros from histogram_macros.h.
19 #define CACHE_UMA_BACKEND_IMPL_OBJ backend_
21 namespace disk_cache {
23 BackendIO::BackendIO(InFlightIO* controller, BackendImpl* backend,
24 const net::CompletionCallback& callback)
25 : BackgroundIO(controller),
26 backend_(backend),
27 callback_(callback),
28 operation_(OP_NONE),
29 entry_ptr_(NULL),
30 iterator_(NULL),
31 entry_(NULL),
32 index_(0),
33 offset_(0),
34 buf_len_(0),
35 truncate_(false),
36 offset64_(0),
37 start_(NULL) {
38 start_time_ = base::TimeTicks::Now();
41 // Runs on the background thread.
42 void BackendIO::ExecuteOperation() {
43 if (IsEntryOperation())
44 return ExecuteEntryOperation();
46 ExecuteBackendOperation();
49 // Runs on the background thread.
50 void BackendIO::OnIOComplete(int result) {
51 DCHECK(IsEntryOperation());
52 DCHECK_NE(result, net::ERR_IO_PENDING);
53 result_ = result;
54 NotifyController();
57 // Runs on the primary thread.
58 void BackendIO::OnDone(bool cancel) {
59 if (IsEntryOperation()) {
60 CACHE_UMA(TIMES, "TotalIOTime", 0, ElapsedTime());
63 if (!ReturnsEntry())
64 return;
66 if (result() == net::OK) {
67 static_cast<EntryImpl*>(*entry_ptr_)->OnEntryCreated(backend_);
68 if (cancel) {
69 // TODO(vadimt): Remove ScopedProfile below once crbug.com/422516 is
70 // fixed.
71 tracked_objects::ScopedProfile tracking_profile(
72 FROM_HERE_WITH_EXPLICIT_FUNCTION("422516 BackendIO::OnDone"));
74 (*entry_ptr_)->Close();
79 bool BackendIO::IsEntryOperation() {
80 return operation_ > OP_MAX_BACKEND;
83 // Runs on the background thread.
84 void BackendIO::ReferenceEntry() {
85 entry_->AddRef();
88 void BackendIO::Init() {
89 operation_ = OP_INIT;
92 void BackendIO::OpenEntry(const std::string& key, Entry** entry) {
93 operation_ = OP_OPEN;
94 key_ = key;
95 entry_ptr_ = entry;
98 void BackendIO::CreateEntry(const std::string& key, Entry** entry) {
99 operation_ = OP_CREATE;
100 key_ = key;
101 entry_ptr_ = entry;
104 void BackendIO::DoomEntry(const std::string& key) {
105 operation_ = OP_DOOM;
106 key_ = key;
109 void BackendIO::DoomAllEntries() {
110 operation_ = OP_DOOM_ALL;
113 void BackendIO::DoomEntriesBetween(const base::Time initial_time,
114 const base::Time end_time) {
115 operation_ = OP_DOOM_BETWEEN;
116 initial_time_ = initial_time;
117 end_time_ = end_time;
120 void BackendIO::DoomEntriesSince(const base::Time initial_time) {
121 operation_ = OP_DOOM_SINCE;
122 initial_time_ = initial_time;
125 void BackendIO::OpenNextEntry(Rankings::Iterator* iterator,
126 Entry** next_entry) {
127 operation_ = OP_OPEN_NEXT;
128 iterator_ = iterator;
129 entry_ptr_ = next_entry;
132 void BackendIO::EndEnumeration(scoped_ptr<Rankings::Iterator> iterator) {
133 operation_ = OP_END_ENUMERATION;
134 scoped_iterator_ = iterator.Pass();
137 void BackendIO::OnExternalCacheHit(const std::string& key) {
138 operation_ = OP_ON_EXTERNAL_CACHE_HIT;
139 key_ = key;
142 void BackendIO::CloseEntryImpl(EntryImpl* entry) {
143 operation_ = OP_CLOSE_ENTRY;
144 entry_ = entry;
147 void BackendIO::DoomEntryImpl(EntryImpl* entry) {
148 operation_ = OP_DOOM_ENTRY;
149 entry_ = entry;
152 void BackendIO::FlushQueue() {
153 operation_ = OP_FLUSH_QUEUE;
156 void BackendIO::RunTask(const base::Closure& task) {
157 operation_ = OP_RUN_TASK;
158 task_ = task;
161 void BackendIO::ReadData(EntryImpl* entry, int index, int offset,
162 net::IOBuffer* buf, int buf_len) {
163 operation_ = OP_READ;
164 entry_ = entry;
165 index_ = index;
166 offset_ = offset;
167 buf_ = buf;
168 buf_len_ = buf_len;
171 void BackendIO::WriteData(EntryImpl* entry, int index, int offset,
172 net::IOBuffer* buf, int buf_len, bool truncate) {
173 operation_ = OP_WRITE;
174 entry_ = entry;
175 index_ = index;
176 offset_ = offset;
177 buf_ = buf;
178 buf_len_ = buf_len;
179 truncate_ = truncate;
182 void BackendIO::ReadSparseData(EntryImpl* entry, int64 offset,
183 net::IOBuffer* buf, int buf_len) {
184 operation_ = OP_READ_SPARSE;
185 entry_ = entry;
186 offset64_ = offset;
187 buf_ = buf;
188 buf_len_ = buf_len;
191 void BackendIO::WriteSparseData(EntryImpl* entry, int64 offset,
192 net::IOBuffer* buf, int buf_len) {
193 operation_ = OP_WRITE_SPARSE;
194 entry_ = entry;
195 offset64_ = offset;
196 buf_ = buf;
197 buf_len_ = buf_len;
200 void BackendIO::GetAvailableRange(EntryImpl* entry, int64 offset, int len,
201 int64* start) {
202 operation_ = OP_GET_RANGE;
203 entry_ = entry;
204 offset64_ = offset;
205 buf_len_ = len;
206 start_ = start;
209 void BackendIO::CancelSparseIO(EntryImpl* entry) {
210 operation_ = OP_CANCEL_IO;
211 entry_ = entry;
214 void BackendIO::ReadyForSparseIO(EntryImpl* entry) {
215 operation_ = OP_IS_READY;
216 entry_ = entry;
219 BackendIO::~BackendIO() {}
221 bool BackendIO::ReturnsEntry() {
222 return operation_ == OP_OPEN || operation_ == OP_CREATE ||
223 operation_ == OP_OPEN_NEXT;
226 base::TimeDelta BackendIO::ElapsedTime() const {
227 return base::TimeTicks::Now() - start_time_;
230 // Runs on the background thread.
231 void BackendIO::ExecuteBackendOperation() {
232 switch (operation_) {
233 case OP_INIT:
234 result_ = backend_->SyncInit();
235 break;
236 case OP_OPEN:
237 result_ = backend_->SyncOpenEntry(key_, entry_ptr_);
238 break;
239 case OP_CREATE:
240 result_ = backend_->SyncCreateEntry(key_, entry_ptr_);
241 break;
242 case OP_DOOM:
243 result_ = backend_->SyncDoomEntry(key_);
244 break;
245 case OP_DOOM_ALL:
246 result_ = backend_->SyncDoomAllEntries();
247 break;
248 case OP_DOOM_BETWEEN:
249 result_ = backend_->SyncDoomEntriesBetween(initial_time_, end_time_);
250 break;
251 case OP_DOOM_SINCE:
252 result_ = backend_->SyncDoomEntriesSince(initial_time_);
253 break;
254 case OP_OPEN_NEXT:
255 result_ = backend_->SyncOpenNextEntry(iterator_, entry_ptr_);
256 break;
257 case OP_END_ENUMERATION:
258 backend_->SyncEndEnumeration(scoped_iterator_.Pass());
259 result_ = net::OK;
260 break;
261 case OP_ON_EXTERNAL_CACHE_HIT:
262 backend_->SyncOnExternalCacheHit(key_);
263 result_ = net::OK;
264 break;
265 case OP_CLOSE_ENTRY:
266 entry_->Release();
267 result_ = net::OK;
268 break;
269 case OP_DOOM_ENTRY:
270 entry_->DoomImpl();
271 result_ = net::OK;
272 break;
273 case OP_FLUSH_QUEUE:
274 result_ = net::OK;
275 break;
276 case OP_RUN_TASK:
277 task_.Run();
278 result_ = net::OK;
279 break;
280 default:
281 NOTREACHED() << "Invalid Operation";
282 result_ = net::ERR_UNEXPECTED;
284 DCHECK_NE(net::ERR_IO_PENDING, result_);
285 NotifyController();
288 // Runs on the background thread.
289 void BackendIO::ExecuteEntryOperation() {
290 switch (operation_) {
291 case OP_READ:
292 result_ =
293 entry_->ReadDataImpl(index_, offset_, buf_.get(), buf_len_,
294 base::Bind(&BackendIO::OnIOComplete, this));
295 break;
296 case OP_WRITE:
297 result_ =
298 entry_->WriteDataImpl(index_, offset_, buf_.get(), buf_len_,
299 base::Bind(&BackendIO::OnIOComplete, this),
300 truncate_);
301 break;
302 case OP_READ_SPARSE:
303 result_ = entry_->ReadSparseDataImpl(
304 offset64_, buf_.get(), buf_len_,
305 base::Bind(&BackendIO::OnIOComplete, this));
306 break;
307 case OP_WRITE_SPARSE:
308 result_ = entry_->WriteSparseDataImpl(
309 offset64_, buf_.get(), buf_len_,
310 base::Bind(&BackendIO::OnIOComplete, this));
311 break;
312 case OP_GET_RANGE:
313 result_ = entry_->GetAvailableRangeImpl(offset64_, buf_len_, start_);
314 break;
315 case OP_CANCEL_IO:
316 entry_->CancelSparseIOImpl();
317 result_ = net::OK;
318 break;
319 case OP_IS_READY:
320 result_ = entry_->ReadyForSparseIOImpl(
321 base::Bind(&BackendIO::OnIOComplete, this));
322 break;
323 default:
324 NOTREACHED() << "Invalid Operation";
325 result_ = net::ERR_UNEXPECTED;
327 buf_ = NULL;
328 if (result_ != net::ERR_IO_PENDING)
329 NotifyController();
332 InFlightBackendIO::InFlightBackendIO(
333 BackendImpl* backend,
334 const scoped_refptr<base::SingleThreadTaskRunner>& background_thread)
335 : backend_(backend),
336 background_thread_(background_thread),
337 ptr_factory_(this) {
340 InFlightBackendIO::~InFlightBackendIO() {
343 void InFlightBackendIO::Init(const net::CompletionCallback& callback) {
344 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback));
345 operation->Init();
346 PostOperation(operation.get());
349 void InFlightBackendIO::OpenEntry(const std::string& key, Entry** entry,
350 const net::CompletionCallback& callback) {
351 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback));
352 operation->OpenEntry(key, entry);
353 PostOperation(operation.get());
356 void InFlightBackendIO::CreateEntry(const std::string& key, Entry** entry,
357 const net::CompletionCallback& callback) {
358 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback));
359 operation->CreateEntry(key, entry);
360 PostOperation(operation.get());
363 void InFlightBackendIO::DoomEntry(const std::string& key,
364 const net::CompletionCallback& callback) {
365 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback));
366 operation->DoomEntry(key);
367 PostOperation(operation.get());
370 void InFlightBackendIO::DoomAllEntries(
371 const net::CompletionCallback& callback) {
372 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback));
373 operation->DoomAllEntries();
374 PostOperation(operation.get());
377 void InFlightBackendIO::DoomEntriesBetween(const base::Time initial_time,
378 const base::Time end_time,
379 const net::CompletionCallback& callback) {
380 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback));
381 operation->DoomEntriesBetween(initial_time, end_time);
382 PostOperation(operation.get());
385 void InFlightBackendIO::DoomEntriesSince(
386 const base::Time initial_time, const net::CompletionCallback& callback) {
387 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback));
388 operation->DoomEntriesSince(initial_time);
389 PostOperation(operation.get());
392 void InFlightBackendIO::OpenNextEntry(Rankings::Iterator* iterator,
393 Entry** next_entry,
394 const net::CompletionCallback& callback) {
395 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback));
396 operation->OpenNextEntry(iterator, next_entry);
397 PostOperation(operation.get());
400 void InFlightBackendIO::EndEnumeration(
401 scoped_ptr<Rankings::Iterator> iterator) {
402 scoped_refptr<BackendIO> operation(
403 new BackendIO(this, backend_, net::CompletionCallback()));
404 operation->EndEnumeration(iterator.Pass());
405 PostOperation(operation.get());
408 void InFlightBackendIO::OnExternalCacheHit(const std::string& key) {
409 scoped_refptr<BackendIO> operation(
410 new BackendIO(this, backend_, net::CompletionCallback()));
411 operation->OnExternalCacheHit(key);
412 PostOperation(operation.get());
415 void InFlightBackendIO::CloseEntryImpl(EntryImpl* entry) {
416 scoped_refptr<BackendIO> operation(
417 new BackendIO(this, backend_, net::CompletionCallback()));
418 operation->CloseEntryImpl(entry);
419 PostOperation(operation.get());
422 void InFlightBackendIO::DoomEntryImpl(EntryImpl* entry) {
423 scoped_refptr<BackendIO> operation(
424 new BackendIO(this, backend_, net::CompletionCallback()));
425 operation->DoomEntryImpl(entry);
426 PostOperation(operation.get());
429 void InFlightBackendIO::FlushQueue(const net::CompletionCallback& callback) {
430 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback));
431 operation->FlushQueue();
432 PostOperation(operation.get());
435 void InFlightBackendIO::RunTask(
436 const base::Closure& task, const net::CompletionCallback& callback) {
437 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback));
438 operation->RunTask(task);
439 PostOperation(operation.get());
442 void InFlightBackendIO::ReadData(EntryImpl* entry, int index, int offset,
443 net::IOBuffer* buf, int buf_len,
444 const net::CompletionCallback& callback) {
445 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback));
446 operation->ReadData(entry, index, offset, buf, buf_len);
447 PostOperation(operation.get());
450 void InFlightBackendIO::WriteData(EntryImpl* entry, int index, int offset,
451 net::IOBuffer* buf, int buf_len,
452 bool truncate,
453 const net::CompletionCallback& callback) {
454 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback));
455 operation->WriteData(entry, index, offset, buf, buf_len, truncate);
456 PostOperation(operation.get());
459 void InFlightBackendIO::ReadSparseData(
460 EntryImpl* entry, int64 offset, net::IOBuffer* buf, int buf_len,
461 const net::CompletionCallback& callback) {
462 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback));
463 operation->ReadSparseData(entry, offset, buf, buf_len);
464 PostOperation(operation.get());
467 void InFlightBackendIO::WriteSparseData(
468 EntryImpl* entry, int64 offset, net::IOBuffer* buf, int buf_len,
469 const net::CompletionCallback& callback) {
470 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback));
471 operation->WriteSparseData(entry, offset, buf, buf_len);
472 PostOperation(operation.get());
475 void InFlightBackendIO::GetAvailableRange(
476 EntryImpl* entry, int64 offset, int len, int64* start,
477 const net::CompletionCallback& callback) {
478 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback));
479 operation->GetAvailableRange(entry, offset, len, start);
480 PostOperation(operation.get());
483 void InFlightBackendIO::CancelSparseIO(EntryImpl* entry) {
484 scoped_refptr<BackendIO> operation(
485 new BackendIO(this, backend_, net::CompletionCallback()));
486 operation->CancelSparseIO(entry);
487 PostOperation(operation.get());
490 void InFlightBackendIO::ReadyForSparseIO(
491 EntryImpl* entry, const net::CompletionCallback& callback) {
492 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback));
493 operation->ReadyForSparseIO(entry);
494 PostOperation(operation.get());
497 void InFlightBackendIO::WaitForPendingIO() {
498 InFlightIO::WaitForPendingIO();
501 void InFlightBackendIO::OnOperationComplete(BackgroundIO* operation,
502 bool cancel) {
503 BackendIO* op = static_cast<BackendIO*>(operation);
504 op->OnDone(cancel);
506 if (!op->callback().is_null() && (!cancel || op->IsEntryOperation())) {
507 // TODO(vadimt): Remove ScopedProfile below once crbug.com/422516 is fixed.
508 tracked_objects::ScopedProfile tracking_profile(
509 FROM_HERE_WITH_EXPLICIT_FUNCTION(
510 "422516 InFlightBackendIO::OnOperationComplete"));
512 op->callback().Run(op->result());
516 void InFlightBackendIO::PostOperation(BackendIO* operation) {
517 background_thread_->PostTask(
518 FROM_HERE, base::Bind(&BackendIO::ExecuteOperation, operation));
519 OnOperationPosted(operation);
522 base::WeakPtr<InFlightBackendIO> InFlightBackendIO::GetWeakPtr() {
523 return ptr_factory_.GetWeakPtr();
526 } // namespace