OLD | NEW |
1 // Copyright (c) 2006-2008 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" |
11 | 11 |
12 using base::Time; | 12 using base::Time; |
13 | 13 |
14 namespace disk_cache { | 14 namespace disk_cache { |
15 | 15 |
16 MemEntryImpl::MemEntryImpl(MemBackendImpl* backend) { | 16 MemEntryImpl::MemEntryImpl(MemBackendImpl* backend) { |
17 doomed_ = false; | 17 doomed_ = false; |
18 backend_ = backend; | 18 backend_ = backend; |
19 ref_count_ = 0; | 19 ref_count_ = 0; |
| 20 parent_ = NULL; |
20 next_ = NULL; | 21 next_ = NULL; |
21 prev_ = NULL; | 22 prev_ = NULL; |
22 for (int i = 0; i < NUM_STREAMS; i++) | 23 for (int i = 0; i < NUM_STREAMS; i++) |
23 data_size_[i] = 0; | 24 data_size_[i] = 0; |
24 } | 25 } |
25 | 26 |
26 MemEntryImpl::~MemEntryImpl() { | 27 MemEntryImpl::~MemEntryImpl() { |
27 for (int i = 0; i < NUM_STREAMS; i++) | 28 for (int i = 0; i < NUM_STREAMS; i++) |
28 backend_->ModifyStorageSize(data_size_[i], 0); | 29 backend_->ModifyStorageSize(data_size_[i], 0); |
29 backend_->ModifyStorageSize(static_cast<int32>(key_.size()), 0); | 30 backend_->ModifyStorageSize(static_cast<int32>(key_.size()), 0); |
30 } | 31 } |
31 | 32 |
32 bool MemEntryImpl::CreateEntry(const std::string& key) { | 33 bool MemEntryImpl::CreateEntry(const std::string& key) { |
33 key_ = key; | 34 key_ = key; |
34 last_modified_ = Time::Now(); | 35 last_modified_ = Time::Now(); |
35 last_used_ = Time::Now(); | 36 last_used_ = Time::Now(); |
| 37 type_ = kParentEntry; |
36 Open(); | 38 Open(); |
37 backend_->ModifyStorageSize(0, static_cast<int32>(key.size())); | 39 backend_->ModifyStorageSize(0, static_cast<int32>(key.size())); |
38 return true; | 40 return true; |
39 } | 41 } |
40 | 42 |
| 43 bool MemEntryImpl::CreateChildEntry(MemEntryImpl* parent) { |
| 44 parent_ = parent; |
| 45 last_modified_ = Time::Now(); |
| 46 last_used_ = Time::Now(); |
| 47 type_ = kChildEntry; |
| 48 // Insert this to the backend's ranking list. |
| 49 backend_->InsertIntoRankingList(this); |
| 50 return true; |
| 51 } |
| 52 |
41 void MemEntryImpl::Close() { | 53 void MemEntryImpl::Close() { |
| 54 // Only a parent entry can be closed. |
| 55 DCHECK(type_ == kParentEntry); |
42 ref_count_--; | 56 ref_count_--; |
43 DCHECK(ref_count_ >= 0); | 57 DCHECK(ref_count_ >= 0); |
44 if (!ref_count_ && doomed_) | 58 if (!ref_count_ && doomed_) |
45 delete this; | 59 delete this; |
46 } | 60 } |
47 | 61 |
48 void MemEntryImpl::Open() { | 62 void MemEntryImpl::Open() { |
| 63 // Only a parent entry can be opened. |
| 64 DCHECK(type_ == kParentEntry); |
49 ref_count_++; | 65 ref_count_++; |
50 DCHECK(ref_count_ >= 0); | 66 DCHECK(ref_count_ >= 0); |
51 DCHECK(!doomed_); | 67 DCHECK(!doomed_); |
52 } | 68 } |
53 | 69 |
54 bool MemEntryImpl::InUse() { | 70 bool MemEntryImpl::InUse() { |
55 return ref_count_ > 0; | 71 if (type_ == kParentEntry) { |
| 72 return ref_count_ > 0; |
| 73 } else { |
| 74 // A child entry is always not in use. The consequence is that a child entry |
| 75 // can always be evicted while the associated parent entry is currently in |
| 76 // used (i.e. opened). |
| 77 return false; |
| 78 } |
56 } | 79 } |
57 | 80 |
58 void MemEntryImpl::Doom() { | 81 void MemEntryImpl::Doom() { |
59 if (doomed_) | 82 if (doomed_) |
60 return; | 83 return; |
61 backend_->InternalDoomEntry(this); | 84 if (type_ == kParentEntry) { |
| 85 // Perform internal doom from the backend if this is a parent entry. |
| 86 backend_->InternalDoomEntry(this); |
| 87 } else { |
| 88 // Manually detach from the parent entry and perform internal doom. |
| 89 backend_->RemoveFromRankingList(this); |
| 90 InternalDoom(); |
| 91 } |
62 } | 92 } |
63 | 93 |
64 void MemEntryImpl::InternalDoom() { | 94 void MemEntryImpl::InternalDoom() { |
65 doomed_ = true; | 95 doomed_ = true; |
66 if (!ref_count_) | 96 if (!ref_count_) { |
| 97 if (type_ == kParentEntry) { |
| 98 // TODO(hclam): doom all child entries associated with this entry. |
| 99 } else { |
| 100 // TODO(hclam): detach this child entry from the parent entry. |
| 101 } |
67 delete this; | 102 delete this; |
| 103 } |
68 } | 104 } |
69 | 105 |
70 std::string MemEntryImpl::GetKey() const { | 106 std::string MemEntryImpl::GetKey() const { |
| 107 // A child entry doesn't have key so this method should not be called. |
| 108 DCHECK(type_ == kParentEntry); |
71 return key_; | 109 return key_; |
72 } | 110 } |
73 | 111 |
74 Time MemEntryImpl::GetLastUsed() const { | 112 Time MemEntryImpl::GetLastUsed() const { |
75 return last_used_; | 113 return last_used_; |
76 } | 114 } |
77 | 115 |
78 Time MemEntryImpl::GetLastModified() const { | 116 Time MemEntryImpl::GetLastModified() const { |
79 return last_modified_; | 117 return last_modified_; |
80 } | 118 } |
81 | 119 |
82 int32 MemEntryImpl::GetDataSize(int index) const { | 120 int32 MemEntryImpl::GetDataSize(int index) const { |
83 if (index < 0 || index >= NUM_STREAMS) | 121 if (index < 0 || index >= NUM_STREAMS) |
84 return 0; | 122 return 0; |
85 | 123 |
| 124 // TODO(hclam): handle the case when this is a parent entry and has associated |
| 125 // child entries. |
86 return data_size_[index]; | 126 return data_size_[index]; |
87 } | 127 } |
88 | 128 |
89 int MemEntryImpl::ReadData(int index, int offset, net::IOBuffer* buf, | 129 int MemEntryImpl::ReadData(int index, int offset, net::IOBuffer* buf, |
90 int buf_len, net::CompletionCallback* completion_callback) { | 130 int buf_len, net::CompletionCallback* completion_callback) { |
| 131 // This method can only be called with a parent entry. |
| 132 DCHECK(type_ == kParentEntry); |
| 133 |
91 if (index < 0 || index >= NUM_STREAMS) | 134 if (index < 0 || index >= NUM_STREAMS) |
92 return net::ERR_INVALID_ARGUMENT; | 135 return net::ERR_INVALID_ARGUMENT; |
93 | 136 |
94 int entry_size = GetDataSize(index); | 137 int entry_size = GetDataSize(index); |
95 if (offset >= entry_size || offset < 0 || !buf_len) | 138 if (offset >= entry_size || offset < 0 || !buf_len) |
96 return 0; | 139 return 0; |
97 | 140 |
98 if (buf_len < 0) | 141 if (buf_len < 0) |
99 return net::ERR_INVALID_ARGUMENT; | 142 return net::ERR_INVALID_ARGUMENT; |
100 | 143 |
101 if (offset + buf_len > entry_size) | 144 if (offset + buf_len > entry_size) |
102 buf_len = entry_size - offset; | 145 buf_len = entry_size - offset; |
103 | 146 |
104 UpdateRank(false); | 147 UpdateRank(false); |
105 | 148 |
106 memcpy(buf->data() , &(data_[index])[offset], buf_len); | 149 memcpy(buf->data() , &(data_[index])[offset], buf_len); |
107 return buf_len; | 150 return buf_len; |
108 } | 151 } |
109 | 152 |
110 int MemEntryImpl::WriteData(int index, int offset, net::IOBuffer* buf, | 153 int MemEntryImpl::WriteData(int index, int offset, net::IOBuffer* buf, |
111 int buf_len, net::CompletionCallback* completion_callback, bool truncate) { | 154 int buf_len, net::CompletionCallback* completion_callback, bool truncate) { |
| 155 // This method can only be called with a parent entry. |
| 156 DCHECK(type_ == kParentEntry); |
| 157 |
112 if (index < 0 || index >= NUM_STREAMS) | 158 if (index < 0 || index >= NUM_STREAMS) |
113 return net::ERR_INVALID_ARGUMENT; | 159 return net::ERR_INVALID_ARGUMENT; |
114 | 160 |
115 if (offset < 0 || buf_len < 0) | 161 if (offset < 0 || buf_len < 0) |
116 return net::ERR_INVALID_ARGUMENT; | 162 return net::ERR_INVALID_ARGUMENT; |
117 | 163 |
118 int max_file_size = backend_->MaxFileSize(); | 164 int max_file_size = backend_->MaxFileSize(); |
119 | 165 |
120 // offset of buf_len could be negative numbers. | 166 // offset of buf_len could be negative numbers. |
121 if (offset > max_file_size || buf_len > max_file_size || | 167 if (offset > max_file_size || buf_len > max_file_size || |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 last_used_ = current; | 232 last_used_ = current; |
187 | 233 |
188 if (modified) | 234 if (modified) |
189 last_modified_ = current; | 235 last_modified_ = current; |
190 | 236 |
191 if (!doomed_) | 237 if (!doomed_) |
192 backend_->UpdateRank(this); | 238 backend_->UpdateRank(this); |
193 } | 239 } |
194 | 240 |
195 } // namespace disk_cache | 241 } // namespace disk_cache |
OLD | NEW |