| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 } | 178 } |
| 179 | 179 |
| 180 int32 MemEntryImpl::GetDataSize(int index) const { | 180 int32 MemEntryImpl::GetDataSize(int index) const { |
| 181 if (index < 0 || index >= NUM_STREAMS) | 181 if (index < 0 || index >= NUM_STREAMS) |
| 182 return 0; | 182 return 0; |
| 183 return data_size_[index]; | 183 return data_size_[index]; |
| 184 } | 184 } |
| 185 | 185 |
| 186 int MemEntryImpl::ReadData(int index, int offset, IOBuffer* buf, int buf_len, | 186 int MemEntryImpl::ReadData(int index, int offset, IOBuffer* buf, int buf_len, |
| 187 const CompletionCallback& callback) { | 187 const CompletionCallback& callback) { |
| 188 if (net_log_.GetCaptureMode().enabled()) { | 188 if (net_log_.HasObservers()) { |
| 189 net_log_.BeginEvent( | 189 net_log_.BeginEvent( |
| 190 net::NetLog::TYPE_ENTRY_READ_DATA, | 190 net::NetLog::TYPE_ENTRY_READ_DATA, |
| 191 CreateNetLogReadWriteDataCallback(index, offset, buf_len, false)); | 191 CreateNetLogReadWriteDataCallback(index, offset, buf_len, false)); |
| 192 } | 192 } |
| 193 | 193 |
| 194 int result = InternalReadData(index, offset, buf, buf_len); | 194 int result = InternalReadData(index, offset, buf, buf_len); |
| 195 | 195 |
| 196 if (net_log_.GetCaptureMode().enabled()) { | 196 if (net_log_.HasObservers()) { |
| 197 net_log_.EndEvent( | 197 net_log_.EndEvent( |
| 198 net::NetLog::TYPE_ENTRY_READ_DATA, | 198 net::NetLog::TYPE_ENTRY_READ_DATA, |
| 199 CreateNetLogReadWriteCompleteCallback(result)); | 199 CreateNetLogReadWriteCompleteCallback(result)); |
| 200 } | 200 } |
| 201 return result; | 201 return result; |
| 202 } | 202 } |
| 203 | 203 |
| 204 int MemEntryImpl::WriteData(int index, int offset, IOBuffer* buf, int buf_len, | 204 int MemEntryImpl::WriteData(int index, int offset, IOBuffer* buf, int buf_len, |
| 205 const CompletionCallback& callback, bool truncate) { | 205 const CompletionCallback& callback, bool truncate) { |
| 206 if (net_log_.GetCaptureMode().enabled()) { | 206 if (net_log_.HasObservers()) { |
| 207 net_log_.BeginEvent( | 207 net_log_.BeginEvent( |
| 208 net::NetLog::TYPE_ENTRY_WRITE_DATA, | 208 net::NetLog::TYPE_ENTRY_WRITE_DATA, |
| 209 CreateNetLogReadWriteDataCallback(index, offset, buf_len, truncate)); | 209 CreateNetLogReadWriteDataCallback(index, offset, buf_len, truncate)); |
| 210 } | 210 } |
| 211 | 211 |
| 212 int result = InternalWriteData(index, offset, buf, buf_len, truncate); | 212 int result = InternalWriteData(index, offset, buf, buf_len, truncate); |
| 213 | 213 |
| 214 if (net_log_.GetCaptureMode().enabled()) { | 214 if (net_log_.HasObservers()) { |
| 215 net_log_.EndEvent( | 215 net_log_.EndEvent( |
| 216 net::NetLog::TYPE_ENTRY_WRITE_DATA, | 216 net::NetLog::TYPE_ENTRY_WRITE_DATA, |
| 217 CreateNetLogReadWriteCompleteCallback(result)); | 217 CreateNetLogReadWriteCompleteCallback(result)); |
| 218 } | 218 } |
| 219 return result; | 219 return result; |
| 220 } | 220 } |
| 221 | 221 |
| 222 int MemEntryImpl::ReadSparseData(int64 offset, IOBuffer* buf, int buf_len, | 222 int MemEntryImpl::ReadSparseData(int64 offset, IOBuffer* buf, int buf_len, |
| 223 const CompletionCallback& callback) { | 223 const CompletionCallback& callback) { |
| 224 if (net_log_.GetCaptureMode().enabled()) { | 224 if (net_log_.HasObservers()) { |
| 225 net_log_.BeginEvent( | 225 net_log_.BeginEvent( |
| 226 net::NetLog::TYPE_SPARSE_READ, | 226 net::NetLog::TYPE_SPARSE_READ, |
| 227 CreateNetLogSparseOperationCallback(offset, buf_len)); | 227 CreateNetLogSparseOperationCallback(offset, buf_len)); |
| 228 } | 228 } |
| 229 int result = InternalReadSparseData(offset, buf, buf_len); | 229 int result = InternalReadSparseData(offset, buf, buf_len); |
| 230 if (net_log_.GetCaptureMode().enabled()) | 230 if (net_log_.HasObservers()) |
| 231 net_log_.EndEvent(net::NetLog::TYPE_SPARSE_READ); | 231 net_log_.EndEvent(net::NetLog::TYPE_SPARSE_READ); |
| 232 return result; | 232 return result; |
| 233 } | 233 } |
| 234 | 234 |
| 235 int MemEntryImpl::WriteSparseData(int64 offset, IOBuffer* buf, int buf_len, | 235 int MemEntryImpl::WriteSparseData(int64 offset, IOBuffer* buf, int buf_len, |
| 236 const CompletionCallback& callback) { | 236 const CompletionCallback& callback) { |
| 237 if (net_log_.GetCaptureMode().enabled()) { | 237 if (net_log_.HasObservers()) { |
| 238 net_log_.BeginEvent( | 238 net_log_.BeginEvent( |
| 239 net::NetLog::TYPE_SPARSE_WRITE, | 239 net::NetLog::TYPE_SPARSE_WRITE, |
| 240 CreateNetLogSparseOperationCallback(offset, buf_len)); | 240 CreateNetLogSparseOperationCallback(offset, buf_len)); |
| 241 } | 241 } |
| 242 int result = InternalWriteSparseData(offset, buf, buf_len); | 242 int result = InternalWriteSparseData(offset, buf, buf_len); |
| 243 if (net_log_.GetCaptureMode().enabled()) | 243 if (net_log_.HasObservers()) |
| 244 net_log_.EndEvent(net::NetLog::TYPE_SPARSE_WRITE); | 244 net_log_.EndEvent(net::NetLog::TYPE_SPARSE_WRITE); |
| 245 return result; | 245 return result; |
| 246 } | 246 } |
| 247 | 247 |
| 248 int MemEntryImpl::GetAvailableRange(int64 offset, int len, int64* start, | 248 int MemEntryImpl::GetAvailableRange(int64 offset, int len, int64* start, |
| 249 const CompletionCallback& callback) { | 249 const CompletionCallback& callback) { |
| 250 if (net_log_.GetCaptureMode().enabled()) { | 250 if (net_log_.HasObservers()) { |
| 251 net_log_.BeginEvent( | 251 net_log_.BeginEvent( |
| 252 net::NetLog::TYPE_SPARSE_GET_RANGE, | 252 net::NetLog::TYPE_SPARSE_GET_RANGE, |
| 253 CreateNetLogSparseOperationCallback(offset, len)); | 253 CreateNetLogSparseOperationCallback(offset, len)); |
| 254 } | 254 } |
| 255 int result = GetAvailableRange(offset, len, start); | 255 int result = GetAvailableRange(offset, len, start); |
| 256 if (net_log_.GetCaptureMode().enabled()) { | 256 if (net_log_.HasObservers()) { |
| 257 net_log_.EndEvent( | 257 net_log_.EndEvent( |
| 258 net::NetLog::TYPE_SPARSE_GET_RANGE, | 258 net::NetLog::TYPE_SPARSE_GET_RANGE, |
| 259 CreateNetLogGetAvailableRangeResultCallback(*start, result)); | 259 CreateNetLogGetAvailableRangeResultCallback(*start, result)); |
| 260 } | 260 } |
| 261 return result; | 261 return result; |
| 262 } | 262 } |
| 263 | 263 |
| 264 bool MemEntryImpl::CouldBeSparse() const { | 264 bool MemEntryImpl::CouldBeSparse() const { |
| 265 DCHECK_EQ(kParentEntry, type()); | 265 DCHECK_EQ(kParentEntry, type()); |
| 266 return (children_.get() != NULL); | 266 return (children_.get() != NULL); |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 if (!child) | 366 if (!child) |
| 367 break; | 367 break; |
| 368 | 368 |
| 369 // We then need to prepare the child offset and len. | 369 // We then need to prepare the child offset and len. |
| 370 int child_offset = ToChildOffset(offset + io_buf->BytesConsumed()); | 370 int child_offset = ToChildOffset(offset + io_buf->BytesConsumed()); |
| 371 | 371 |
| 372 // If we are trying to read from a position that the child entry has no data | 372 // If we are trying to read from a position that the child entry has no data |
| 373 // we should stop. | 373 // we should stop. |
| 374 if (child_offset < child->child_first_pos_) | 374 if (child_offset < child->child_first_pos_) |
| 375 break; | 375 break; |
| 376 if (net_log_.GetCaptureMode().enabled()) { | 376 if (net_log_.HasObservers()) { |
| 377 net_log_.BeginEvent( | 377 net_log_.BeginEvent( |
| 378 net::NetLog::TYPE_SPARSE_READ_CHILD_DATA, | 378 net::NetLog::TYPE_SPARSE_READ_CHILD_DATA, |
| 379 CreateNetLogSparseReadWriteCallback(child->net_log().source(), | 379 CreateNetLogSparseReadWriteCallback(child->net_log().source(), |
| 380 io_buf->BytesRemaining())); | 380 io_buf->BytesRemaining())); |
| 381 } | 381 } |
| 382 int ret = child->ReadData(kSparseData, child_offset, io_buf.get(), | 382 int ret = child->ReadData(kSparseData, child_offset, io_buf.get(), |
| 383 io_buf->BytesRemaining(), CompletionCallback()); | 383 io_buf->BytesRemaining(), CompletionCallback()); |
| 384 if (net_log_.GetCaptureMode().enabled()) { | 384 if (net_log_.HasObservers()) { |
| 385 net_log_.EndEventWithNetErrorCode( | 385 net_log_.EndEventWithNetErrorCode( |
| 386 net::NetLog::TYPE_SPARSE_READ_CHILD_DATA, ret); | 386 net::NetLog::TYPE_SPARSE_READ_CHILD_DATA, ret); |
| 387 } | 387 } |
| 388 | 388 |
| 389 // If we encounter an error in one entry, return immediately. | 389 // If we encounter an error in one entry, return immediately. |
| 390 if (ret < 0) | 390 if (ret < 0) |
| 391 return ret; | 391 return ret; |
| 392 else if (ret == 0) | 392 else if (ret == 0) |
| 393 break; | 393 break; |
| 394 | 394 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 423 int child_offset = ToChildOffset(offset + io_buf->BytesConsumed()); | 423 int child_offset = ToChildOffset(offset + io_buf->BytesConsumed()); |
| 424 | 424 |
| 425 // Find the right amount to write, this evaluates the remaining bytes to | 425 // Find the right amount to write, this evaluates the remaining bytes to |
| 426 // write and remaining capacity of this child entry. | 426 // write and remaining capacity of this child entry. |
| 427 int write_len = std::min(static_cast<int>(io_buf->BytesRemaining()), | 427 int write_len = std::min(static_cast<int>(io_buf->BytesRemaining()), |
| 428 kMaxSparseEntrySize - child_offset); | 428 kMaxSparseEntrySize - child_offset); |
| 429 | 429 |
| 430 // Keep a record of the last byte position (exclusive) in the child. | 430 // Keep a record of the last byte position (exclusive) in the child. |
| 431 int data_size = child->GetDataSize(kSparseData); | 431 int data_size = child->GetDataSize(kSparseData); |
| 432 | 432 |
| 433 if (net_log_.GetCaptureMode().enabled()) { | 433 if (net_log_.HasObservers()) { |
| 434 net_log_.BeginEvent( | 434 net_log_.BeginEvent( |
| 435 net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA, | 435 net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA, |
| 436 CreateNetLogSparseReadWriteCallback(child->net_log().source(), | 436 CreateNetLogSparseReadWriteCallback(child->net_log().source(), |
| 437 write_len)); | 437 write_len)); |
| 438 } | 438 } |
| 439 | 439 |
| 440 // Always writes to the child entry. This operation may overwrite data | 440 // Always writes to the child entry. This operation may overwrite data |
| 441 // previously written. | 441 // previously written. |
| 442 // TODO(hclam): if there is data in the entry and this write is not | 442 // TODO(hclam): if there is data in the entry and this write is not |
| 443 // continuous we may want to discard this write. | 443 // continuous we may want to discard this write. |
| 444 int ret = child->WriteData(kSparseData, child_offset, io_buf.get(), | 444 int ret = child->WriteData(kSparseData, child_offset, io_buf.get(), |
| 445 write_len, CompletionCallback(), true); | 445 write_len, CompletionCallback(), true); |
| 446 if (net_log_.GetCaptureMode().enabled()) { | 446 if (net_log_.HasObservers()) { |
| 447 net_log_.EndEventWithNetErrorCode( | 447 net_log_.EndEventWithNetErrorCode( |
| 448 net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA, ret); | 448 net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA, ret); |
| 449 } | 449 } |
| 450 if (ret < 0) | 450 if (ret < 0) |
| 451 return ret; | 451 return ret; |
| 452 else if (ret == 0) | 452 else if (ret == 0) |
| 453 break; | 453 break; |
| 454 | 454 |
| 455 // Keep a record of the first byte position in the child if the write was | 455 // Keep a record of the first byte position in the child if the write was |
| 456 // not aligned nor continuous. This is to enable witting to the middle | 456 // not aligned nor continuous. This is to enable witting to the middle |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 622 scanned_len += kMaxSparseEntrySize - current_child_offset; | 622 scanned_len += kMaxSparseEntrySize - current_child_offset; |
| 623 } | 623 } |
| 624 return scanned_len; | 624 return scanned_len; |
| 625 } | 625 } |
| 626 | 626 |
| 627 void MemEntryImpl::DetachChild(int child_id) { | 627 void MemEntryImpl::DetachChild(int child_id) { |
| 628 children_->erase(child_id); | 628 children_->erase(child_id); |
| 629 } | 629 } |
| 630 | 630 |
| 631 } // namespace disk_cache | 631 } // namespace disk_cache |
| OLD | NEW |