| 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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 } | 73 } |
| 74 | 74 |
| 75 // ------------------------------------------------------------------------ | 75 // ------------------------------------------------------------------------ |
| 76 | 76 |
| 77 bool MemEntryImpl::CreateEntry(const std::string& key, net::NetLog* net_log) { | 77 bool MemEntryImpl::CreateEntry(const std::string& key, net::NetLog* net_log) { |
| 78 key_ = key; | 78 key_ = key; |
| 79 Time current = Time::Now(); | 79 Time current = Time::Now(); |
| 80 last_modified_ = current; | 80 last_modified_ = current; |
| 81 last_used_ = current; | 81 last_used_ = current; |
| 82 | 82 |
| 83 net_log_ = net::BoundNetLog::Make(net_log, | 83 net_log_ = |
| 84 net::NetLog::SOURCE_MEMORY_CACHE_ENTRY); | 84 net::BoundNetLog::Make(net_log, net::NetLog::SOURCE_MEMORY_CACHE_ENTRY); |
| 85 // Must be called after |key_| is set, so GetKey() works. | 85 // Must be called after |key_| is set, so GetKey() works. |
| 86 net_log_.BeginEvent( | 86 net_log_.BeginEvent(net::NetLog::TYPE_DISK_CACHE_MEM_ENTRY_IMPL, |
| 87 net::NetLog::TYPE_DISK_CACHE_MEM_ENTRY_IMPL, | 87 CreateNetLogEntryCreationCallback(this, true)); |
| 88 CreateNetLogEntryCreationCallback(this, true)); | |
| 89 | 88 |
| 90 Open(); | 89 Open(); |
| 91 backend_->ModifyStorageSize(0, static_cast<int32>(key.size())); | 90 backend_->ModifyStorageSize(0, static_cast<int32>(key.size())); |
| 92 return true; | 91 return true; |
| 93 } | 92 } |
| 94 | 93 |
| 95 void MemEntryImpl::InternalDoom() { | 94 void MemEntryImpl::InternalDoom() { |
| 96 net_log_.AddEvent(net::NetLog::TYPE_ENTRY_DOOM); | 95 net_log_.AddEvent(net::NetLog::TYPE_ENTRY_DOOM); |
| 97 doomed_ = true; | 96 doomed_ = true; |
| 98 if (!ref_count_) { | 97 if (!ref_count_) { |
| 99 if (type() == kParentEntry) { | 98 if (type() == kParentEntry) { |
| 100 // If this is a parent entry, we need to doom all the child entries. | 99 // If this is a parent entry, we need to doom all the child entries. |
| 101 if (children_.get()) { | 100 if (children_.get()) { |
| 102 EntryMap children; | 101 EntryMap children; |
| 103 children.swap(*children_); | 102 children.swap(*children_); |
| 104 for (EntryMap::iterator i = children.begin(); | 103 for (EntryMap::iterator i = children.begin(); i != children.end(); |
| 105 i != children.end(); ++i) { | 104 ++i) { |
| 106 // Since a pointer to this object is also saved in the map, avoid | 105 // Since a pointer to this object is also saved in the map, avoid |
| 107 // dooming it. | 106 // dooming it. |
| 108 if (i->second != this) | 107 if (i->second != this) |
| 109 i->second->Doom(); | 108 i->second->Doom(); |
| 110 } | 109 } |
| 111 DCHECK(children_->empty()); | 110 DCHECK(children_->empty()); |
| 112 } | 111 } |
| 113 } else { | 112 } else { |
| 114 // If this is a child entry, detach it from the parent. | 113 // If this is a child entry, detach it from the parent. |
| 115 parent_->DetachChild(child_id_); | 114 parent_->DetachChild(child_id_); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 Time MemEntryImpl::GetLastModified() const { | 175 Time MemEntryImpl::GetLastModified() const { |
| 177 return last_modified_; | 176 return last_modified_; |
| 178 } | 177 } |
| 179 | 178 |
| 180 int32 MemEntryImpl::GetDataSize(int index) const { | 179 int32 MemEntryImpl::GetDataSize(int index) const { |
| 181 if (index < 0 || index >= NUM_STREAMS) | 180 if (index < 0 || index >= NUM_STREAMS) |
| 182 return 0; | 181 return 0; |
| 183 return data_size_[index]; | 182 return data_size_[index]; |
| 184 } | 183 } |
| 185 | 184 |
| 186 int MemEntryImpl::ReadData(int index, int offset, IOBuffer* buf, int buf_len, | 185 int MemEntryImpl::ReadData(int index, |
| 186 int offset, |
| 187 IOBuffer* buf, |
| 188 int buf_len, |
| 187 const CompletionCallback& callback) { | 189 const CompletionCallback& callback) { |
| 188 if (net_log_.IsLogging()) { | 190 if (net_log_.IsLogging()) { |
| 189 net_log_.BeginEvent( | 191 net_log_.BeginEvent( |
| 190 net::NetLog::TYPE_ENTRY_READ_DATA, | 192 net::NetLog::TYPE_ENTRY_READ_DATA, |
| 191 CreateNetLogReadWriteDataCallback(index, offset, buf_len, false)); | 193 CreateNetLogReadWriteDataCallback(index, offset, buf_len, false)); |
| 192 } | 194 } |
| 193 | 195 |
| 194 int result = InternalReadData(index, offset, buf, buf_len); | 196 int result = InternalReadData(index, offset, buf, buf_len); |
| 195 | 197 |
| 196 if (net_log_.IsLogging()) { | 198 if (net_log_.IsLogging()) { |
| 197 net_log_.EndEvent( | 199 net_log_.EndEvent(net::NetLog::TYPE_ENTRY_READ_DATA, |
| 198 net::NetLog::TYPE_ENTRY_READ_DATA, | 200 CreateNetLogReadWriteCompleteCallback(result)); |
| 199 CreateNetLogReadWriteCompleteCallback(result)); | |
| 200 } | 201 } |
| 201 return result; | 202 return result; |
| 202 } | 203 } |
| 203 | 204 |
| 204 int MemEntryImpl::WriteData(int index, int offset, IOBuffer* buf, int buf_len, | 205 int MemEntryImpl::WriteData(int index, |
| 205 const CompletionCallback& callback, bool truncate) { | 206 int offset, |
| 207 IOBuffer* buf, |
| 208 int buf_len, |
| 209 const CompletionCallback& callback, |
| 210 bool truncate) { |
| 206 if (net_log_.IsLogging()) { | 211 if (net_log_.IsLogging()) { |
| 207 net_log_.BeginEvent( | 212 net_log_.BeginEvent( |
| 208 net::NetLog::TYPE_ENTRY_WRITE_DATA, | 213 net::NetLog::TYPE_ENTRY_WRITE_DATA, |
| 209 CreateNetLogReadWriteDataCallback(index, offset, buf_len, truncate)); | 214 CreateNetLogReadWriteDataCallback(index, offset, buf_len, truncate)); |
| 210 } | 215 } |
| 211 | 216 |
| 212 int result = InternalWriteData(index, offset, buf, buf_len, truncate); | 217 int result = InternalWriteData(index, offset, buf, buf_len, truncate); |
| 213 | 218 |
| 214 if (net_log_.IsLogging()) { | 219 if (net_log_.IsLogging()) { |
| 215 net_log_.EndEvent( | 220 net_log_.EndEvent(net::NetLog::TYPE_ENTRY_WRITE_DATA, |
| 216 net::NetLog::TYPE_ENTRY_WRITE_DATA, | 221 CreateNetLogReadWriteCompleteCallback(result)); |
| 217 CreateNetLogReadWriteCompleteCallback(result)); | |
| 218 } | 222 } |
| 219 return result; | 223 return result; |
| 220 } | 224 } |
| 221 | 225 |
| 222 int MemEntryImpl::ReadSparseData(int64 offset, IOBuffer* buf, int buf_len, | 226 int MemEntryImpl::ReadSparseData(int64 offset, |
| 227 IOBuffer* buf, |
| 228 int buf_len, |
| 223 const CompletionCallback& callback) { | 229 const CompletionCallback& callback) { |
| 224 if (net_log_.IsLogging()) { | 230 if (net_log_.IsLogging()) { |
| 225 net_log_.BeginEvent( | 231 net_log_.BeginEvent(net::NetLog::TYPE_SPARSE_READ, |
| 226 net::NetLog::TYPE_SPARSE_READ, | 232 CreateNetLogSparseOperationCallback(offset, buf_len)); |
| 227 CreateNetLogSparseOperationCallback(offset, buf_len)); | |
| 228 } | 233 } |
| 229 int result = InternalReadSparseData(offset, buf, buf_len); | 234 int result = InternalReadSparseData(offset, buf, buf_len); |
| 230 if (net_log_.IsLogging()) | 235 if (net_log_.IsLogging()) |
| 231 net_log_.EndEvent(net::NetLog::TYPE_SPARSE_READ); | 236 net_log_.EndEvent(net::NetLog::TYPE_SPARSE_READ); |
| 232 return result; | 237 return result; |
| 233 } | 238 } |
| 234 | 239 |
| 235 int MemEntryImpl::WriteSparseData(int64 offset, IOBuffer* buf, int buf_len, | 240 int MemEntryImpl::WriteSparseData(int64 offset, |
| 241 IOBuffer* buf, |
| 242 int buf_len, |
| 236 const CompletionCallback& callback) { | 243 const CompletionCallback& callback) { |
| 237 if (net_log_.IsLogging()) { | 244 if (net_log_.IsLogging()) { |
| 238 net_log_.BeginEvent( | 245 net_log_.BeginEvent(net::NetLog::TYPE_SPARSE_WRITE, |
| 239 net::NetLog::TYPE_SPARSE_WRITE, | 246 CreateNetLogSparseOperationCallback(offset, buf_len)); |
| 240 CreateNetLogSparseOperationCallback(offset, buf_len)); | |
| 241 } | 247 } |
| 242 int result = InternalWriteSparseData(offset, buf, buf_len); | 248 int result = InternalWriteSparseData(offset, buf, buf_len); |
| 243 if (net_log_.IsLogging()) | 249 if (net_log_.IsLogging()) |
| 244 net_log_.EndEvent(net::NetLog::TYPE_SPARSE_WRITE); | 250 net_log_.EndEvent(net::NetLog::TYPE_SPARSE_WRITE); |
| 245 return result; | 251 return result; |
| 246 } | 252 } |
| 247 | 253 |
| 248 int MemEntryImpl::GetAvailableRange(int64 offset, int len, int64* start, | 254 int MemEntryImpl::GetAvailableRange(int64 offset, |
| 255 int len, |
| 256 int64* start, |
| 249 const CompletionCallback& callback) { | 257 const CompletionCallback& callback) { |
| 250 if (net_log_.IsLogging()) { | 258 if (net_log_.IsLogging()) { |
| 251 net_log_.BeginEvent( | 259 net_log_.BeginEvent(net::NetLog::TYPE_SPARSE_GET_RANGE, |
| 252 net::NetLog::TYPE_SPARSE_GET_RANGE, | 260 CreateNetLogSparseOperationCallback(offset, len)); |
| 253 CreateNetLogSparseOperationCallback(offset, len)); | |
| 254 } | 261 } |
| 255 int result = GetAvailableRange(offset, len, start); | 262 int result = GetAvailableRange(offset, len, start); |
| 256 if (net_log_.IsLogging()) { | 263 if (net_log_.IsLogging()) { |
| 257 net_log_.EndEvent( | 264 net_log_.EndEvent( |
| 258 net::NetLog::TYPE_SPARSE_GET_RANGE, | 265 net::NetLog::TYPE_SPARSE_GET_RANGE, |
| 259 CreateNetLogGetAvailableRangeResultCallback(*start, result)); | 266 CreateNetLogGetAvailableRangeResultCallback(*start, result)); |
| 260 } | 267 } |
| 261 return result; | 268 return result; |
| 262 } | 269 } |
| 263 | 270 |
| 264 bool MemEntryImpl::CouldBeSparse() const { | 271 bool MemEntryImpl::CouldBeSparse() const { |
| 265 DCHECK_EQ(kParentEntry, type()); | 272 DCHECK_EQ(kParentEntry, type()); |
| 266 return (children_.get() != NULL); | 273 return (children_.get() != NULL); |
| 267 } | 274 } |
| 268 | 275 |
| 269 int MemEntryImpl::ReadyForSparseIO(const CompletionCallback& callback) { | 276 int MemEntryImpl::ReadyForSparseIO(const CompletionCallback& callback) { |
| 270 return net::OK; | 277 return net::OK; |
| 271 } | 278 } |
| 272 | 279 |
| 273 // ------------------------------------------------------------------------ | 280 // ------------------------------------------------------------------------ |
| 274 | 281 |
| 275 MemEntryImpl::~MemEntryImpl() { | 282 MemEntryImpl::~MemEntryImpl() { |
| 276 for (int i = 0; i < NUM_STREAMS; i++) | 283 for (int i = 0; i < NUM_STREAMS; i++) |
| 277 backend_->ModifyStorageSize(data_size_[i], 0); | 284 backend_->ModifyStorageSize(data_size_[i], 0); |
| 278 backend_->ModifyStorageSize(static_cast<int32>(key_.size()), 0); | 285 backend_->ModifyStorageSize(static_cast<int32>(key_.size()), 0); |
| 279 net_log_.EndEvent(net::NetLog::TYPE_DISK_CACHE_MEM_ENTRY_IMPL); | 286 net_log_.EndEvent(net::NetLog::TYPE_DISK_CACHE_MEM_ENTRY_IMPL); |
| 280 } | 287 } |
| 281 | 288 |
| 282 int MemEntryImpl::InternalReadData(int index, int offset, IOBuffer* buf, | 289 int MemEntryImpl::InternalReadData(int index, |
| 290 int offset, |
| 291 IOBuffer* buf, |
| 283 int buf_len) { | 292 int buf_len) { |
| 284 DCHECK(type() == kParentEntry || index == kSparseData); | 293 DCHECK(type() == kParentEntry || index == kSparseData); |
| 285 | 294 |
| 286 if (index < 0 || index >= NUM_STREAMS) | 295 if (index < 0 || index >= NUM_STREAMS) |
| 287 return net::ERR_INVALID_ARGUMENT; | 296 return net::ERR_INVALID_ARGUMENT; |
| 288 | 297 |
| 289 int entry_size = GetDataSize(index); | 298 int entry_size = GetDataSize(index); |
| 290 if (offset >= entry_size || offset < 0 || !buf_len) | 299 if (offset >= entry_size || offset < 0 || !buf_len) |
| 291 return 0; | 300 return 0; |
| 292 | 301 |
| 293 if (buf_len < 0) | 302 if (buf_len < 0) |
| 294 return net::ERR_INVALID_ARGUMENT; | 303 return net::ERR_INVALID_ARGUMENT; |
| 295 | 304 |
| 296 if (offset + buf_len > entry_size) | 305 if (offset + buf_len > entry_size) |
| 297 buf_len = entry_size - offset; | 306 buf_len = entry_size - offset; |
| 298 | 307 |
| 299 UpdateRank(false); | 308 UpdateRank(false); |
| 300 | 309 |
| 301 memcpy(buf->data(), &(data_[index])[offset], buf_len); | 310 memcpy(buf->data(), &(data_[index])[offset], buf_len); |
| 302 return buf_len; | 311 return buf_len; |
| 303 } | 312 } |
| 304 | 313 |
| 305 int MemEntryImpl::InternalWriteData(int index, int offset, IOBuffer* buf, | 314 int MemEntryImpl::InternalWriteData(int index, |
| 306 int buf_len, bool truncate) { | 315 int offset, |
| 316 IOBuffer* buf, |
| 317 int buf_len, |
| 318 bool truncate) { |
| 307 DCHECK(type() == kParentEntry || index == kSparseData); | 319 DCHECK(type() == kParentEntry || index == kSparseData); |
| 308 | 320 |
| 309 if (index < 0 || index >= NUM_STREAMS) | 321 if (index < 0 || index >= NUM_STREAMS) |
| 310 return net::ERR_INVALID_ARGUMENT; | 322 return net::ERR_INVALID_ARGUMENT; |
| 311 | 323 |
| 312 if (offset < 0 || buf_len < 0) | 324 if (offset < 0 || buf_len < 0) |
| 313 return net::ERR_INVALID_ARGUMENT; | 325 return net::ERR_INVALID_ARGUMENT; |
| 314 | 326 |
| 315 int max_file_size = backend_->MaxFileSize(); | 327 int max_file_size = backend_->MaxFileSize(); |
| 316 | 328 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 337 | 349 |
| 338 UpdateRank(true); | 350 UpdateRank(true); |
| 339 | 351 |
| 340 if (!buf_len) | 352 if (!buf_len) |
| 341 return 0; | 353 return 0; |
| 342 | 354 |
| 343 memcpy(&(data_[index])[offset], buf->data(), buf_len); | 355 memcpy(&(data_[index])[offset], buf->data(), buf_len); |
| 344 return buf_len; | 356 return buf_len; |
| 345 } | 357 } |
| 346 | 358 |
| 347 int MemEntryImpl::InternalReadSparseData(int64 offset, IOBuffer* buf, | 359 int MemEntryImpl::InternalReadSparseData(int64 offset, |
| 360 IOBuffer* buf, |
| 348 int buf_len) { | 361 int buf_len) { |
| 349 DCHECK(type() == kParentEntry); | 362 DCHECK(type() == kParentEntry); |
| 350 | 363 |
| 351 if (!InitSparseInfo()) | 364 if (!InitSparseInfo()) |
| 352 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 365 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
| 353 | 366 |
| 354 if (offset < 0 || buf_len < 0) | 367 if (offset < 0 || buf_len < 0) |
| 355 return net::ERR_INVALID_ARGUMENT; | 368 return net::ERR_INVALID_ARGUMENT; |
| 356 | 369 |
| 357 // We will keep using this buffer and adjust the offset in this buffer. | 370 // We will keep using this buffer and adjust the offset in this buffer. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 372 // If we are trying to read from a position that the child entry has no data | 385 // If we are trying to read from a position that the child entry has no data |
| 373 // we should stop. | 386 // we should stop. |
| 374 if (child_offset < child->child_first_pos_) | 387 if (child_offset < child->child_first_pos_) |
| 375 break; | 388 break; |
| 376 if (net_log_.IsLogging()) { | 389 if (net_log_.IsLogging()) { |
| 377 net_log_.BeginEvent( | 390 net_log_.BeginEvent( |
| 378 net::NetLog::TYPE_SPARSE_READ_CHILD_DATA, | 391 net::NetLog::TYPE_SPARSE_READ_CHILD_DATA, |
| 379 CreateNetLogSparseReadWriteCallback(child->net_log().source(), | 392 CreateNetLogSparseReadWriteCallback(child->net_log().source(), |
| 380 io_buf->BytesRemaining())); | 393 io_buf->BytesRemaining())); |
| 381 } | 394 } |
| 382 int ret = child->ReadData(kSparseData, child_offset, io_buf.get(), | 395 int ret = child->ReadData(kSparseData, |
| 383 io_buf->BytesRemaining(), CompletionCallback()); | 396 child_offset, |
| 397 io_buf.get(), |
| 398 io_buf->BytesRemaining(), |
| 399 CompletionCallback()); |
| 384 if (net_log_.IsLogging()) { | 400 if (net_log_.IsLogging()) { |
| 385 net_log_.EndEventWithNetErrorCode( | 401 net_log_.EndEventWithNetErrorCode( |
| 386 net::NetLog::TYPE_SPARSE_READ_CHILD_DATA, ret); | 402 net::NetLog::TYPE_SPARSE_READ_CHILD_DATA, ret); |
| 387 } | 403 } |
| 388 | 404 |
| 389 // If we encounter an error in one entry, return immediately. | 405 // If we encounter an error in one entry, return immediately. |
| 390 if (ret < 0) | 406 if (ret < 0) |
| 391 return ret; | 407 return ret; |
| 392 else if (ret == 0) | 408 else if (ret == 0) |
| 393 break; | 409 break; |
| 394 | 410 |
| 395 // Increment the counter by number of bytes read in the child entry. | 411 // Increment the counter by number of bytes read in the child entry. |
| 396 io_buf->DidConsume(ret); | 412 io_buf->DidConsume(ret); |
| 397 } | 413 } |
| 398 | 414 |
| 399 UpdateRank(false); | 415 UpdateRank(false); |
| 400 | 416 |
| 401 return io_buf->BytesConsumed(); | 417 return io_buf->BytesConsumed(); |
| 402 } | 418 } |
| 403 | 419 |
| 404 int MemEntryImpl::InternalWriteSparseData(int64 offset, IOBuffer* buf, | 420 int MemEntryImpl::InternalWriteSparseData(int64 offset, |
| 421 IOBuffer* buf, |
| 405 int buf_len) { | 422 int buf_len) { |
| 406 DCHECK(type() == kParentEntry); | 423 DCHECK(type() == kParentEntry); |
| 407 | 424 |
| 408 if (!InitSparseInfo()) | 425 if (!InitSparseInfo()) |
| 409 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 426 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
| 410 | 427 |
| 411 if (offset < 0 || buf_len < 0) | 428 if (offset < 0 || buf_len < 0) |
| 412 return net::ERR_INVALID_ARGUMENT; | 429 return net::ERR_INVALID_ARGUMENT; |
| 413 | 430 |
| 414 scoped_refptr<net::DrainableIOBuffer> io_buf( | 431 scoped_refptr<net::DrainableIOBuffer> io_buf( |
| 415 new net::DrainableIOBuffer(buf, buf_len)); | 432 new net::DrainableIOBuffer(buf, buf_len)); |
| 416 | 433 |
| 417 // This loop walks through child entries continuously starting from |offset| | 434 // This loop walks through child entries continuously starting from |offset| |
| 418 // and writes blocks of data (of maximum size kMaxSparseEntrySize) into each | 435 // and writes blocks of data (of maximum size kMaxSparseEntrySize) into each |
| 419 // child entry until all |buf_len| bytes are written. The write operation can | 436 // child entry until all |buf_len| bytes are written. The write operation can |
| 420 // start in the middle of an entry. | 437 // start in the middle of an entry. |
| 421 while (io_buf->BytesRemaining()) { | 438 while (io_buf->BytesRemaining()) { |
| 422 MemEntryImpl* child = OpenChild(offset + io_buf->BytesConsumed(), true); | 439 MemEntryImpl* child = OpenChild(offset + io_buf->BytesConsumed(), true); |
| 423 int child_offset = ToChildOffset(offset + io_buf->BytesConsumed()); | 440 int child_offset = ToChildOffset(offset + io_buf->BytesConsumed()); |
| 424 | 441 |
| 425 // Find the right amount to write, this evaluates the remaining bytes to | 442 // Find the right amount to write, this evaluates the remaining bytes to |
| 426 // write and remaining capacity of this child entry. | 443 // write and remaining capacity of this child entry. |
| 427 int write_len = std::min(static_cast<int>(io_buf->BytesRemaining()), | 444 int write_len = std::min(static_cast<int>(io_buf->BytesRemaining()), |
| 428 kMaxSparseEntrySize - child_offset); | 445 kMaxSparseEntrySize - child_offset); |
| 429 | 446 |
| 430 // Keep a record of the last byte position (exclusive) in the child. | 447 // Keep a record of the last byte position (exclusive) in the child. |
| 431 int data_size = child->GetDataSize(kSparseData); | 448 int data_size = child->GetDataSize(kSparseData); |
| 432 | 449 |
| 433 if (net_log_.IsLogging()) { | 450 if (net_log_.IsLogging()) { |
| 434 net_log_.BeginEvent( | 451 net_log_.BeginEvent(net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA, |
| 435 net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA, | 452 CreateNetLogSparseReadWriteCallback( |
| 436 CreateNetLogSparseReadWriteCallback(child->net_log().source(), | 453 child->net_log().source(), write_len)); |
| 437 write_len)); | |
| 438 } | 454 } |
| 439 | 455 |
| 440 // Always writes to the child entry. This operation may overwrite data | 456 // Always writes to the child entry. This operation may overwrite data |
| 441 // previously written. | 457 // previously written. |
| 442 // TODO(hclam): if there is data in the entry and this write is not | 458 // TODO(hclam): if there is data in the entry and this write is not |
| 443 // continuous we may want to discard this write. | 459 // continuous we may want to discard this write. |
| 444 int ret = child->WriteData(kSparseData, child_offset, io_buf.get(), | 460 int ret = child->WriteData(kSparseData, |
| 445 write_len, CompletionCallback(), true); | 461 child_offset, |
| 462 io_buf.get(), |
| 463 write_len, |
| 464 CompletionCallback(), |
| 465 true); |
| 446 if (net_log_.IsLogging()) { | 466 if (net_log_.IsLogging()) { |
| 447 net_log_.EndEventWithNetErrorCode( | 467 net_log_.EndEventWithNetErrorCode( |
| 448 net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA, ret); | 468 net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA, ret); |
| 449 } | 469 } |
| 450 if (ret < 0) | 470 if (ret < 0) |
| 451 return ret; | 471 return ret; |
| 452 else if (ret == 0) | 472 else if (ret == 0) |
| 453 break; | 473 break; |
| 454 | 474 |
| 455 // Keep a record of the first byte position in the child if the write was | 475 // Keep a record of the first byte position in the child if the write was |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 549 return false; | 569 return false; |
| 550 children_.reset(new EntryMap()); | 570 children_.reset(new EntryMap()); |
| 551 | 571 |
| 552 // The parent entry stores data for the first block, so save this object to | 572 // The parent entry stores data for the first block, so save this object to |
| 553 // index 0. | 573 // index 0. |
| 554 (*children_)[0] = this; | 574 (*children_)[0] = this; |
| 555 } | 575 } |
| 556 return true; | 576 return true; |
| 557 } | 577 } |
| 558 | 578 |
| 559 bool MemEntryImpl::InitChildEntry(MemEntryImpl* parent, int child_id, | 579 bool MemEntryImpl::InitChildEntry(MemEntryImpl* parent, |
| 580 int child_id, |
| 560 net::NetLog* net_log) { | 581 net::NetLog* net_log) { |
| 561 DCHECK(!parent_); | 582 DCHECK(!parent_); |
| 562 DCHECK(!child_id_); | 583 DCHECK(!child_id_); |
| 563 | 584 |
| 564 net_log_ = net::BoundNetLog::Make(net_log, | 585 net_log_ = |
| 565 net::NetLog::SOURCE_MEMORY_CACHE_ENTRY); | 586 net::BoundNetLog::Make(net_log, net::NetLog::SOURCE_MEMORY_CACHE_ENTRY); |
| 566 net_log_.BeginEvent( | 587 net_log_.BeginEvent( |
| 567 net::NetLog::TYPE_DISK_CACHE_MEM_ENTRY_IMPL, | 588 net::NetLog::TYPE_DISK_CACHE_MEM_ENTRY_IMPL, |
| 568 base::Bind(&NetLogChildEntryCreationCallback, parent, child_id_)); | 589 base::Bind(&NetLogChildEntryCreationCallback, parent, child_id_)); |
| 569 | 590 |
| 570 parent_ = parent; | 591 parent_ = parent; |
| 571 child_id_ = child_id; | 592 child_id_ = child_id; |
| 572 Time current = Time::Now(); | 593 Time current = Time::Now(); |
| 573 last_modified_ = current; | 594 last_modified_ = current; |
| 574 last_used_ = current; | 595 last_used_ = current; |
| 575 // Insert this to the backend's ranking list. | 596 // Insert this to the backend's ranking list. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 600 // This loop tries to find the first existing child. | 621 // This loop tries to find the first existing child. |
| 601 while (scanned_len < len) { | 622 while (scanned_len < len) { |
| 602 // This points to the current offset in the child. | 623 // This points to the current offset in the child. |
| 603 int current_child_offset = ToChildOffset(offset + scanned_len); | 624 int current_child_offset = ToChildOffset(offset + scanned_len); |
| 604 MemEntryImpl* current_child = OpenChild(offset + scanned_len, false); | 625 MemEntryImpl* current_child = OpenChild(offset + scanned_len, false); |
| 605 if (current_child) { | 626 if (current_child) { |
| 606 int child_first_pos = current_child->child_first_pos_; | 627 int child_first_pos = current_child->child_first_pos_; |
| 607 | 628 |
| 608 // This points to the first byte that we should be reading from, we need | 629 // This points to the first byte that we should be reading from, we need |
| 609 // to take care of the filled region and the current offset in the child. | 630 // to take care of the filled region and the current offset in the child. |
| 610 int first_pos = std::max(current_child_offset, child_first_pos); | 631 int first_pos = std::max(current_child_offset, child_first_pos); |
| 611 | 632 |
| 612 // If the first byte position we should read from doesn't exceed the | 633 // If the first byte position we should read from doesn't exceed the |
| 613 // filled region, we have found the first child. | 634 // filled region, we have found the first child. |
| 614 if (first_pos < current_child->GetDataSize(kSparseData)) { | 635 if (first_pos < current_child->GetDataSize(kSparseData)) { |
| 615 *child = current_child; | 636 *child = current_child; |
| 616 | 637 |
| 617 // We need to advance the scanned length. | 638 // We need to advance the scanned length. |
| 618 scanned_len += first_pos - current_child_offset; | 639 scanned_len += first_pos - current_child_offset; |
| 619 break; | 640 break; |
| 620 } | 641 } |
| 621 } | 642 } |
| 622 scanned_len += kMaxSparseEntrySize - current_child_offset; | 643 scanned_len += kMaxSparseEntrySize - current_child_offset; |
| 623 } | 644 } |
| 624 return scanned_len; | 645 return scanned_len; |
| 625 } | 646 } |
| 626 | 647 |
| 627 void MemEntryImpl::DetachChild(int child_id) { | 648 void MemEntryImpl::DetachChild(int child_id) { |
| 628 children_->erase(child_id); | 649 children_->erase(child_id); |
| 629 } | 650 } |
| 630 | 651 |
| 631 } // namespace disk_cache | 652 } // namespace disk_cache |
| OLD | NEW |