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/v3/sparse_control_v3.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/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/strings/string_util.h" | |
12 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
13 #include "base/time/time.h" | 12 #include "base/time/time.h" |
14 #include "net/base/io_buffer.h" | 13 #include "net/base/io_buffer.h" |
15 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
16 #include "net/disk_cache/backend_impl.h" | |
17 #include "net/disk_cache/entry_impl.h" | |
18 #include "net/disk_cache/file.h" | |
19 #include "net/disk_cache/net_log_parameters.h" | 15 #include "net/disk_cache/net_log_parameters.h" |
| 16 #include "net/disk_cache/v3/backend_impl_v3.h" |
| 17 #include "net/disk_cache/v3/entry_impl_v3.h" |
20 | 18 |
21 using base::Time; | 19 using base::Time; |
| 20 using base::Bind; |
22 | 21 |
23 namespace { | 22 namespace { |
24 | 23 |
25 // Stream of the sparse data index. | 24 // Stream of the sparse data index. |
26 const int kSparseIndex = 2; | 25 const int kSparseIndex = 2; |
27 | 26 |
28 // Stream of the sparse data. | 27 // Stream of the sparse data. |
29 const int kSparseData = 1; | 28 const int kSparseData = 1; |
30 | 29 |
31 // We can have up to 64k children. | 30 // We can have up to 64k children. |
(...skipping 10 matching lines...) Expand all Loading... |
42 // 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 |
43 // 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 |
44 // number of the particular child. | 43 // number of the particular child. |
45 std::string GenerateChildName(const std::string& base_name, int64 signature, | 44 std::string GenerateChildName(const std::string& base_name, int64 signature, |
46 int64 child_id) { | 45 int64 child_id) { |
47 return base::StringPrintf("Range_%s:%" PRIx64 ":%" PRIx64, base_name.c_str(), | 46 return base::StringPrintf("Range_%s:%" PRIx64 ":%" PRIx64, base_name.c_str(), |
48 signature, child_id); | 47 signature, child_id); |
49 } | 48 } |
50 | 49 |
51 // This class deletes the children of a sparse entry. | 50 // This class deletes the children of a sparse entry. |
52 class ChildrenDeleter | 51 class ChildrenDeleter : public base::RefCountedThreadSafe<ChildrenDeleter> { |
53 : public base::RefCounted<ChildrenDeleter>, | |
54 public disk_cache::FileIOCallback { | |
55 public: | 52 public: |
56 ChildrenDeleter(disk_cache::BackendImpl* backend, const std::string& name) | 53 ChildrenDeleter(disk_cache::BackendImplV3* backend, const std::string& name) |
57 : backend_(backend->GetWeakPtr()), name_(name), signature_(0) {} | 54 : backend_(backend->GetWeakPtr()), name_(name), signature_(0) {} |
58 | 55 |
59 virtual void OnFileIOComplete(int bytes_copied) OVERRIDE; | |
60 | |
61 // Two ways of deleting the children: if we have the children map, use Start() | 56 // Two ways of deleting the children: if we have the children map, use Start() |
62 // directly, otherwise pass the data address to ReadData(). | 57 // directly, otherwise pass the data address to ReadData(). |
63 void Start(char* buffer, int len); | 58 void Start(net::IOBuffer* buffer, int len); |
64 void ReadData(disk_cache::Addr address, int len); | 59 void ReadData(disk_cache::Addr address, int len); |
65 | 60 |
66 private: | 61 private: |
67 friend class base::RefCounted<ChildrenDeleter>; | 62 friend class base::RefCountedThreadSafe<ChildrenDeleter>; |
68 virtual ~ChildrenDeleter() {} | 63 virtual ~ChildrenDeleter() {} |
69 | 64 |
70 void DeleteChildren(); | 65 void DeleteChildren(); |
| 66 void OnReadComplete(int result); |
| 67 void OnDoomComplete(int result); |
71 | 68 |
72 base::WeakPtr<disk_cache::BackendImpl> backend_; | 69 base::WeakPtr<disk_cache::BackendImplV3> backend_; |
73 std::string name_; | 70 std::string name_; |
74 disk_cache::Bitmap children_map_; | 71 disk_cache::Bitmap children_map_; |
75 int64 signature_; | 72 int64 signature_; |
76 scoped_ptr<char[]> buffer_; | 73 scoped_refptr<net::IOBuffer> buffer_; |
77 DISALLOW_COPY_AND_ASSIGN(ChildrenDeleter); | 74 DISALLOW_COPY_AND_ASSIGN(ChildrenDeleter); |
78 }; | 75 }; |
79 | 76 |
80 // This is the callback of the file operation. | 77 void ChildrenDeleter::Start(net::IOBuffer* buffer, int len) { |
81 void ChildrenDeleter::OnFileIOComplete(int bytes_copied) { | |
82 char* buffer = buffer_.release(); | |
83 Start(buffer, bytes_copied); | |
84 } | |
85 | |
86 void ChildrenDeleter::Start(char* buffer, int len) { | |
87 buffer_.reset(buffer); | |
88 if (len < static_cast<int>(sizeof(disk_cache::SparseData))) | 78 if (len < static_cast<int>(sizeof(disk_cache::SparseData))) |
89 return Release(); | 79 return Release(); |
90 | 80 |
91 // Just copy the information from |buffer|, delete |buffer| and start deleting | 81 // Just copy the information from |buffer|, delete |buffer| and start deleting |
92 // the child entries. | 82 // the child entries. |
93 disk_cache::SparseData* data = | 83 disk_cache::SparseData* data = |
94 reinterpret_cast<disk_cache::SparseData*>(buffer); | 84 reinterpret_cast<disk_cache::SparseData*>(buffer->data()); |
95 signature_ = data->header.signature; | 85 signature_ = data->header.signature; |
96 | 86 |
97 int num_bits = (len - sizeof(disk_cache::SparseHeader)) * 8; | 87 int num_bits = (len - sizeof(disk_cache::SparseHeader)) * 8; |
98 children_map_.Resize(num_bits, false); | 88 children_map_.Resize(num_bits, false); |
99 children_map_.SetMap(data->bitmap, num_bits / 32); | 89 children_map_.SetMap(data->bitmap, num_bits / 32); |
100 buffer_.reset(); | |
101 | 90 |
102 DeleteChildren(); | 91 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 92 Bind(&ChildrenDeleter::DeleteChildren, |
| 93 this)); |
103 } | 94 } |
104 | 95 |
105 void ChildrenDeleter::ReadData(disk_cache::Addr address, int len) { | 96 void ChildrenDeleter::ReadData(disk_cache::Addr address, int len) { |
106 DCHECK(address.is_block_file()); | 97 DCHECK(address.is_block_file()); |
107 if (!backend_) | 98 if (!backend_) |
108 return Release(); | 99 return Release(); |
109 | 100 |
110 disk_cache::File* file(backend_->File(address)); | 101 buffer_ = new net::IOBuffer(len); |
111 if (!file) | 102 backend_->ReadData(NULL, address, 0, buffer_, len, |
112 return Release(); | 103 Bind(&ChildrenDeleter::OnReadComplete, this)); |
113 | |
114 size_t file_offset = address.start_block() * address.BlockSize() + | |
115 disk_cache::kBlockHeaderSize; | |
116 | |
117 buffer_.reset(new char[len]); | |
118 bool completed; | |
119 if (!file->Read(buffer_.get(), len, file_offset, this, &completed)) | |
120 return Release(); | |
121 | |
122 if (completed) | |
123 OnFileIOComplete(len); | |
124 | |
125 // And wait until OnFileIOComplete gets called. | |
126 } | 104 } |
127 | 105 |
128 void ChildrenDeleter::DeleteChildren() { | 106 void ChildrenDeleter::DeleteChildren() { |
129 int child_id = 0; | 107 int child_id = 0; |
130 if (!children_map_.FindNextSetBit(&child_id) || !backend_) { | 108 for (int rv = net::OK; rv != net::ERR_IO_PENDING;) { |
131 // We are done. Just delete this object. | 109 if (!children_map_.FindNextSetBit(&child_id) || !backend_) { |
132 return Release(); | 110 // We are done. Just delete this object. |
| 111 return Release(); |
| 112 } |
| 113 std::string child_name = GenerateChildName(name_, signature_, child_id); |
| 114 rv = backend_->DoomEntry(child_name, |
| 115 Bind(&ChildrenDeleter::OnDoomComplete, this)); |
| 116 children_map_.Set(child_id, false); |
133 } | 117 } |
134 std::string child_name = GenerateChildName(name_, signature_, child_id); | 118 } |
135 backend_->SyncDoomEntry(child_name); | |
136 children_map_.Set(child_id, false); | |
137 | 119 |
138 // Post a task to delete the next child. | 120 void ChildrenDeleter::OnReadComplete(int result) { |
139 base::MessageLoop::current()->PostTask( | 121 Start(buffer_, result); |
140 FROM_HERE, base::Bind(&ChildrenDeleter::DeleteChildren, this)); | 122 } |
| 123 |
| 124 void ChildrenDeleter::OnDoomComplete(int result) { |
| 125 DeleteChildren(); |
141 } | 126 } |
142 | 127 |
143 // ----------------------------------------------------------------------- | 128 // ----------------------------------------------------------------------- |
144 | 129 |
145 // Returns the NetLog event type corresponding to a SparseOperation. | 130 // Returns the NetLog event type corresponding to a SparseOperation. |
146 net::NetLog::EventType GetSparseEventType( | 131 net::NetLog::EventType GetSparseEventType( |
147 disk_cache::SparseControl::SparseOperation operation) { | 132 disk_cache::SparseControlV3::SparseOperation operation) { |
148 switch (operation) { | 133 switch (operation) { |
149 case disk_cache::SparseControl::kReadOperation: | 134 case disk_cache::SparseControlV3::kReadOperation: |
150 return net::NetLog::TYPE_SPARSE_READ; | 135 return net::NetLog::TYPE_SPARSE_READ; |
151 case disk_cache::SparseControl::kWriteOperation: | 136 case disk_cache::SparseControlV3::kWriteOperation: |
152 return net::NetLog::TYPE_SPARSE_WRITE; | 137 return net::NetLog::TYPE_SPARSE_WRITE; |
153 case disk_cache::SparseControl::kGetRangeOperation: | 138 case disk_cache::SparseControlV3::kGetRangeOperation: |
154 return net::NetLog::TYPE_SPARSE_GET_RANGE; | 139 return net::NetLog::TYPE_SPARSE_GET_RANGE; |
155 default: | 140 default: |
156 NOTREACHED(); | 141 NOTREACHED(); |
157 return net::NetLog::TYPE_CANCELLED; | 142 return net::NetLog::TYPE_CANCELLED; |
158 } | 143 } |
159 } | 144 } |
160 | 145 |
161 // Logs the end event for |operation| on a child entry. Range operations log | |
162 // no events for each child they search through. | |
163 void LogChildOperationEnd(const net::BoundNetLog& net_log, | |
164 disk_cache::SparseControl::SparseOperation operation, | |
165 int result) { | |
166 if (net_log.IsLoggingAllEvents()) { | |
167 net::NetLog::EventType event_type; | |
168 switch (operation) { | |
169 case disk_cache::SparseControl::kReadOperation: | |
170 event_type = net::NetLog::TYPE_SPARSE_READ_CHILD_DATA; | |
171 break; | |
172 case disk_cache::SparseControl::kWriteOperation: | |
173 event_type = net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA; | |
174 break; | |
175 case disk_cache::SparseControl::kGetRangeOperation: | |
176 return; | |
177 default: | |
178 NOTREACHED(); | |
179 return; | |
180 } | |
181 net_log.EndEventWithNetErrorCode(event_type, result); | |
182 } | |
183 } | |
184 | |
185 } // namespace. | 146 } // namespace. |
186 | 147 |
187 namespace disk_cache { | 148 namespace disk_cache { |
188 | 149 |
189 SparseControl::SparseControl(EntryImpl* entry) | 150 SparseControlV3::SparseControlV3(EntryImplV3* entry) |
190 : entry_(entry), | 151 : entry_(entry), |
191 child_(NULL), | 152 child_(NULL), |
192 operation_(kNoOperation), | 153 operation_(kNoOperation), |
193 pending_(false), | 154 next_state_(STATE_NONE), |
194 finished_(false), | |
195 init_(false), | 155 init_(false), |
196 range_found_(false), | 156 range_found_(false), |
197 abort_(false), | 157 abort_(false), |
| 158 valid_(false), |
| 159 closing_(false), |
198 child_map_(child_data_.bitmap, kNumSparseBits, kNumSparseBits / 32), | 160 child_map_(child_data_.bitmap, kNumSparseBits, kNumSparseBits / 32), |
| 161 callback_(base::Bind(&SparseControlV3::OnIOComplete, |
| 162 base::Unretained(this))), |
199 offset_(0), | 163 offset_(0), |
200 buf_len_(0), | 164 buf_len_(0), |
201 child_offset_(0), | 165 child_offset_(0), |
202 child_len_(0), | 166 child_len_(0), |
203 result_(0) { | 167 result_(0), |
| 168 range_start_(NULL) { |
204 memset(&sparse_header_, 0, sizeof(sparse_header_)); | 169 memset(&sparse_header_, 0, sizeof(sparse_header_)); |
205 memset(&child_data_, 0, sizeof(child_data_)); | 170 memset(&child_data_, 0, sizeof(child_data_)); |
206 } | 171 } |
207 | 172 |
208 SparseControl::~SparseControl() { | 173 SparseControlV3::~SparseControlV3() { |
209 if (child_) | |
210 CloseChild(); | |
211 if (init_) | |
212 WriteSparseData(); | |
213 } | 174 } |
214 | 175 |
215 bool SparseControl::CouldBeSparse() const { | 176 void SparseControlV3::Close() { |
216 DCHECK(!init_); | 177 if (closing_) |
| 178 return; |
| 179 if (operation_ != kNoOperation || valid_) { |
| 180 closing_ = true; |
| 181 entry_->AddRef(); |
| 182 if (operation_ != kNoOperation) |
| 183 return; |
| 184 |
| 185 DCHECK_EQ(next_state_, STATE_NONE); |
| 186 DCHECK(user_callback_.is_null()); |
| 187 next_state_ = STATE_CLOSE; |
| 188 int rv = DoLoop(net::OK); |
| 189 return; |
| 190 } |
| 191 } |
| 192 |
| 193 bool SparseControlV3::CouldBeSparse() const { |
| 194 if (init_) |
| 195 return valid_; |
217 | 196 |
218 if (entry_->GetDataSize(kSparseData)) | 197 if (entry_->GetDataSize(kSparseData)) |
219 return false; | 198 return false; |
220 | 199 |
221 // We don't verify the data, just see if it could be there. | 200 // We don't verify the data, just see if it could be there. |
222 return (entry_->GetDataSize(kSparseIndex) != 0); | 201 return (entry_->GetDataSize(kSparseIndex) != 0); |
223 } | 202 } |
224 | 203 |
225 int SparseControl::StartIO(SparseOperation op, int64 offset, net::IOBuffer* buf, | 204 int SparseControlV3::StartIO(SparseOperation op, int64 offset, |
226 int buf_len, const CompletionCallback& callback) { | 205 net::IOBuffer* buf, int buf_len, |
227 DCHECK(init_); | 206 const CompletionCallback& callback) { |
228 // We don't support simultaneous IO for sparse data. | 207 // We don't support simultaneous IO for sparse data. |
229 if (operation_ != kNoOperation) | 208 if (operation_ != kNoOperation) |
230 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 209 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
231 | 210 |
232 if (offset < 0 || buf_len < 0) | 211 if (offset < 0 || buf_len < 0) |
233 return net::ERR_INVALID_ARGUMENT; | 212 return net::ERR_INVALID_ARGUMENT; |
234 | 213 |
235 // We only support up to 64 GB. | 214 // We only support up to 64 GB. |
236 if (offset + buf_len >= 0x1000000000LL || offset + buf_len < 0) | 215 if (offset + buf_len >= 0x1000000000LL || offset + buf_len < 0) |
237 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 216 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
238 | 217 |
239 DCHECK(!user_buf_); | 218 DCHECK(!user_buf_); |
240 DCHECK(user_callback_.is_null()); | 219 DCHECK(user_callback_.is_null()); |
241 | 220 |
| 221 if (init_ && !valid_) |
| 222 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
| 223 |
242 if (!buf && (op == kReadOperation || op == kWriteOperation)) | 224 if (!buf && (op == kReadOperation || op == kWriteOperation)) |
243 return 0; | 225 return 0; |
244 | 226 |
245 // Copy the operation parameters. | 227 // Copy the operation parameters. |
246 operation_ = op; | 228 operation_ = op; |
247 offset_ = offset; | 229 offset_ = offset; |
248 user_buf_ = buf ? new net::DrainableIOBuffer(buf, buf_len) : NULL; | 230 user_buf_ = buf ? new net::DrainableIOBuffer(buf, buf_len) : NULL; |
249 buf_len_ = buf_len; | 231 buf_len_ = buf_len; |
250 user_callback_ = callback; | |
251 | 232 |
252 result_ = 0; | 233 result_ = 0; |
253 pending_ = false; | |
254 finished_ = false; | |
255 abort_ = false; | 234 abort_ = false; |
256 | 235 |
257 if (entry_->net_log().IsLoggingAllEvents()) { | 236 if (entry_->net_log().IsLoggingAllEvents()) { |
258 entry_->net_log().BeginEvent( | 237 entry_->net_log().BeginEvent( |
259 GetSparseEventType(operation_), | 238 GetSparseEventType(operation_), |
260 CreateNetLogSparseOperationCallback(offset_, buf_len_)); | 239 CreateNetLogSparseOperationCallback(offset_, buf_len_)); |
261 } | 240 } |
262 DoChildrenIO(); | |
263 | 241 |
264 if (!pending_) { | 242 DCHECK_EQ(next_state_, STATE_NONE); |
265 // Everything was done synchronously. | 243 next_state_ = init_ ? STATE_GET_CHILD_KEY : STATE_INIT; |
| 244 |
| 245 int rv = DoLoop(net::OK); |
| 246 if (rv == net::ERR_IO_PENDING) { |
| 247 user_callback_ = callback; |
| 248 entry_->AddRef(); // Self preservation while we're working. |
| 249 } else { |
266 operation_ = kNoOperation; | 250 operation_ = kNoOperation; |
267 user_buf_ = NULL; | 251 user_buf_ = NULL; |
268 user_callback_.Reset(); | |
269 return result_; | |
270 } | 252 } |
271 | 253 |
272 return net::ERR_IO_PENDING; | 254 return rv; |
273 } | 255 } |
274 | 256 |
275 int SparseControl::GetAvailableRange(int64 offset, int len, int64* start) { | 257 int SparseControlV3::GetAvailableRange(int64 offset, int len, int64* start, |
276 DCHECK(init_); | 258 const CompletionCallback& callback) { |
277 // We don't support simultaneous IO for sparse data. | 259 // We don't support simultaneous IO for sparse data. |
278 if (operation_ != kNoOperation) | 260 if (operation_ != kNoOperation) |
279 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 261 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
280 | 262 |
281 DCHECK(start); | 263 DCHECK(start); |
282 | 264 |
| 265 // If there is a failure, we want to return a valid start. |
| 266 *start = offset; |
283 range_found_ = false; | 267 range_found_ = false; |
284 int result = StartIO( | 268 range_start_ = start; |
285 kGetRangeOperation, offset, NULL, len, CompletionCallback()); | 269 int result = StartIO(kGetRangeOperation, offset, NULL, len, callback); |
286 if (range_found_) { | 270 if (range_found_) { |
287 *start = offset_; | 271 *start = offset_; |
288 return result; | 272 return result; |
289 } | 273 } |
290 | 274 |
291 // This is a failure. We want to return a valid start value in any case. | 275 return result; |
292 *start = offset; | |
293 return result < 0 ? result : 0; // Don't mask error codes to the caller. | |
294 } | 276 } |
295 | 277 |
296 void SparseControl::CancelIO() { | 278 void SparseControlV3::CancelIO() { |
297 if (operation_ == kNoOperation) | 279 if (operation_ == kNoOperation) |
298 return; | 280 return; |
299 abort_ = true; | 281 abort_ = true; |
300 } | 282 } |
301 | 283 |
302 int SparseControl::ReadyToUse(const CompletionCallback& callback) { | 284 int SparseControlV3::ReadyToUse(const CompletionCallback& callback) { |
303 if (!abort_) | 285 if (!abort_) |
304 return net::OK; | 286 return net::OK; |
305 | 287 |
306 // We'll grab another reference to keep this object alive because we just have | 288 // We'll grab another reference to keep this object alive because we just have |
307 // one extra reference due to the pending IO operation itself, but we'll | 289 // one extra reference due to the pending IO operation itself, but we'll |
308 // release that one before invoking user_callback_. | 290 // release that one before invoking user_callback_. |
309 entry_->AddRef(); // Balanced in DoAbortCallbacks. | 291 entry_->AddRef(); // Balanced in HanldeAbortCallbacks. |
310 abort_callbacks_.push_back(callback); | 292 abort_callbacks_.push_back(callback); |
311 return net::ERR_IO_PENDING; | 293 return net::ERR_IO_PENDING; |
312 } | 294 } |
313 | 295 |
314 // Static | 296 // Static |
315 void SparseControl::DeleteChildren(EntryImpl* entry) { | 297 void SparseControlV3::DeleteChildren(EntryImplV3* entry) { |
316 DCHECK(entry->GetEntryFlags() & PARENT_ENTRY); | 298 DCHECK(entry->GetEntryFlags() & PARENT_ENTRY); |
317 int data_len = entry->GetDataSize(kSparseIndex); | 299 int data_len = entry->GetDataSize(kSparseIndex); |
318 if (data_len < static_cast<int>(sizeof(SparseData)) || | 300 if (data_len < static_cast<int>(sizeof(SparseData)) || |
319 entry->GetDataSize(kSparseData)) | 301 entry->GetDataSize(kSparseData)) |
320 return; | 302 return; |
321 | 303 |
322 int map_len = data_len - sizeof(SparseHeader); | 304 int map_len = data_len - sizeof(SparseHeader); |
323 if (map_len > kMaxMapSize || map_len % 4) | 305 if (map_len > kMaxMapSize || map_len % 4) |
324 return; | 306 return; |
325 | 307 |
326 char* buffer; | 308 scoped_refptr<net::IOBuffer> buffer; |
327 Addr address; | 309 Addr address; |
328 entry->GetData(kSparseIndex, &buffer, &address); | 310 entry->GetData(kSparseIndex, &buffer, &address); |
329 if (!buffer && !address.is_initialized()) | 311 if (!buffer && !address.is_initialized()) |
330 return; | 312 return; |
331 | 313 |
332 entry->net_log().AddEvent(net::NetLog::TYPE_SPARSE_DELETE_CHILDREN); | 314 entry->net_log().AddEvent(net::NetLog::TYPE_SPARSE_DELETE_CHILDREN); |
333 | 315 |
334 DCHECK(entry->backend_); | 316 DCHECK(entry->backend_); |
335 ChildrenDeleter* deleter = new ChildrenDeleter(entry->backend_.get(), | 317 ChildrenDeleter* deleter = new ChildrenDeleter(entry->backend_.get(), |
336 entry->GetKey()); | 318 entry->GetKey()); |
337 // The object will self destruct when finished. | 319 // The object will self destruct when finished. |
338 deleter->AddRef(); | 320 deleter->AddRef(); |
339 | 321 |
340 if (buffer) { | 322 if (buffer) |
341 base::MessageLoop::current()->PostTask( | 323 deleter->Start(buffer, data_len); |
342 FROM_HERE, | 324 else |
343 base::Bind(&ChildrenDeleter::Start, deleter, buffer, data_len)); | 325 deleter->ReadData(address, data_len); |
344 } else { | |
345 base::MessageLoop::current()->PostTask( | |
346 FROM_HERE, | |
347 base::Bind(&ChildrenDeleter::ReadData, deleter, address, data_len)); | |
348 } | |
349 } | 326 } |
350 | 327 |
351 // ----------------------------------------------------------------------- | 328 // ----------------------------------------------------------------------- |
352 | 329 |
353 int SparseControl::Init() { | 330 int SparseControlV3::DoLoop(int result) { |
| 331 DCHECK(next_state_ != STATE_NONE); |
| 332 |
| 333 int rv = result; |
| 334 do { |
| 335 State state = next_state_; |
| 336 next_state_ = STATE_NONE; |
| 337 switch (state) { |
| 338 case STATE_INIT: |
| 339 DCHECK_EQ(net::OK, rv); |
| 340 rv = DoInit(); |
| 341 break; |
| 342 case STATE_CREATE_SPARSE_ENTRY: |
| 343 DCHECK_EQ(net::OK, rv); |
| 344 rv = DoCreateSparseEntry(); |
| 345 break; |
| 346 case STATE_CREATE_SPARSE_ENTRY_COMPLETE: |
| 347 rv = DoCreateSparseEntryComplete(rv); |
| 348 break; |
| 349 case STATE_OPEN_SPARSE_ENTRY: |
| 350 rv = DoOpenSparseEntry(rv); |
| 351 break; |
| 352 case STATE_OPEN_SPARSE_ENTRY_COMPLETE: |
| 353 rv = DoOpenSparseEntryComplete(rv); |
| 354 break; |
| 355 case STATE_READ_BITMAP_COMPLETE: |
| 356 rv = DoReadBitmapComplete(rv); |
| 357 break; |
| 358 case STATE_GET_CHILD_KEY: |
| 359 DCHECK_EQ(net::OK, rv); |
| 360 rv = DoGetChildKey(); |
| 361 break; |
| 362 case STATE_OPEN_CHILD: |
| 363 DCHECK_EQ(net::OK, rv); |
| 364 rv = DoOpenChild(); |
| 365 break; |
| 366 case STATE_OPEN_CHILD_COMPLETE: |
| 367 rv = DoOpenChildComplete(rv); |
| 368 break; |
| 369 case STATE_CREATE_CHILD: |
| 370 DCHECK_EQ(net::OK, rv); |
| 371 rv = DoCreateChild(); |
| 372 break; |
| 373 case STATE_CREATE_CHILD_COMPLETE: |
| 374 rv = DoCreateChildComplete(rv); |
| 375 break; |
| 376 case STATE_READ_SIGNATURE_COMPLETE: |
| 377 rv = DoReadSignatureComplete(rv); |
| 378 break; |
| 379 case STATE_CLOSE_CHILD: |
| 380 DCHECK_EQ(net::OK, rv); |
| 381 rv = DoCloseChild(); |
| 382 break; |
| 383 case STATE_CLOSE_CHILD_COMPLETE: |
| 384 rv = DoCloseChildComplete(rv); |
| 385 break; |
| 386 case STATE_DO_CHILD_IO: |
| 387 DCHECK_EQ(net::OK, rv); |
| 388 rv = DoChildIO(); |
| 389 break; |
| 390 case STATE_DO_CHILD_IO_COMPLETE: |
| 391 rv = DoChildIOComplete(rv); |
| 392 break; |
| 393 case STATE_CLOSE: |
| 394 DCHECK_EQ(net::OK, rv); |
| 395 rv = DoClose(); |
| 396 break; |
| 397 case STATE_WRITE_BITMAP: |
| 398 DCHECK_EQ(net::OK, rv); |
| 399 rv = DoWriteBitmap(); |
| 400 break; |
| 401 case STATE_WRITE_BITMAP_COMPLETE: |
| 402 rv = DoWriteBitmapComplete(rv); |
| 403 break; |
| 404 default: |
| 405 NOTREACHED(); |
| 406 } |
| 407 } while (rv != net::ERR_IO_PENDING && next_state_ != STATE_NONE); |
| 408 |
| 409 if (rv != net::ERR_IO_PENDING) |
| 410 HandleResult(rv); |
| 411 |
| 412 return rv; |
| 413 } |
| 414 |
| 415 int SparseControlV3::DoInit() { |
354 DCHECK(!init_); | 416 DCHECK(!init_); |
355 | 417 |
356 // We should not have sparse data for the exposed entry. | 418 // We should not have sparse data for the exposed entry. |
357 if (entry_->GetDataSize(kSparseData)) | 419 if (entry_->GetDataSize(kSparseData)) |
358 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 420 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
359 | 421 |
360 // Now see if there is something where we store our data. | 422 // Now see if there is something where we store our data. |
361 int rv = net::OK; | 423 int rv = net::OK; |
362 int data_len = entry_->GetDataSize(kSparseIndex); | 424 int data_len = entry_->GetDataSize(kSparseIndex); |
363 if (!data_len) { | 425 if (data_len) { |
364 rv = CreateSparseEntry(); | 426 next_state_ = STATE_OPEN_SPARSE_ENTRY; |
365 } else { | 427 return data_len; |
366 rv = OpenSparseEntry(data_len); | |
367 } | 428 } |
368 | 429 next_state_ = STATE_CREATE_SPARSE_ENTRY; |
369 if (rv == net::OK) | 430 return net::OK; |
370 init_ = true; | |
371 return rv; | |
372 } | 431 } |
373 | 432 |
374 // We are going to start using this entry to store sparse data, so we have to | 433 // We are going to start using this entry to store sparse data, so we have to |
375 // initialize our control info. | 434 // initialize our control info. |
376 int SparseControl::CreateSparseEntry() { | 435 int SparseControlV3::DoCreateSparseEntry() { |
377 if (CHILD_ENTRY & entry_->GetEntryFlags()) | 436 if (CHILD_ENTRY & entry_->GetEntryFlags()) |
378 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 437 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
379 | 438 |
380 memset(&sparse_header_, 0, sizeof(sparse_header_)); | 439 memset(&sparse_header_, 0, sizeof(sparse_header_)); |
381 sparse_header_.signature = Time::Now().ToInternalValue(); | 440 sparse_header_.signature = Time::Now().ToInternalValue(); |
382 sparse_header_.magic = kIndexMagic; | 441 sparse_header_.magic = kIndexMagicV3; |
383 sparse_header_.parent_key_len = entry_->GetKey().size(); | 442 sparse_header_.parent_key_len = entry_->GetKey().size(); |
384 children_map_.Resize(kNumSparseBits, true); | 443 children_map_.Resize(kNumSparseBits, true); |
385 | 444 |
386 // Save the header. The bitmap is saved in the destructor. | 445 // Save the header. The bitmap is saved in the destructor. |
387 scoped_refptr<net::IOBuffer> buf( | 446 scoped_refptr<net::IOBuffer> buf( |
388 new net::WrappedIOBuffer(reinterpret_cast<char*>(&sparse_header_))); | 447 new net::WrappedIOBuffer(reinterpret_cast<char*>(&sparse_header_))); |
389 | 448 |
390 int rv = entry_->WriteData(kSparseIndex, 0, buf.get(), sizeof(sparse_header_), | 449 next_state_ = STATE_CREATE_SPARSE_ENTRY_COMPLETE; |
391 CompletionCallback(), false); | 450 return entry_->WriteData(kSparseIndex, 0, buf, sizeof(sparse_header_), |
392 if (rv != sizeof(sparse_header_)) { | 451 callback_, false); |
| 452 } |
| 453 |
| 454 int SparseControlV3::DoCreateSparseEntryComplete(int result) { |
| 455 if (result != sizeof(sparse_header_)) { |
393 DLOG(ERROR) << "Unable to save sparse_header_"; | 456 DLOG(ERROR) << "Unable to save sparse_header_"; |
394 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 457 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
395 } | 458 } |
396 | 459 |
397 entry_->SetEntryFlags(PARENT_ENTRY); | 460 entry_->SetEntryFlags(PARENT_ENTRY); |
| 461 init_ = true; |
| 462 valid_ = true; |
| 463 next_state_ = STATE_GET_CHILD_KEY; |
398 return net::OK; | 464 return net::OK; |
399 } | 465 } |
400 | 466 |
401 // We are opening an entry from disk. Make sure that our control data is there. | 467 // We are opening an entry from disk. Make sure that our control data is there. |
402 int SparseControl::OpenSparseEntry(int data_len) { | 468 int SparseControlV3::DoOpenSparseEntry(int data_len) { |
403 if (data_len < static_cast<int>(sizeof(SparseData))) | 469 if (data_len < static_cast<int>(sizeof(SparseData))) |
404 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 470 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
405 | 471 |
406 if (entry_->GetDataSize(kSparseData)) | 472 if (entry_->GetDataSize(kSparseData)) |
407 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 473 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
408 | 474 |
409 if (!(PARENT_ENTRY & entry_->GetEntryFlags())) | 475 if (!(PARENT_ENTRY & entry_->GetEntryFlags())) |
410 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 476 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
411 | 477 |
412 // Dont't go over board with the bitmap. 8 KB gives us offsets up to 64 GB. | 478 // Dont't go over board with the bitmap. 8 KB gives us offsets up to 64 GB. |
413 int map_len = data_len - sizeof(sparse_header_); | 479 int map_len = data_len - sizeof(sparse_header_); |
414 if (map_len > kMaxMapSize || map_len % 4) | 480 if (map_len > kMaxMapSize || map_len % 4) |
415 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 481 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
416 | 482 |
417 scoped_refptr<net::IOBuffer> buf( | 483 scoped_refptr<net::IOBuffer> buf( |
418 new net::WrappedIOBuffer(reinterpret_cast<char*>(&sparse_header_))); | 484 new net::WrappedIOBuffer(reinterpret_cast<char*>(&sparse_header_))); |
419 | 485 |
420 // Read header. | 486 // Read header. |
421 int rv = entry_->ReadData(kSparseIndex, 0, buf.get(), sizeof(sparse_header_), | 487 next_state_ = STATE_OPEN_SPARSE_ENTRY_COMPLETE; |
422 CompletionCallback()); | 488 return entry_->ReadData(kSparseIndex, 0, buf, sizeof(sparse_header_), |
423 if (rv != static_cast<int>(sizeof(sparse_header_))) | 489 callback_); |
| 490 } |
| 491 |
| 492 int SparseControlV3::DoOpenSparseEntryComplete(int result) { |
| 493 if (result != static_cast<int>(sizeof(sparse_header_))) |
424 return net::ERR_CACHE_READ_FAILURE; | 494 return net::ERR_CACHE_READ_FAILURE; |
425 | 495 |
426 // The real validation should be performed by the caller. This is just to | 496 // The real validation should be performed by the caller. This is just to |
427 // double check. | 497 // double check. |
428 if (sparse_header_.magic != kIndexMagic || | 498 if (sparse_header_.magic != kIndexMagicV3 || |
429 sparse_header_.parent_key_len != | 499 sparse_header_.parent_key_len != |
430 static_cast<int>(entry_->GetKey().size())) | 500 static_cast<int>(entry_->GetKey().size())) |
431 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 501 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
432 | 502 |
433 // Read the actual bitmap. | 503 // Read the actual bitmap. |
434 buf = new net::IOBuffer(map_len); | 504 int map_len = entry_->GetDataSize(kSparseIndex) - sizeof(sparse_header_); |
435 rv = entry_->ReadData(kSparseIndex, sizeof(sparse_header_), buf.get(), | 505 buf_ = new net::IOBuffer(map_len); |
436 map_len, CompletionCallback()); | 506 next_state_ = STATE_READ_BITMAP_COMPLETE; |
437 if (rv != map_len) | 507 return entry_->ReadData(kSparseIndex, sizeof(sparse_header_), buf_, map_len, |
| 508 callback_); |
| 509 } |
| 510 |
| 511 int SparseControlV3::DoReadBitmapComplete(int result) { |
| 512 int map_len = entry_->GetDataSize(kSparseIndex) - sizeof(sparse_header_); |
| 513 if (result != map_len) |
438 return net::ERR_CACHE_READ_FAILURE; | 514 return net::ERR_CACHE_READ_FAILURE; |
439 | 515 |
440 // Grow the bitmap to the current size and copy the bits. | 516 // Grow the bitmap to the current size and copy the bits. |
441 children_map_.Resize(map_len * 8, false); | 517 children_map_.Resize(map_len * 8, false); |
442 children_map_.SetMap(reinterpret_cast<uint32*>(buf->data()), map_len); | 518 children_map_.SetMap(reinterpret_cast<uint32*>(buf_->data()), map_len); |
| 519 init_ = true; |
| 520 valid_ = true; |
| 521 next_state_ = STATE_GET_CHILD_KEY; |
443 return net::OK; | 522 return net::OK; |
444 } | 523 } |
445 | 524 |
446 bool SparseControl::OpenChild() { | 525 int SparseControlV3::DoGetChildKey() { |
447 DCHECK_GE(result_, 0); | 526 key_ = GenerateChildKey(); |
448 | |
449 std::string key = GenerateChildKey(); | |
450 if (child_) { | 527 if (child_) { |
451 // Keep using the same child or open another one?. | 528 // Keep using the same child or open another one?. |
452 if (key == child_->GetKey()) | 529 if (key_ == child_->GetKey()) { |
453 return true; | 530 next_state_ = STATE_DO_CHILD_IO; |
454 CloseChild(); | 531 return net::OK; |
| 532 } |
| 533 next_state_ = STATE_CLOSE_CHILD; |
| 534 return net::OK; |
| 535 } |
| 536 next_state_ = STATE_OPEN_CHILD; |
| 537 return net::OK; |
| 538 } |
| 539 |
| 540 int SparseControlV3::DoOpenChild() { |
| 541 // See if we are tracking this child. |
| 542 if (!ChildPresent()) { |
| 543 next_state_ = STATE_CREATE_CHILD; |
| 544 return net::OK; |
455 } | 545 } |
456 | 546 |
457 // See if we are tracking this child. | 547 if (!entry_->backend_) |
458 if (!ChildPresent()) | 548 return net::ERR_FAILED; |
459 return ContinueWithoutChild(key); | |
460 | 549 |
461 if (!entry_->backend_) | 550 next_state_ = STATE_OPEN_CHILD_COMPLETE; |
462 return false; | 551 return entry_->backend_->OpenEntry(key_, &child_, callback_); |
| 552 } |
463 | 553 |
464 child_ = entry_->backend_->OpenEntryImpl(key); | 554 int SparseControlV3::DoOpenChildComplete(int result) { |
465 if (!child_) | 555 if (!child_) { |
466 return ContinueWithoutChild(key); | 556 next_state_ = STATE_CREATE_CHILD; |
| 557 return net::OK; |
| 558 } |
467 | 559 |
468 EntryImpl* child = static_cast<EntryImpl*>(child_); | 560 EntryImplV3* child = static_cast<EntryImplV3*>(child_); |
469 if (!(CHILD_ENTRY & child->GetEntryFlags()) || | 561 if (!(CHILD_ENTRY & child->GetEntryFlags()) || |
470 child->GetDataSize(kSparseIndex) < | 562 child->GetDataSize(kSparseIndex) < |
471 static_cast<int>(sizeof(child_data_))) | 563 static_cast<int>(sizeof(child_data_))) { |
472 return KillChildAndContinue(key, false); | 564 return KillChildAndContinue(); |
| 565 } |
473 | 566 |
474 scoped_refptr<net::WrappedIOBuffer> buf( | 567 scoped_refptr<net::WrappedIOBuffer> buf( |
475 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_))); | 568 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_))); |
476 | 569 |
477 // Read signature. | 570 // Read signature. |
478 int rv = child_->ReadData(kSparseIndex, 0, buf.get(), sizeof(child_data_), | 571 next_state_ = STATE_READ_SIGNATURE_COMPLETE; |
479 CompletionCallback()); | 572 return child_->ReadData(kSparseIndex, 0, buf, sizeof(child_data_), |
480 if (rv != sizeof(child_data_)) | 573 callback_); |
481 return KillChildAndContinue(key, true); // This is a fatal failure. | 574 } |
| 575 |
| 576 int SparseControlV3::DoReadSignatureComplete(int result) { |
| 577 if (result != sizeof(child_data_)) |
| 578 return KillChildAndContinue(); |
482 | 579 |
483 if (child_data_.header.signature != sparse_header_.signature || | 580 if (child_data_.header.signature != sparse_header_.signature || |
484 child_data_.header.magic != kIndexMagic) | 581 child_data_.header.magic != kIndexMagicV3) |
485 return KillChildAndContinue(key, false); | 582 return KillChildAndContinue(); |
486 | 583 |
487 if (child_data_.header.last_block_len < 0 || | 584 if (child_data_.header.last_block_len < 0 || |
488 child_data_.header.last_block_len > kBlockSize) { | 585 child_data_.header.last_block_len > kBlockSize) { |
489 // Make sure these values are always within range. | 586 // Make sure these values are always within range. |
490 child_data_.header.last_block_len = 0; | 587 child_data_.header.last_block_len = 0; |
491 child_data_.header.last_block = -1; | 588 child_data_.header.last_block = -1; |
492 } | 589 } |
493 | 590 |
494 return true; | 591 next_state_ = STATE_DO_CHILD_IO; |
| 592 return net::OK; |
495 } | 593 } |
496 | 594 |
497 void SparseControl::CloseChild() { | 595 int SparseControlV3::DoCloseChild() { |
| 596 next_state_ = STATE_CLOSE_CHILD_COMPLETE; |
498 scoped_refptr<net::WrappedIOBuffer> buf( | 597 scoped_refptr<net::WrappedIOBuffer> buf( |
499 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_))); | 598 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_))); |
500 | 599 |
501 // Save the allocation bitmap before closing the child entry. | 600 // Save the allocation bitmap before closing the child entry. |
502 int rv = child_->WriteData(kSparseIndex, 0, buf.get(), sizeof(child_data_), | 601 return child_->WriteData(kSparseIndex, 0, buf, sizeof(child_data_), |
503 CompletionCallback(), | 602 callback_, false); |
504 false); | 603 } |
505 if (rv != sizeof(child_data_)) | 604 |
| 605 int SparseControlV3::DoCloseChildComplete(int result) { |
| 606 if (result != sizeof(child_data_)) |
506 DLOG(ERROR) << "Failed to save child data"; | 607 DLOG(ERROR) << "Failed to save child data"; |
507 child_->Release(); | 608 child_->Close(); |
508 child_ = NULL; | 609 child_ = NULL; |
| 610 |
| 611 DCHECK(valid_); |
| 612 if (closing_ && user_callback_.is_null()) |
| 613 next_state_= STATE_WRITE_BITMAP; |
| 614 else |
| 615 next_state_ = STATE_OPEN_CHILD; |
| 616 |
| 617 return net::OK; |
509 } | 618 } |
510 | 619 |
511 // We were not able to open this child; see what we can do. | 620 // We were not able to open this child; see what we can do. |
512 bool SparseControl::ContinueWithoutChild(const std::string& key) { | 621 int SparseControlV3::DoCreateChild() { |
513 if (kReadOperation == operation_) | 622 if (kReadOperation == operation_) |
514 return false; | 623 return 0; |
515 if (kGetRangeOperation == operation_) | 624 if (kGetRangeOperation == operation_) { |
516 return true; | 625 next_state_ = STATE_DO_CHILD_IO; |
| 626 return net::OK; |
| 627 } |
517 | 628 |
518 if (!entry_->backend_) | 629 if (!entry_->backend_) |
519 return false; | 630 return net::ERR_FAILED; |
520 | 631 |
521 child_ = entry_->backend_->CreateEntryImpl(key); | 632 next_state_ = STATE_CREATE_CHILD_COMPLETE; |
522 if (!child_) { | 633 return entry_->backend_->CreateEntry(key_, &child_, callback_); |
523 child_ = NULL; | 634 } |
524 result_ = net::ERR_CACHE_READ_FAILURE; | 635 |
525 return false; | 636 int SparseControlV3::DoCreateChildComplete(int result) { |
526 } | 637 if (result != net::OK) |
| 638 return net::ERR_CACHE_READ_FAILURE; |
| 639 |
527 // Write signature. | 640 // Write signature. |
528 InitChildData(); | 641 InitChildData(); |
529 return true; | 642 next_state_ = STATE_DO_CHILD_IO; |
| 643 return net::OK; |
530 } | 644 } |
531 | 645 |
532 void SparseControl::WriteSparseData() { | 646 int SparseControlV3::DoWriteBitmap() { |
| 647 next_state_ = STATE_WRITE_BITMAP_COMPLETE; |
533 scoped_refptr<net::IOBuffer> buf(new net::WrappedIOBuffer( | 648 scoped_refptr<net::IOBuffer> buf(new net::WrappedIOBuffer( |
534 reinterpret_cast<const char*>(children_map_.GetMap()))); | 649 reinterpret_cast<const char*>(children_map_.GetMap()))); |
535 | 650 |
536 int len = children_map_.ArraySize() * 4; | 651 int len = children_map_.ArraySize() * 4; |
537 int rv = entry_->WriteData(kSparseIndex, sizeof(sparse_header_), buf.get(), | 652 return entry_->WriteData(kSparseIndex, sizeof(sparse_header_), buf, len, |
538 len, CompletionCallback(), false); | 653 callback_, false); |
539 if (rv != len) { | 654 } |
| 655 |
| 656 int SparseControlV3::DoWriteBitmapComplete(int result) { |
| 657 if (result != children_map_.ArraySize() * 4) { |
540 DLOG(ERROR) << "Unable to save sparse map"; | 658 DLOG(ERROR) << "Unable to save sparse map"; |
541 } | 659 } |
| 660 return net::OK; |
542 } | 661 } |
543 | 662 |
544 bool SparseControl::DoChildIO() { | 663 int SparseControlV3::DoChildIO() { |
545 finished_ = true; | |
546 if (!buf_len_ || result_ < 0) | |
547 return false; | |
548 | |
549 if (!OpenChild()) | |
550 return false; | |
551 | |
552 if (!VerifyRange()) | 664 if (!VerifyRange()) |
553 return false; | 665 return 0; |
554 | |
555 // We have more work to do. Let's not trigger a callback to the caller. | |
556 finished_ = false; | |
557 CompletionCallback callback; | |
558 if (!user_callback_.is_null()) { | |
559 callback = | |
560 base::Bind(&SparseControl::OnChildIOCompleted, base::Unretained(this)); | |
561 } | |
562 | 666 |
563 int rv = 0; | 667 int rv = 0; |
564 switch (operation_) { | 668 switch (operation_) { |
565 case kReadOperation: | 669 case kReadOperation: |
566 if (entry_->net_log().IsLoggingAllEvents()) { | 670 LogChildOperationStart(); |
567 entry_->net_log().BeginEvent( | 671 rv = child_->ReadData(kSparseData, child_offset_, user_buf_, |
568 net::NetLog::TYPE_SPARSE_READ_CHILD_DATA, | 672 child_len_, callback_); |
569 CreateNetLogSparseReadWriteCallback(child_->net_log().source(), | |
570 child_len_)); | |
571 } | |
572 rv = child_->ReadDataImpl(kSparseData, child_offset_, user_buf_.get(), | |
573 child_len_, callback); | |
574 break; | 673 break; |
575 case kWriteOperation: | 674 case kWriteOperation: |
576 if (entry_->net_log().IsLoggingAllEvents()) { | 675 LogChildOperationStart(); |
577 entry_->net_log().BeginEvent( | 676 rv = child_->WriteData(kSparseData, child_offset_, user_buf_, |
578 net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA, | 677 child_len_, callback_, false); |
579 CreateNetLogSparseReadWriteCallback(child_->net_log().source(), | |
580 child_len_)); | |
581 } | |
582 rv = child_->WriteDataImpl(kSparseData, child_offset_, user_buf_.get(), | |
583 child_len_, callback, false); | |
584 break; | 678 break; |
585 case kGetRangeOperation: | 679 case kGetRangeOperation: |
586 rv = DoGetAvailableRange(); | 680 rv = GetAvailableRangeImpl(); |
587 break; | 681 break; |
588 default: | 682 default: |
589 NOTREACHED(); | 683 NOTREACHED(); |
590 } | 684 } |
591 | 685 |
592 if (rv == net::ERR_IO_PENDING) { | 686 next_state_ = STATE_DO_CHILD_IO_COMPLETE; |
593 if (!pending_) { | 687 return rv; |
594 pending_ = true; | |
595 // The child will protect himself against closing the entry while IO is in | |
596 // progress. However, this entry can still be closed, and that would not | |
597 // be a good thing for us, so we increase the refcount until we're | |
598 // finished doing sparse stuff. | |
599 entry_->AddRef(); // Balanced in DoUserCallback. | |
600 } | |
601 return false; | |
602 } | |
603 if (!rv) | |
604 return false; | |
605 | |
606 DoChildIOCompleted(rv); | |
607 return true; | |
608 } | 688 } |
609 | 689 |
610 void SparseControl::DoChildIOCompleted(int result) { | 690 int SparseControlV3::DoChildIOComplete(int result) { |
611 LogChildOperationEnd(entry_->net_log(), operation_, result); | 691 LogChildOperationEnd(result); |
612 if (result < 0) { | 692 |
613 // We fail the whole operation if we encounter an error. | 693 if (result < 0) |
614 result_ = result; | 694 return LogCompletion(result); |
615 return; | |
616 } | |
617 | 695 |
618 UpdateRange(result); | 696 UpdateRange(result); |
619 | 697 |
620 result_ += result; | 698 if (operation_ != kGetRangeOperation) |
| 699 result_ += result; |
621 offset_ += result; | 700 offset_ += result; |
622 buf_len_ -= result; | 701 buf_len_ -= result; |
623 | 702 |
| 703 if (!buf_len_) |
| 704 return LogCompletion(result_); |
| 705 |
624 // We'll be reusing the user provided buffer for the next chunk. | 706 // We'll be reusing the user provided buffer for the next chunk. |
625 if (buf_len_ && user_buf_) | 707 if (buf_len_ && user_buf_) |
626 user_buf_->DidConsume(result); | 708 user_buf_->DidConsume(result); |
| 709 |
| 710 next_state_ = STATE_GET_CHILD_KEY; |
| 711 return net::OK; |
627 } | 712 } |
628 | 713 |
629 std::string SparseControl::GenerateChildKey() { | 714 int SparseControlV3::DoClose() { |
| 715 DCHECK(valid_); |
| 716 DCHECK(user_callback_.is_null()); |
| 717 if (child_) |
| 718 next_state_= STATE_CLOSE_CHILD; |
| 719 else if (valid_) |
| 720 next_state_= STATE_WRITE_BITMAP; |
| 721 |
| 722 return net::OK; |
| 723 } |
| 724 |
| 725 std::string SparseControlV3::GenerateChildKey() { |
630 return GenerateChildName(entry_->GetKey(), sparse_header_.signature, | 726 return GenerateChildName(entry_->GetKey(), sparse_header_.signature, |
631 offset_ >> 20); | 727 offset_ >> 20); |
632 } | 728 } |
633 | 729 |
634 // We are deleting the child because something went wrong. | 730 // We are deleting the child because something went wrong. |
635 bool SparseControl::KillChildAndContinue(const std::string& key, bool fatal) { | 731 int SparseControlV3::KillChildAndContinue() { |
636 SetChildBit(false); | 732 SetChildBit(false); |
637 child_->DoomImpl(); | 733 child_->Doom(); |
638 child_->Release(); | 734 child_->Close(); |
639 child_ = NULL; | 735 child_ = NULL; |
640 if (fatal) { | 736 next_state_ = STATE_CREATE_CHILD; |
641 result_ = net::ERR_CACHE_READ_FAILURE; | 737 return net::OK; |
642 return false; | |
643 } | |
644 return ContinueWithoutChild(key); | |
645 } | 738 } |
646 | 739 |
647 bool SparseControl::ChildPresent() { | 740 bool SparseControlV3::ChildPresent() { |
648 int child_bit = static_cast<int>(offset_ >> 20); | 741 int child_bit = static_cast<int>(offset_ >> 20); |
649 if (children_map_.Size() <= child_bit) | 742 if (children_map_.Size() <= child_bit) |
650 return false; | 743 return false; |
651 | 744 |
652 return children_map_.Get(child_bit); | 745 return children_map_.Get(child_bit); |
653 } | 746 } |
654 | 747 |
655 void SparseControl::SetChildBit(bool value) { | 748 void SparseControlV3::SetChildBit(bool value) { |
656 int child_bit = static_cast<int>(offset_ >> 20); | 749 int child_bit = static_cast<int>(offset_ >> 20); |
657 | 750 |
658 // We may have to increase the bitmap of child entries. | 751 // We may have to increase the bitmap of child entries. |
659 if (children_map_.Size() <= child_bit) | 752 if (children_map_.Size() <= child_bit) |
660 children_map_.Resize(Bitmap::RequiredArraySize(child_bit + 1) * 32, true); | 753 children_map_.Resize(Bitmap::RequiredArraySize(child_bit + 1) * 32, true); |
661 | 754 |
662 children_map_.Set(child_bit, value); | 755 children_map_.Set(child_bit, value); |
663 } | 756 } |
664 | 757 |
665 bool SparseControl::VerifyRange() { | 758 bool SparseControlV3::VerifyRange() { |
666 DCHECK_GE(result_, 0); | |
667 | |
668 child_offset_ = static_cast<int>(offset_) & (kMaxEntrySize - 1); | 759 child_offset_ = static_cast<int>(offset_) & (kMaxEntrySize - 1); |
669 child_len_ = std::min(buf_len_, kMaxEntrySize - child_offset_); | 760 child_len_ = std::min(buf_len_, kMaxEntrySize - child_offset_); |
670 | 761 |
671 // We can write to (or get info from) anywhere in this child. | 762 // We can write to (or get info from) anywhere in this child. |
672 if (operation_ != kReadOperation) | 763 if (operation_ != kReadOperation) |
673 return true; | 764 return true; |
674 | 765 |
675 // Check that there are no holes in this range. | 766 // Check that there are no holes in this range. |
676 int last_bit = (child_offset_ + child_len_ + 1023) >> 10; | 767 int last_bit = (child_offset_ + child_len_ + 1023) >> 10; |
677 int start = child_offset_ >> 10; | 768 int start = child_offset_ >> 10; |
(...skipping 13 matching lines...) Expand all Loading... |
691 if (partial_block_len) { | 782 if (partial_block_len) { |
692 // We may have a few extra bytes. | 783 // We may have a few extra bytes. |
693 child_len_ = std::min(child_len_ + partial_block_len, buf_len_); | 784 child_len_ = std::min(child_len_ + partial_block_len, buf_len_); |
694 } | 785 } |
695 // There is no need to read more after this one. | 786 // There is no need to read more after this one. |
696 buf_len_ = child_len_; | 787 buf_len_ = child_len_; |
697 } | 788 } |
698 return true; | 789 return true; |
699 } | 790 } |
700 | 791 |
701 void SparseControl::UpdateRange(int result) { | 792 void SparseControlV3::UpdateRange(int result) { |
702 if (result <= 0 || operation_ != kWriteOperation) | 793 if (result <= 0 || operation_ != kWriteOperation) |
703 return; | 794 return; |
704 | 795 |
705 DCHECK_GE(child_data_.header.last_block_len, 0); | 796 DCHECK_GE(child_data_.header.last_block_len, 0); |
706 DCHECK_LT(child_data_.header.last_block_len, kMaxEntrySize); | 797 DCHECK_LT(child_data_.header.last_block_len, kMaxEntrySize); |
707 | 798 |
708 // Write the bitmap. | 799 // Write the bitmap. |
709 int first_bit = child_offset_ >> 10; | 800 int first_bit = child_offset_ >> 10; |
710 int block_offset = child_offset_ & (kBlockSize - 1); | 801 int block_offset = child_offset_ & (kBlockSize - 1); |
711 if (block_offset && (child_data_.header.last_block != first_bit || | 802 if (block_offset && (child_data_.header.last_block != first_bit || |
(...skipping 16 matching lines...) Expand all Loading... |
728 // The last block is not completely filled; save it for later. | 819 // The last block is not completely filled; save it for later. |
729 child_data_.header.last_block = last_bit; | 820 child_data_.header.last_block = last_bit; |
730 child_data_.header.last_block_len = block_offset; | 821 child_data_.header.last_block_len = block_offset; |
731 } else { | 822 } else { |
732 child_data_.header.last_block = -1; | 823 child_data_.header.last_block = -1; |
733 } | 824 } |
734 | 825 |
735 child_map_.SetRange(first_bit, last_bit, true); | 826 child_map_.SetRange(first_bit, last_bit, true); |
736 } | 827 } |
737 | 828 |
738 int SparseControl::PartialBlockLength(int block_index) const { | 829 int SparseControlV3::PartialBlockLength(int block_index) const { |
739 if (block_index == child_data_.header.last_block) | 830 if (block_index == child_data_.header.last_block) |
740 return child_data_.header.last_block_len; | 831 return child_data_.header.last_block_len; |
741 | 832 |
742 // This may be the last stored index. | 833 // This may be the last stored index. |
743 int entry_len = child_->GetDataSize(kSparseData); | 834 int entry_len = child_->GetDataSize(kSparseData); |
744 if (block_index == entry_len >> 10) | 835 if (block_index == entry_len >> 10) |
745 return entry_len & (kBlockSize - 1); | 836 return entry_len & (kBlockSize - 1); |
746 | 837 |
747 // This is really empty. | 838 // This is really empty. |
748 return 0; | 839 return 0; |
749 } | 840 } |
750 | 841 |
751 void SparseControl::InitChildData() { | 842 void SparseControlV3::InitChildData() { |
752 // We know the real type of child_. | 843 // We know the real type of child_. |
753 EntryImpl* child = static_cast<EntryImpl*>(child_); | 844 EntryImplV3* child = static_cast<EntryImplV3*>(child_); |
754 child->SetEntryFlags(CHILD_ENTRY); | 845 child->SetEntryFlags(CHILD_ENTRY); |
755 | 846 |
756 memset(&child_data_, 0, sizeof(child_data_)); | 847 memset(&child_data_, 0, sizeof(child_data_)); |
757 child_data_.header = sparse_header_; | 848 child_data_.header = sparse_header_; |
758 | 849 |
759 scoped_refptr<net::WrappedIOBuffer> buf( | 850 scoped_refptr<net::WrappedIOBuffer> buf( |
760 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_))); | 851 new net::WrappedIOBuffer(reinterpret_cast<char*>(&child_data_))); |
761 | 852 |
762 int rv = child_->WriteData(kSparseIndex, 0, buf.get(), sizeof(child_data_), | 853 int rv = child_->WriteData(kSparseIndex, 0, buf, sizeof(child_data_), |
763 CompletionCallback(), false); | 854 CompletionCallback(), false); |
764 if (rv != sizeof(child_data_)) | 855 if (rv != sizeof(child_data_)) |
765 DLOG(ERROR) << "Failed to save child data"; | 856 DLOG(ERROR) << "Failed to save child data"; |
766 SetChildBit(true); | 857 SetChildBit(true); |
767 } | 858 } |
768 | 859 |
769 int SparseControl::DoGetAvailableRange() { | 860 int SparseControlV3::GetAvailableRangeImpl() { |
770 if (!child_) | 861 if (!child_) |
771 return child_len_; // Move on to the next child. | 862 return child_len_; // Move on to the next child. |
772 | 863 |
773 // Check that there are no holes in this range. | 864 // Check that there are no holes in this range. |
774 int last_bit = (child_offset_ + child_len_ + 1023) >> 10; | 865 int last_bit = (child_offset_ + child_len_ + 1023) >> 10; |
775 int start = child_offset_ >> 10; | 866 int start = child_offset_ >> 10; |
776 int partial_start_bytes = PartialBlockLength(start); | 867 int partial_start_bytes = PartialBlockLength(start); |
777 int found = start; | 868 int found = start; |
778 int bits_found = child_map_.FindBits(&found, last_bit, true); | 869 int bits_found = child_map_.FindBits(&found, last_bit, true); |
779 | 870 |
(...skipping 26 matching lines...) Expand all Loading... |
806 | 897 |
807 // Only update offset_ when this query found zeros at the start. | 898 // Only update offset_ when this query found zeros at the start. |
808 if (empty_start) | 899 if (empty_start) |
809 offset_ += empty_start; | 900 offset_ += empty_start; |
810 | 901 |
811 // This will actually break the loop. | 902 // This will actually break the loop. |
812 buf_len_ = 0; | 903 buf_len_ = 0; |
813 return 0; | 904 return 0; |
814 } | 905 } |
815 | 906 |
816 void SparseControl::DoUserCallback() { | 907 void SparseControlV3::LogChildOperationStart() { |
817 DCHECK(!user_callback_.is_null()); | 908 net::NetLog::EventType type = (operation_ == kReadOperation) ? |
| 909 net::NetLog::TYPE_SPARSE_READ_CHILD_DATA : |
| 910 net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA; |
| 911 |
| 912 if (entry_->net_log().IsLoggingAllEvents()) { |
| 913 disk_cache::EntryImplV3* entry = |
| 914 reinterpret_cast<disk_cache::EntryImplV3*>(child_); |
| 915 entry_->net_log().BeginEvent( |
| 916 type, |
| 917 CreateNetLogSparseReadWriteCallback(entry->net_log().source(), |
| 918 child_len_)); |
| 919 } |
| 920 } |
| 921 |
| 922 void SparseControlV3::LogChildOperationEnd(int result) { |
| 923 if (entry_->net_log().IsLoggingAllEvents()) { |
| 924 net::NetLog::EventType event_type; |
| 925 switch (operation_) { |
| 926 case disk_cache::SparseControlV3::kReadOperation: |
| 927 event_type = net::NetLog::TYPE_SPARSE_READ_CHILD_DATA; |
| 928 break; |
| 929 case disk_cache::SparseControlV3::kWriteOperation: |
| 930 event_type = net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA; |
| 931 break; |
| 932 case disk_cache::SparseControlV3::kGetRangeOperation: |
| 933 return; |
| 934 default: |
| 935 NOTREACHED(); |
| 936 return; |
| 937 } |
| 938 entry_->net_log().EndEventWithNetErrorCode(event_type, result); |
| 939 } |
| 940 } |
| 941 |
| 942 int SparseControlV3::LogCompletion(int result) { |
| 943 if (!entry_->net_log().IsLoggingAllEvents()) |
| 944 return result; |
| 945 |
| 946 if (kGetRangeOperation == operation_) { |
| 947 entry_->net_log().EndEvent( |
| 948 net::NetLog::TYPE_SPARSE_GET_RANGE, |
| 949 CreateNetLogGetAvailableRangeResultCallback(offset_, result)); |
| 950 } else { |
| 951 entry_->net_log().EndEvent(GetSparseEventType(operation_)); |
| 952 } |
| 953 return result; |
| 954 } |
| 955 |
| 956 void SparseControlV3::HandleResult(int result) { |
| 957 if (!result && result_) |
| 958 result = result_; |
| 959 |
| 960 if (result > 0 && operation_ == kGetRangeOperation) |
| 961 *range_start_ = offset_; |
| 962 |
| 963 user_buf_ = NULL; |
| 964 operation_ = kNoOperation; |
| 965 next_state_ = STATE_NONE; |
| 966 |
| 967 if (user_callback_.is_null()) { |
| 968 if (closing_) { |
| 969 closing_ = false; |
| 970 entry_->Release(); // Don't touch object after this line. |
| 971 } |
| 972 return; |
| 973 } |
| 974 |
818 CompletionCallback cb = user_callback_; | 975 CompletionCallback cb = user_callback_; |
819 user_callback_.Reset(); | 976 user_callback_.Reset(); |
820 user_buf_ = NULL; | 977 bool closing = closing_; |
821 pending_ = false; | 978 DCHECK(!closing_ || !entry_->HasOneRef()); |
822 operation_ = kNoOperation; | 979 |
823 int rv = result_; | |
824 entry_->Release(); // Don't touch object after this line. | 980 entry_->Release(); // Don't touch object after this line. |
825 cb.Run(rv); | 981 cb.Run(result); |
| 982 |
| 983 if (closing) { |
| 984 // This object is not gone yet, but there's more work to do before the |
| 985 // destructor runs. |
| 986 next_state_ = STATE_CLOSE; |
| 987 int rv = DoLoop(net::OK); |
| 988 } |
826 } | 989 } |
827 | 990 |
828 void SparseControl::DoAbortCallbacks() { | 991 void SparseControlV3::HanldeAbortCallbacks() { |
829 for (size_t i = 0; i < abort_callbacks_.size(); i++) { | 992 for (size_t i = 0; i < abort_callbacks_.size(); i++) { |
830 // Releasing all references to entry_ may result in the destruction of this | 993 // Releasing all references to entry_ may result in the destruction of this |
831 // object so we should not be touching it after the last Release(). | 994 // object so we should not be touching it after the last Release(). |
832 CompletionCallback cb = abort_callbacks_[i]; | 995 CompletionCallback cb = abort_callbacks_[i]; |
833 if (i == abort_callbacks_.size() - 1) | 996 if (i == abort_callbacks_.size() - 1) |
834 abort_callbacks_.clear(); | 997 abort_callbacks_.clear(); |
835 | 998 |
836 entry_->Release(); // Don't touch object after this line. | 999 entry_->Release(); // Don't touch object after this line. |
837 cb.Run(net::OK); | 1000 cb.Run(net::OK); |
838 } | 1001 } |
839 } | 1002 } |
840 | 1003 |
841 void SparseControl::OnChildIOCompleted(int result) { | 1004 void SparseControlV3::OnIOComplete(int result) { |
842 DCHECK_NE(net::ERR_IO_PENDING, result); | 1005 DCHECK_NE(net::ERR_IO_PENDING, result); |
843 DoChildIOCompleted(result); | |
844 | 1006 |
845 if (abort_) { | 1007 if (abort_) { |
846 // We'll return the current result of the operation, which may be less than | 1008 // We'll return the current result of the operation, which may be less than |
847 // the bytes to read or write, but the user cancelled the operation. | 1009 // the bytes to read or write, but the user cancelled the operation. |
848 abort_ = false; | 1010 abort_ = false; |
849 if (entry_->net_log().IsLoggingAllEvents()) { | 1011 if (entry_->net_log().IsLoggingAllEvents()) { |
850 entry_->net_log().AddEvent(net::NetLog::TYPE_CANCELLED); | 1012 entry_->net_log().AddEvent(net::NetLog::TYPE_CANCELLED); |
851 entry_->net_log().EndEvent(GetSparseEventType(operation_)); | 1013 entry_->net_log().EndEvent(GetSparseEventType(operation_)); |
852 } | 1014 } |
853 // We have an indirect reference to this object for every callback so if | 1015 // We have an indirect reference to this object for every callback so if |
854 // there is only one callback, we may delete this object before reaching | 1016 // there is only one callback, we may delete this object before reaching |
855 // DoAbortCallbacks. | 1017 // HanldeAbortCallbacks. |
856 bool has_abort_callbacks = !abort_callbacks_.empty(); | 1018 bool has_abort_callbacks = !abort_callbacks_.empty(); |
857 DoUserCallback(); | 1019 HandleResult(result); |
858 if (has_abort_callbacks) | 1020 if (has_abort_callbacks) |
859 DoAbortCallbacks(); | 1021 HanldeAbortCallbacks(); |
860 return; | 1022 return; |
861 } | 1023 } |
862 | 1024 |
863 // We are running a callback from the message loop. It's time to restart what | 1025 DoLoop(result); |
864 // we were doing before. | |
865 DoChildrenIO(); | |
866 } | 1026 } |
867 | 1027 |
868 } // namespace disk_cache | 1028 } // namespace disk_cache |
OLD | NEW |