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/memory/mem_entry_impl.h" | 5 #include "net/disk_cache/memory/mem_entry_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
15 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
16 #include "net/disk_cache/memory/mem_backend_impl.h" | 16 #include "net/disk_cache/memory/mem_backend_impl.h" |
17 #include "net/disk_cache/net_log_parameters.h" | 17 #include "net/disk_cache/net_log_parameters.h" |
| 18 #include "net/log/net_log_event_type.h" |
| 19 #include "net/log/net_log_source_type.h" |
18 | 20 |
19 using base::Time; | 21 using base::Time; |
20 | 22 |
21 namespace disk_cache { | 23 namespace disk_cache { |
22 | 24 |
23 namespace { | 25 namespace { |
24 | 26 |
25 const int kSparseData = 1; | 27 const int kSparseData = 1; |
26 | 28 |
27 // Maximum size of a sparse entry is 2 to the power of this number. | 29 // Maximum size of a sparse entry is 2 to the power of this number. |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 | 124 |
123 last_used_ = Time::Now(); | 125 last_used_ = Time::Now(); |
124 if (modified_enum == ENTRY_WAS_MODIFIED) | 126 if (modified_enum == ENTRY_WAS_MODIFIED) |
125 last_modified_ = last_used_; | 127 last_modified_ = last_used_; |
126 } | 128 } |
127 | 129 |
128 void MemEntryImpl::Doom() { | 130 void MemEntryImpl::Doom() { |
129 if (!doomed_) { | 131 if (!doomed_) { |
130 doomed_ = true; | 132 doomed_ = true; |
131 backend_->OnEntryDoomed(this); | 133 backend_->OnEntryDoomed(this); |
132 net_log_.AddEvent(net::NetLog::TYPE_ENTRY_DOOM); | 134 net_log_.AddEvent(net::NetLogEventType::ENTRY_DOOM); |
133 } | 135 } |
134 if (!ref_count_) | 136 if (!ref_count_) |
135 delete this; | 137 delete this; |
136 } | 138 } |
137 | 139 |
138 void MemEntryImpl::Close() { | 140 void MemEntryImpl::Close() { |
139 DCHECK_EQ(PARENT_ENTRY, type()); | 141 DCHECK_EQ(PARENT_ENTRY, type()); |
140 --ref_count_; | 142 --ref_count_; |
141 DCHECK_GE(ref_count_, 0); | 143 DCHECK_GE(ref_count_, 0); |
142 if (!ref_count_ && doomed_) | 144 if (!ref_count_ && doomed_) |
(...skipping 17 matching lines...) Expand all Loading... |
160 int32_t MemEntryImpl::GetDataSize(int index) const { | 162 int32_t MemEntryImpl::GetDataSize(int index) const { |
161 if (index < 0 || index >= kNumStreams) | 163 if (index < 0 || index >= kNumStreams) |
162 return 0; | 164 return 0; |
163 return data_[index].size(); | 165 return data_[index].size(); |
164 } | 166 } |
165 | 167 |
166 int MemEntryImpl::ReadData(int index, int offset, IOBuffer* buf, int buf_len, | 168 int MemEntryImpl::ReadData(int index, int offset, IOBuffer* buf, int buf_len, |
167 const CompletionCallback& callback) { | 169 const CompletionCallback& callback) { |
168 if (net_log_.IsCapturing()) { | 170 if (net_log_.IsCapturing()) { |
169 net_log_.BeginEvent( | 171 net_log_.BeginEvent( |
170 net::NetLog::TYPE_ENTRY_READ_DATA, | 172 net::NetLogEventType::ENTRY_READ_DATA, |
171 CreateNetLogReadWriteDataCallback(index, offset, buf_len, false)); | 173 CreateNetLogReadWriteDataCallback(index, offset, buf_len, false)); |
172 } | 174 } |
173 | 175 |
174 int result = InternalReadData(index, offset, buf, buf_len); | 176 int result = InternalReadData(index, offset, buf, buf_len); |
175 | 177 |
176 if (net_log_.IsCapturing()) { | 178 if (net_log_.IsCapturing()) { |
177 net_log_.EndEvent( | 179 net_log_.EndEvent(net::NetLogEventType::ENTRY_READ_DATA, |
178 net::NetLog::TYPE_ENTRY_READ_DATA, | 180 CreateNetLogReadWriteCompleteCallback(result)); |
179 CreateNetLogReadWriteCompleteCallback(result)); | |
180 } | 181 } |
181 return result; | 182 return result; |
182 } | 183 } |
183 | 184 |
184 int MemEntryImpl::WriteData(int index, int offset, IOBuffer* buf, int buf_len, | 185 int MemEntryImpl::WriteData(int index, int offset, IOBuffer* buf, int buf_len, |
185 const CompletionCallback& callback, bool truncate) { | 186 const CompletionCallback& callback, bool truncate) { |
186 if (net_log_.IsCapturing()) { | 187 if (net_log_.IsCapturing()) { |
187 net_log_.BeginEvent( | 188 net_log_.BeginEvent( |
188 net::NetLog::TYPE_ENTRY_WRITE_DATA, | 189 net::NetLogEventType::ENTRY_WRITE_DATA, |
189 CreateNetLogReadWriteDataCallback(index, offset, buf_len, truncate)); | 190 CreateNetLogReadWriteDataCallback(index, offset, buf_len, truncate)); |
190 } | 191 } |
191 | 192 |
192 int result = InternalWriteData(index, offset, buf, buf_len, truncate); | 193 int result = InternalWriteData(index, offset, buf, buf_len, truncate); |
193 | 194 |
194 if (net_log_.IsCapturing()) { | 195 if (net_log_.IsCapturing()) { |
195 net_log_.EndEvent( | 196 net_log_.EndEvent(net::NetLogEventType::ENTRY_WRITE_DATA, |
196 net::NetLog::TYPE_ENTRY_WRITE_DATA, | 197 CreateNetLogReadWriteCompleteCallback(result)); |
197 CreateNetLogReadWriteCompleteCallback(result)); | |
198 } | 198 } |
199 return result; | 199 return result; |
200 } | 200 } |
201 | 201 |
202 int MemEntryImpl::ReadSparseData(int64_t offset, | 202 int MemEntryImpl::ReadSparseData(int64_t offset, |
203 IOBuffer* buf, | 203 IOBuffer* buf, |
204 int buf_len, | 204 int buf_len, |
205 const CompletionCallback& callback) { | 205 const CompletionCallback& callback) { |
206 if (net_log_.IsCapturing()) { | 206 if (net_log_.IsCapturing()) { |
207 net_log_.BeginEvent( | 207 net_log_.BeginEvent(net::NetLogEventType::SPARSE_READ, |
208 net::NetLog::TYPE_SPARSE_READ, | 208 CreateNetLogSparseOperationCallback(offset, buf_len)); |
209 CreateNetLogSparseOperationCallback(offset, buf_len)); | |
210 } | 209 } |
211 int result = InternalReadSparseData(offset, buf, buf_len); | 210 int result = InternalReadSparseData(offset, buf, buf_len); |
212 if (net_log_.IsCapturing()) | 211 if (net_log_.IsCapturing()) |
213 net_log_.EndEvent(net::NetLog::TYPE_SPARSE_READ); | 212 net_log_.EndEvent(net::NetLogEventType::SPARSE_READ); |
214 return result; | 213 return result; |
215 } | 214 } |
216 | 215 |
217 int MemEntryImpl::WriteSparseData(int64_t offset, | 216 int MemEntryImpl::WriteSparseData(int64_t offset, |
218 IOBuffer* buf, | 217 IOBuffer* buf, |
219 int buf_len, | 218 int buf_len, |
220 const CompletionCallback& callback) { | 219 const CompletionCallback& callback) { |
221 if (net_log_.IsCapturing()) { | 220 if (net_log_.IsCapturing()) { |
222 net_log_.BeginEvent( | 221 net_log_.BeginEvent(net::NetLogEventType::SPARSE_WRITE, |
223 net::NetLog::TYPE_SPARSE_WRITE, | 222 CreateNetLogSparseOperationCallback(offset, buf_len)); |
224 CreateNetLogSparseOperationCallback(offset, buf_len)); | |
225 } | 223 } |
226 int result = InternalWriteSparseData(offset, buf, buf_len); | 224 int result = InternalWriteSparseData(offset, buf, buf_len); |
227 if (net_log_.IsCapturing()) | 225 if (net_log_.IsCapturing()) |
228 net_log_.EndEvent(net::NetLog::TYPE_SPARSE_WRITE); | 226 net_log_.EndEvent(net::NetLogEventType::SPARSE_WRITE); |
229 return result; | 227 return result; |
230 } | 228 } |
231 | 229 |
232 int MemEntryImpl::GetAvailableRange(int64_t offset, | 230 int MemEntryImpl::GetAvailableRange(int64_t offset, |
233 int len, | 231 int len, |
234 int64_t* start, | 232 int64_t* start, |
235 const CompletionCallback& callback) { | 233 const CompletionCallback& callback) { |
236 if (net_log_.IsCapturing()) { | 234 if (net_log_.IsCapturing()) { |
237 net_log_.BeginEvent( | 235 net_log_.BeginEvent(net::NetLogEventType::SPARSE_GET_RANGE, |
238 net::NetLog::TYPE_SPARSE_GET_RANGE, | 236 CreateNetLogSparseOperationCallback(offset, len)); |
239 CreateNetLogSparseOperationCallback(offset, len)); | |
240 } | 237 } |
241 int result = InternalGetAvailableRange(offset, len, start); | 238 int result = InternalGetAvailableRange(offset, len, start); |
242 if (net_log_.IsCapturing()) { | 239 if (net_log_.IsCapturing()) { |
243 net_log_.EndEvent( | 240 net_log_.EndEvent( |
244 net::NetLog::TYPE_SPARSE_GET_RANGE, | 241 net::NetLogEventType::SPARSE_GET_RANGE, |
245 CreateNetLogGetAvailableRangeResultCallback(*start, result)); | 242 CreateNetLogGetAvailableRangeResultCallback(*start, result)); |
246 } | 243 } |
247 return result; | 244 return result; |
248 } | 245 } |
249 | 246 |
250 bool MemEntryImpl::CouldBeSparse() const { | 247 bool MemEntryImpl::CouldBeSparse() const { |
251 DCHECK_EQ(PARENT_ENTRY, type()); | 248 DCHECK_EQ(PARENT_ENTRY, type()); |
252 return (children_.get() != nullptr); | 249 return (children_.get() != nullptr); |
253 } | 250 } |
254 | 251 |
(...skipping 11 matching lines...) Expand all Loading... |
266 : key_(key), | 263 : key_(key), |
267 ref_count_(0), | 264 ref_count_(0), |
268 child_id_(child_id), | 265 child_id_(child_id), |
269 child_first_pos_(0), | 266 child_first_pos_(0), |
270 parent_(parent), | 267 parent_(parent), |
271 last_modified_(Time::Now()), | 268 last_modified_(Time::Now()), |
272 last_used_(last_modified_), | 269 last_used_(last_modified_), |
273 backend_(backend), | 270 backend_(backend), |
274 doomed_(false) { | 271 doomed_(false) { |
275 backend_->OnEntryInserted(this); | 272 backend_->OnEntryInserted(this); |
276 net_log_ = | 273 net_log_ = net::BoundNetLog::Make(net_log, |
277 net::BoundNetLog::Make(net_log, net::NetLog::SOURCE_MEMORY_CACHE_ENTRY); | 274 net::NetLogSourceType::MEMORY_CACHE_ENTRY); |
278 net_log_.BeginEvent(net::NetLog::TYPE_DISK_CACHE_MEM_ENTRY_IMPL, | 275 net_log_.BeginEvent(net::NetLogEventType::DISK_CACHE_MEM_ENTRY_IMPL, |
279 base::Bind(&NetLogEntryCreationCallback, this)); | 276 base::Bind(&NetLogEntryCreationCallback, this)); |
280 } | 277 } |
281 | 278 |
282 MemEntryImpl::~MemEntryImpl() { | 279 MemEntryImpl::~MemEntryImpl() { |
283 backend_->ModifyStorageSize(-GetStorageSize()); | 280 backend_->ModifyStorageSize(-GetStorageSize()); |
284 | 281 |
285 if (type() == PARENT_ENTRY) { | 282 if (type() == PARENT_ENTRY) { |
286 if (children_) { | 283 if (children_) { |
287 EntryMap children; | 284 EntryMap children; |
288 children_->swap(children); | 285 children_->swap(children); |
289 | 286 |
290 for (auto& it : children) { | 287 for (auto& it : children) { |
291 // Since |this| is stored in the map, it should be guarded against | 288 // Since |this| is stored in the map, it should be guarded against |
292 // double dooming, which will result in double destruction. | 289 // double dooming, which will result in double destruction. |
293 if (it.second != this) | 290 if (it.second != this) |
294 it.second->Doom(); | 291 it.second->Doom(); |
295 } | 292 } |
296 } | 293 } |
297 } else { | 294 } else { |
298 parent_->children_->erase(child_id_); | 295 parent_->children_->erase(child_id_); |
299 } | 296 } |
300 net_log_.EndEvent(net::NetLog::TYPE_DISK_CACHE_MEM_ENTRY_IMPL); | 297 net_log_.EndEvent(net::NetLogEventType::DISK_CACHE_MEM_ENTRY_IMPL); |
301 } | 298 } |
302 | 299 |
303 int MemEntryImpl::InternalReadData(int index, int offset, IOBuffer* buf, | 300 int MemEntryImpl::InternalReadData(int index, int offset, IOBuffer* buf, |
304 int buf_len) { | 301 int buf_len) { |
305 DCHECK(type() == PARENT_ENTRY || index == kSparseData); | 302 DCHECK(type() == PARENT_ENTRY || index == kSparseData); |
306 | 303 |
307 if (index < 0 || index >= kNumStreams || buf_len < 0) | 304 if (index < 0 || index >= kNumStreams || buf_len < 0) |
308 return net::ERR_INVALID_ARGUMENT; | 305 return net::ERR_INVALID_ARGUMENT; |
309 | 306 |
310 int entry_size = data_[index].size(); | 307 int entry_size = data_[index].size(); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 | 382 |
386 // We then need to prepare the child offset and len. | 383 // We then need to prepare the child offset and len. |
387 int child_offset = ToChildOffset(offset + io_buf->BytesConsumed()); | 384 int child_offset = ToChildOffset(offset + io_buf->BytesConsumed()); |
388 | 385 |
389 // If we are trying to read from a position that the child entry has no data | 386 // If we are trying to read from a position that the child entry has no data |
390 // we should stop. | 387 // we should stop. |
391 if (child_offset < child->child_first_pos_) | 388 if (child_offset < child->child_first_pos_) |
392 break; | 389 break; |
393 if (net_log_.IsCapturing()) { | 390 if (net_log_.IsCapturing()) { |
394 net_log_.BeginEvent( | 391 net_log_.BeginEvent( |
395 net::NetLog::TYPE_SPARSE_READ_CHILD_DATA, | 392 net::NetLogEventType::SPARSE_READ_CHILD_DATA, |
396 CreateNetLogSparseReadWriteCallback(child->net_log_.source(), | 393 CreateNetLogSparseReadWriteCallback(child->net_log_.source(), |
397 io_buf->BytesRemaining())); | 394 io_buf->BytesRemaining())); |
398 } | 395 } |
399 int ret = child->ReadData(kSparseData, child_offset, io_buf.get(), | 396 int ret = child->ReadData(kSparseData, child_offset, io_buf.get(), |
400 io_buf->BytesRemaining(), CompletionCallback()); | 397 io_buf->BytesRemaining(), CompletionCallback()); |
401 if (net_log_.IsCapturing()) { | 398 if (net_log_.IsCapturing()) { |
402 net_log_.EndEventWithNetErrorCode( | 399 net_log_.EndEventWithNetErrorCode( |
403 net::NetLog::TYPE_SPARSE_READ_CHILD_DATA, ret); | 400 net::NetLogEventType::SPARSE_READ_CHILD_DATA, ret); |
404 } | 401 } |
405 | 402 |
406 // If we encounter an error in one entry, return immediately. | 403 // If we encounter an error in one entry, return immediately. |
407 if (ret < 0) | 404 if (ret < 0) |
408 return ret; | 405 return ret; |
409 else if (ret == 0) | 406 else if (ret == 0) |
410 break; | 407 break; |
411 | 408 |
412 // Increment the counter by number of bytes read in the child entry. | 409 // Increment the counter by number of bytes read in the child entry. |
413 io_buf->DidConsume(ret); | 410 io_buf->DidConsume(ret); |
(...skipping 27 matching lines...) Expand all Loading... |
441 | 438 |
442 // Find the right amount to write, this evaluates the remaining bytes to | 439 // Find the right amount to write, this evaluates the remaining bytes to |
443 // write and remaining capacity of this child entry. | 440 // write and remaining capacity of this child entry. |
444 int write_len = std::min(static_cast<int>(io_buf->BytesRemaining()), | 441 int write_len = std::min(static_cast<int>(io_buf->BytesRemaining()), |
445 kMaxSparseEntrySize - child_offset); | 442 kMaxSparseEntrySize - child_offset); |
446 | 443 |
447 // Keep a record of the last byte position (exclusive) in the child. | 444 // Keep a record of the last byte position (exclusive) in the child. |
448 int data_size = child->GetDataSize(kSparseData); | 445 int data_size = child->GetDataSize(kSparseData); |
449 | 446 |
450 if (net_log_.IsCapturing()) { | 447 if (net_log_.IsCapturing()) { |
451 net_log_.BeginEvent(net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA, | 448 net_log_.BeginEvent(net::NetLogEventType::SPARSE_WRITE_CHILD_DATA, |
452 CreateNetLogSparseReadWriteCallback( | 449 CreateNetLogSparseReadWriteCallback( |
453 child->net_log_.source(), write_len)); | 450 child->net_log_.source(), write_len)); |
454 } | 451 } |
455 | 452 |
456 // Always writes to the child entry. This operation may overwrite data | 453 // Always writes to the child entry. This operation may overwrite data |
457 // previously written. | 454 // previously written. |
458 // TODO(hclam): if there is data in the entry and this write is not | 455 // TODO(hclam): if there is data in the entry and this write is not |
459 // continuous we may want to discard this write. | 456 // continuous we may want to discard this write. |
460 int ret = child->WriteData(kSparseData, child_offset, io_buf.get(), | 457 int ret = child->WriteData(kSparseData, child_offset, io_buf.get(), |
461 write_len, CompletionCallback(), true); | 458 write_len, CompletionCallback(), true); |
462 if (net_log_.IsCapturing()) { | 459 if (net_log_.IsCapturing()) { |
463 net_log_.EndEventWithNetErrorCode( | 460 net_log_.EndEventWithNetErrorCode( |
464 net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA, ret); | 461 net::NetLogEventType::SPARSE_WRITE_CHILD_DATA, ret); |
465 } | 462 } |
466 if (ret < 0) | 463 if (ret < 0) |
467 return ret; | 464 return ret; |
468 else if (ret == 0) | 465 else if (ret == 0) |
469 break; | 466 break; |
470 | 467 |
471 // Keep a record of the first byte position in the child if the write was | 468 // Keep a record of the first byte position in the child if the write was |
472 // not aligned nor continuous. This is to enable witting to the middle | 469 // not aligned nor continuous. This is to enable witting to the middle |
473 // of an entry and still keep track of data off the aligned edge. | 470 // of an entry and still keep track of data off the aligned edge. |
474 if (data_size != child_offset) | 471 if (data_size != child_offset) |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
582 scanned_len += first_pos - current_child_offset; | 579 scanned_len += first_pos - current_child_offset; |
583 break; | 580 break; |
584 } | 581 } |
585 } | 582 } |
586 scanned_len += kMaxSparseEntrySize - current_child_offset; | 583 scanned_len += kMaxSparseEntrySize - current_child_offset; |
587 } | 584 } |
588 return scanned_len; | 585 return scanned_len; |
589 } | 586 } |
590 | 587 |
591 } // namespace disk_cache | 588 } // namespace disk_cache |
OLD | NEW |