| 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( |
| 178 net::NetLog::TYPE_ENTRY_READ_DATA, | 180 net::NetLogEventType::ENTRY_READ_DATA, |
| 179 CreateNetLogReadWriteCompleteCallback(result)); | 181 CreateNetLogReadWriteCompleteCallback(result)); |
| 180 } | 182 } |
| 181 return result; | 183 return result; |
| 182 } | 184 } |
| 183 | 185 |
| 184 int MemEntryImpl::WriteData(int index, int offset, IOBuffer* buf, int buf_len, | 186 int MemEntryImpl::WriteData(int index, int offset, IOBuffer* buf, int buf_len, |
| 185 const CompletionCallback& callback, bool truncate) { | 187 const CompletionCallback& callback, bool truncate) { |
| 186 if (net_log_.IsCapturing()) { | 188 if (net_log_.IsCapturing()) { |
| 187 net_log_.BeginEvent( | 189 net_log_.BeginEvent( |
| 188 net::NetLog::TYPE_ENTRY_WRITE_DATA, | 190 net::NetLogEventType::ENTRY_WRITE_DATA, |
| 189 CreateNetLogReadWriteDataCallback(index, offset, buf_len, truncate)); | 191 CreateNetLogReadWriteDataCallback(index, offset, buf_len, truncate)); |
| 190 } | 192 } |
| 191 | 193 |
| 192 int result = InternalWriteData(index, offset, buf, buf_len, truncate); | 194 int result = InternalWriteData(index, offset, buf, buf_len, truncate); |
| 193 | 195 |
| 194 if (net_log_.IsCapturing()) { | 196 if (net_log_.IsCapturing()) { |
| 195 net_log_.EndEvent( | 197 net_log_.EndEvent( |
| 196 net::NetLog::TYPE_ENTRY_WRITE_DATA, | 198 net::NetLogEventType::ENTRY_WRITE_DATA, |
| 197 CreateNetLogReadWriteCompleteCallback(result)); | 199 CreateNetLogReadWriteCompleteCallback(result)); |
| 198 } | 200 } |
| 199 return result; | 201 return result; |
| 200 } | 202 } |
| 201 | 203 |
| 202 int MemEntryImpl::ReadSparseData(int64_t offset, | 204 int MemEntryImpl::ReadSparseData(int64_t offset, |
| 203 IOBuffer* buf, | 205 IOBuffer* buf, |
| 204 int buf_len, | 206 int buf_len, |
| 205 const CompletionCallback& callback) { | 207 const CompletionCallback& callback) { |
| 206 if (net_log_.IsCapturing()) { | 208 if (net_log_.IsCapturing()) { |
| 207 net_log_.BeginEvent( | 209 net_log_.BeginEvent( |
| 208 net::NetLog::TYPE_SPARSE_READ, | 210 net::NetLogEventType::SPARSE_READ, |
| 209 CreateNetLogSparseOperationCallback(offset, buf_len)); | 211 CreateNetLogSparseOperationCallback(offset, buf_len)); |
| 210 } | 212 } |
| 211 int result = InternalReadSparseData(offset, buf, buf_len); | 213 int result = InternalReadSparseData(offset, buf, buf_len); |
| 212 if (net_log_.IsCapturing()) | 214 if (net_log_.IsCapturing()) |
| 213 net_log_.EndEvent(net::NetLog::TYPE_SPARSE_READ); | 215 net_log_.EndEvent(net::NetLogEventType::SPARSE_READ); |
| 214 return result; | 216 return result; |
| 215 } | 217 } |
| 216 | 218 |
| 217 int MemEntryImpl::WriteSparseData(int64_t offset, | 219 int MemEntryImpl::WriteSparseData(int64_t offset, |
| 218 IOBuffer* buf, | 220 IOBuffer* buf, |
| 219 int buf_len, | 221 int buf_len, |
| 220 const CompletionCallback& callback) { | 222 const CompletionCallback& callback) { |
| 221 if (net_log_.IsCapturing()) { | 223 if (net_log_.IsCapturing()) { |
| 222 net_log_.BeginEvent( | 224 net_log_.BeginEvent( |
| 223 net::NetLog::TYPE_SPARSE_WRITE, | 225 net::NetLogEventType::SPARSE_WRITE, |
| 224 CreateNetLogSparseOperationCallback(offset, buf_len)); | 226 CreateNetLogSparseOperationCallback(offset, buf_len)); |
| 225 } | 227 } |
| 226 int result = InternalWriteSparseData(offset, buf, buf_len); | 228 int result = InternalWriteSparseData(offset, buf, buf_len); |
| 227 if (net_log_.IsCapturing()) | 229 if (net_log_.IsCapturing()) |
| 228 net_log_.EndEvent(net::NetLog::TYPE_SPARSE_WRITE); | 230 net_log_.EndEvent(net::NetLogEventType::SPARSE_WRITE); |
| 229 return result; | 231 return result; |
| 230 } | 232 } |
| 231 | 233 |
| 232 int MemEntryImpl::GetAvailableRange(int64_t offset, | 234 int MemEntryImpl::GetAvailableRange(int64_t offset, |
| 233 int len, | 235 int len, |
| 234 int64_t* start, | 236 int64_t* start, |
| 235 const CompletionCallback& callback) { | 237 const CompletionCallback& callback) { |
| 236 if (net_log_.IsCapturing()) { | 238 if (net_log_.IsCapturing()) { |
| 237 net_log_.BeginEvent( | 239 net_log_.BeginEvent( |
| 238 net::NetLog::TYPE_SPARSE_GET_RANGE, | 240 net::NetLogEventType::SPARSE_GET_RANGE, |
| 239 CreateNetLogSparseOperationCallback(offset, len)); | 241 CreateNetLogSparseOperationCallback(offset, len)); |
| 240 } | 242 } |
| 241 int result = InternalGetAvailableRange(offset, len, start); | 243 int result = InternalGetAvailableRange(offset, len, start); |
| 242 if (net_log_.IsCapturing()) { | 244 if (net_log_.IsCapturing()) { |
| 243 net_log_.EndEvent( | 245 net_log_.EndEvent( |
| 244 net::NetLog::TYPE_SPARSE_GET_RANGE, | 246 net::NetLogEventType::SPARSE_GET_RANGE, |
| 245 CreateNetLogGetAvailableRangeResultCallback(*start, result)); | 247 CreateNetLogGetAvailableRangeResultCallback(*start, result)); |
| 246 } | 248 } |
| 247 return result; | 249 return result; |
| 248 } | 250 } |
| 249 | 251 |
| 250 bool MemEntryImpl::CouldBeSparse() const { | 252 bool MemEntryImpl::CouldBeSparse() const { |
| 251 DCHECK_EQ(PARENT_ENTRY, type()); | 253 DCHECK_EQ(PARENT_ENTRY, type()); |
| 252 return (children_.get() != nullptr); | 254 return (children_.get() != nullptr); |
| 253 } | 255 } |
| 254 | 256 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 267 ref_count_(0), | 269 ref_count_(0), |
| 268 child_id_(child_id), | 270 child_id_(child_id), |
| 269 child_first_pos_(0), | 271 child_first_pos_(0), |
| 270 parent_(parent), | 272 parent_(parent), |
| 271 last_modified_(Time::Now()), | 273 last_modified_(Time::Now()), |
| 272 last_used_(last_modified_), | 274 last_used_(last_modified_), |
| 273 backend_(backend), | 275 backend_(backend), |
| 274 doomed_(false) { | 276 doomed_(false) { |
| 275 backend_->OnEntryInserted(this); | 277 backend_->OnEntryInserted(this); |
| 276 net_log_ = | 278 net_log_ = |
| 277 net::BoundNetLog::Make(net_log, net::NetLog::SOURCE_MEMORY_CACHE_ENTRY); | 279 net::BoundNetLog::Make( |
| 278 net_log_.BeginEvent(net::NetLog::TYPE_DISK_CACHE_MEM_ENTRY_IMPL, | 280 net_log, net::NetLogSourceType::MEMORY_CACHE_ENTRY); |
| 281 net_log_.BeginEvent(net::NetLogEventType::DISK_CACHE_MEM_ENTRY_IMPL, |
| 279 base::Bind(&NetLogEntryCreationCallback, this)); | 282 base::Bind(&NetLogEntryCreationCallback, this)); |
| 280 } | 283 } |
| 281 | 284 |
| 282 MemEntryImpl::~MemEntryImpl() { | 285 MemEntryImpl::~MemEntryImpl() { |
| 283 backend_->ModifyStorageSize(-GetStorageSize()); | 286 backend_->ModifyStorageSize(-GetStorageSize()); |
| 284 | 287 |
| 285 if (type() == PARENT_ENTRY) { | 288 if (type() == PARENT_ENTRY) { |
| 286 if (children_) { | 289 if (children_) { |
| 287 EntryMap children; | 290 EntryMap children; |
| 288 children_->swap(children); | 291 children_->swap(children); |
| 289 | 292 |
| 290 for (auto& it : children) { | 293 for (auto& it : children) { |
| 291 // Since |this| is stored in the map, it should be guarded against | 294 // Since |this| is stored in the map, it should be guarded against |
| 292 // double dooming, which will result in double destruction. | 295 // double dooming, which will result in double destruction. |
| 293 if (it.second != this) | 296 if (it.second != this) |
| 294 it.second->Doom(); | 297 it.second->Doom(); |
| 295 } | 298 } |
| 296 } | 299 } |
| 297 } else { | 300 } else { |
| 298 parent_->children_->erase(child_id_); | 301 parent_->children_->erase(child_id_); |
| 299 } | 302 } |
| 300 net_log_.EndEvent(net::NetLog::TYPE_DISK_CACHE_MEM_ENTRY_IMPL); | 303 net_log_.EndEvent(net::NetLogEventType::DISK_CACHE_MEM_ENTRY_IMPL); |
| 301 } | 304 } |
| 302 | 305 |
| 303 int MemEntryImpl::InternalReadData(int index, int offset, IOBuffer* buf, | 306 int MemEntryImpl::InternalReadData(int index, int offset, IOBuffer* buf, |
| 304 int buf_len) { | 307 int buf_len) { |
| 305 DCHECK(type() == PARENT_ENTRY || index == kSparseData); | 308 DCHECK(type() == PARENT_ENTRY || index == kSparseData); |
| 306 | 309 |
| 307 if (index < 0 || index >= kNumStreams || buf_len < 0) | 310 if (index < 0 || index >= kNumStreams || buf_len < 0) |
| 308 return net::ERR_INVALID_ARGUMENT; | 311 return net::ERR_INVALID_ARGUMENT; |
| 309 | 312 |
| 310 int entry_size = data_[index].size(); | 313 int entry_size = data_[index].size(); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 | 388 |
| 386 // We then need to prepare the child offset and len. | 389 // We then need to prepare the child offset and len. |
| 387 int child_offset = ToChildOffset(offset + io_buf->BytesConsumed()); | 390 int child_offset = ToChildOffset(offset + io_buf->BytesConsumed()); |
| 388 | 391 |
| 389 // If we are trying to read from a position that the child entry has no data | 392 // If we are trying to read from a position that the child entry has no data |
| 390 // we should stop. | 393 // we should stop. |
| 391 if (child_offset < child->child_first_pos_) | 394 if (child_offset < child->child_first_pos_) |
| 392 break; | 395 break; |
| 393 if (net_log_.IsCapturing()) { | 396 if (net_log_.IsCapturing()) { |
| 394 net_log_.BeginEvent( | 397 net_log_.BeginEvent( |
| 395 net::NetLog::TYPE_SPARSE_READ_CHILD_DATA, | 398 net::NetLogEventType::SPARSE_READ_CHILD_DATA, |
| 396 CreateNetLogSparseReadWriteCallback(child->net_log_.source(), | 399 CreateNetLogSparseReadWriteCallback(child->net_log_.source(), |
| 397 io_buf->BytesRemaining())); | 400 io_buf->BytesRemaining())); |
| 398 } | 401 } |
| 399 int ret = child->ReadData(kSparseData, child_offset, io_buf.get(), | 402 int ret = child->ReadData(kSparseData, child_offset, io_buf.get(), |
| 400 io_buf->BytesRemaining(), CompletionCallback()); | 403 io_buf->BytesRemaining(), CompletionCallback()); |
| 401 if (net_log_.IsCapturing()) { | 404 if (net_log_.IsCapturing()) { |
| 402 net_log_.EndEventWithNetErrorCode( | 405 net_log_.EndEventWithNetErrorCode( |
| 403 net::NetLog::TYPE_SPARSE_READ_CHILD_DATA, ret); | 406 net::NetLogEventType::SPARSE_READ_CHILD_DATA, ret); |
| 404 } | 407 } |
| 405 | 408 |
| 406 // If we encounter an error in one entry, return immediately. | 409 // If we encounter an error in one entry, return immediately. |
| 407 if (ret < 0) | 410 if (ret < 0) |
| 408 return ret; | 411 return ret; |
| 409 else if (ret == 0) | 412 else if (ret == 0) |
| 410 break; | 413 break; |
| 411 | 414 |
| 412 // Increment the counter by number of bytes read in the child entry. | 415 // Increment the counter by number of bytes read in the child entry. |
| 413 io_buf->DidConsume(ret); | 416 io_buf->DidConsume(ret); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 441 | 444 |
| 442 // Find the right amount to write, this evaluates the remaining bytes to | 445 // Find the right amount to write, this evaluates the remaining bytes to |
| 443 // write and remaining capacity of this child entry. | 446 // write and remaining capacity of this child entry. |
| 444 int write_len = std::min(static_cast<int>(io_buf->BytesRemaining()), | 447 int write_len = std::min(static_cast<int>(io_buf->BytesRemaining()), |
| 445 kMaxSparseEntrySize - child_offset); | 448 kMaxSparseEntrySize - child_offset); |
| 446 | 449 |
| 447 // Keep a record of the last byte position (exclusive) in the child. | 450 // Keep a record of the last byte position (exclusive) in the child. |
| 448 int data_size = child->GetDataSize(kSparseData); | 451 int data_size = child->GetDataSize(kSparseData); |
| 449 | 452 |
| 450 if (net_log_.IsCapturing()) { | 453 if (net_log_.IsCapturing()) { |
| 451 net_log_.BeginEvent(net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA, | 454 net_log_.BeginEvent(net::NetLogEventType::SPARSE_WRITE_CHILD_DATA, |
| 452 CreateNetLogSparseReadWriteCallback( | 455 CreateNetLogSparseReadWriteCallback( |
| 453 child->net_log_.source(), write_len)); | 456 child->net_log_.source(), write_len)); |
| 454 } | 457 } |
| 455 | 458 |
| 456 // Always writes to the child entry. This operation may overwrite data | 459 // Always writes to the child entry. This operation may overwrite data |
| 457 // previously written. | 460 // previously written. |
| 458 // TODO(hclam): if there is data in the entry and this write is not | 461 // TODO(hclam): if there is data in the entry and this write is not |
| 459 // continuous we may want to discard this write. | 462 // continuous we may want to discard this write. |
| 460 int ret = child->WriteData(kSparseData, child_offset, io_buf.get(), | 463 int ret = child->WriteData(kSparseData, child_offset, io_buf.get(), |
| 461 write_len, CompletionCallback(), true); | 464 write_len, CompletionCallback(), true); |
| 462 if (net_log_.IsCapturing()) { | 465 if (net_log_.IsCapturing()) { |
| 463 net_log_.EndEventWithNetErrorCode( | 466 net_log_.EndEventWithNetErrorCode( |
| 464 net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA, ret); | 467 net::NetLogEventType::SPARSE_WRITE_CHILD_DATA, ret); |
| 465 } | 468 } |
| 466 if (ret < 0) | 469 if (ret < 0) |
| 467 return ret; | 470 return ret; |
| 468 else if (ret == 0) | 471 else if (ret == 0) |
| 469 break; | 472 break; |
| 470 | 473 |
| 471 // Keep a record of the first byte position in the child if the write was | 474 // 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 | 475 // 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. | 476 // of an entry and still keep track of data off the aligned edge. |
| 474 if (data_size != child_offset) | 477 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; | 585 scanned_len += first_pos - current_child_offset; |
| 583 break; | 586 break; |
| 584 } | 587 } |
| 585 } | 588 } |
| 586 scanned_len += kMaxSparseEntrySize - current_child_offset; | 589 scanned_len += kMaxSparseEntrySize - current_child_offset; |
| 587 } | 590 } |
| 588 return scanned_len; | 591 return scanned_len; |
| 589 } | 592 } |
| 590 | 593 |
| 591 } // namespace disk_cache | 594 } // namespace disk_cache |
| OLD | NEW |