OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "net/base/net_errors.h" | 7 #include "net/base/net_errors.h" |
8 #include "net/disk_cache/mem_backend_impl.h" | 8 #include "net/disk_cache/mem_backend_impl.h" |
9 | 9 |
10 using base::Time; | 10 using base::Time; |
11 | 11 |
12 namespace disk_cache { | 12 namespace disk_cache { |
13 | 13 |
14 MemEntryImpl::MemEntryImpl(MemBackendImpl* backend) { | 14 MemEntryImpl::MemEntryImpl(MemBackendImpl* backend) { |
15 doomed_ = false; | 15 doomed_ = false; |
16 backend_ = backend; | 16 backend_ = backend; |
17 ref_count_ = 0; | 17 ref_count_ = 0; |
18 data_size_[0] = data_size_[1] = 0; | 18 for (int i = 0; i < NUM_STREAMS; i++) |
| 19 data_size_[i] = 0; |
19 } | 20 } |
20 | 21 |
21 MemEntryImpl::~MemEntryImpl() { | 22 MemEntryImpl::~MemEntryImpl() { |
22 backend_->ModifyStorageSize(data_size_[0], 0); | 23 for (int i = 0; i < NUM_STREAMS; i++) |
23 backend_->ModifyStorageSize(data_size_[1], 0); | 24 backend_->ModifyStorageSize(data_size_[i], 0); |
24 backend_->ModifyStorageSize(static_cast<int32>(key_.size()), 0); | 25 backend_->ModifyStorageSize(static_cast<int32>(key_.size()), 0); |
25 } | 26 } |
26 | 27 |
27 bool MemEntryImpl::CreateEntry(const std::string& key) { | 28 bool MemEntryImpl::CreateEntry(const std::string& key) { |
28 key_ = key; | 29 key_ = key; |
29 last_modified_ = Time::Now(); | 30 last_modified_ = Time::Now(); |
30 last_used_ = Time::Now(); | 31 last_used_ = Time::Now(); |
31 Open(); | 32 Open(); |
32 backend_->ModifyStorageSize(0, static_cast<int32>(key.size())); | 33 backend_->ModifyStorageSize(0, static_cast<int32>(key.size())); |
33 return true; | 34 return true; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 | 69 |
69 Time MemEntryImpl::GetLastUsed() const { | 70 Time MemEntryImpl::GetLastUsed() const { |
70 return last_used_; | 71 return last_used_; |
71 } | 72 } |
72 | 73 |
73 Time MemEntryImpl::GetLastModified() const { | 74 Time MemEntryImpl::GetLastModified() const { |
74 return last_modified_; | 75 return last_modified_; |
75 } | 76 } |
76 | 77 |
77 int32 MemEntryImpl::GetDataSize(int index) const { | 78 int32 MemEntryImpl::GetDataSize(int index) const { |
78 if (index < 0 || index > 1) | 79 if (index < 0 || index >= NUM_STREAMS) |
79 return 0; | 80 return 0; |
80 | 81 |
81 return data_size_[index]; | 82 return data_size_[index]; |
82 } | 83 } |
83 | 84 |
84 int MemEntryImpl::ReadData(int index, int offset, char* buf, int buf_len, | 85 int MemEntryImpl::ReadData(int index, int offset, char* buf, int buf_len, |
85 net::CompletionCallback* completion_callback) { | 86 net::CompletionCallback* completion_callback) { |
86 if (index < 0 || index > 1) | 87 if (index < 0 || index >= NUM_STREAMS) |
87 return net::ERR_INVALID_ARGUMENT; | 88 return net::ERR_INVALID_ARGUMENT; |
88 | 89 |
89 int entry_size = GetDataSize(index); | 90 int entry_size = GetDataSize(index); |
90 if (offset >= entry_size || offset < 0 || !buf_len) | 91 if (offset >= entry_size || offset < 0 || !buf_len) |
91 return 0; | 92 return 0; |
92 | 93 |
93 if (buf_len < 0) | 94 if (buf_len < 0) |
94 return net::ERR_INVALID_ARGUMENT; | 95 return net::ERR_INVALID_ARGUMENT; |
95 | 96 |
96 if (offset + buf_len > entry_size) | 97 if (offset + buf_len > entry_size) |
97 buf_len = entry_size - offset; | 98 buf_len = entry_size - offset; |
98 | 99 |
99 UpdateRank(false); | 100 UpdateRank(false); |
100 | 101 |
101 memcpy(buf , &(data_[index])[offset], buf_len); | 102 memcpy(buf , &(data_[index])[offset], buf_len); |
102 return buf_len; | 103 return buf_len; |
103 } | 104 } |
104 | 105 |
105 int MemEntryImpl::WriteData(int index, int offset, const char* buf, int buf_len, | 106 int MemEntryImpl::WriteData(int index, int offset, const char* buf, int buf_len, |
106 net::CompletionCallback* completion_callback, | 107 net::CompletionCallback* completion_callback, |
107 bool truncate) { | 108 bool truncate) { |
108 if (index < 0 || index > 1) | 109 if (index < 0 || index >= NUM_STREAMS) |
109 return net::ERR_INVALID_ARGUMENT; | 110 return net::ERR_INVALID_ARGUMENT; |
110 | 111 |
111 if (offset < 0 || buf_len < 0) | 112 if (offset < 0 || buf_len < 0) |
112 return net::ERR_INVALID_ARGUMENT; | 113 return net::ERR_INVALID_ARGUMENT; |
113 | 114 |
114 int max_file_size = backend_->MaxFileSize(); | 115 int max_file_size = backend_->MaxFileSize(); |
115 | 116 |
116 // offset of buf_len could be negative numbers. | 117 // offset of buf_len could be negative numbers. |
117 if (offset > max_file_size || buf_len > max_file_size || | 118 if (offset > max_file_size || buf_len > max_file_size || |
118 offset + buf_len > max_file_size) { | 119 offset + buf_len > max_file_size) { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 | 170 |
170 if (modified) | 171 if (modified) |
171 last_modified_ = current; | 172 last_modified_ = current; |
172 | 173 |
173 if (!doomed_) | 174 if (!doomed_) |
174 backend_->UpdateRank(this); | 175 backend_->UpdateRank(this); |
175 } | 176 } |
176 | 177 |
177 } // namespace disk_cache | 178 } // namespace disk_cache |
178 | 179 |
OLD | NEW |