| 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 24 matching lines...) Expand all Loading... |
| 35 return ExecuteEntryOperation(); | 35 return ExecuteEntryOperation(); |
| 36 | 36 |
| 37 ExecuteBackendOperation(); | 37 ExecuteBackendOperation(); |
| 38 } | 38 } |
| 39 | 39 |
| 40 // Runs on the background thread. | 40 // Runs on the background thread. |
| 41 void BackendIO::OnIOComplete(int result) { | 41 void BackendIO::OnIOComplete(int result) { |
| 42 DCHECK(IsEntryOperation()); | 42 DCHECK(IsEntryOperation()); |
| 43 DCHECK_NE(result, net::ERR_IO_PENDING); | 43 DCHECK_NE(result, net::ERR_IO_PENDING); |
| 44 result_ = result; | 44 result_ = result; |
| 45 controller_->OnIOComplete(this); | 45 NotifyController(); |
| 46 } |
| 47 |
| 48 // Runs on the primary thread. |
| 49 void BackendIO::OnDone(bool cancel) { |
| 50 if (IsEntryOperation()) { |
| 51 CACHE_UMA(TIMES, "TotalIOTime", 0, ElapsedTime()); |
| 52 } |
| 53 |
| 54 if (!ReturnsEntry()) |
| 55 return; |
| 56 |
| 57 if (result() == net::OK) { |
| 58 static_cast<EntryImpl*>(*entry_ptr_)->OnEntryCreated(backend_); |
| 59 if (cancel) |
| 60 (*entry_ptr_)->Close(); |
| 61 } |
| 46 } | 62 } |
| 47 | 63 |
| 48 bool BackendIO::IsEntryOperation() { | 64 bool BackendIO::IsEntryOperation() { |
| 49 return operation_ > OP_MAX_BACKEND; | 65 return operation_ > OP_MAX_BACKEND; |
| 50 } | 66 } |
| 51 | 67 |
| 52 // Runs on the background thread. | 68 // Runs on the background thread. |
| 53 void BackendIO::ReferenceEntry() { | 69 void BackendIO::ReferenceEntry() { |
| 54 entry_->AddRef(); | 70 entry_->AddRef(); |
| 55 } | 71 } |
| 56 | 72 |
| 57 base::TimeDelta BackendIO::ElapsedTime() const { | |
| 58 return base::TimeTicks::Now() - start_time_; | |
| 59 } | |
| 60 | |
| 61 void BackendIO::Init() { | 73 void BackendIO::Init() { |
| 62 operation_ = OP_INIT; | 74 operation_ = OP_INIT; |
| 63 } | 75 } |
| 64 | 76 |
| 65 void BackendIO::OpenEntry(const std::string& key, Entry** entry) { | 77 void BackendIO::OpenEntry(const std::string& key, Entry** entry) { |
| 66 operation_ = OP_OPEN; | 78 operation_ = OP_OPEN; |
| 67 key_ = key; | 79 key_ = key; |
| 68 entry_ptr_ = entry; | 80 entry_ptr_ = entry; |
| 69 } | 81 } |
| 70 | 82 |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 entry_ = entry; | 201 entry_ = entry; |
| 190 } | 202 } |
| 191 | 203 |
| 192 void BackendIO::ReadyForSparseIO(EntryImpl* entry) { | 204 void BackendIO::ReadyForSparseIO(EntryImpl* entry) { |
| 193 operation_ = OP_IS_READY; | 205 operation_ = OP_IS_READY; |
| 194 entry_ = entry; | 206 entry_ = entry; |
| 195 } | 207 } |
| 196 | 208 |
| 197 BackendIO::~BackendIO() {} | 209 BackendIO::~BackendIO() {} |
| 198 | 210 |
| 211 bool BackendIO::ReturnsEntry() { |
| 212 return (operation_ == OP_OPEN || operation_ == OP_CREATE || |
| 213 operation_ == OP_OPEN_NEXT || operation_ == OP_OPEN_PREV); |
| 214 } |
| 215 |
| 216 base::TimeDelta BackendIO::ElapsedTime() const { |
| 217 return base::TimeTicks::Now() - start_time_; |
| 218 } |
| 219 |
| 199 // Runs on the background thread. | 220 // Runs on the background thread. |
| 200 void BackendIO::ExecuteBackendOperation() { | 221 void BackendIO::ExecuteBackendOperation() { |
| 201 switch (operation_) { | 222 switch (operation_) { |
| 202 case OP_INIT: | 223 case OP_INIT: |
| 203 result_ = backend_->SyncInit(); | 224 result_ = backend_->SyncInit(); |
| 204 break; | 225 break; |
| 205 case OP_OPEN: | 226 case OP_OPEN: |
| 206 result_ = backend_->SyncOpenEntry(key_, entry_ptr_); | 227 result_ = backend_->SyncOpenEntry(key_, entry_ptr_); |
| 207 break; | 228 break; |
| 208 case OP_CREATE: | 229 case OP_CREATE: |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 break; | 268 break; |
| 248 case OP_RUN_TASK: | 269 case OP_RUN_TASK: |
| 249 task_.Run(); | 270 task_.Run(); |
| 250 result_ = net::OK; | 271 result_ = net::OK; |
| 251 break; | 272 break; |
| 252 default: | 273 default: |
| 253 NOTREACHED() << "Invalid Operation"; | 274 NOTREACHED() << "Invalid Operation"; |
| 254 result_ = net::ERR_UNEXPECTED; | 275 result_ = net::ERR_UNEXPECTED; |
| 255 } | 276 } |
| 256 DCHECK_NE(net::ERR_IO_PENDING, result_); | 277 DCHECK_NE(net::ERR_IO_PENDING, result_); |
| 257 controller_->OnIOComplete(this); | 278 NotifyController(); |
| 258 } | 279 } |
| 259 | 280 |
| 260 // Runs on the background thread. | 281 // Runs on the background thread. |
| 261 void BackendIO::ExecuteEntryOperation() { | 282 void BackendIO::ExecuteEntryOperation() { |
| 262 switch (operation_) { | 283 switch (operation_) { |
| 263 case OP_READ: | 284 case OP_READ: |
| 264 result_ = entry_->ReadDataImpl( | 285 result_ = entry_->ReadDataImpl( |
| 265 index_, offset_, buf_, buf_len_, | 286 index_, offset_, buf_, buf_len_, |
| 266 base::Bind(&BackendIO::OnIOComplete, base::Unretained(this))); | 287 base::Bind(&BackendIO::OnIOComplete, this)); |
| 267 break; | 288 break; |
| 268 case OP_WRITE: | 289 case OP_WRITE: |
| 269 result_ = entry_->WriteDataImpl( | 290 result_ = entry_->WriteDataImpl( |
| 270 index_, offset_, buf_, buf_len_, | 291 index_, offset_, buf_, buf_len_, |
| 271 base::Bind(&BackendIO::OnIOComplete, base::Unretained(this)), | 292 base::Bind(&BackendIO::OnIOComplete, this), |
| 272 truncate_); | 293 truncate_); |
| 273 break; | 294 break; |
| 274 case OP_READ_SPARSE: | 295 case OP_READ_SPARSE: |
| 275 result_ = entry_->ReadSparseDataImpl( | 296 result_ = entry_->ReadSparseDataImpl( |
| 276 offset64_, buf_, buf_len_, | 297 offset64_, buf_, buf_len_, |
| 277 base::Bind(&BackendIO::OnIOComplete, base::Unretained(this))); | 298 base::Bind(&BackendIO::OnIOComplete, this)); |
| 278 break; | 299 break; |
| 279 case OP_WRITE_SPARSE: | 300 case OP_WRITE_SPARSE: |
| 280 result_ = entry_->WriteSparseDataImpl( | 301 result_ = entry_->WriteSparseDataImpl( |
| 281 offset64_, buf_, buf_len_, | 302 offset64_, buf_, buf_len_, |
| 282 base::Bind(&BackendIO::OnIOComplete, base::Unretained(this))); | 303 base::Bind(&BackendIO::OnIOComplete, this)); |
| 283 break; | 304 break; |
| 284 case OP_GET_RANGE: | 305 case OP_GET_RANGE: |
| 285 result_ = entry_->GetAvailableRangeImpl(offset64_, buf_len_, start_); | 306 result_ = entry_->GetAvailableRangeImpl(offset64_, buf_len_, start_); |
| 286 break; | 307 break; |
| 287 case OP_CANCEL_IO: | 308 case OP_CANCEL_IO: |
| 288 entry_->CancelSparseIOImpl(); | 309 entry_->CancelSparseIOImpl(); |
| 289 result_ = net::OK; | 310 result_ = net::OK; |
| 290 break; | 311 break; |
| 291 case OP_IS_READY: | 312 case OP_IS_READY: |
| 292 result_ = entry_->ReadyForSparseIOImpl( | 313 result_ = entry_->ReadyForSparseIOImpl( |
| 293 base::Bind(&BackendIO::OnIOComplete, base::Unretained(this))); | 314 base::Bind(&BackendIO::OnIOComplete, this)); |
| 294 break; | 315 break; |
| 295 default: | 316 default: |
| 296 NOTREACHED() << "Invalid Operation"; | 317 NOTREACHED() << "Invalid Operation"; |
| 297 result_ = net::ERR_UNEXPECTED; | 318 result_ = net::ERR_UNEXPECTED; |
| 298 } | 319 } |
| 299 if (result_ != net::ERR_IO_PENDING) | 320 if (result_ != net::ERR_IO_PENDING) |
| 300 controller_->OnIOComplete(this); | 321 NotifyController(); |
| 301 } | 322 } |
| 302 | 323 |
| 303 InFlightBackendIO::InFlightBackendIO(BackendImpl* backend, | 324 InFlightBackendIO::InFlightBackendIO(BackendImpl* backend, |
| 304 base::MessageLoopProxy* background_thread) | 325 base::MessageLoopProxy* background_thread) |
| 305 : backend_(backend), | 326 : backend_(backend), |
| 306 background_thread_(background_thread) { | 327 background_thread_(background_thread), |
| 328 ALLOW_THIS_IN_INITIALIZER_LIST(ptr_factory_(this)) { |
| 307 } | 329 } |
| 308 | 330 |
| 309 InFlightBackendIO::~InFlightBackendIO() { | 331 InFlightBackendIO::~InFlightBackendIO() { |
| 310 } | 332 } |
| 311 | 333 |
| 312 void InFlightBackendIO::Init(const net::CompletionCallback& callback) { | 334 void InFlightBackendIO::Init(const net::CompletionCallback& callback) { |
| 313 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); | 335 scoped_refptr<BackendIO> operation(new BackendIO(this, backend_, callback)); |
| 314 operation->Init(); | 336 operation->Init(); |
| 315 PostOperation(operation); | 337 PostOperation(operation); |
| 316 } | 338 } |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 468 PostOperation(operation); | 490 PostOperation(operation); |
| 469 } | 491 } |
| 470 | 492 |
| 471 void InFlightBackendIO::WaitForPendingIO() { | 493 void InFlightBackendIO::WaitForPendingIO() { |
| 472 InFlightIO::WaitForPendingIO(); | 494 InFlightIO::WaitForPendingIO(); |
| 473 } | 495 } |
| 474 | 496 |
| 475 void InFlightBackendIO::OnOperationComplete(BackgroundIO* operation, | 497 void InFlightBackendIO::OnOperationComplete(BackgroundIO* operation, |
| 476 bool cancel) { | 498 bool cancel) { |
| 477 BackendIO* op = static_cast<BackendIO*>(operation); | 499 BackendIO* op = static_cast<BackendIO*>(operation); |
| 478 | 500 op->OnDone(cancel); |
| 479 if (op->IsEntryOperation()) { | |
| 480 CACHE_UMA(TIMES, "TotalIOTime", 0, op->ElapsedTime()); | |
| 481 } | |
| 482 | 501 |
| 483 if (!op->callback().is_null() && (!cancel || op->IsEntryOperation())) | 502 if (!op->callback().is_null() && (!cancel || op->IsEntryOperation())) |
| 484 op->callback().Run(op->result()); | 503 op->callback().Run(op->result()); |
| 485 } | 504 } |
| 486 | 505 |
| 487 void InFlightBackendIO::PostOperation(BackendIO* operation) { | 506 void InFlightBackendIO::PostOperation(BackendIO* operation) { |
| 488 background_thread_->PostTask(FROM_HERE, | 507 background_thread_->PostTask(FROM_HERE, |
| 489 base::Bind(&BackendIO::ExecuteOperation, operation)); | 508 base::Bind(&BackendIO::ExecuteOperation, operation)); |
| 490 OnOperationPosted(operation); | 509 OnOperationPosted(operation); |
| 491 } | 510 } |
| 492 | 511 |
| 512 base::WeakPtr<InFlightBackendIO> InFlightBackendIO::GetWeakPtr() { |
| 513 return ptr_factory_.GetWeakPtr(); |
| 514 } |
| 515 |
| 493 } // namespace | 516 } // namespace |
| OLD | NEW |