| OLD | NEW |
| 1 // Copyright (c) 2009-2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009-2010 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/format_macros.h" | 7 #include "base/format_macros.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
| 12 #include "base/time.h" | 12 #include "base/time.h" |
| 13 #include "net/base/io_buffer.h" | 13 #include "net/base/io_buffer.h" |
| 14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 15 #include "net/disk_cache/backend_impl.h" | 15 #include "net/disk_cache/backend_impl.h" |
| 16 #include "net/disk_cache/entry_impl.h" | 16 #include "net/disk_cache/entry_impl.h" |
| 17 #include "net/disk_cache/file.h" | 17 #include "net/disk_cache/file.h" |
| 18 #include "net/disk_cache/net_log_parameters.h" |
| 18 | 19 |
| 19 using base::Time; | 20 using base::Time; |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 // Stream of the sparse data index. | 24 // Stream of the sparse data index. |
| 24 const int kSparseIndex = 2; | 25 const int kSparseIndex = 2; |
| 25 | 26 |
| 26 // Stream of the sparse data. | 27 // Stream of the sparse data. |
| 27 const int kSparseData = 1; | 28 const int kSparseData = 1; |
| 28 | 29 |
| 29 // We can have up to 64k children. | 30 // We can have up to 64k children. |
| 30 const int kMaxMapSize = 8 * 1024; | 31 const int kMaxMapSize = 8 * 1024; |
| 31 | 32 |
| 32 // The maximum number of bytes that a child can store. | 33 // The maximum number of bytes that a child can store. |
| 33 const int kMaxEntrySize = 0x100000; | 34 const int kMaxEntrySize = 0x100000; |
| 34 | 35 |
| 35 // The size of each data block (tracked by the child allocation bitmap). | 36 // The size of each data block (tracked by the child allocation bitmap). |
| 36 const int kBlockSize = 1024; | 37 const int kBlockSize = 1024; |
| 37 | 38 |
| 38 // Returns the name of of a child entry given the base_name and signature of the | 39 // Returns the name of a child entry given the base_name and signature of the |
| 39 // parent and the child_id. | 40 // parent and the child_id. |
| 40 // If the entry is called entry_name, child entries will be named something | 41 // If the entry is called entry_name, child entries will be named something |
| 41 // like Range_entry_name:XXX:YYY where XXX is the entry signature and YYY is the | 42 // like Range_entry_name:XXX:YYY where XXX is the entry signature and YYY is the |
| 42 // number of the particular child. | 43 // number of the particular child. |
| 43 std::string GenerateChildName(const std::string& base_name, int64 signature, | 44 std::string GenerateChildName(const std::string& base_name, int64 signature, |
| 44 int64 child_id) { | 45 int64 child_id) { |
| 45 return base::StringPrintf("Range_%s:%" PRIx64 ":%" PRIx64, base_name.c_str(), | 46 return base::StringPrintf("Range_%s:%" PRIx64 ":%" PRIx64, base_name.c_str(), |
| 46 signature, child_id); | 47 signature, child_id); |
| 47 } | 48 } |
| 48 | 49 |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 } | 132 } |
| 132 std::string child_name = GenerateChildName(name_, signature_, child_id); | 133 std::string child_name = GenerateChildName(name_, signature_, child_id); |
| 133 backend_->SyncDoomEntry(child_name); | 134 backend_->SyncDoomEntry(child_name); |
| 134 children_map_.Set(child_id, false); | 135 children_map_.Set(child_id, false); |
| 135 | 136 |
| 136 // Post a task to delete the next child. | 137 // Post a task to delete the next child. |
| 137 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | 138 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 138 this, &ChildrenDeleter::DeleteChildren)); | 139 this, &ChildrenDeleter::DeleteChildren)); |
| 139 } | 140 } |
| 140 | 141 |
| 141 // Logs the end event for |operation|, if all events are being logged. | 142 // Returns the NetLog event type corresponding to a SparseOperation. |
| 142 void LogOperationEnd(const net::BoundNetLog& net_log, | 143 net::NetLog::EventType GetSparseEventType( |
| 143 disk_cache::SparseControl::SparseOperation operation, | 144 disk_cache::SparseControl::SparseOperation operation) { |
| 144 int result) { | 145 switch (operation) { |
| 146 case disk_cache::SparseControl::kReadOperation: |
| 147 return net::NetLog::TYPE_SPARSE_READ; |
| 148 case disk_cache::SparseControl::kWriteOperation: |
| 149 return net::NetLog::TYPE_SPARSE_WRITE; |
| 150 case disk_cache::SparseControl::kGetRangeOperation: |
| 151 return net::NetLog::TYPE_SPARSE_GET_RANGE; |
| 152 default: |
| 153 NOTREACHED(); |
| 154 return net::NetLog::TYPE_CANCELLED; |
| 155 } |
| 156 } |
| 157 |
| 158 // Logs the end event for |operation| on a child entry. Range operations log |
| 159 // no events for each child they search through. |
| 160 void LogChildOperationEnd(const net::BoundNetLog& net_log, |
| 161 disk_cache::SparseControl::SparseOperation operation, |
| 162 int result) { |
| 145 if (net_log.IsLoggingAllEvents()) { | 163 if (net_log.IsLoggingAllEvents()) { |
| 146 net::NetLog::EventType event_type; | 164 net::NetLog::EventType event_type; |
| 147 switch (operation) { | 165 switch (operation) { |
| 148 case disk_cache::SparseControl::kReadOperation: | 166 case disk_cache::SparseControl::kReadOperation: |
| 149 event_type = net::NetLog::TYPE_SPARSE_CONTROL_READ; | 167 event_type = net::NetLog::TYPE_SPARSE_READ_CHILD_DATA; |
| 150 break; | 168 break; |
| 151 case disk_cache::SparseControl::kWriteOperation: | 169 case disk_cache::SparseControl::kWriteOperation: |
| 152 event_type = net::NetLog::TYPE_SPARSE_CONTROL_WRITE; | 170 event_type = net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA; |
| 153 break; | 171 break; |
| 154 case disk_cache::SparseControl::kGetRangeOperation: | 172 case disk_cache::SparseControl::kGetRangeOperation: |
| 155 event_type = net::NetLog::TYPE_SPARSE_CONTROL_GET_RANGE; | 173 return; |
| 156 break; | |
| 157 default: | 174 default: |
| 158 NOTREACHED(); | 175 NOTREACHED(); |
| 159 return; | 176 return; |
| 160 } | 177 } |
| 161 net_log.EndEventWithNetErrorCode(event_type, result); | 178 net_log.EndEventWithNetErrorCode(event_type, result); |
| 162 } | 179 } |
| 163 } | 180 } |
| 164 | 181 |
| 165 } // namespace. | 182 } // namespace. |
| 166 | 183 |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 offset_ = offset; | 257 offset_ = offset; |
| 241 user_buf_ = buf ? new net::DrainableIOBuffer(buf, buf_len) : NULL; | 258 user_buf_ = buf ? new net::DrainableIOBuffer(buf, buf_len) : NULL; |
| 242 buf_len_ = buf_len; | 259 buf_len_ = buf_len; |
| 243 user_callback_ = callback; | 260 user_callback_ = callback; |
| 244 | 261 |
| 245 result_ = 0; | 262 result_ = 0; |
| 246 pending_ = false; | 263 pending_ = false; |
| 247 finished_ = false; | 264 finished_ = false; |
| 248 abort_ = false; | 265 abort_ = false; |
| 249 | 266 |
| 250 entry_->net_log().BeginEvent(net::NetLog::TYPE_SPARSE_CONTROL, NULL); | 267 if (entry_->net_log().IsLoggingAllEvents()) { |
| 268 entry_->net_log().BeginEvent( |
| 269 GetSparseEventType(operation_), |
| 270 make_scoped_refptr(new SparseOperationParameters(offset_, buf_len_))); |
| 271 } |
| 251 DoChildrenIO(); | 272 DoChildrenIO(); |
| 252 | 273 |
| 253 if (!pending_) { | 274 if (!pending_) { |
| 254 // Everything was done synchronously. | 275 // Everything was done synchronously. |
| 255 operation_ = kNoOperation; | 276 operation_ = kNoOperation; |
| 256 user_buf_ = NULL; | 277 user_buf_ = NULL; |
| 257 user_callback_ = NULL; | 278 user_callback_ = NULL; |
| 258 return result_; | 279 return result_; |
| 259 } | 280 } |
| 260 | 281 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 int map_len = data_len - sizeof(SparseHeader); | 331 int map_len = data_len - sizeof(SparseHeader); |
| 311 if (map_len > kMaxMapSize || map_len % 4) | 332 if (map_len > kMaxMapSize || map_len % 4) |
| 312 return; | 333 return; |
| 313 | 334 |
| 314 char* buffer; | 335 char* buffer; |
| 315 Addr address; | 336 Addr address; |
| 316 entry->GetData(kSparseIndex, &buffer, &address); | 337 entry->GetData(kSparseIndex, &buffer, &address); |
| 317 if (!buffer && !address.is_initialized()) | 338 if (!buffer && !address.is_initialized()) |
| 318 return; | 339 return; |
| 319 | 340 |
| 341 entry->net_log().AddEvent(net::NetLog::TYPE_SPARSE_DELETE_CHILDREN, NULL); |
| 342 |
| 320 ChildrenDeleter* deleter = new ChildrenDeleter(entry->backend_, | 343 ChildrenDeleter* deleter = new ChildrenDeleter(entry->backend_, |
| 321 entry->GetKey()); | 344 entry->GetKey()); |
| 322 // The object will self destruct when finished. | 345 // The object will self destruct when finished. |
| 323 deleter->AddRef(); | 346 deleter->AddRef(); |
| 324 | 347 |
| 325 if (buffer) { | 348 if (buffer) { |
| 326 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | 349 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 327 deleter, &ChildrenDeleter::Start, buffer, data_len)); | 350 deleter, &ChildrenDeleter::Start, buffer, data_len)); |
| 328 } else { | 351 } else { |
| 329 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | 352 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| (...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 628 int rv = child_->WriteData(kSparseIndex, 0, buf, sizeof(child_data_), | 651 int rv = child_->WriteData(kSparseIndex, 0, buf, sizeof(child_data_), |
| 629 NULL, false); | 652 NULL, false); |
| 630 if (rv != sizeof(child_data_)) | 653 if (rv != sizeof(child_data_)) |
| 631 DLOG(ERROR) << "Failed to save child data"; | 654 DLOG(ERROR) << "Failed to save child data"; |
| 632 SetChildBit(true); | 655 SetChildBit(true); |
| 633 } | 656 } |
| 634 | 657 |
| 635 void SparseControl::DoChildrenIO() { | 658 void SparseControl::DoChildrenIO() { |
| 636 while (DoChildIO()) continue; | 659 while (DoChildIO()) continue; |
| 637 | 660 |
| 661 // Range operations are finished synchronously, often without setting |
| 662 // |finished_| to true. |
| 663 if (kGetRangeOperation == operation_ && |
| 664 entry_->net_log().IsLoggingAllEvents()) { |
| 665 entry_->net_log().EndEvent( |
| 666 net::NetLog::TYPE_SPARSE_GET_RANGE, |
| 667 make_scoped_refptr( |
| 668 new GetAvailableRangeResultParameters(offset_, result_))); |
| 669 } |
| 638 if (finished_) { | 670 if (finished_) { |
| 639 entry_->net_log().EndEvent(net::NetLog::TYPE_SPARSE_CONTROL, NULL); | 671 if (kGetRangeOperation != operation_ && |
| 672 entry_->net_log().IsLoggingAllEvents()) { |
| 673 entry_->net_log().EndEvent(GetSparseEventType(operation_), NULL); |
| 674 } |
| 640 if (pending_) | 675 if (pending_) |
| 641 DoUserCallback(); | 676 DoUserCallback(); |
| 642 } | 677 } |
| 643 } | 678 } |
| 644 | 679 |
| 645 bool SparseControl::DoChildIO() { | 680 bool SparseControl::DoChildIO() { |
| 646 finished_ = true; | 681 finished_ = true; |
| 647 if (!buf_len_ || result_ < 0) | 682 if (!buf_len_ || result_ < 0) |
| 648 return false; | 683 return false; |
| 649 | 684 |
| 650 if (!OpenChild()) | 685 if (!OpenChild()) |
| 651 return false; | 686 return false; |
| 652 | 687 |
| 653 if (!VerifyRange()) | 688 if (!VerifyRange()) |
| 654 return false; | 689 return false; |
| 655 | 690 |
| 656 // We have more work to do. Let's not trigger a callback to the caller. | 691 // We have more work to do. Let's not trigger a callback to the caller. |
| 657 finished_ = false; | 692 finished_ = false; |
| 658 net::CompletionCallback* callback = user_callback_ ? &child_callback_ : NULL; | 693 net::CompletionCallback* callback = user_callback_ ? &child_callback_ : NULL; |
| 659 | 694 |
| 660 int rv = 0; | 695 int rv = 0; |
| 661 switch (operation_) { | 696 switch (operation_) { |
| 662 case kReadOperation: | 697 case kReadOperation: |
| 663 if (entry_->net_log().IsLoggingAllEvents()) { | 698 if (entry_->net_log().IsLoggingAllEvents()) { |
| 664 entry_->net_log().BeginEvent( | 699 entry_->net_log().BeginEvent( |
| 665 net::NetLog::TYPE_SPARSE_CONTROL_READ, | 700 net::NetLog::TYPE_SPARSE_READ_CHILD_DATA, |
| 666 make_scoped_refptr(new net::NetLogSourceParameter( | 701 make_scoped_refptr(new SparseReadWriteParameters( |
| 667 "source_dependency", | 702 child_->net_log().source(), |
| 668 child_->net_log().source()))); | 703 child_len_))); |
| 669 } | 704 } |
| 670 rv = child_->ReadDataImpl(kSparseData, child_offset_, user_buf_, | 705 rv = child_->ReadDataImpl(kSparseData, child_offset_, user_buf_, |
| 671 child_len_, callback); | 706 child_len_, callback); |
| 672 break; | 707 break; |
| 673 case kWriteOperation: | 708 case kWriteOperation: |
| 674 if (entry_->net_log().IsLoggingAllEvents()) { | 709 if (entry_->net_log().IsLoggingAllEvents()) { |
| 675 entry_->net_log().BeginEvent( | 710 entry_->net_log().BeginEvent( |
| 676 net::NetLog::TYPE_SPARSE_CONTROL_WRITE, | 711 net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA, |
| 677 make_scoped_refptr(new net::NetLogSourceParameter( | 712 make_scoped_refptr(new SparseReadWriteParameters( |
| 678 "source_dependency", | 713 child_->net_log().source(), |
| 679 child_->net_log().source()))); | 714 child_len_))); |
| 680 } | 715 } |
| 681 rv = child_->WriteDataImpl(kSparseData, child_offset_, user_buf_, | 716 rv = child_->WriteDataImpl(kSparseData, child_offset_, user_buf_, |
| 682 child_len_, callback, false); | 717 child_len_, callback, false); |
| 683 break; | 718 break; |
| 684 case kGetRangeOperation: | 719 case kGetRangeOperation: |
| 685 if (entry_->net_log().IsLoggingAllEvents()) { | |
| 686 entry_->net_log().BeginEvent(net::NetLog::TYPE_SPARSE_CONTROL_GET_RANGE, | |
| 687 NULL); | |
| 688 } | |
| 689 rv = DoGetAvailableRange(); | 720 rv = DoGetAvailableRange(); |
| 690 break; | 721 break; |
| 691 default: | 722 default: |
| 692 NOTREACHED(); | 723 NOTREACHED(); |
| 693 } | 724 } |
| 694 | 725 |
| 695 if (rv == net::ERR_IO_PENDING) { | 726 if (rv == net::ERR_IO_PENDING) { |
| 696 if (!pending_) { | 727 if (!pending_) { |
| 697 pending_ = true; | 728 pending_ = true; |
| 698 // The child will protect himself against closing the entry while IO is in | 729 // The child will protect himself against closing the entry while IO is in |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 751 // Only update offset_ when this query found zeros at the start. | 782 // Only update offset_ when this query found zeros at the start. |
| 752 if (empty_start) | 783 if (empty_start) |
| 753 offset_ += empty_start; | 784 offset_ += empty_start; |
| 754 | 785 |
| 755 // This will actually break the loop. | 786 // This will actually break the loop. |
| 756 buf_len_ = 0; | 787 buf_len_ = 0; |
| 757 return 0; | 788 return 0; |
| 758 } | 789 } |
| 759 | 790 |
| 760 void SparseControl::DoChildIOCompleted(int result) { | 791 void SparseControl::DoChildIOCompleted(int result) { |
| 761 LogOperationEnd(entry_->net_log(), operation_, result); | 792 LogChildOperationEnd(entry_->net_log(), operation_, result); |
| 762 if (result < 0) { | 793 if (result < 0) { |
| 763 // We fail the whole operation if we encounter an error. | 794 // We fail the whole operation if we encounter an error. |
| 764 result_ = result; | 795 result_ = result; |
| 765 return; | 796 return; |
| 766 } | 797 } |
| 767 | 798 |
| 768 UpdateRange(result); | 799 UpdateRange(result); |
| 769 | 800 |
| 770 result_ += result; | 801 result_ += result; |
| 771 offset_ += result; | 802 offset_ += result; |
| 772 buf_len_ -= result; | 803 buf_len_ -= result; |
| 773 | 804 |
| 774 // We'll be reusing the user provided buffer for the next chunk. | 805 // We'll be reusing the user provided buffer for the next chunk. |
| 775 if (buf_len_ && user_buf_) | 806 if (buf_len_ && user_buf_) |
| 776 user_buf_->DidConsume(result); | 807 user_buf_->DidConsume(result); |
| 777 } | 808 } |
| 778 | 809 |
| 779 void SparseControl::OnChildIOCompleted(int result) { | 810 void SparseControl::OnChildIOCompleted(int result) { |
| 780 DCHECK_NE(net::ERR_IO_PENDING, result); | 811 DCHECK_NE(net::ERR_IO_PENDING, result); |
| 781 DoChildIOCompleted(result); | 812 DoChildIOCompleted(result); |
| 782 | 813 |
| 783 if (abort_) { | 814 if (abort_) { |
| 784 // We'll return the current result of the operation, which may be less than | 815 // We'll return the current result of the operation, which may be less than |
| 785 // the bytes to read or write, but the user cancelled the operation. | 816 // the bytes to read or write, but the user cancelled the operation. |
| 786 abort_ = false; | 817 abort_ = false; |
| 787 entry_->net_log().AddEvent(net::NetLog::TYPE_CANCELLED, NULL); | 818 if (entry_->net_log().IsLoggingAllEvents()) { |
| 788 entry_->net_log().EndEvent(net::NetLog::TYPE_SPARSE_CONTROL, NULL); | 819 entry_->net_log().AddEvent(net::NetLog::TYPE_CANCELLED, NULL); |
| 820 entry_->net_log().EndEvent(GetSparseEventType(operation_), NULL); |
| 821 } |
| 789 DoUserCallback(); | 822 DoUserCallback(); |
| 790 return DoAbortCallbacks(); | 823 return DoAbortCallbacks(); |
| 791 } | 824 } |
| 792 | 825 |
| 793 // We are running a callback from the message loop. It's time to restart what | 826 // We are running a callback from the message loop. It's time to restart what |
| 794 // we were doing before. | 827 // we were doing before. |
| 795 DoChildrenIO(); | 828 DoChildrenIO(); |
| 796 } | 829 } |
| 797 | 830 |
| 798 void SparseControl::DoUserCallback() { | 831 void SparseControl::DoUserCallback() { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 813 net::CompletionCallback* c = abort_callbacks_[i]; | 846 net::CompletionCallback* c = abort_callbacks_[i]; |
| 814 if (i == abort_callbacks_.size() - 1) | 847 if (i == abort_callbacks_.size() - 1) |
| 815 abort_callbacks_.clear(); | 848 abort_callbacks_.clear(); |
| 816 | 849 |
| 817 entry_->Release(); // Don't touch object after this line. | 850 entry_->Release(); // Don't touch object after this line. |
| 818 c->Run(net::OK); | 851 c->Run(net::OK); |
| 819 } | 852 } |
| 820 } | 853 } |
| 821 | 854 |
| 822 } // namespace disk_cache | 855 } // namespace disk_cache |
| OLD | NEW |