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/mem_entry_impl.h" | 5 #include "net/disk_cache/mem_entry_impl.h" |
6 | 6 |
| 7 #include "base/bind.h" |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
8 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
| 10 #include "base/values.h" |
9 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
10 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
11 #include "net/disk_cache/mem_backend_impl.h" | 13 #include "net/disk_cache/mem_backend_impl.h" |
12 #include "net/disk_cache/net_log_parameters.h" | 14 #include "net/disk_cache/net_log_parameters.h" |
13 | 15 |
14 using base::Time; | 16 using base::Time; |
15 | 17 |
16 namespace { | 18 namespace { |
17 | 19 |
18 const int kSparseData = 1; | 20 const int kSparseData = 1; |
(...skipping 15 matching lines...) Expand all Loading... |
34 } | 36 } |
35 | 37 |
36 // 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 |
37 // child_id. This name is only used for logging purposes. | 39 // child_id. This name is only used for logging purposes. |
38 // 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 |
39 // 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. |
40 std::string GenerateChildName(const std::string& base_name, int child_id) { | 42 std::string GenerateChildName(const std::string& base_name, int child_id) { |
41 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); |
42 } | 44 } |
43 | 45 |
| 46 // Returns NetLog parameters for the creation of a child MemEntryImpl. Separate |
| 47 // function needed because child entries don't suppport GetKey(). |
| 48 Value* NetLogChildEntryCreationCallback(const disk_cache::MemEntryImpl* parent, |
| 49 int child_id, |
| 50 net::NetLog::LogLevel /* log_level */) { |
| 51 DictionaryValue* dict = new DictionaryValue(); |
| 52 dict->SetString("key", GenerateChildName(parent->GetKey(), child_id)); |
| 53 dict->SetBoolean("created", true); |
| 54 return dict; |
| 55 } |
| 56 |
44 } // namespace | 57 } // namespace |
45 | 58 |
46 namespace disk_cache { | 59 namespace disk_cache { |
47 | 60 |
48 MemEntryImpl::MemEntryImpl(MemBackendImpl* backend) { | 61 MemEntryImpl::MemEntryImpl(MemBackendImpl* backend) { |
49 doomed_ = false; | 62 doomed_ = false; |
50 backend_ = backend; | 63 backend_ = backend; |
51 ref_count_ = 0; | 64 ref_count_ = 0; |
52 parent_ = NULL; | 65 parent_ = NULL; |
53 child_id_ = 0; | 66 child_id_ = 0; |
54 child_first_pos_ = 0; | 67 child_first_pos_ = 0; |
55 next_ = NULL; | 68 next_ = NULL; |
56 prev_ = NULL; | 69 prev_ = NULL; |
57 for (int i = 0; i < NUM_STREAMS; i++) | 70 for (int i = 0; i < NUM_STREAMS; i++) |
58 data_size_[i] = 0; | 71 data_size_[i] = 0; |
59 } | 72 } |
60 | 73 |
61 // ------------------------------------------------------------------------ | 74 // ------------------------------------------------------------------------ |
62 | 75 |
63 bool MemEntryImpl::CreateEntry(const std::string& key, net::NetLog* net_log) { | 76 bool MemEntryImpl::CreateEntry(const std::string& key, net::NetLog* net_log) { |
64 net_log_ = net::BoundNetLog::Make(net_log, | |
65 net::NetLog::SOURCE_MEMORY_CACHE_ENTRY); | |
66 net_log_.BeginEvent( | |
67 net::NetLog::TYPE_DISK_CACHE_MEM_ENTRY_IMPL, | |
68 make_scoped_refptr(new EntryCreationParameters(key, true))); | |
69 key_ = key; | 77 key_ = key; |
70 Time current = Time::Now(); | 78 Time current = Time::Now(); |
71 last_modified_ = current; | 79 last_modified_ = current; |
72 last_used_ = current; | 80 last_used_ = current; |
| 81 |
| 82 net_log_ = net::BoundNetLog::Make(net_log, |
| 83 net::NetLog::SOURCE_MEMORY_CACHE_ENTRY); |
| 84 // Must be called after |key_| is set, so GetKey() works. |
| 85 net_log_.BeginEvent( |
| 86 net::NetLog::TYPE_DISK_CACHE_MEM_ENTRY_IMPL, |
| 87 CreateNetLogEntryCreationCallback(this, true)); |
| 88 |
73 Open(); | 89 Open(); |
74 backend_->ModifyStorageSize(0, static_cast<int32>(key.size())); | 90 backend_->ModifyStorageSize(0, static_cast<int32>(key.size())); |
75 return true; | 91 return true; |
76 } | 92 } |
77 | 93 |
78 void MemEntryImpl::InternalDoom() { | 94 void MemEntryImpl::InternalDoom() { |
79 net_log_.AddEvent(net::NetLog::TYPE_ENTRY_DOOM, NULL); | 95 net_log_.AddEvent(net::NetLog::TYPE_ENTRY_DOOM); |
80 doomed_ = true; | 96 doomed_ = true; |
81 if (!ref_count_) { | 97 if (!ref_count_) { |
82 if (type() == kParentEntry) { | 98 if (type() == kParentEntry) { |
83 // 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. |
84 if (children_.get()) { | 100 if (children_.get()) { |
85 EntryMap children; | 101 EntryMap children; |
86 children.swap(*children_); | 102 children.swap(*children_); |
87 for (EntryMap::iterator i = children.begin(); | 103 for (EntryMap::iterator i = children.begin(); |
88 i != children.end(); ++i) { | 104 i != children.end(); ++i) { |
89 // 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 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 if (index < 0 || index >= NUM_STREAMS) | 180 if (index < 0 || index >= NUM_STREAMS) |
165 return 0; | 181 return 0; |
166 return data_size_[index]; | 182 return data_size_[index]; |
167 } | 183 } |
168 | 184 |
169 int MemEntryImpl::ReadData(int index, int offset, IOBuffer* buf, int buf_len, | 185 int MemEntryImpl::ReadData(int index, int offset, IOBuffer* buf, int buf_len, |
170 const CompletionCallback& callback) { | 186 const CompletionCallback& callback) { |
171 if (net_log_.IsLoggingAllEvents()) { | 187 if (net_log_.IsLoggingAllEvents()) { |
172 net_log_.BeginEvent( | 188 net_log_.BeginEvent( |
173 net::NetLog::TYPE_ENTRY_READ_DATA, | 189 net::NetLog::TYPE_ENTRY_READ_DATA, |
174 make_scoped_refptr( | 190 CreateNetLogReadWriteDataCallback(index, offset, buf_len, false)); |
175 new ReadWriteDataParameters(index, offset, buf_len, false))); | |
176 } | 191 } |
177 | 192 |
178 int result = InternalReadData(index, offset, buf, buf_len); | 193 int result = InternalReadData(index, offset, buf, buf_len); |
179 | 194 |
180 if (net_log_.IsLoggingAllEvents()) { | 195 if (net_log_.IsLoggingAllEvents()) { |
181 net_log_.EndEvent( | 196 net_log_.EndEvent( |
182 net::NetLog::TYPE_ENTRY_READ_DATA, | 197 net::NetLog::TYPE_ENTRY_READ_DATA, |
183 make_scoped_refptr(new ReadWriteCompleteParameters(result))); | 198 CreateNetLogReadWriteCompleteCallback(result)); |
184 } | 199 } |
185 return result; | 200 return result; |
186 } | 201 } |
187 | 202 |
188 int MemEntryImpl::WriteData(int index, int offset, IOBuffer* buf, int buf_len, | 203 int MemEntryImpl::WriteData(int index, int offset, IOBuffer* buf, int buf_len, |
189 const CompletionCallback& callback, bool truncate) { | 204 const CompletionCallback& callback, bool truncate) { |
190 if (net_log_.IsLoggingAllEvents()) { | 205 if (net_log_.IsLoggingAllEvents()) { |
191 net_log_.BeginEvent( | 206 net_log_.BeginEvent( |
192 net::NetLog::TYPE_ENTRY_WRITE_DATA, | 207 net::NetLog::TYPE_ENTRY_WRITE_DATA, |
193 make_scoped_refptr( | 208 CreateNetLogReadWriteDataCallback(index, offset, buf_len, truncate)); |
194 new ReadWriteDataParameters(index, offset, buf_len, truncate))); | |
195 } | 209 } |
196 | 210 |
197 int result = InternalWriteData(index, offset, buf, buf_len, truncate); | 211 int result = InternalWriteData(index, offset, buf, buf_len, truncate); |
198 | 212 |
199 if (net_log_.IsLoggingAllEvents()) { | 213 if (net_log_.IsLoggingAllEvents()) { |
200 net_log_.EndEvent( | 214 net_log_.EndEvent( |
201 net::NetLog::TYPE_ENTRY_WRITE_DATA, | 215 net::NetLog::TYPE_ENTRY_WRITE_DATA, |
202 make_scoped_refptr(new ReadWriteCompleteParameters(result))); | 216 CreateNetLogReadWriteCompleteCallback(result)); |
203 } | 217 } |
204 return result; | 218 return result; |
205 } | 219 } |
206 | 220 |
207 int MemEntryImpl::ReadSparseData(int64 offset, IOBuffer* buf, int buf_len, | 221 int MemEntryImpl::ReadSparseData(int64 offset, IOBuffer* buf, int buf_len, |
208 const CompletionCallback& callback) { | 222 const CompletionCallback& callback) { |
209 if (net_log_.IsLoggingAllEvents()) { | 223 if (net_log_.IsLoggingAllEvents()) { |
210 net_log_.BeginEvent( | 224 net_log_.BeginEvent( |
211 net::NetLog::TYPE_SPARSE_READ, | 225 net::NetLog::TYPE_SPARSE_READ, |
212 make_scoped_refptr( | 226 CreateNetLogSparseOperationCallback(offset, buf_len)); |
213 new SparseOperationParameters(offset, buf_len))); | |
214 } | 227 } |
215 int result = InternalReadSparseData(offset, buf, buf_len); | 228 int result = InternalReadSparseData(offset, buf, buf_len); |
216 if (net_log_.IsLoggingAllEvents()) | 229 if (net_log_.IsLoggingAllEvents()) |
217 net_log_.EndEvent(net::NetLog::TYPE_SPARSE_READ, NULL); | 230 net_log_.EndEvent(net::NetLog::TYPE_SPARSE_READ); |
218 return result; | 231 return result; |
219 } | 232 } |
220 | 233 |
221 int MemEntryImpl::WriteSparseData(int64 offset, IOBuffer* buf, int buf_len, | 234 int MemEntryImpl::WriteSparseData(int64 offset, IOBuffer* buf, int buf_len, |
222 const CompletionCallback& callback) { | 235 const CompletionCallback& callback) { |
223 if (net_log_.IsLoggingAllEvents()) { | 236 if (net_log_.IsLoggingAllEvents()) { |
224 net_log_.BeginEvent(net::NetLog::TYPE_SPARSE_WRITE, | 237 net_log_.BeginEvent( |
225 make_scoped_refptr( | 238 net::NetLog::TYPE_SPARSE_WRITE, |
226 new SparseOperationParameters(offset, buf_len))); | 239 CreateNetLogSparseOperationCallback(offset, buf_len)); |
227 } | 240 } |
228 int result = InternalWriteSparseData(offset, buf, buf_len); | 241 int result = InternalWriteSparseData(offset, buf, buf_len); |
229 if (net_log_.IsLoggingAllEvents()) | 242 if (net_log_.IsLoggingAllEvents()) |
230 net_log_.EndEvent(net::NetLog::TYPE_SPARSE_WRITE, NULL); | 243 net_log_.EndEvent(net::NetLog::TYPE_SPARSE_WRITE); |
231 return result; | 244 return result; |
232 } | 245 } |
233 | 246 |
234 int MemEntryImpl::GetAvailableRange(int64 offset, int len, int64* start, | 247 int MemEntryImpl::GetAvailableRange(int64 offset, int len, int64* start, |
235 const CompletionCallback& callback) { | 248 const CompletionCallback& callback) { |
236 if (net_log_.IsLoggingAllEvents()) { | 249 if (net_log_.IsLoggingAllEvents()) { |
237 net_log_.BeginEvent( | 250 net_log_.BeginEvent( |
238 net::NetLog::TYPE_SPARSE_GET_RANGE, | 251 net::NetLog::TYPE_SPARSE_GET_RANGE, |
239 make_scoped_refptr( | 252 CreateNetLogSparseOperationCallback(offset, len)); |
240 new SparseOperationParameters(offset, len))); | |
241 } | 253 } |
242 int result = GetAvailableRange(offset, len, start); | 254 int result = GetAvailableRange(offset, len, start); |
243 if (net_log_.IsLoggingAllEvents()) { | 255 if (net_log_.IsLoggingAllEvents()) { |
244 net_log_.EndEvent( | 256 net_log_.EndEvent( |
245 net::NetLog::TYPE_SPARSE_GET_RANGE, | 257 net::NetLog::TYPE_SPARSE_GET_RANGE, |
246 make_scoped_refptr( | 258 CreateNetLogGetAvailableRangeResultCallback(*start, result)); |
247 new GetAvailableRangeResultParameters(*start, result))); | |
248 } | 259 } |
249 return result; | 260 return result; |
250 } | 261 } |
251 | 262 |
252 bool MemEntryImpl::CouldBeSparse() const { | 263 bool MemEntryImpl::CouldBeSparse() const { |
253 DCHECK_EQ(kParentEntry, type()); | 264 DCHECK_EQ(kParentEntry, type()); |
254 return (children_.get() != NULL); | 265 return (children_.get() != NULL); |
255 } | 266 } |
256 | 267 |
257 int MemEntryImpl::ReadyForSparseIO(const CompletionCallback& callback) { | 268 int MemEntryImpl::ReadyForSparseIO(const CompletionCallback& callback) { |
258 return net::OK; | 269 return net::OK; |
259 } | 270 } |
260 | 271 |
261 // ------------------------------------------------------------------------ | 272 // ------------------------------------------------------------------------ |
262 | 273 |
263 MemEntryImpl::~MemEntryImpl() { | 274 MemEntryImpl::~MemEntryImpl() { |
264 for (int i = 0; i < NUM_STREAMS; i++) | 275 for (int i = 0; i < NUM_STREAMS; i++) |
265 backend_->ModifyStorageSize(data_size_[i], 0); | 276 backend_->ModifyStorageSize(data_size_[i], 0); |
266 backend_->ModifyStorageSize(static_cast<int32>(key_.size()), 0); | 277 backend_->ModifyStorageSize(static_cast<int32>(key_.size()), 0); |
267 net_log_.EndEvent(net::NetLog::TYPE_DISK_CACHE_MEM_ENTRY_IMPL, NULL); | 278 net_log_.EndEvent(net::NetLog::TYPE_DISK_CACHE_MEM_ENTRY_IMPL); |
268 } | 279 } |
269 | 280 |
270 int MemEntryImpl::InternalReadData(int index, int offset, IOBuffer* buf, | 281 int MemEntryImpl::InternalReadData(int index, int offset, IOBuffer* buf, |
271 int buf_len) { | 282 int buf_len) { |
272 DCHECK(type() == kParentEntry || index == kSparseData); | 283 DCHECK(type() == kParentEntry || index == kSparseData); |
273 | 284 |
274 if (index < 0 || index >= NUM_STREAMS) | 285 if (index < 0 || index >= NUM_STREAMS) |
275 return net::ERR_INVALID_ARGUMENT; | 286 return net::ERR_INVALID_ARGUMENT; |
276 | 287 |
277 int entry_size = GetDataSize(index); | 288 int entry_size = GetDataSize(index); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 // We then need to prepare the child offset and len. | 368 // We then need to prepare the child offset and len. |
358 int child_offset = ToChildOffset(offset + io_buf->BytesConsumed()); | 369 int child_offset = ToChildOffset(offset + io_buf->BytesConsumed()); |
359 | 370 |
360 // If we are trying to read from a position that the child entry has no data | 371 // If we are trying to read from a position that the child entry has no data |
361 // we should stop. | 372 // we should stop. |
362 if (child_offset < child->child_first_pos_) | 373 if (child_offset < child->child_first_pos_) |
363 break; | 374 break; |
364 if (net_log_.IsLoggingAllEvents()) { | 375 if (net_log_.IsLoggingAllEvents()) { |
365 net_log_.BeginEvent( | 376 net_log_.BeginEvent( |
366 net::NetLog::TYPE_SPARSE_READ_CHILD_DATA, | 377 net::NetLog::TYPE_SPARSE_READ_CHILD_DATA, |
367 make_scoped_refptr(new SparseReadWriteParameters( | 378 CreateNetLogSparseReadWriteCallback(child->net_log().source(), |
368 child->net_log().source(), | 379 io_buf->BytesRemaining())); |
369 io_buf->BytesRemaining()))); | |
370 } | 380 } |
371 int ret = child->ReadData(kSparseData, child_offset, io_buf, | 381 int ret = child->ReadData(kSparseData, child_offset, io_buf, |
372 io_buf->BytesRemaining(), CompletionCallback()); | 382 io_buf->BytesRemaining(), CompletionCallback()); |
373 if (net_log_.IsLoggingAllEvents()) { | 383 if (net_log_.IsLoggingAllEvents()) { |
374 net_log_.EndEventWithNetErrorCode( | 384 net_log_.EndEventWithNetErrorCode( |
375 net::NetLog::TYPE_SPARSE_READ_CHILD_DATA, ret); | 385 net::NetLog::TYPE_SPARSE_READ_CHILD_DATA, ret); |
376 } | 386 } |
377 | 387 |
378 // If we encounter an error in one entry, return immediately. | 388 // If we encounter an error in one entry, return immediately. |
379 if (ret < 0) | 389 if (ret < 0) |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
415 // write and remaining capacity of this child entry. | 425 // write and remaining capacity of this child entry. |
416 int write_len = std::min(static_cast<int>(io_buf->BytesRemaining()), | 426 int write_len = std::min(static_cast<int>(io_buf->BytesRemaining()), |
417 kMaxSparseEntrySize - child_offset); | 427 kMaxSparseEntrySize - child_offset); |
418 | 428 |
419 // Keep a record of the last byte position (exclusive) in the child. | 429 // Keep a record of the last byte position (exclusive) in the child. |
420 int data_size = child->GetDataSize(kSparseData); | 430 int data_size = child->GetDataSize(kSparseData); |
421 | 431 |
422 if (net_log_.IsLoggingAllEvents()) { | 432 if (net_log_.IsLoggingAllEvents()) { |
423 net_log_.BeginEvent( | 433 net_log_.BeginEvent( |
424 net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA, | 434 net::NetLog::TYPE_SPARSE_WRITE_CHILD_DATA, |
425 make_scoped_refptr(new SparseReadWriteParameters( | 435 CreateNetLogSparseReadWriteCallback(child->net_log().source(), |
426 child->net_log().source(), | 436 write_len)); |
427 write_len))); | |
428 } | 437 } |
429 | 438 |
430 // Always writes to the child entry. This operation may overwrite data | 439 // Always writes to the child entry. This operation may overwrite data |
431 // previously written. | 440 // previously written. |
432 // TODO(hclam): if there is data in the entry and this write is not | 441 // TODO(hclam): if there is data in the entry and this write is not |
433 // continuous we may want to discard this write. | 442 // continuous we may want to discard this write. |
434 int ret = child->WriteData(kSparseData, child_offset, io_buf, write_len, | 443 int ret = child->WriteData(kSparseData, child_offset, io_buf, write_len, |
435 CompletionCallback(), true); | 444 CompletionCallback(), true); |
436 if (net_log_.IsLoggingAllEvents()) { | 445 if (net_log_.IsLoggingAllEvents()) { |
437 net_log_.EndEventWithNetErrorCode( | 446 net_log_.EndEventWithNetErrorCode( |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
548 | 557 |
549 bool MemEntryImpl::InitChildEntry(MemEntryImpl* parent, int child_id, | 558 bool MemEntryImpl::InitChildEntry(MemEntryImpl* parent, int child_id, |
550 net::NetLog* net_log) { | 559 net::NetLog* net_log) { |
551 DCHECK(!parent_); | 560 DCHECK(!parent_); |
552 DCHECK(!child_id_); | 561 DCHECK(!child_id_); |
553 | 562 |
554 net_log_ = net::BoundNetLog::Make(net_log, | 563 net_log_ = net::BoundNetLog::Make(net_log, |
555 net::NetLog::SOURCE_MEMORY_CACHE_ENTRY); | 564 net::NetLog::SOURCE_MEMORY_CACHE_ENTRY); |
556 net_log_.BeginEvent( | 565 net_log_.BeginEvent( |
557 net::NetLog::TYPE_DISK_CACHE_MEM_ENTRY_IMPL, | 566 net::NetLog::TYPE_DISK_CACHE_MEM_ENTRY_IMPL, |
558 make_scoped_refptr(new EntryCreationParameters( | 567 base::Bind(&NetLogChildEntryCreationCallback, parent, child_id_)); |
559 GenerateChildName(parent->key(), child_id_), | |
560 true))); | |
561 | 568 |
562 parent_ = parent; | 569 parent_ = parent; |
563 child_id_ = child_id; | 570 child_id_ = child_id; |
564 Time current = Time::Now(); | 571 Time current = Time::Now(); |
565 last_modified_ = current; | 572 last_modified_ = current; |
566 last_used_ = current; | 573 last_used_ = current; |
567 // Insert this to the backend's ranking list. | 574 // Insert this to the backend's ranking list. |
568 backend_->InsertIntoRankingList(this); | 575 backend_->InsertIntoRankingList(this); |
569 return true; | 576 return true; |
570 } | 577 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
614 scanned_len += kMaxSparseEntrySize - current_child_offset; | 621 scanned_len += kMaxSparseEntrySize - current_child_offset; |
615 } | 622 } |
616 return scanned_len; | 623 return scanned_len; |
617 } | 624 } |
618 | 625 |
619 void MemEntryImpl::DetachChild(int child_id) { | 626 void MemEntryImpl::DetachChild(int child_id) { |
620 children_->erase(child_id); | 627 children_->erase(child_id); |
621 } | 628 } |
622 | 629 |
623 } // namespace disk_cache | 630 } // namespace disk_cache |
OLD | NEW |