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/sparse_control.h" | 5 #include "net/disk_cache/sparse_control.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/format_macros.h" | 8 #include "base/format_macros.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 int child_id = 0; | 129 int child_id = 0; |
130 if (!children_map_.FindNextSetBit(&child_id) || !backend_) { | 130 if (!children_map_.FindNextSetBit(&child_id) || !backend_) { |
131 // We are done. Just delete this object. | 131 // We are done. Just delete this object. |
132 return Release(); | 132 return Release(); |
133 } | 133 } |
134 std::string child_name = GenerateChildName(name_, signature_, child_id); | 134 std::string child_name = GenerateChildName(name_, signature_, child_id); |
135 backend_->SyncDoomEntry(child_name); | 135 backend_->SyncDoomEntry(child_name); |
136 children_map_.Set(child_id, false); | 136 children_map_.Set(child_id, false); |
137 | 137 |
138 // Post a task to delete the next child. | 138 // Post a task to delete the next child. |
139 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | 139 base::MessageLoop::current()->PostTask( |
140 &ChildrenDeleter::DeleteChildren, this)); | 140 FROM_HERE, base::Bind(&ChildrenDeleter::DeleteChildren, this)); |
141 } | 141 } |
142 | 142 |
143 // Returns the NetLog event type corresponding to a SparseOperation. | 143 // Returns the NetLog event type corresponding to a SparseOperation. |
144 net::NetLog::EventType GetSparseEventType( | 144 net::NetLog::EventType GetSparseEventType( |
145 disk_cache::SparseControl::SparseOperation operation) { | 145 disk_cache::SparseControl::SparseOperation operation) { |
146 switch (operation) { | 146 switch (operation) { |
147 case disk_cache::SparseControl::kReadOperation: | 147 case disk_cache::SparseControl::kReadOperation: |
148 return net::NetLog::TYPE_SPARSE_READ; | 148 return net::NetLog::TYPE_SPARSE_READ; |
149 case disk_cache::SparseControl::kWriteOperation: | 149 case disk_cache::SparseControl::kWriteOperation: |
150 return net::NetLog::TYPE_SPARSE_WRITE; | 150 return net::NetLog::TYPE_SPARSE_WRITE; |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 | 344 |
345 char* buffer; | 345 char* buffer; |
346 Addr address; | 346 Addr address; |
347 entry->GetData(kSparseIndex, &buffer, &address); | 347 entry->GetData(kSparseIndex, &buffer, &address); |
348 if (!buffer && !address.is_initialized()) | 348 if (!buffer && !address.is_initialized()) |
349 return; | 349 return; |
350 | 350 |
351 entry->net_log().AddEvent(net::NetLog::TYPE_SPARSE_DELETE_CHILDREN); | 351 entry->net_log().AddEvent(net::NetLog::TYPE_SPARSE_DELETE_CHILDREN); |
352 | 352 |
353 DCHECK(entry->backend_); | 353 DCHECK(entry->backend_); |
354 ChildrenDeleter* deleter = new ChildrenDeleter(entry->backend_, | 354 ChildrenDeleter* deleter = new ChildrenDeleter(entry->backend_.get(), |
355 entry->GetKey()); | 355 entry->GetKey()); |
356 // The object will self destruct when finished. | 356 // The object will self destruct when finished. |
357 deleter->AddRef(); | 357 deleter->AddRef(); |
358 | 358 |
359 if (buffer) { | 359 if (buffer) { |
360 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | 360 base::MessageLoop::current()->PostTask( |
361 &ChildrenDeleter::Start, deleter, buffer, data_len)); | 361 FROM_HERE, |
| 362 base::Bind(&ChildrenDeleter::Start, deleter, buffer, data_len)); |
362 } else { | 363 } else { |
363 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | 364 base::MessageLoop::current()->PostTask( |
364 &ChildrenDeleter::ReadData, deleter, address, data_len)); | 365 FROM_HERE, |
| 366 base::Bind(&ChildrenDeleter::ReadData, deleter, address, data_len)); |
365 } | 367 } |
366 } | 368 } |
367 | 369 |
368 // We are going to start using this entry to store sparse data, so we have to | 370 // We are going to start using this entry to store sparse data, so we have to |
369 // initialize our control info. | 371 // initialize our control info. |
370 int SparseControl::CreateSparseEntry() { | 372 int SparseControl::CreateSparseEntry() { |
371 if (CHILD_ENTRY & entry_->GetEntryFlags()) | 373 if (CHILD_ENTRY & entry_->GetEntryFlags()) |
372 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 374 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
373 | 375 |
374 memset(&sparse_header_, 0, sizeof(sparse_header_)); | 376 memset(&sparse_header_, 0, sizeof(sparse_header_)); |
375 sparse_header_.signature = Time::Now().ToInternalValue(); | 377 sparse_header_.signature = Time::Now().ToInternalValue(); |
376 sparse_header_.magic = kIndexMagic; | 378 sparse_header_.magic = kIndexMagic; |
377 sparse_header_.parent_key_len = entry_->GetKey().size(); | 379 sparse_header_.parent_key_len = entry_->GetKey().size(); |
378 children_map_.Resize(kNumSparseBits, true); | 380 children_map_.Resize(kNumSparseBits, true); |
379 | 381 |
380 // Save the header. The bitmap is saved in the destructor. | 382 // Save the header. The bitmap is saved in the destructor. |
381 scoped_refptr<net::IOBuffer> buf( | 383 scoped_refptr<net::IOBuffer> buf( |
382 new net::WrappedIOBuffer(reinterpret_cast<char*>(&sparse_header_))); | 384 new net::WrappedIOBuffer(reinterpret_cast<char*>(&sparse_header_))); |
383 | 385 |
384 int rv = entry_->WriteData( | 386 int rv = entry_->WriteData(kSparseIndex, 0, buf.get(), sizeof(sparse_header_), |
385 kSparseIndex, 0, buf, sizeof(sparse_header_), CompletionCallback(), | 387 CompletionCallback(), false); |
386 false); | |
387 if (rv != sizeof(sparse_header_)) { | 388 if (rv != sizeof(sparse_header_)) { |
388 DLOG(ERROR) << "Unable to save sparse_header_"; | 389 DLOG(ERROR) << "Unable to save sparse_header_"; |
389 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 390 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
390 } | 391 } |
391 | 392 |
392 entry_->SetEntryFlags(PARENT_ENTRY); | 393 entry_->SetEntryFlags(PARENT_ENTRY); |
393 return net::OK; | 394 return net::OK; |
394 } | 395 } |
395 | 396 |
396 // We are opening an entry from disk. Make sure that our control data is there. | 397 // We are opening an entry from disk. Make sure that our control data is there. |
397 int SparseControl::OpenSparseEntry(int data_len) { | 398 int SparseControl::OpenSparseEntry(int data_len) { |
398 if (data_len < static_cast<int>(sizeof(SparseData))) | 399 if (data_len < static_cast<int>(sizeof(SparseData))) |
399 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 400 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
400 | 401 |
401 if (entry_->GetDataSize(kSparseData)) | 402 if (entry_->GetDataSize(kSparseData)) |
402 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 403 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
403 | 404 |
404 if (!(PARENT_ENTRY & entry_->GetEntryFlags())) | 405 if (!(PARENT_ENTRY & entry_->GetEntryFlags())) |
405 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 406 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
406 | 407 |
407 // Dont't go over board with the bitmap. 8 KB gives us offsets up to 64 GB. | 408 // Dont't go over board with the bitmap. 8 KB gives us offsets up to 64 GB. |
408 int map_len = data_len - sizeof(sparse_header_); | 409 int map_len = data_len - sizeof(sparse_header_); |
409 if (map_len > kMaxMapSize || map_len % 4) | 410 if (map_len > kMaxMapSize || map_len % 4) |
410 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 411 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
411 | 412 |
412 scoped_refptr<net::IOBuffer> buf( | 413 scoped_refptr<net::IOBuffer> buf( |
413 new net::WrappedIOBuffer(reinterpret_cast<char*>(&sparse_header_))); | 414 new net::WrappedIOBuffer(reinterpret_cast<char*>(&sparse_header_))); |
414 | 415 |
415 // Read header. | 416 // Read header. |
416 int rv = entry_->ReadData( | 417 int rv = entry_->ReadData(kSparseIndex, 0, buf.get(), sizeof(sparse_header_), |
417 kSparseIndex, 0, buf, sizeof(sparse_header_), CompletionCallback()); | 418 CompletionCallback()); |
418 if (rv != static_cast<int>(sizeof(sparse_header_))) | 419 if (rv != static_cast<int>(sizeof(sparse_header_))) |
419 return net::ERR_CACHE_READ_FAILURE; | 420 return net::ERR_CACHE_READ_FAILURE; |
420 | 421 |
421 // The real validation should be performed by the caller. This is just to | 422 // The real validation should be performed by the caller. This is just to |
422 // double check. | 423 // double check. |
423 if (sparse_header_.magic != kIndexMagic || | 424 if (sparse_header_.magic != kIndexMagic || |
424 sparse_header_.parent_key_len != | 425 sparse_header_.parent_key_len != |
425 static_cast<int>(entry_->GetKey().size())) | 426 static_cast<int>(entry_->GetKey().size())) |
426 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 427 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
427 | 428 |
428 // Read the actual bitmap. | 429 // Read the actual bitmap. |
429 buf = new net::IOBuffer(map_len); | 430 buf = new net::IOBuffer(map_len); |
430 rv = entry_->ReadData(kSparseIndex, sizeof(sparse_header_), buf, map_len, | 431 rv = entry_->ReadData(kSparseIndex, sizeof(sparse_header_), buf.get(), |
431 CompletionCallback()); | 432 map_len, CompletionCallback()); |
432 if (rv != map_len) | 433 if (rv != map_len) |
433 return net::ERR_CACHE_READ_FAILURE; | 434 return net::ERR_CACHE_READ_FAILURE; |
434 | 435 |
435 // Grow the bitmap to the current size and copy the bits. | 436 // Grow the bitmap to the current size and copy the bits. |
436 children_map_.Resize(map_len * 8, false); | 437 children_map_.Resize(map_len * 8, false); |
437 children_map_.SetMap(reinterpret_cast<uint32*>(buf->data()), map_len); | 438 children_map_.SetMap(reinterpret_cast<uint32*>(buf->data()), map_len); |
438 return net::OK; | 439 return net::OK; |
439 } | 440 } |
440 | 441 |
441 bool SparseControl::OpenChild() { | 442 bool SparseControl::OpenChild() { |
(...skipping 21 matching lines...) Expand all Loading... |
463 EntryImpl* child = static_cast<EntryImpl*>(child_); | 464 EntryImpl* child = static_cast<EntryImpl*>(child_); |
464 if (!(CHILD_ENTRY & child->GetEntryFlags()) || | 465 if (!(CHILD_ENTRY & child->GetEntryFlags()) || |
465 child->GetDataSize(kSparseIndex) < | 466 child->GetDataSize(kSparseIndex) < |
466 static_cast<int>(sizeof(child_data_))) | 467 static_cast<int>(sizeof(child_data_))) |
467 return KillChildAndContinue(key, false); | 468 return KillChildAndContinue(key, false); |
468 | 469 |
469 scoped_refptr<net::WrappedIOBuffer> buf( | 470 scoped_refptr<net::WrappedIOBuffer> buf( |
470 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_))); | 471 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_))); |
471 | 472 |
472 // Read signature. | 473 // Read signature. |
473 int rv = child_->ReadData(kSparseIndex, 0, buf, sizeof(child_data_), | 474 int rv = child_->ReadData(kSparseIndex, 0, buf.get(), sizeof(child_data_), |
474 CompletionCallback()); | 475 CompletionCallback()); |
475 if (rv != sizeof(child_data_)) | 476 if (rv != sizeof(child_data_)) |
476 return KillChildAndContinue(key, true); // This is a fatal failure. | 477 return KillChildAndContinue(key, true); // This is a fatal failure. |
477 | 478 |
478 if (child_data_.header.signature != sparse_header_.signature || | 479 if (child_data_.header.signature != sparse_header_.signature || |
479 child_data_.header.magic != kIndexMagic) | 480 child_data_.header.magic != kIndexMagic) |
480 return KillChildAndContinue(key, false); | 481 return KillChildAndContinue(key, false); |
481 | 482 |
482 if (child_data_.header.last_block_len < 0 || | 483 if (child_data_.header.last_block_len < 0 || |
483 child_data_.header.last_block_len > kBlockSize) { | 484 child_data_.header.last_block_len > kBlockSize) { |
484 // Make sure these values are always within range. | 485 // Make sure these values are always within range. |
485 child_data_.header.last_block_len = 0; | 486 child_data_.header.last_block_len = 0; |
486 child_data_.header.last_block = -1; | 487 child_data_.header.last_block = -1; |
487 } | 488 } |
488 | 489 |
489 return true; | 490 return true; |
490 } | 491 } |
491 | 492 |
492 void SparseControl::CloseChild() { | 493 void SparseControl::CloseChild() { |
493 scoped_refptr<net::WrappedIOBuffer> buf( | 494 scoped_refptr<net::WrappedIOBuffer> buf( |
494 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_))); | 495 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_))); |
495 | 496 |
496 // Save the allocation bitmap before closing the child entry. | 497 // Save the allocation bitmap before closing the child entry. |
497 int rv = child_->WriteData(kSparseIndex, 0, buf, sizeof(child_data_), | 498 int rv = child_->WriteData(kSparseIndex, 0, buf.get(), sizeof(child_data_), |
498 CompletionCallback(), | 499 CompletionCallback(), false); |
499 false); | |
500 if (rv != sizeof(child_data_)) | 500 if (rv != sizeof(child_data_)) |
501 DLOG(ERROR) << "Failed to save child data"; | 501 DLOG(ERROR) << "Failed to save child data"; |
502 child_->Release(); | 502 child_->Release(); |
503 child_ = NULL; | 503 child_ = NULL; |
504 } | 504 } |
505 | 505 |
506 std::string SparseControl::GenerateChildKey() { | 506 std::string SparseControl::GenerateChildKey() { |
507 return GenerateChildName(entry_->GetKey(), sparse_header_.signature, | 507 return GenerateChildName(entry_->GetKey(), sparse_header_.signature, |
508 offset_ >> 20); | 508 offset_ >> 20); |
509 } | 509 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
558 children_map_.Resize(Bitmap::RequiredArraySize(child_bit + 1) * 32, true); | 558 children_map_.Resize(Bitmap::RequiredArraySize(child_bit + 1) * 32, true); |
559 | 559 |
560 children_map_.Set(child_bit, value); | 560 children_map_.Set(child_bit, value); |
561 } | 561 } |
562 | 562 |
563 void SparseControl::WriteSparseData() { | 563 void SparseControl::WriteSparseData() { |
564 scoped_refptr<net::IOBuffer> buf(new net::WrappedIOBuffer( | 564 scoped_refptr<net::IOBuffer> buf(new net::WrappedIOBuffer( |
565 reinterpret_cast<const char*>(children_map_.GetMap()))); | 565 reinterpret_cast<const char*>(children_map_.GetMap()))); |
566 | 566 |
567 int len = children_map_.ArraySize() * 4; | 567 int len = children_map_.ArraySize() * 4; |
568 int rv = entry_->WriteData(kSparseIndex, sizeof(sparse_header_), buf, len, | 568 int rv = entry_->WriteData(kSparseIndex, sizeof(sparse_header_), buf.get(), |
569 CompletionCallback(), false); | 569 len, CompletionCallback(), false); |
570 if (rv != len) { | 570 if (rv != len) { |
571 DLOG(ERROR) << "Unable to save sparse map"; | 571 DLOG(ERROR) << "Unable to save sparse map"; |
572 } | 572 } |
573 } | 573 } |
574 | 574 |
575 bool SparseControl::VerifyRange() { | 575 bool SparseControl::VerifyRange() { |
576 DCHECK_GE(result_, 0); | 576 DCHECK_GE(result_, 0); |
577 | 577 |
578 child_offset_ = static_cast<int>(offset_) & (kMaxEntrySize - 1); | 578 child_offset_ = static_cast<int>(offset_) & (kMaxEntrySize - 1); |
579 child_len_ = std::min(buf_len_, kMaxEntrySize - child_offset_); | 579 child_len_ = std::min(buf_len_, kMaxEntrySize - child_offset_); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
662 // We know the real type of child_. | 662 // We know the real type of child_. |
663 EntryImpl* child = static_cast<EntryImpl*>(child_); | 663 EntryImpl* child = static_cast<EntryImpl*>(child_); |
664 child->SetEntryFlags(CHILD_ENTRY); | 664 child->SetEntryFlags(CHILD_ENTRY); |
665 | 665 |
666 memset(&child_data_, 0, sizeof(child_data_)); | 666 memset(&child_data_, 0, sizeof(child_data_)); |
667 child_data_.header = sparse_header_; | 667 child_data_.header = sparse_header_; |
668 | 668 |
669 scoped_refptr<net::WrappedIOBuffer> buf( | 669 scoped_refptr<net::WrappedIOBuffer> buf( |
670 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_))); | 670 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_))); |
671 | 671 |
672 int rv = child_->WriteData(kSparseIndex, 0, buf, sizeof(child_data_), | 672 int rv = child_->WriteData(kSparseIndex, 0, buf.get(), sizeof(child_data_), |
673 CompletionCallback(), false); | 673 CompletionCallback(), false); |
674 if (rv != sizeof(child_data_)) | 674 if (rv != sizeof(child_data_)) |
675 DLOG(ERROR) << "Failed to save child data"; | 675 DLOG(ERROR) << "Failed to save child data"; |
676 SetChildBit(true); | 676 SetChildBit(true); |
677 } | 677 } |
678 | 678 |
679 void SparseControl::DoChildrenIO() { | 679 void SparseControl::DoChildrenIO() { |
680 while (DoChildIO()) continue; | 680 while (DoChildIO()) continue; |
681 | 681 |
682 // Range operations are finished synchronously, often without setting | 682 // Range operations are finished synchronously, often without setting |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
718 | 718 |
719 int rv = 0; | 719 int rv = 0; |
720 switch (operation_) { | 720 switch (operation_) { |
721 case kReadOperation: | 721 case kReadOperation: |
722 if (entry_->net_log().IsLoggingAllEvents()) { | 722 if (entry_->net_log().IsLoggingAllEvents()) { |
723 entry_->net_log().BeginEvent( | 723 entry_->net_log().BeginEvent( |
724 net::NetLog::TYPE_SPARSE_READ_CHILD_DATA, | 724 net::NetLog::TYPE_SPARSE_READ_CHILD_DATA, |
725 CreateNetLogSparseReadWriteCallback(child_->net_log().source(), | 725 CreateNetLogSparseReadWriteCallback(child_->net_log().source(), |
726 child_len_)); | 726 child_len_)); |
727 } | 727 } |
728 rv = child_->ReadDataImpl(kSparseData, child_offset_, user_buf_, | 728 rv = child_->ReadDataImpl(kSparseData, child_offset_, user_buf_.get(), |
729 child_len_, callback); | 729 child_len_, callback); |
730 break; | 730 break; |
731 case kWriteOperation: | 731 case kWriteOperation: |
732 if (entry_->net_log().IsLoggingAllEvents()) { | 732 if (entry_->net_log().IsLoggingAllEvents()) { |
733 entry_->net_log().BeginEvent( | 733 entry_->net_log().BeginEvent( |
734 net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA, | 734 net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA, |
735 CreateNetLogSparseReadWriteCallback(child_->net_log().source(), | 735 CreateNetLogSparseReadWriteCallback(child_->net_log().source(), |
736 child_len_)); | 736 child_len_)); |
737 } | 737 } |
738 rv = child_->WriteDataImpl(kSparseData, child_offset_, user_buf_, | 738 rv = child_->WriteDataImpl(kSparseData, child_offset_, user_buf_.get(), |
739 child_len_, callback, false); | 739 child_len_, callback, false); |
740 break; | 740 break; |
741 case kGetRangeOperation: | 741 case kGetRangeOperation: |
742 rv = DoGetAvailableRange(); | 742 rv = DoGetAvailableRange(); |
743 break; | 743 break; |
744 default: | 744 default: |
745 NOTREACHED(); | 745 NOTREACHED(); |
746 } | 746 } |
747 | 747 |
748 if (rv == net::ERR_IO_PENDING) { | 748 if (rv == net::ERR_IO_PENDING) { |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
875 CompletionCallback cb = abort_callbacks_[i]; | 875 CompletionCallback cb = abort_callbacks_[i]; |
876 if (i == abort_callbacks_.size() - 1) | 876 if (i == abort_callbacks_.size() - 1) |
877 abort_callbacks_.clear(); | 877 abort_callbacks_.clear(); |
878 | 878 |
879 entry_->Release(); // Don't touch object after this line. | 879 entry_->Release(); // Don't touch object after this line. |
880 cb.Run(net::OK); | 880 cb.Run(net::OK); |
881 } | 881 } |
882 } | 882 } |
883 | 883 |
884 } // namespace disk_cache | 884 } // namespace disk_cache |
OLD | NEW |