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"
8 #include "base/bind_helpers.h"
9 #include "base/compiler_specific.h"
10 #include "base/logging.h"
11 #include "base/profiler/scoped_tracker.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
),
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
);
57 // Runs on the primary thread.
58 void BackendIO::OnDone(bool cancel
) {
59 if (IsEntryOperation()) {
60 CACHE_UMA(TIMES
, "TotalIOTime", 0, ElapsedTime());
66 if (result() == net::OK
) {
67 static_cast<EntryImpl
*>(*entry_ptr_
)->OnEntryCreated(backend_
);
69 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is
71 tracked_objects::ScopedTracker
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() {
88 void BackendIO::Init() {
92 void BackendIO::OpenEntry(const std::string
& key
, Entry
** entry
) {
98 void BackendIO::CreateEntry(const std::string
& key
, Entry
** entry
) {
99 operation_
= OP_CREATE
;
104 void BackendIO::DoomEntry(const std::string
& key
) {
105 operation_
= OP_DOOM
;
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
;
142 void BackendIO::CloseEntryImpl(EntryImpl
* entry
) {
143 operation_
= OP_CLOSE_ENTRY
;
147 void BackendIO::DoomEntryImpl(EntryImpl
* entry
) {
148 operation_
= OP_DOOM_ENTRY
;
152 void BackendIO::FlushQueue() {
153 operation_
= OP_FLUSH_QUEUE
;
156 void BackendIO::RunTask(const base::Closure
& task
) {
157 operation_
= OP_RUN_TASK
;
161 void BackendIO::ReadData(EntryImpl
* entry
, int index
, int offset
,
162 net::IOBuffer
* buf
, int buf_len
) {
163 operation_
= OP_READ
;
171 void BackendIO::WriteData(EntryImpl
* entry
, int index
, int offset
,
172 net::IOBuffer
* buf
, int buf_len
, bool truncate
) {
173 operation_
= OP_WRITE
;
179 truncate_
= truncate
;
182 void BackendIO::ReadSparseData(EntryImpl
* entry
, int64 offset
,
183 net::IOBuffer
* buf
, int buf_len
) {
184 operation_
= OP_READ_SPARSE
;
191 void BackendIO::WriteSparseData(EntryImpl
* entry
, int64 offset
,
192 net::IOBuffer
* buf
, int buf_len
) {
193 operation_
= OP_WRITE_SPARSE
;
200 void BackendIO::GetAvailableRange(EntryImpl
* entry
, int64 offset
, int len
,
202 operation_
= OP_GET_RANGE
;
209 void BackendIO::CancelSparseIO(EntryImpl
* entry
) {
210 operation_
= OP_CANCEL_IO
;
214 void BackendIO::ReadyForSparseIO(EntryImpl
* entry
) {
215 operation_
= OP_IS_READY
;
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_
) {
234 result_
= backend_
->SyncInit();
237 result_
= backend_
->SyncOpenEntry(key_
, entry_ptr_
);
240 result_
= backend_
->SyncCreateEntry(key_
, entry_ptr_
);
243 result_
= backend_
->SyncDoomEntry(key_
);
246 result_
= backend_
->SyncDoomAllEntries();
248 case OP_DOOM_BETWEEN
:
249 result_
= backend_
->SyncDoomEntriesBetween(initial_time_
, end_time_
);
252 result_
= backend_
->SyncDoomEntriesSince(initial_time_
);
255 result_
= backend_
->SyncOpenNextEntry(iterator_
, entry_ptr_
);
257 case OP_END_ENUMERATION
:
258 backend_
->SyncEndEnumeration(scoped_iterator_
.Pass());
261 case OP_ON_EXTERNAL_CACHE_HIT
:
262 backend_
->SyncOnExternalCacheHit(key_
);
281 NOTREACHED() << "Invalid Operation";
282 result_
= net::ERR_UNEXPECTED
;
284 DCHECK_NE(net::ERR_IO_PENDING
, result_
);
288 // Runs on the background thread.
289 void BackendIO::ExecuteEntryOperation() {
290 switch (operation_
) {
293 entry_
->ReadDataImpl(index_
, offset_
, buf_
.get(), buf_len_
,
294 base::Bind(&BackendIO::OnIOComplete
, this));
298 entry_
->WriteDataImpl(index_
, offset_
, buf_
.get(), buf_len_
,
299 base::Bind(&BackendIO::OnIOComplete
, this),
303 result_
= entry_
->ReadSparseDataImpl(
304 offset64_
, buf_
.get(), buf_len_
,
305 base::Bind(&BackendIO::OnIOComplete
, this));
307 case OP_WRITE_SPARSE
:
308 result_
= entry_
->WriteSparseDataImpl(
309 offset64_
, buf_
.get(), buf_len_
,
310 base::Bind(&BackendIO::OnIOComplete
, this));
313 result_
= entry_
->GetAvailableRangeImpl(offset64_
, buf_len_
, start_
);
316 entry_
->CancelSparseIOImpl();
320 result_
= entry_
->ReadyForSparseIOImpl(
321 base::Bind(&BackendIO::OnIOComplete
, this));
324 NOTREACHED() << "Invalid Operation";
325 result_
= net::ERR_UNEXPECTED
;
328 if (result_
!= net::ERR_IO_PENDING
)
332 InFlightBackendIO::InFlightBackendIO(
333 BackendImpl
* backend
,
334 const scoped_refptr
<base::SingleThreadTaskRunner
>& background_thread
)
336 background_thread_(background_thread
),
340 InFlightBackendIO::~InFlightBackendIO() {
343 void InFlightBackendIO::Init(const net::CompletionCallback
& callback
) {
344 // TODO(vadimt): Remove wrapping the callback with
345 // ScopedTracker::TrackCallback() once crbug.com/422516 is fixed.
346 scoped_refptr
<BackendIO
> operation(new BackendIO(
348 tracked_objects::ScopedTracker::TrackCallback(
349 FROM_HERE_WITH_EXPLICIT_FUNCTION("422516 InFlightBackendIO::Init"),
352 PostOperation(operation
.get());
355 void InFlightBackendIO::OpenEntry(const std::string
& key
, Entry
** entry
,
356 const net::CompletionCallback
& callback
) {
357 scoped_refptr
<BackendIO
> operation(new BackendIO(
358 this, backend_
, tracked_objects::ScopedTracker::TrackCallback(
359 FROM_HERE_WITH_EXPLICIT_FUNCTION(
360 "422516 InFlightBackendIO::OpenEntry"),
362 operation
->OpenEntry(key
, entry
);
363 PostOperation(operation
.get());
366 void InFlightBackendIO::CreateEntry(const std::string
& key
, Entry
** entry
,
367 const net::CompletionCallback
& callback
) {
368 scoped_refptr
<BackendIO
> operation(new BackendIO(
369 this, backend_
, tracked_objects::ScopedTracker::TrackCallback(
370 FROM_HERE_WITH_EXPLICIT_FUNCTION(
371 "422516 InFlightBackendIO::CreateEntry"),
373 operation
->CreateEntry(key
, entry
);
374 PostOperation(operation
.get());
377 void InFlightBackendIO::DoomEntry(const std::string
& key
,
378 const net::CompletionCallback
& callback
) {
379 scoped_refptr
<BackendIO
> operation(new BackendIO(
380 this, backend_
, tracked_objects::ScopedTracker::TrackCallback(
381 FROM_HERE_WITH_EXPLICIT_FUNCTION(
382 "422516 InFlightBackendIO::DoomEntry"),
384 operation
->DoomEntry(key
);
385 PostOperation(operation
.get());
388 void InFlightBackendIO::DoomAllEntries(
389 const net::CompletionCallback
& callback
) {
390 scoped_refptr
<BackendIO
> operation(new BackendIO(
391 this, backend_
, tracked_objects::ScopedTracker::TrackCallback(
392 FROM_HERE_WITH_EXPLICIT_FUNCTION(
393 "422516 InFlightBackendIO::DoomAllEntries"),
395 operation
->DoomAllEntries();
396 PostOperation(operation
.get());
399 void InFlightBackendIO::DoomEntriesBetween(const base::Time initial_time
,
400 const base::Time end_time
,
401 const net::CompletionCallback
& callback
) {
402 scoped_refptr
<BackendIO
> operation(new BackendIO(
403 this, backend_
, tracked_objects::ScopedTracker::TrackCallback(
404 FROM_HERE_WITH_EXPLICIT_FUNCTION(
405 "422516 InFlightBackendIO::DoomEntriesBetween"),
407 operation
->DoomEntriesBetween(initial_time
, end_time
);
408 PostOperation(operation
.get());
411 void InFlightBackendIO::DoomEntriesSince(
412 const base::Time initial_time
, const net::CompletionCallback
& callback
) {
413 scoped_refptr
<BackendIO
> operation(new BackendIO(
414 this, backend_
, tracked_objects::ScopedTracker::TrackCallback(
415 FROM_HERE_WITH_EXPLICIT_FUNCTION(
416 "422516 InFlightBackendIO::DoomEntriesSince"),
418 operation
->DoomEntriesSince(initial_time
);
419 PostOperation(operation
.get());
422 void InFlightBackendIO::OpenNextEntry(Rankings::Iterator
* iterator
,
424 const net::CompletionCallback
& callback
) {
425 scoped_refptr
<BackendIO
> operation(new BackendIO(
426 this, backend_
, tracked_objects::ScopedTracker::TrackCallback(
427 FROM_HERE_WITH_EXPLICIT_FUNCTION(
428 "422516 InFlightBackendIO::OpenNextEntry"),
430 operation
->OpenNextEntry(iterator
, next_entry
);
431 PostOperation(operation
.get());
434 void InFlightBackendIO::EndEnumeration(
435 scoped_ptr
<Rankings::Iterator
> iterator
) {
436 scoped_refptr
<BackendIO
> operation(
437 new BackendIO(this, backend_
, net::CompletionCallback()));
438 operation
->EndEnumeration(iterator
.Pass());
439 PostOperation(operation
.get());
442 void InFlightBackendIO::OnExternalCacheHit(const std::string
& key
) {
443 scoped_refptr
<BackendIO
> operation(
444 new BackendIO(this, backend_
, net::CompletionCallback()));
445 operation
->OnExternalCacheHit(key
);
446 PostOperation(operation
.get());
449 void InFlightBackendIO::CloseEntryImpl(EntryImpl
* entry
) {
450 scoped_refptr
<BackendIO
> operation(
451 new BackendIO(this, backend_
, net::CompletionCallback()));
452 operation
->CloseEntryImpl(entry
);
453 PostOperation(operation
.get());
456 void InFlightBackendIO::DoomEntryImpl(EntryImpl
* entry
) {
457 scoped_refptr
<BackendIO
> operation(
458 new BackendIO(this, backend_
, net::CompletionCallback()));
459 operation
->DoomEntryImpl(entry
);
460 PostOperation(operation
.get());
463 void InFlightBackendIO::FlushQueue(const net::CompletionCallback
& callback
) {
464 scoped_refptr
<BackendIO
> operation(new BackendIO(
465 this, backend_
, tracked_objects::ScopedTracker::TrackCallback(
466 FROM_HERE_WITH_EXPLICIT_FUNCTION(
467 "422516 InFlightBackendIO::FlushQueue"),
469 operation
->FlushQueue();
470 PostOperation(operation
.get());
473 void InFlightBackendIO::RunTask(
474 const base::Closure
& task
, const net::CompletionCallback
& callback
) {
475 scoped_refptr
<BackendIO
> operation(new BackendIO(
477 tracked_objects::ScopedTracker::TrackCallback(
478 FROM_HERE_WITH_EXPLICIT_FUNCTION("422516 InFlightBackendIO::RunTask"),
480 operation
->RunTask(task
);
481 PostOperation(operation
.get());
484 void InFlightBackendIO::ReadData(EntryImpl
* entry
, int index
, int offset
,
485 net::IOBuffer
* buf
, int buf_len
,
486 const net::CompletionCallback
& callback
) {
487 scoped_refptr
<BackendIO
> operation(new BackendIO(
488 this, backend_
, tracked_objects::ScopedTracker::TrackCallback(
489 FROM_HERE_WITH_EXPLICIT_FUNCTION(
490 "422516 InFlightBackendIO::ReadData"),
492 operation
->ReadData(entry
, index
, offset
, buf
, buf_len
);
493 PostOperation(operation
.get());
496 void InFlightBackendIO::WriteData(EntryImpl
* entry
, int index
, int offset
,
497 net::IOBuffer
* buf
, int buf_len
,
499 const net::CompletionCallback
& callback
) {
500 scoped_refptr
<BackendIO
> operation(new BackendIO(
501 this, backend_
, tracked_objects::ScopedTracker::TrackCallback(
502 FROM_HERE_WITH_EXPLICIT_FUNCTION(
503 "422516 InFlightBackendIO::WriteData"),
505 operation
->WriteData(entry
, index
, offset
, buf
, buf_len
, truncate
);
506 PostOperation(operation
.get());
509 void InFlightBackendIO::ReadSparseData(
510 EntryImpl
* entry
, int64 offset
, net::IOBuffer
* buf
, int buf_len
,
511 const net::CompletionCallback
& callback
) {
512 scoped_refptr
<BackendIO
> operation(new BackendIO(
513 this, backend_
, tracked_objects::ScopedTracker::TrackCallback(
514 FROM_HERE_WITH_EXPLICIT_FUNCTION(
515 "422516 InFlightBackendIO::ReadSparseData"),
517 operation
->ReadSparseData(entry
, offset
, buf
, buf_len
);
518 PostOperation(operation
.get());
521 void InFlightBackendIO::WriteSparseData(
522 EntryImpl
* entry
, int64 offset
, net::IOBuffer
* buf
, int buf_len
,
523 const net::CompletionCallback
& callback
) {
524 scoped_refptr
<BackendIO
> operation(new BackendIO(
525 this, backend_
, tracked_objects::ScopedTracker::TrackCallback(
526 FROM_HERE_WITH_EXPLICIT_FUNCTION(
527 "422516 InFlightBackendIO::WriteSparseData"),
529 operation
->WriteSparseData(entry
, offset
, buf
, buf_len
);
530 PostOperation(operation
.get());
533 void InFlightBackendIO::GetAvailableRange(
534 EntryImpl
* entry
, int64 offset
, int len
, int64
* start
,
535 const net::CompletionCallback
& callback
) {
536 scoped_refptr
<BackendIO
> operation(new BackendIO(
537 this, backend_
, tracked_objects::ScopedTracker::TrackCallback(
538 FROM_HERE_WITH_EXPLICIT_FUNCTION(
539 "422516 InFlightBackendIO::GetAvailableRange"),
541 operation
->GetAvailableRange(entry
, offset
, len
, start
);
542 PostOperation(operation
.get());
545 void InFlightBackendIO::CancelSparseIO(EntryImpl
* entry
) {
546 scoped_refptr
<BackendIO
> operation(
547 new BackendIO(this, backend_
, net::CompletionCallback()));
548 operation
->CancelSparseIO(entry
);
549 PostOperation(operation
.get());
552 void InFlightBackendIO::ReadyForSparseIO(
553 EntryImpl
* entry
, const net::CompletionCallback
& callback
) {
554 scoped_refptr
<BackendIO
> operation(new BackendIO(
555 this, backend_
, tracked_objects::ScopedTracker::TrackCallback(
556 FROM_HERE_WITH_EXPLICIT_FUNCTION(
557 "422516 InFlightBackendIO::CancelSparseIO"),
559 operation
->ReadyForSparseIO(entry
);
560 PostOperation(operation
.get());
563 void InFlightBackendIO::WaitForPendingIO() {
564 InFlightIO::WaitForPendingIO();
567 void InFlightBackendIO::OnOperationComplete(BackgroundIO
* operation
,
569 BackendIO
* op
= static_cast<BackendIO
*>(operation
);
572 if (!op
->callback().is_null() && (!cancel
|| op
->IsEntryOperation())) {
573 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
574 tracked_objects::ScopedTracker
tracking_profile(
575 FROM_HERE_WITH_EXPLICIT_FUNCTION(
576 "422516 InFlightBackendIO::OnOperationComplete"));
578 op
->callback().Run(op
->result());
582 void InFlightBackendIO::PostOperation(BackendIO
* operation
) {
583 background_thread_
->PostTask(
584 FROM_HERE
, base::Bind(&BackendIO::ExecuteOperation
, operation
));
585 OnOperationPosted(operation
);
588 base::WeakPtr
<InFlightBackendIO
> InFlightBackendIO::GetWeakPtr() {
589 return ptr_factory_
.GetWeakPtr();