| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/disk_cache/in_flight_backend_io.h" | 5 #include "net/disk_cache/in_flight_backend_io.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 result_ = net::ERR_UNEXPECTED; | 280 result_ = net::ERR_UNEXPECTED; |
| 281 } | 281 } |
| 282 DCHECK_NE(net::ERR_IO_PENDING, result_); | 282 DCHECK_NE(net::ERR_IO_PENDING, result_); |
| 283 NotifyController(); | 283 NotifyController(); |
| 284 } | 284 } |
| 285 | 285 |
| 286 // Runs on the background thread. | 286 // Runs on the background thread. |
| 287 void BackendIO::ExecuteEntryOperation() { | 287 void BackendIO::ExecuteEntryOperation() { |
| 288 switch (operation_) { | 288 switch (operation_) { |
| 289 case OP_READ: | 289 case OP_READ: |
| 290 result_ = entry_->ReadDataImpl( | 290 result_ = |
| 291 index_, offset_, buf_, buf_len_, | 291 entry_->ReadDataImpl(index_, offset_, buf_.get(), buf_len_, |
| 292 base::Bind(&BackendIO::OnIOComplete, this)); | 292 base::Bind(&BackendIO::OnIOComplete, this)); |
| 293 break; | 293 break; |
| 294 case OP_WRITE: | 294 case OP_WRITE: |
| 295 result_ = entry_->WriteDataImpl( | 295 result_ = |
| 296 index_, offset_, buf_, buf_len_, | 296 entry_->WriteDataImpl(index_, offset_, buf_.get(), buf_len_, |
| 297 base::Bind(&BackendIO::OnIOComplete, this), | 297 base::Bind(&BackendIO::OnIOComplete, this), |
| 298 truncate_); | 298 truncate_); |
| 299 break; | 299 break; |
| 300 case OP_READ_SPARSE: | 300 case OP_READ_SPARSE: |
| 301 result_ = entry_->ReadSparseDataImpl( | 301 result_ = entry_->ReadSparseDataImpl( |
| 302 offset64_, buf_, buf_len_, | 302 offset64_, buf_.get(), buf_len_, |
| 303 base::Bind(&BackendIO::OnIOComplete, this)); | 303 base::Bind(&BackendIO::OnIOComplete, this)); |
| 304 break; | 304 break; |
| 305 case OP_WRITE_SPARSE: | 305 case OP_WRITE_SPARSE: |
| 306 result_ = entry_->WriteSparseDataImpl( | 306 result_ = entry_->WriteSparseDataImpl( |
| 307 offset64_, buf_, buf_len_, | 307 offset64_, buf_.get(), buf_len_, |
| 308 base::Bind(&BackendIO::OnIOComplete, this)); | 308 base::Bind(&BackendIO::OnIOComplete, this)); |
| 309 break; | 309 break; |
| 310 case OP_GET_RANGE: | 310 case OP_GET_RANGE: |
| 311 result_ = entry_->GetAvailableRangeImpl(offset64_, buf_len_, start_); | 311 result_ = entry_->GetAvailableRangeImpl(offset64_, buf_len_, start_); |
| 312 break; | 312 break; |
| 313 case OP_CANCEL_IO: | 313 case OP_CANCEL_IO: |
| 314 entry_->CancelSparseIOImpl(); | 314 entry_->CancelSparseIOImpl(); |
| 315 result_ = net::OK; | 315 result_ = net::OK; |
| 316 break; | 316 break; |
| 317 case OP_IS_READY: | 317 case OP_IS_READY: |
| 318 result_ = entry_->ReadyForSparseIOImpl( | 318 result_ = entry_->ReadyForSparseIOImpl( |
| 319 base::Bind(&BackendIO::OnIOComplete, this)); | 319 base::Bind(&BackendIO::OnIOComplete, this)); |
| 320 break; | 320 break; |
| 321 default: | 321 default: |
| 322 NOTREACHED() << "Invalid Operation"; | 322 NOTREACHED() << "Invalid Operation"; |
| 323 result_ = net::ERR_UNEXPECTED; | 323 result_ = net::ERR_UNEXPECTED; |
| 324 } | 324 } |
| 325 buf_ = NULL; | 325 buf_ = NULL; |
| 326 if (result_ != net::ERR_IO_PENDING) | 326 if (result_ != net::ERR_IO_PENDING) |
| 327 NotifyController(); | 327 NotifyController(); |
| 328 } | 328 } |
| 329 | 329 |
| 330 InFlightBackendIO::InFlightBackendIO(BackendImpl* backend, | 330 InFlightBackendIO::InFlightBackendIO(BackendImpl* backend, |
| 331 base::MessageLoopProxy* background_thread) | 331 base::MessageLoopProxy* background_thread) |
| 332 : backend_(backend), | 332 : backend_(backend), |
| 333 background_thread_(background_thread), | 333 background_thread_(background_thread), |
| 334 ptr_factory_(this) { | 334 ptr_factory_(this) { |
| 335 } | 335 } |
| 336 | 336 |
| 337 InFlightBackendIO::~InFlightBackendIO() { | 337 InFlightBackendIO::~InFlightBackendIO() { |
| 338 } | 338 } |
| 339 | 339 |
| 340 void InFlightBackendIO::Init(const net::CompletionCallback& callback) { | 340 void InFlightBackendIO::Init(const net::CompletionCallback& callback) { |
| 341 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); | 341 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
| 342 operation->Init(); | 342 operation->Init(); |
| 343 PostOperation(operation); | 343 PostOperation(operation.get()); |
| 344 } | 344 } |
| 345 | 345 |
| 346 void InFlightBackendIO::OpenEntry(const std::string& key, Entry** entry, | 346 void InFlightBackendIO::OpenEntry(const std::string& key, Entry** entry, |
| 347 const net::CompletionCallback& callback) { | 347 const net::CompletionCallback& callback) { |
| 348 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); | 348 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
| 349 operation->OpenEntry(key, entry); | 349 operation->OpenEntry(key, entry); |
| 350 PostOperation(operation); | 350 PostOperation(operation.get()); |
| 351 } | 351 } |
| 352 | 352 |
| 353 void InFlightBackendIO::CreateEntry(const std::string& key, Entry** entry, | 353 void InFlightBackendIO::CreateEntry(const std::string& key, Entry** entry, |
| 354 const net::CompletionCallback& callback) { | 354 const net::CompletionCallback& callback) { |
| 355 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); | 355 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
| 356 operation->CreateEntry(key, entry); | 356 operation->CreateEntry(key, entry); |
| 357 PostOperation(operation); | 357 PostOperation(operation.get()); |
| 358 } | 358 } |
| 359 | 359 |
| 360 void InFlightBackendIO::DoomEntry(const std::string& key, | 360 void InFlightBackendIO::DoomEntry(const std::string& key, |
| 361 const net::CompletionCallback& callback) { | 361 const net::CompletionCallback& callback) { |
| 362 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); | 362 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
| 363 operation->DoomEntry(key); | 363 operation->DoomEntry(key); |
| 364 PostOperation(operation); | 364 PostOperation(operation.get()); |
| 365 } | 365 } |
| 366 | 366 |
| 367 void InFlightBackendIO::DoomAllEntries( | 367 void InFlightBackendIO::DoomAllEntries( |
| 368 const net::CompletionCallback& callback) { | 368 const net::CompletionCallback& callback) { |
| 369 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); | 369 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
| 370 operation->DoomAllEntries(); | 370 operation->DoomAllEntries(); |
| 371 PostOperation(operation); | 371 PostOperation(operation.get()); |
| 372 } | 372 } |
| 373 | 373 |
| 374 void InFlightBackendIO::DoomEntriesBetween(const base::Time initial_time, | 374 void InFlightBackendIO::DoomEntriesBetween(const base::Time initial_time, |
| 375 const base::Time end_time, | 375 const base::Time end_time, |
| 376 const net::CompletionCallback& callback) { | 376 const net::CompletionCallback& callback) { |
| 377 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); | 377 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
| 378 operation->DoomEntriesBetween(initial_time, end_time); | 378 operation->DoomEntriesBetween(initial_time, end_time); |
| 379 PostOperation(operation); | 379 PostOperation(operation.get()); |
| 380 } | 380 } |
| 381 | 381 |
| 382 void InFlightBackendIO::DoomEntriesSince( | 382 void InFlightBackendIO::DoomEntriesSince( |
| 383 const base::Time initial_time, const net::CompletionCallback& callback) { | 383 const base::Time initial_time, const net::CompletionCallback& callback) { |
| 384 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); | 384 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
| 385 operation->DoomEntriesSince(initial_time); | 385 operation->DoomEntriesSince(initial_time); |
| 386 PostOperation(operation); | 386 PostOperation(operation.get()); |
| 387 } | 387 } |
| 388 | 388 |
| 389 void InFlightBackendIO::OpenNextEntry(void** iter, Entry** next_entry, | 389 void InFlightBackendIO::OpenNextEntry(void** iter, Entry** next_entry, |
| 390 const net::CompletionCallback& callback) { | 390 const net::CompletionCallback& callback) { |
| 391 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); | 391 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
| 392 operation->OpenNextEntry(iter, next_entry); | 392 operation->OpenNextEntry(iter, next_entry); |
| 393 PostOperation(operation); | 393 PostOperation(operation.get()); |
| 394 } | 394 } |
| 395 | 395 |
| 396 void InFlightBackendIO::OpenPrevEntry(void** iter, Entry** prev_entry, | 396 void InFlightBackendIO::OpenPrevEntry(void** iter, Entry** prev_entry, |
| 397 const net::CompletionCallback& callback) { | 397 const net::CompletionCallback& callback) { |
| 398 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); | 398 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
| 399 operation->OpenPrevEntry(iter, prev_entry); | 399 operation->OpenPrevEntry(iter, prev_entry); |
| 400 PostOperation(operation); | 400 PostOperation(operation.get()); |
| 401 } | 401 } |
| 402 | 402 |
| 403 void InFlightBackendIO::EndEnumeration(void* iterator) { | 403 void InFlightBackendIO::EndEnumeration(void* iterator) { |
| 404 scoped_refptr<BackendIO> operation( | 404 scoped_refptr<BackendIO> operation( |
| 405 new BackendIO(this, backend_, net::CompletionCallback())); | 405 new BackendIO(this, backend_, net::CompletionCallback())); |
| 406 operation->EndEnumeration(iterator); | 406 operation->EndEnumeration(iterator); |
| 407 PostOperation(operation); | 407 PostOperation(operation.get()); |
| 408 } | 408 } |
| 409 | 409 |
| 410 void InFlightBackendIO::OnExternalCacheHit(const std::string& key) { | 410 void InFlightBackendIO::OnExternalCacheHit(const std::string& key) { |
| 411 scoped_refptr<BackendIO> operation( | 411 scoped_refptr<BackendIO> operation( |
| 412 new BackendIO(this, backend_, net::CompletionCallback())); | 412 new BackendIO(this, backend_, net::CompletionCallback())); |
| 413 operation->OnExternalCacheHit(key); | 413 operation->OnExternalCacheHit(key); |
| 414 PostOperation(operation); | 414 PostOperation(operation.get()); |
| 415 } | 415 } |
| 416 | 416 |
| 417 void InFlightBackendIO::CloseEntryImpl(EntryImpl* entry) { | 417 void InFlightBackendIO::CloseEntryImpl(EntryImpl* entry) { |
| 418 scoped_refptr<BackendIO> operation( | 418 scoped_refptr<BackendIO> operation( |
| 419 new BackendIO(this, backend_, net::CompletionCallback())); | 419 new BackendIO(this, backend_, net::CompletionCallback())); |
| 420 operation->CloseEntryImpl(entry); | 420 operation->CloseEntryImpl(entry); |
| 421 PostOperation(operation); | 421 PostOperation(operation.get()); |
| 422 } | 422 } |
| 423 | 423 |
| 424 void InFlightBackendIO::DoomEntryImpl(EntryImpl* entry) { | 424 void InFlightBackendIO::DoomEntryImpl(EntryImpl* entry) { |
| 425 scoped_refptr<BackendIO> operation( | 425 scoped_refptr<BackendIO> operation( |
| 426 new BackendIO(this, backend_, net::CompletionCallback())); | 426 new BackendIO(this, backend_, net::CompletionCallback())); |
| 427 operation->DoomEntryImpl(entry); | 427 operation->DoomEntryImpl(entry); |
| 428 PostOperation(operation); | 428 PostOperation(operation.get()); |
| 429 } | 429 } |
| 430 | 430 |
| 431 void InFlightBackendIO::FlushQueue(const net::CompletionCallback& callback) { | 431 void InFlightBackendIO::FlushQueue(const net::CompletionCallback& callback) { |
| 432 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); | 432 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
| 433 operation->FlushQueue(); | 433 operation->FlushQueue(); |
| 434 PostOperation(operation); | 434 PostOperation(operation.get()); |
| 435 } | 435 } |
| 436 | 436 |
| 437 void InFlightBackendIO::RunTask( | 437 void InFlightBackendIO::RunTask( |
| 438 const base::Closure& task, const net::CompletionCallback& callback) { | 438 const base::Closure& task, const net::CompletionCallback& callback) { |
| 439 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); | 439 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
| 440 operation->RunTask(task); | 440 operation->RunTask(task); |
| 441 PostOperation(operation); | 441 PostOperation(operation.get()); |
| 442 } | 442 } |
| 443 | 443 |
| 444 void InFlightBackendIO::ReadData(EntryImpl* entry, int index, int offset, | 444 void InFlightBackendIO::ReadData(EntryImpl* entry, int index, int offset, |
| 445 net::IOBuffer* buf, int buf_len, | 445 net::IOBuffer* buf, int buf_len, |
| 446 const net::CompletionCallback& callback) { | 446 const net::CompletionCallback& callback) { |
| 447 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); | 447 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
| 448 operation->ReadData(entry, index, offset, buf, buf_len); | 448 operation->ReadData(entry, index, offset, buf, buf_len); |
| 449 PostOperation(operation); | 449 PostOperation(operation.get()); |
| 450 } | 450 } |
| 451 | 451 |
| 452 void InFlightBackendIO::WriteData(EntryImpl* entry, int index, int offset, | 452 void InFlightBackendIO::WriteData(EntryImpl* entry, int index, int offset, |
| 453 net::IOBuffer* buf, int buf_len, | 453 net::IOBuffer* buf, int buf_len, |
| 454 bool truncate, | 454 bool truncate, |
| 455 const net::CompletionCallback& callback) { | 455 const net::CompletionCallback& callback) { |
| 456 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); | 456 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
| 457 operation->WriteData(entry, index, offset, buf, buf_len, truncate); | 457 operation->WriteData(entry, index, offset, buf, buf_len, truncate); |
| 458 PostOperation(operation); | 458 PostOperation(operation.get()); |
| 459 } | 459 } |
| 460 | 460 |
| 461 void InFlightBackendIO::ReadSparseData( | 461 void InFlightBackendIO::ReadSparseData( |
| 462 EntryImpl* entry, int64 offset, net::IOBuffer* buf, int buf_len, | 462 EntryImpl* entry, int64 offset, net::IOBuffer* buf, int buf_len, |
| 463 const net::CompletionCallback& callback) { | 463 const net::CompletionCallback& callback) { |
| 464 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); | 464 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
| 465 operation->ReadSparseData(entry, offset, buf, buf_len); | 465 operation->ReadSparseData(entry, offset, buf, buf_len); |
| 466 PostOperation(operation); | 466 PostOperation(operation.get()); |
| 467 } | 467 } |
| 468 | 468 |
| 469 void InFlightBackendIO::WriteSparseData( | 469 void InFlightBackendIO::WriteSparseData( |
| 470 EntryImpl* entry, int64 offset, net::IOBuffer* buf, int buf_len, | 470 EntryImpl* entry, int64 offset, net::IOBuffer* buf, int buf_len, |
| 471 const net::CompletionCallback& callback) { | 471 const net::CompletionCallback& callback) { |
| 472 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); | 472 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
| 473 operation->WriteSparseData(entry, offset, buf, buf_len); | 473 operation->WriteSparseData(entry, offset, buf, buf_len); |
| 474 PostOperation(operation); | 474 PostOperation(operation.get()); |
| 475 } | 475 } |
| 476 | 476 |
| 477 void InFlightBackendIO::GetAvailableRange( | 477 void InFlightBackendIO::GetAvailableRange( |
| 478 EntryImpl* entry, int64 offset, int len, int64* start, | 478 EntryImpl* entry, int64 offset, int len, int64* start, |
| 479 const net::CompletionCallback& callback) { | 479 const net::CompletionCallback& callback) { |
| 480 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); | 480 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
| 481 operation->GetAvailableRange(entry, offset, len, start); | 481 operation->GetAvailableRange(entry, offset, len, start); |
| 482 PostOperation(operation); | 482 PostOperation(operation.get()); |
| 483 } | 483 } |
| 484 | 484 |
| 485 void InFlightBackendIO::CancelSparseIO(EntryImpl* entry) { | 485 void InFlightBackendIO::CancelSparseIO(EntryImpl* entry) { |
| 486 scoped_refptr<BackendIO> operation( | 486 scoped_refptr<BackendIO> operation( |
| 487 new BackendIO(this, backend_, net::CompletionCallback())); | 487 new BackendIO(this, backend_, net::CompletionCallback())); |
| 488 operation->CancelSparseIO(entry); | 488 operation->CancelSparseIO(entry); |
| 489 PostOperation(operation); | 489 PostOperation(operation.get()); |
| 490 } | 490 } |
| 491 | 491 |
| 492 void InFlightBackendIO::ReadyForSparseIO( | 492 void InFlightBackendIO::ReadyForSparseIO( |
| 493 EntryImpl* entry, const net::CompletionCallback& callback) { | 493 EntryImpl* entry, const net::CompletionCallback& callback) { |
| 494 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); | 494 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
| 495 operation->ReadyForSparseIO(entry); | 495 operation->ReadyForSparseIO(entry); |
| 496 PostOperation(operation); | 496 PostOperation(operation.get()); |
| 497 } | 497 } |
| 498 | 498 |
| 499 void InFlightBackendIO::WaitForPendingIO() { | 499 void InFlightBackendIO::WaitForPendingIO() { |
| 500 InFlightIO::WaitForPendingIO(); | 500 InFlightIO::WaitForPendingIO(); |
| 501 } | 501 } |
| 502 | 502 |
| 503 void InFlightBackendIO::OnOperationComplete(BackgroundIO* operation, | 503 void InFlightBackendIO::OnOperationComplete(BackgroundIO* operation, |
| 504 bool cancel) { | 504 bool cancel) { |
| 505 BackendIO* op = static_cast<BackendIO*>(operation); | 505 BackendIO* op = static_cast<BackendIO*>(operation); |
| 506 op->OnDone(cancel); | 506 op->OnDone(cancel); |
| 507 | 507 |
| 508 if (!op->callback().is_null() && (!cancel || op->IsEntryOperation())) | 508 if (!op->callback().is_null() && (!cancel || op->IsEntryOperation())) |
| 509 op->callback().Run(op->result()); | 509 op->callback().Run(op->result()); |
| 510 } | 510 } |
| 511 | 511 |
| 512 void InFlightBackendIO::PostOperation(BackendIO* operation) { | 512 void InFlightBackendIO::PostOperation(BackendIO* operation) { |
| 513 background_thread_->PostTask(FROM_HERE, | 513 background_thread_->PostTask(FROM_HERE, |
| 514 base::Bind(&BackendIO::ExecuteOperation, operation)); | 514 base::Bind(&BackendIO::ExecuteOperation, operation)); |
| 515 OnOperationPosted(operation); | 515 OnOperationPosted(operation); |
| 516 } | 516 } |
| 517 | 517 |
| 518 base::WeakPtr<InFlightBackendIO> InFlightBackendIO::GetWeakPtr() { | 518 base::WeakPtr<InFlightBackendIO> InFlightBackendIO::GetWeakPtr() { |
| 519 return ptr_factory_.GetWeakPtr(); | 519 return ptr_factory_.GetWeakPtr(); |
| 520 } | 520 } |
| 521 | 521 |
| 522 } // namespace | 522 } // namespace |
| OLD | NEW |