OLD | NEW |
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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/mem_entry_impl.h" | 5 #include "net/disk_cache/mem_entry_impl.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "net/base/io_buffer.h" | 8 #include "net/base/io_buffer.h" |
9 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
10 #include "net/disk_cache/mem_backend_impl.h" | 10 #include "net/disk_cache/mem_backend_impl.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 for (int i = 0; i < NUM_STREAMS; i++) | 47 for (int i = 0; i < NUM_STREAMS; i++) |
48 data_size_[i] = 0; | 48 data_size_[i] = 0; |
49 } | 49 } |
50 | 50 |
51 MemEntryImpl::~MemEntryImpl() { | 51 MemEntryImpl::~MemEntryImpl() { |
52 for (int i = 0; i < NUM_STREAMS; i++) | 52 for (int i = 0; i < NUM_STREAMS; i++) |
53 backend_->ModifyStorageSize(data_size_[i], 0); | 53 backend_->ModifyStorageSize(data_size_[i], 0); |
54 backend_->ModifyStorageSize(static_cast<int32>(key_.size()), 0); | 54 backend_->ModifyStorageSize(static_cast<int32>(key_.size()), 0); |
55 } | 55 } |
56 | 56 |
57 bool MemEntryImpl::CreateEntry(const std::string& key) { | |
58 key_ = key; | |
59 last_modified_ = Time::Now(); | |
60 last_used_ = Time::Now(); | |
61 Open(); | |
62 backend_->ModifyStorageSize(0, static_cast<int32>(key.size())); | |
63 return true; | |
64 } | |
65 | |
66 void MemEntryImpl::Close() { | |
67 // Only a parent entry can be closed. | |
68 DCHECK(type() == kParentEntry); | |
69 ref_count_--; | |
70 DCHECK(ref_count_ >= 0); | |
71 if (!ref_count_ && doomed_) | |
72 InternalDoom(); | |
73 } | |
74 | |
75 void MemEntryImpl::Open() { | |
76 // Only a parent entry can be opened. | |
77 // TODO(hclam): make sure it's correct to not apply the concept of ref | |
78 // counting to child entry. | |
79 DCHECK(type() == kParentEntry); | |
80 ref_count_++; | |
81 DCHECK(ref_count_ >= 0); | |
82 DCHECK(!doomed_); | |
83 } | |
84 | |
85 bool MemEntryImpl::InUse() { | |
86 if (type() == kParentEntry) { | |
87 return ref_count_ > 0; | |
88 } else { | |
89 // A child entry is always not in use. The consequence is that a child entry | |
90 // can always be evicted while the associated parent entry is currently in | |
91 // used (i.e. opened). | |
92 return false; | |
93 } | |
94 } | |
95 | |
96 void MemEntryImpl::Doom() { | 57 void MemEntryImpl::Doom() { |
97 if (doomed_) | 58 if (doomed_) |
98 return; | 59 return; |
99 if (type() == kParentEntry) { | 60 if (type() == kParentEntry) { |
100 // Perform internal doom from the backend if this is a parent entry. | 61 // Perform internal doom from the backend if this is a parent entry. |
101 backend_->InternalDoomEntry(this); | 62 backend_->InternalDoomEntry(this); |
102 } else { | 63 } else { |
103 // Manually detach from the backend and perform internal doom. | 64 // Manually detach from the backend and perform internal doom. |
104 backend_->RemoveFromRankingList(this); | 65 backend_->RemoveFromRankingList(this); |
105 InternalDoom(); | 66 InternalDoom(); |
106 } | 67 } |
107 } | 68 } |
108 | 69 |
109 void MemEntryImpl::InternalDoom() { | 70 void MemEntryImpl::Close() { |
110 doomed_ = true; | 71 // Only a parent entry can be closed. |
111 if (!ref_count_) { | 72 DCHECK(type() == kParentEntry); |
112 if (type() == kParentEntry) { | 73 ref_count_--; |
113 // If this is a parent entry, we need to doom all the child entries. | 74 DCHECK(ref_count_ >= 0); |
114 if (children_.get()) { | 75 if (!ref_count_ && doomed_) |
115 EntryMap children; | 76 InternalDoom(); |
116 children.swap(*children_); | |
117 for (EntryMap::iterator i = children.begin(); | |
118 i != children.end(); ++i) { | |
119 // Since a pointer to this object is also saved in the map, avoid | |
120 // dooming it. | |
121 if (i->second != this) | |
122 i->second->Doom(); | |
123 } | |
124 DCHECK(children_->size() == 0); | |
125 } | |
126 } else { | |
127 // If this is a child entry, detach it from the parent. | |
128 parent_->DetachChild(child_id_); | |
129 } | |
130 delete this; | |
131 } | |
132 } | 77 } |
133 | 78 |
134 std::string MemEntryImpl::GetKey() const { | 79 std::string MemEntryImpl::GetKey() const { |
135 // A child entry doesn't have key so this method should not be called. | 80 // A child entry doesn't have key so this method should not be called. |
136 DCHECK(type() == kParentEntry); | 81 DCHECK(type() == kParentEntry); |
137 return key_; | 82 return key_; |
138 } | 83 } |
139 | 84 |
140 Time MemEntryImpl::GetLastUsed() const { | 85 Time MemEntryImpl::GetLastUsed() const { |
141 return last_used_; | 86 return last_used_; |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 // If the next child is discontinuous, break the loop. | 312 // If the next child is discontinuous, break the loop. |
368 if (FindNextChild(*start + continuous, len, ¤t_child)) | 313 if (FindNextChild(*start + continuous, len, ¤t_child)) |
369 break; | 314 break; |
370 } | 315 } |
371 return continuous; | 316 return continuous; |
372 } | 317 } |
373 *start = offset; | 318 *start = offset; |
374 return 0; | 319 return 0; |
375 } | 320 } |
376 | 321 |
| 322 // ------------------------------------------------------------------------ |
| 323 |
| 324 bool MemEntryImpl::CreateEntry(const std::string& key) { |
| 325 key_ = key; |
| 326 last_modified_ = Time::Now(); |
| 327 last_used_ = Time::Now(); |
| 328 Open(); |
| 329 backend_->ModifyStorageSize(0, static_cast<int32>(key.size())); |
| 330 return true; |
| 331 } |
| 332 |
| 333 void MemEntryImpl::InternalDoom() { |
| 334 doomed_ = true; |
| 335 if (!ref_count_) { |
| 336 if (type() == kParentEntry) { |
| 337 // If this is a parent entry, we need to doom all the child entries. |
| 338 if (children_.get()) { |
| 339 EntryMap children; |
| 340 children.swap(*children_); |
| 341 for (EntryMap::iterator i = children.begin(); |
| 342 i != children.end(); ++i) { |
| 343 // Since a pointer to this object is also saved in the map, avoid |
| 344 // dooming it. |
| 345 if (i->second != this) |
| 346 i->second->Doom(); |
| 347 } |
| 348 DCHECK(children_->size() == 0); |
| 349 } |
| 350 } else { |
| 351 // If this is a child entry, detach it from the parent. |
| 352 parent_->DetachChild(child_id_); |
| 353 } |
| 354 delete this; |
| 355 } |
| 356 } |
| 357 |
| 358 void MemEntryImpl::Open() { |
| 359 // Only a parent entry can be opened. |
| 360 // TODO(hclam): make sure it's correct to not apply the concept of ref |
| 361 // counting to child entry. |
| 362 DCHECK(type() == kParentEntry); |
| 363 ref_count_++; |
| 364 DCHECK(ref_count_ >= 0); |
| 365 DCHECK(!doomed_); |
| 366 } |
| 367 |
| 368 bool MemEntryImpl::InUse() { |
| 369 if (type() == kParentEntry) { |
| 370 return ref_count_ > 0; |
| 371 } else { |
| 372 // A child entry is always not in use. The consequence is that a child entry |
| 373 // can always be evicted while the associated parent entry is currently in |
| 374 // used (i.e. opened). |
| 375 return false; |
| 376 } |
| 377 } |
| 378 |
| 379 // ------------------------------------------------------------------------ |
| 380 |
377 void MemEntryImpl::PrepareTarget(int index, int offset, int buf_len) { | 381 void MemEntryImpl::PrepareTarget(int index, int offset, int buf_len) { |
378 int entry_size = GetDataSize(index); | 382 int entry_size = GetDataSize(index); |
379 | 383 |
380 if (entry_size >= offset + buf_len) | 384 if (entry_size >= offset + buf_len) |
381 return; // Not growing the stored data. | 385 return; // Not growing the stored data. |
382 | 386 |
383 if (static_cast<int>(data_[index].size()) < offset + buf_len) | 387 if (static_cast<int>(data_[index].size()) < offset + buf_len) |
384 data_[index].resize(offset + buf_len); | 388 data_[index].resize(offset + buf_len); |
385 | 389 |
386 if (offset <= entry_size) | 390 if (offset <= entry_size) |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
476 scanned_len += kMaxSparseEntrySize - current_child_offset; | 480 scanned_len += kMaxSparseEntrySize - current_child_offset; |
477 } | 481 } |
478 return scanned_len; | 482 return scanned_len; |
479 } | 483 } |
480 | 484 |
481 void MemEntryImpl::DetachChild(int child_id) { | 485 void MemEntryImpl::DetachChild(int child_id) { |
482 children_->erase(child_id); | 486 children_->erase(child_id); |
483 } | 487 } |
484 | 488 |
485 } // namespace disk_cache | 489 } // namespace disk_cache |
OLD | NEW |