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" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 return StringPrintf("Range_%s:%" PRIx64 ":%" PRIx64, base_name.c_str(), | 44 return StringPrintf("Range_%s:%" PRIx64 ":%" PRIx64, base_name.c_str(), |
45 signature, child_id); | 45 signature, child_id); |
46 } | 46 } |
47 | 47 |
48 // This class deletes the children of a sparse entry. | 48 // This class deletes the children of a sparse entry. |
49 class ChildrenDeleter | 49 class ChildrenDeleter |
50 : public base::RefCounted<ChildrenDeleter>, | 50 : public base::RefCounted<ChildrenDeleter>, |
51 public disk_cache::FileIOCallback { | 51 public disk_cache::FileIOCallback { |
52 public: | 52 public: |
53 ChildrenDeleter(disk_cache::BackendImpl* backend, const std::string& name) | 53 ChildrenDeleter(disk_cache::BackendImpl* backend, const std::string& name) |
54 : backend_(backend), name_(name) {} | 54 : backend_(backend->GetWeakPtr()), name_(name) {} |
55 | 55 |
56 virtual void OnFileIOComplete(int bytes_copied); | 56 virtual void OnFileIOComplete(int bytes_copied); |
57 | 57 |
58 // Two ways of deleting the children: if we have the children map, use Start() | 58 // Two ways of deleting the children: if we have the children map, use Start() |
59 // directly, otherwise pass the data address to ReadData(). | 59 // directly, otherwise pass the data address to ReadData(). |
60 void Start(char* buffer, int len); | 60 void Start(char* buffer, int len); |
61 void ReadData(disk_cache::Addr address, int len); | 61 void ReadData(disk_cache::Addr address, int len); |
62 | 62 |
63 private: | 63 private: |
64 friend class base::RefCounted<ChildrenDeleter>; | 64 friend class base::RefCounted<ChildrenDeleter>; |
65 ~ChildrenDeleter() {} | 65 ~ChildrenDeleter() {} |
66 | 66 |
67 void DeleteChildren(); | 67 void DeleteChildren(); |
68 | 68 |
69 disk_cache::BackendImpl* backend_; | 69 base::WeakPtr<disk_cache::BackendImpl> backend_; |
70 std::string name_; | 70 std::string name_; |
71 disk_cache::Bitmap children_map_; | 71 disk_cache::Bitmap children_map_; |
72 int64 signature_; | 72 int64 signature_; |
73 scoped_array<char> buffer_; | 73 scoped_array<char> buffer_; |
74 DISALLOW_COPY_AND_ASSIGN(ChildrenDeleter); | 74 DISALLOW_COPY_AND_ASSIGN(ChildrenDeleter); |
75 }; | 75 }; |
76 | 76 |
77 // This is the callback of the file operation. | 77 // This is the callback of the file operation. |
78 void ChildrenDeleter::OnFileIOComplete(int bytes_copied) { | 78 void ChildrenDeleter::OnFileIOComplete(int bytes_copied) { |
79 char* buffer = buffer_.release(); | 79 char* buffer = buffer_.release(); |
(...skipping 14 matching lines...) Expand all Loading... |
94 int num_bits = (len - sizeof(disk_cache::SparseHeader)) * 8; | 94 int num_bits = (len - sizeof(disk_cache::SparseHeader)) * 8; |
95 children_map_.Resize(num_bits, false); | 95 children_map_.Resize(num_bits, false); |
96 children_map_.SetMap(data->bitmap, num_bits / 32); | 96 children_map_.SetMap(data->bitmap, num_bits / 32); |
97 buffer_.reset(); | 97 buffer_.reset(); |
98 | 98 |
99 DeleteChildren(); | 99 DeleteChildren(); |
100 } | 100 } |
101 | 101 |
102 void ChildrenDeleter::ReadData(disk_cache::Addr address, int len) { | 102 void ChildrenDeleter::ReadData(disk_cache::Addr address, int len) { |
103 DCHECK(address.is_block_file()); | 103 DCHECK(address.is_block_file()); |
| 104 if (!backend_) |
| 105 return Release(); |
| 106 |
104 disk_cache::File* file(backend_->File(address)); | 107 disk_cache::File* file(backend_->File(address)); |
105 if (!file) | 108 if (!file) |
106 return Release(); | 109 return Release(); |
107 | 110 |
108 size_t file_offset = address.start_block() * address.BlockSize() + | 111 size_t file_offset = address.start_block() * address.BlockSize() + |
109 disk_cache::kBlockHeaderSize; | 112 disk_cache::kBlockHeaderSize; |
110 | 113 |
111 buffer_.reset(new char[len]); | 114 buffer_.reset(new char[len]); |
112 bool completed; | 115 bool completed; |
113 if (!file->Read(buffer_.get(), len, file_offset, this, &completed)) | 116 if (!file->Read(buffer_.get(), len, file_offset, this, &completed)) |
114 return Release(); | 117 return Release(); |
115 | 118 |
116 if (completed) | 119 if (completed) |
117 OnFileIOComplete(len); | 120 OnFileIOComplete(len); |
118 | 121 |
119 // And wait until OnFileIOComplete gets called. | 122 // And wait until OnFileIOComplete gets called. |
120 } | 123 } |
121 | 124 |
122 void ChildrenDeleter::DeleteChildren() { | 125 void ChildrenDeleter::DeleteChildren() { |
123 int child_id = 0; | 126 int child_id = 0; |
124 if (!children_map_.FindNextSetBit(&child_id)) { | 127 if (!children_map_.FindNextSetBit(&child_id) || !backend_) { |
125 // We are done. Just delete this object. | 128 // We are done. Just delete this object. |
126 return Release(); | 129 return Release(); |
127 } | 130 } |
128 std::string child_name = GenerateChildName(name_, signature_, child_id); | 131 std::string child_name = GenerateChildName(name_, signature_, child_id); |
129 backend_->SyncDoomEntry(child_name); | 132 backend_->SyncDoomEntry(child_name); |
130 children_map_.Set(child_id, false); | 133 children_map_.Set(child_id, false); |
131 | 134 |
132 // Post a task to delete the next child. | 135 // Post a task to delete the next child. |
133 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | 136 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
134 this, &ChildrenDeleter::DeleteChildren)); | 137 this, &ChildrenDeleter::DeleteChildren)); |
(...skipping 614 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
749 net::CompletionCallback* c = abort_callbacks_[i]; | 752 net::CompletionCallback* c = abort_callbacks_[i]; |
750 if (i == abort_callbacks_.size() - 1) | 753 if (i == abort_callbacks_.size() - 1) |
751 abort_callbacks_.clear(); | 754 abort_callbacks_.clear(); |
752 | 755 |
753 entry_->Release(); // Don't touch object after this line. | 756 entry_->Release(); // Don't touch object after this line. |
754 c->Run(net::OK); | 757 c->Run(net::OK); |
755 } | 758 } |
756 } | 759 } |
757 | 760 |
758 } // namespace disk_cache | 761 } // namespace disk_cache |
OLD | NEW |