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" |
11 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
13 #include "net/disk_cache/memory/mem_backend_impl.h" | 13 #include "net/disk_cache/memory/mem_backend_impl.h" |
14 #include "net/disk_cache/net_log_parameters.h" | 14 #include "net/disk_cache/net_log_parameters.h" |
15 | 15 |
16 using base::Time; | 16 using base::Time; |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 const int kSparseData = 1; | 20 const int kSparseData = 1; |
21 | 21 |
22 // Maximum size of a sparse entry is 2 to the power of this number. | 22 // Maximum size of a sparse entry is 2 to the power of this number. |
23 const int kMaxSparseEntryBits = 12; | 23 const int kMaxSparseEntryBits = 12; |
24 | 24 |
25 // Sparse entry has maximum size of 4KB. | 25 // Sparse entry has maximum size of 4KB. |
26 const int kMaxSparseEntrySize = 1 << kMaxSparseEntryBits; | 26 const int kMaxSparseEntrySize = 1 << kMaxSparseEntryBits; |
27 | 27 |
28 // Convert global offset to child index. | 28 // Convert global offset to child index. |
29 inline int ToChildIndex(int64 offset) { | 29 inline int ToChildIndex(int64_t offset) { |
30 return static_cast<int>(offset >> kMaxSparseEntryBits); | 30 return static_cast<int>(offset >> kMaxSparseEntryBits); |
31 } | 31 } |
32 | 32 |
33 // Convert global offset to offset in child entry. | 33 // Convert global offset to offset in child entry. |
34 inline int ToChildOffset(int64 offset) { | 34 inline int ToChildOffset(int64_t offset) { |
35 return static_cast<int>(offset & (kMaxSparseEntrySize - 1)); | 35 return static_cast<int>(offset & (kMaxSparseEntrySize - 1)); |
36 } | 36 } |
37 | 37 |
38 // Returns a name for a child entry given the base_name of the parent and the | 38 // Returns a name for a child entry given the base_name of the parent and the |
39 // child_id. This name is only used for logging purposes. | 39 // child_id. This name is only used for logging purposes. |
40 // If the entry is called entry_name, child entries will be named something | 40 // If the entry is called entry_name, child entries will be named something |
41 // like Range_entry_name:YYY where YYY is the number of the particular child. | 41 // like Range_entry_name:YYY where YYY is the number of the particular child. |
42 std::string GenerateChildName(const std::string& base_name, int child_id) { | 42 std::string GenerateChildName(const std::string& base_name, int child_id) { |
43 return base::StringPrintf("Range_%s:%i", base_name.c_str(), child_id); | 43 return base::StringPrintf("Range_%s:%i", base_name.c_str(), child_id); |
44 } | 44 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 last_used_ = current; | 81 last_used_ = current; |
82 | 82 |
83 net_log_ = net::BoundNetLog::Make(net_log, | 83 net_log_ = net::BoundNetLog::Make(net_log, |
84 net::NetLog::SOURCE_MEMORY_CACHE_ENTRY); | 84 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( |
87 net::NetLog::TYPE_DISK_CACHE_MEM_ENTRY_IMPL, | 87 net::NetLog::TYPE_DISK_CACHE_MEM_ENTRY_IMPL, |
88 CreateNetLogEntryCreationCallback(this, true)); | 88 CreateNetLogEntryCreationCallback(this, true)); |
89 | 89 |
90 Open(); | 90 Open(); |
91 backend_->ModifyStorageSize(0, static_cast<int32>(key.size())); | 91 backend_->ModifyStorageSize(0, static_cast<int32_t>(key.size())); |
92 return true; | 92 return true; |
93 } | 93 } |
94 | 94 |
95 void MemEntryImpl::InternalDoom() { | 95 void MemEntryImpl::InternalDoom() { |
96 net_log_.AddEvent(net::NetLog::TYPE_ENTRY_DOOM); | 96 net_log_.AddEvent(net::NetLog::TYPE_ENTRY_DOOM); |
97 doomed_ = true; | 97 doomed_ = true; |
98 if (!ref_count_) { | 98 if (!ref_count_) { |
99 if (type() == kParentEntry) { | 99 if (type() == kParentEntry) { |
100 // If this is a parent entry, we need to doom all the child entries. | 100 // If this is a parent entry, we need to doom all the child entries. |
101 if (children_.get()) { | 101 if (children_.get()) { |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 } | 170 } |
171 | 171 |
172 Time MemEntryImpl::GetLastUsed() const { | 172 Time MemEntryImpl::GetLastUsed() const { |
173 return last_used_; | 173 return last_used_; |
174 } | 174 } |
175 | 175 |
176 Time MemEntryImpl::GetLastModified() const { | 176 Time MemEntryImpl::GetLastModified() const { |
177 return last_modified_; | 177 return last_modified_; |
178 } | 178 } |
179 | 179 |
180 int32 MemEntryImpl::GetDataSize(int index) const { | 180 int32_t 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_.IsCapturing()) { | 188 if (net_log_.IsCapturing()) { |
189 net_log_.BeginEvent( | 189 net_log_.BeginEvent( |
190 net::NetLog::TYPE_ENTRY_READ_DATA, | 190 net::NetLog::TYPE_ENTRY_READ_DATA, |
(...skipping 21 matching lines...) Expand all Loading... |
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_.IsCapturing()) { | 214 if (net_log_.IsCapturing()) { |
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_t offset, |
| 223 IOBuffer* buf, |
| 224 int buf_len, |
223 const CompletionCallback& callback) { | 225 const CompletionCallback& callback) { |
224 if (net_log_.IsCapturing()) { | 226 if (net_log_.IsCapturing()) { |
225 net_log_.BeginEvent( | 227 net_log_.BeginEvent( |
226 net::NetLog::TYPE_SPARSE_READ, | 228 net::NetLog::TYPE_SPARSE_READ, |
227 CreateNetLogSparseOperationCallback(offset, buf_len)); | 229 CreateNetLogSparseOperationCallback(offset, buf_len)); |
228 } | 230 } |
229 int result = InternalReadSparseData(offset, buf, buf_len); | 231 int result = InternalReadSparseData(offset, buf, buf_len); |
230 if (net_log_.IsCapturing()) | 232 if (net_log_.IsCapturing()) |
231 net_log_.EndEvent(net::NetLog::TYPE_SPARSE_READ); | 233 net_log_.EndEvent(net::NetLog::TYPE_SPARSE_READ); |
232 return result; | 234 return result; |
233 } | 235 } |
234 | 236 |
235 int MemEntryImpl::WriteSparseData(int64 offset, IOBuffer* buf, int buf_len, | 237 int MemEntryImpl::WriteSparseData(int64_t offset, |
| 238 IOBuffer* buf, |
| 239 int buf_len, |
236 const CompletionCallback& callback) { | 240 const CompletionCallback& callback) { |
237 if (net_log_.IsCapturing()) { | 241 if (net_log_.IsCapturing()) { |
238 net_log_.BeginEvent( | 242 net_log_.BeginEvent( |
239 net::NetLog::TYPE_SPARSE_WRITE, | 243 net::NetLog::TYPE_SPARSE_WRITE, |
240 CreateNetLogSparseOperationCallback(offset, buf_len)); | 244 CreateNetLogSparseOperationCallback(offset, buf_len)); |
241 } | 245 } |
242 int result = InternalWriteSparseData(offset, buf, buf_len); | 246 int result = InternalWriteSparseData(offset, buf, buf_len); |
243 if (net_log_.IsCapturing()) | 247 if (net_log_.IsCapturing()) |
244 net_log_.EndEvent(net::NetLog::TYPE_SPARSE_WRITE); | 248 net_log_.EndEvent(net::NetLog::TYPE_SPARSE_WRITE); |
245 return result; | 249 return result; |
246 } | 250 } |
247 | 251 |
248 int MemEntryImpl::GetAvailableRange(int64 offset, int len, int64* start, | 252 int MemEntryImpl::GetAvailableRange(int64_t offset, |
| 253 int len, |
| 254 int64_t* start, |
249 const CompletionCallback& callback) { | 255 const CompletionCallback& callback) { |
250 if (net_log_.IsCapturing()) { | 256 if (net_log_.IsCapturing()) { |
251 net_log_.BeginEvent( | 257 net_log_.BeginEvent( |
252 net::NetLog::TYPE_SPARSE_GET_RANGE, | 258 net::NetLog::TYPE_SPARSE_GET_RANGE, |
253 CreateNetLogSparseOperationCallback(offset, len)); | 259 CreateNetLogSparseOperationCallback(offset, len)); |
254 } | 260 } |
255 int result = GetAvailableRange(offset, len, start); | 261 int result = GetAvailableRange(offset, len, start); |
256 if (net_log_.IsCapturing()) { | 262 if (net_log_.IsCapturing()) { |
257 net_log_.EndEvent( | 263 net_log_.EndEvent( |
258 net::NetLog::TYPE_SPARSE_GET_RANGE, | 264 net::NetLog::TYPE_SPARSE_GET_RANGE, |
259 CreateNetLogGetAvailableRangeResultCallback(*start, result)); | 265 CreateNetLogGetAvailableRangeResultCallback(*start, result)); |
260 } | 266 } |
261 return result; | 267 return result; |
262 } | 268 } |
263 | 269 |
264 bool MemEntryImpl::CouldBeSparse() const { | 270 bool MemEntryImpl::CouldBeSparse() const { |
265 DCHECK_EQ(kParentEntry, type()); | 271 DCHECK_EQ(kParentEntry, type()); |
266 return (children_.get() != NULL); | 272 return (children_.get() != NULL); |
267 } | 273 } |
268 | 274 |
269 int MemEntryImpl::ReadyForSparseIO(const CompletionCallback& callback) { | 275 int MemEntryImpl::ReadyForSparseIO(const CompletionCallback& callback) { |
270 return net::OK; | 276 return net::OK; |
271 } | 277 } |
272 | 278 |
273 // ------------------------------------------------------------------------ | 279 // ------------------------------------------------------------------------ |
274 | 280 |
275 MemEntryImpl::~MemEntryImpl() { | 281 MemEntryImpl::~MemEntryImpl() { |
276 for (int i = 0; i < NUM_STREAMS; i++) | 282 for (int i = 0; i < NUM_STREAMS; i++) |
277 backend_->ModifyStorageSize(data_size_[i], 0); | 283 backend_->ModifyStorageSize(data_size_[i], 0); |
278 backend_->ModifyStorageSize(static_cast<int32>(key_.size()), 0); | 284 backend_->ModifyStorageSize(static_cast<int32_t>(key_.size()), 0); |
279 net_log_.EndEvent(net::NetLog::TYPE_DISK_CACHE_MEM_ENTRY_IMPL); | 285 net_log_.EndEvent(net::NetLog::TYPE_DISK_CACHE_MEM_ENTRY_IMPL); |
280 } | 286 } |
281 | 287 |
282 int MemEntryImpl::InternalReadData(int index, int offset, IOBuffer* buf, | 288 int MemEntryImpl::InternalReadData(int index, int offset, IOBuffer* buf, |
283 int buf_len) { | 289 int buf_len) { |
284 DCHECK(type() == kParentEntry || index == kSparseData); | 290 DCHECK(type() == kParentEntry || index == kSparseData); |
285 | 291 |
286 if (index < 0 || index >= NUM_STREAMS) | 292 if (index < 0 || index >= NUM_STREAMS) |
287 return net::ERR_INVALID_ARGUMENT; | 293 return net::ERR_INVALID_ARGUMENT; |
288 | 294 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 | 343 |
338 UpdateRank(true); | 344 UpdateRank(true); |
339 | 345 |
340 if (!buf_len) | 346 if (!buf_len) |
341 return 0; | 347 return 0; |
342 | 348 |
343 memcpy(&(data_[index])[offset], buf->data(), buf_len); | 349 memcpy(&(data_[index])[offset], buf->data(), buf_len); |
344 return buf_len; | 350 return buf_len; |
345 } | 351 } |
346 | 352 |
347 int MemEntryImpl::InternalReadSparseData(int64 offset, IOBuffer* buf, | 353 int MemEntryImpl::InternalReadSparseData(int64_t offset, |
| 354 IOBuffer* buf, |
348 int buf_len) { | 355 int buf_len) { |
349 DCHECK(type() == kParentEntry); | 356 DCHECK(type() == kParentEntry); |
350 | 357 |
351 if (!InitSparseInfo()) | 358 if (!InitSparseInfo()) |
352 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 359 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
353 | 360 |
354 if (offset < 0 || buf_len < 0) | 361 if (offset < 0 || buf_len < 0) |
355 return net::ERR_INVALID_ARGUMENT; | 362 return net::ERR_INVALID_ARGUMENT; |
356 | 363 |
357 // We will keep using this buffer and adjust the offset in this buffer. | 364 // We will keep using this buffer and adjust the offset in this buffer. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 | 401 |
395 // Increment the counter by number of bytes read in the child entry. | 402 // Increment the counter by number of bytes read in the child entry. |
396 io_buf->DidConsume(ret); | 403 io_buf->DidConsume(ret); |
397 } | 404 } |
398 | 405 |
399 UpdateRank(false); | 406 UpdateRank(false); |
400 | 407 |
401 return io_buf->BytesConsumed(); | 408 return io_buf->BytesConsumed(); |
402 } | 409 } |
403 | 410 |
404 int MemEntryImpl::InternalWriteSparseData(int64 offset, IOBuffer* buf, | 411 int MemEntryImpl::InternalWriteSparseData(int64_t offset, |
| 412 IOBuffer* buf, |
405 int buf_len) { | 413 int buf_len) { |
406 DCHECK(type() == kParentEntry); | 414 DCHECK(type() == kParentEntry); |
407 | 415 |
408 if (!InitSparseInfo()) | 416 if (!InitSparseInfo()) |
409 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 417 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
410 | 418 |
411 if (offset < 0 || buf_len < 0) | 419 if (offset < 0 || buf_len < 0) |
412 return net::ERR_INVALID_ARGUMENT; | 420 return net::ERR_INVALID_ARGUMENT; |
413 | 421 |
414 scoped_refptr<net::DrainableIOBuffer> io_buf( | 422 scoped_refptr<net::DrainableIOBuffer> io_buf( |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
460 | 468 |
461 // Adjust the offset in the IO buffer. | 469 // Adjust the offset in the IO buffer. |
462 io_buf->DidConsume(ret); | 470 io_buf->DidConsume(ret); |
463 } | 471 } |
464 | 472 |
465 UpdateRank(true); | 473 UpdateRank(true); |
466 | 474 |
467 return io_buf->BytesConsumed(); | 475 return io_buf->BytesConsumed(); |
468 } | 476 } |
469 | 477 |
470 int MemEntryImpl::GetAvailableRange(int64 offset, int len, int64* start) { | 478 int MemEntryImpl::GetAvailableRange(int64_t offset, int len, int64_t* start) { |
471 DCHECK(type() == kParentEntry); | 479 DCHECK(type() == kParentEntry); |
472 DCHECK(start); | 480 DCHECK(start); |
473 | 481 |
474 if (!InitSparseInfo()) | 482 if (!InitSparseInfo()) |
475 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 483 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
476 | 484 |
477 if (offset < 0 || len < 0 || !start) | 485 if (offset < 0 || len < 0 || !start) |
478 return net::ERR_INVALID_ARGUMENT; | 486 return net::ERR_INVALID_ARGUMENT; |
479 | 487 |
480 MemEntryImpl* current_child = NULL; | 488 MemEntryImpl* current_child = NULL; |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
570 parent_ = parent; | 578 parent_ = parent; |
571 child_id_ = child_id; | 579 child_id_ = child_id; |
572 Time current = Time::Now(); | 580 Time current = Time::Now(); |
573 last_modified_ = current; | 581 last_modified_ = current; |
574 last_used_ = current; | 582 last_used_ = current; |
575 // Insert this to the backend's ranking list. | 583 // Insert this to the backend's ranking list. |
576 backend_->InsertIntoRankingList(this); | 584 backend_->InsertIntoRankingList(this); |
577 return true; | 585 return true; |
578 } | 586 } |
579 | 587 |
580 MemEntryImpl* MemEntryImpl::OpenChild(int64 offset, bool create) { | 588 MemEntryImpl* MemEntryImpl::OpenChild(int64_t offset, bool create) { |
581 DCHECK(type() == kParentEntry); | 589 DCHECK(type() == kParentEntry); |
582 int index = ToChildIndex(offset); | 590 int index = ToChildIndex(offset); |
583 EntryMap::iterator i = children_->find(index); | 591 EntryMap::iterator i = children_->find(index); |
584 if (i != children_->end()) { | 592 if (i != children_->end()) { |
585 return i->second; | 593 return i->second; |
586 } else if (create) { | 594 } else if (create) { |
587 MemEntryImpl* child = new MemEntryImpl(backend_); | 595 MemEntryImpl* child = new MemEntryImpl(backend_); |
588 child->InitChildEntry(this, index, net_log_.net_log()); | 596 child->InitChildEntry(this, index, net_log_.net_log()); |
589 (*children_)[index] = child; | 597 (*children_)[index] = child; |
590 return child; | 598 return child; |
591 } | 599 } |
592 return NULL; | 600 return NULL; |
593 } | 601 } |
594 | 602 |
595 int MemEntryImpl::FindNextChild(int64 offset, int len, MemEntryImpl** child) { | 603 int MemEntryImpl::FindNextChild(int64_t offset, int len, MemEntryImpl** child) { |
596 DCHECK(child); | 604 DCHECK(child); |
597 *child = NULL; | 605 *child = NULL; |
598 int scanned_len = 0; | 606 int scanned_len = 0; |
599 | 607 |
600 // This loop tries to find the first existing child. | 608 // This loop tries to find the first existing child. |
601 while (scanned_len < len) { | 609 while (scanned_len < len) { |
602 // This points to the current offset in the child. | 610 // This points to the current offset in the child. |
603 int current_child_offset = ToChildOffset(offset + scanned_len); | 611 int current_child_offset = ToChildOffset(offset + scanned_len); |
604 MemEntryImpl* current_child = OpenChild(offset + scanned_len, false); | 612 MemEntryImpl* current_child = OpenChild(offset + scanned_len, false); |
605 if (current_child) { | 613 if (current_child) { |
(...skipping 16 matching lines...) Expand all Loading... |
622 scanned_len += kMaxSparseEntrySize - current_child_offset; | 630 scanned_len += kMaxSparseEntrySize - current_child_offset; |
623 } | 631 } |
624 return scanned_len; | 632 return scanned_len; |
625 } | 633 } |
626 | 634 |
627 void MemEntryImpl::DetachChild(int child_id) { | 635 void MemEntryImpl::DetachChild(int child_id) { |
628 children_->erase(child_id); | 636 children_->erase(child_id); |
629 } | 637 } |
630 | 638 |
631 } // namespace disk_cache | 639 } // namespace disk_cache |
OLD | NEW |