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_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
),
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 ScopedProfile below once crbug.com/422516 is
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() {
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 scoped_refptr
<BackendIO
> operation(new BackendIO(this, backend_
, callback
));
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
,
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
,
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
,
503 BackendIO
* op
= static_cast<BackendIO
*>(operation
);
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();