| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/sdch/sdch_owner.h" | 5 #include "net/sdch/sdch_owner.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/metrics/histogram_macros.h" | 8 #include "base/metrics/histogram_macros.h" |
| 9 #include "base/time/default_clock.h" | 9 #include "base/time/default_clock.h" |
| 10 #include "net/base/sdch_manager.h" | 10 #include "net/base/sdch_manager.h" |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 fetcher_(context, | 79 fetcher_(context, |
| 80 base::Bind(&SdchOwner::OnDictionaryFetched, | 80 base::Bind(&SdchOwner::OnDictionaryFetched, |
| 81 // Because |fetcher_| is owned by SdchOwner, the | 81 // Because |fetcher_| is owned by SdchOwner, the |
| 82 // SdchOwner object will be available for the lifetime | 82 // SdchOwner object will be available for the lifetime |
| 83 // of |fetcher_|. | 83 // of |fetcher_|. |
| 84 base::Unretained(this))), | 84 base::Unretained(this))), |
| 85 total_dictionary_bytes_(0), | 85 total_dictionary_bytes_(0), |
| 86 clock_(new base::DefaultClock), | 86 clock_(new base::DefaultClock), |
| 87 max_total_dictionary_size_(kMaxTotalDictionarySize), | 87 max_total_dictionary_size_(kMaxTotalDictionarySize), |
| 88 min_space_for_dictionary_fetch_(kMinSpaceForDictionaryFetch), | 88 min_space_for_dictionary_fetch_(kMinSpaceForDictionaryFetch), |
| 89 #if defined(OS_CHROMEOS) |
| 90 // For debugging http://crbug.com/454198; remove when resolved. |
| 91 destroyed_(0), |
| 92 #endif |
| 89 memory_pressure_listener_( | 93 memory_pressure_listener_( |
| 90 base::Bind(&SdchOwner::OnMemoryPressure, | 94 base::Bind(&SdchOwner::OnMemoryPressure, |
| 91 // Because |memory_pressure_listener_| is owned by | 95 // Because |memory_pressure_listener_| is owned by |
| 92 // SdchOwner, the SdchOwner object will be available | 96 // SdchOwner, the SdchOwner object will be available |
| 93 // for the lifetime of |memory_pressure_listener_|. | 97 // for the lifetime of |memory_pressure_listener_|. |
| 94 base::Unretained(this))) { | 98 base::Unretained(this))) { |
| 99 #if defined(OS_CHROMEOS) |
| 100 // For debugging http://crbug.com/454198; remove when resolved. |
| 101 CHECK(clock_.get()); |
| 102 #endif |
| 95 manager_->AddObserver(this); | 103 manager_->AddObserver(this); |
| 96 } | 104 } |
| 97 | 105 |
| 98 SdchOwner::~SdchOwner() { | 106 SdchOwner::~SdchOwner() { |
| 107 #if defined(OS_CHROMEOS) |
| 108 // For debugging http://crbug.com/454198; remove when resolved. |
| 109 CHECK_EQ(0u, destroyed_); |
| 110 CHECK(clock_.get()); |
| 111 clock_.reset(); |
| 112 #endif |
| 113 |
| 99 for (auto it = local_dictionary_info_.begin(); | 114 for (auto it = local_dictionary_info_.begin(); |
| 100 it != local_dictionary_info_.end(); ++it) { | 115 it != local_dictionary_info_.end(); ++it) { |
| 101 RecordDictionaryEviction(it->second.use_count, | 116 RecordDictionaryEviction(it->second.use_count, |
| 102 DICTIONARY_FATE_EVICT_FOR_DESTRUCTION); | 117 DICTIONARY_FATE_EVICT_FOR_DESTRUCTION); |
| 103 } | 118 } |
| 104 manager_->RemoveObserver(this); | 119 manager_->RemoveObserver(this); |
| 120 #if defined(OS_CHROMEOS) |
| 121 destroyed_ = 0xdeadbeef; |
| 122 #endif |
| 105 } | 123 } |
| 106 | 124 |
| 107 void SdchOwner::SetMaxTotalDictionarySize(size_t max_total_dictionary_size) { | 125 void SdchOwner::SetMaxTotalDictionarySize(size_t max_total_dictionary_size) { |
| 108 max_total_dictionary_size_ = max_total_dictionary_size; | 126 max_total_dictionary_size_ = max_total_dictionary_size; |
| 109 } | 127 } |
| 110 | 128 |
| 111 void SdchOwner::SetMinSpaceForDictionaryFetch( | 129 void SdchOwner::SetMinSpaceForDictionaryFetch( |
| 112 size_t min_space_for_dictionary_fetch) { | 130 size_t min_space_for_dictionary_fetch) { |
| 113 min_space_for_dictionary_fetch_ = min_space_for_dictionary_fetch; | 131 min_space_for_dictionary_fetch_ = min_space_for_dictionary_fetch; |
| 114 } | 132 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 131 server_hash(server_hash), | 149 server_hash(server_hash), |
| 132 use_count(use_count), | 150 use_count(use_count), |
| 133 dictionary_size(dictionary_size) {} | 151 dictionary_size(dictionary_size) {} |
| 134 DictionaryItem(const DictionaryItem& rhs) = default; | 152 DictionaryItem(const DictionaryItem& rhs) = default; |
| 135 DictionaryItem& operator=(const DictionaryItem& rhs) = default; | 153 DictionaryItem& operator=(const DictionaryItem& rhs) = default; |
| 136 bool operator<(const DictionaryItem& rhs) const { | 154 bool operator<(const DictionaryItem& rhs) const { |
| 137 return last_used < rhs.last_used; | 155 return last_used < rhs.last_used; |
| 138 } | 156 } |
| 139 }; | 157 }; |
| 140 | 158 |
| 159 #if defined(OS_CHROMEOS) |
| 160 // For debugging http://crbug.com/454198; remove when resolved. |
| 161 CHECK_EQ(0u, destroyed_); |
| 162 CHECK(clock_.get()); |
| 163 #endif |
| 164 |
| 141 std::vector<DictionaryItem> stale_dictionary_list; | 165 std::vector<DictionaryItem> stale_dictionary_list; |
| 142 size_t recoverable_bytes = 0; | 166 size_t recoverable_bytes = 0; |
| 143 base::Time stale_boundary(clock_->Now() - base::TimeDelta::FromDays(1)); | 167 base::Time stale_boundary(clock_->Now() - base::TimeDelta::FromDays(1)); |
| 144 for (auto used_it = local_dictionary_info_.begin(); | 168 for (auto used_it = local_dictionary_info_.begin(); |
| 145 used_it != local_dictionary_info_.end(); ++used_it) { | 169 used_it != local_dictionary_info_.end(); ++used_it) { |
| 146 if (used_it->second.last_used < stale_boundary) { | 170 if (used_it->second.last_used < stale_boundary) { |
| 147 stale_dictionary_list.push_back( | 171 stale_dictionary_list.push_back( |
| 148 DictionaryItem(used_it->second.last_used, used_it->first, | 172 DictionaryItem(used_it->second.last_used, used_it->first, |
| 149 used_it->second.use_count, used_it->second.size)); | 173 used_it->second.use_count, used_it->second.size)); |
| 150 recoverable_bytes += used_it->second.size; | 174 recoverable_bytes += used_it->second.size; |
| 151 } | 175 } |
| 152 } | 176 } |
| 153 | 177 |
| 178 #if defined(OS_CHROMEOS) |
| 179 // For debugging http://crbug.com/454198; remove when resolved. |
| 180 CHECK_EQ(0u, destroyed_); |
| 181 CHECK(clock_.get()); |
| 182 #endif |
| 183 |
| 154 if (total_dictionary_bytes_ + dictionary_text.size() - recoverable_bytes > | 184 if (total_dictionary_bytes_ + dictionary_text.size() - recoverable_bytes > |
| 155 max_total_dictionary_size_) { | 185 max_total_dictionary_size_) { |
| 156 RecordDictionaryFate(DICTIONARY_FATE_FETCH_IGNORED_NO_SPACE); | 186 RecordDictionaryFate(DICTIONARY_FATE_FETCH_IGNORED_NO_SPACE); |
| 157 net::SdchManager::SdchErrorRecovery(SDCH_DICTIONARY_NO_ROOM); | 187 net::SdchManager::SdchErrorRecovery(SDCH_DICTIONARY_NO_ROOM); |
| 158 net_log.AddEvent(net::NetLog::TYPE_SDCH_DICTIONARY_ERROR, | 188 net_log.AddEvent(net::NetLog::TYPE_SDCH_DICTIONARY_ERROR, |
| 159 base::Bind(&net::NetLogSdchDictionaryFetchProblemCallback, | 189 base::Bind(&net::NetLogSdchDictionaryFetchProblemCallback, |
| 160 SDCH_DICTIONARY_NO_ROOM, dictionary_url, true)); | 190 SDCH_DICTIONARY_NO_ROOM, dictionary_url, true)); |
| 161 return; | 191 return; |
| 162 } | 192 } |
| 163 | 193 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 191 RecordDictionaryFate(DICTIONARY_FATE_ADDED); | 221 RecordDictionaryFate(DICTIONARY_FATE_ADDED); |
| 192 | 222 |
| 193 DCHECK(local_dictionary_info_.end() == | 223 DCHECK(local_dictionary_info_.end() == |
| 194 local_dictionary_info_.find(server_hash)); | 224 local_dictionary_info_.find(server_hash)); |
| 195 total_dictionary_bytes_ += dictionary_text.size(); | 225 total_dictionary_bytes_ += dictionary_text.size(); |
| 196 local_dictionary_info_[server_hash] = DictionaryInfo( | 226 local_dictionary_info_[server_hash] = DictionaryInfo( |
| 197 // Set the time last used to something to avoid thrashing, but not recent, | 227 // Set the time last used to something to avoid thrashing, but not recent, |
| 198 // to avoid taking too much time/space with useless dictionaries/one-off | 228 // to avoid taking too much time/space with useless dictionaries/one-off |
| 199 // visits to web sites. | 229 // visits to web sites. |
| 200 clock_->Now() - base::TimeDelta::FromHours(23), dictionary_text.size()); | 230 clock_->Now() - base::TimeDelta::FromHours(23), dictionary_text.size()); |
| 231 |
| 232 #if defined(OS_CHROMEOS) |
| 233 // For debugging http://crbug.com/454198; remove when resolved. |
| 234 CHECK_EQ(0u, destroyed_); |
| 235 CHECK(clock_.get()); |
| 236 #endif |
| 201 } | 237 } |
| 202 | 238 |
| 203 void SdchOwner::OnDictionaryUsed(SdchManager* manager, | 239 void SdchOwner::OnDictionaryUsed(SdchManager* manager, |
| 204 const std::string& server_hash) { | 240 const std::string& server_hash) { |
| 205 auto it = local_dictionary_info_.find(server_hash); | 241 auto it = local_dictionary_info_.find(server_hash); |
| 206 DCHECK(local_dictionary_info_.end() != it); | 242 DCHECK(local_dictionary_info_.end() != it); |
| 207 | 243 |
| 208 it->second.last_used = clock_->Now(); | 244 it->second.last_used = clock_->Now(); |
| 209 it->second.use_count++; | 245 it->second.use_count++; |
| 210 } | 246 } |
| 211 | 247 |
| 212 void SdchOwner::OnGetDictionary(net::SdchManager* manager, | 248 void SdchOwner::OnGetDictionary(net::SdchManager* manager, |
| 213 const GURL& request_url, | 249 const GURL& request_url, |
| 214 const GURL& dictionary_url) { | 250 const GURL& dictionary_url) { |
| 251 #if defined(OS_CHROMEOS) |
| 252 // For debugging http://crbug.com/454198; remove when resolved. |
| 253 CHECK_EQ(0u, destroyed_); |
| 254 CHECK(clock_.get()); |
| 255 #endif |
| 256 |
| 215 base::Time stale_boundary(clock_->Now() - base::TimeDelta::FromDays(1)); | 257 base::Time stale_boundary(clock_->Now() - base::TimeDelta::FromDays(1)); |
| 216 size_t avail_bytes = 0; | 258 size_t avail_bytes = 0; |
| 217 for (auto it = local_dictionary_info_.begin(); | 259 for (auto it = local_dictionary_info_.begin(); |
| 218 it != local_dictionary_info_.end(); ++it) { | 260 it != local_dictionary_info_.end(); ++it) { |
| 219 if (it->second.last_used < stale_boundary) | 261 if (it->second.last_used < stale_boundary) |
| 220 avail_bytes += it->second.size; | 262 avail_bytes += it->second.size; |
| 221 } | 263 } |
| 222 | 264 |
| 223 // Don't initiate the fetch if we wouldn't be able to store any | 265 // Don't initiate the fetch if we wouldn't be able to store any |
| 224 // reasonable dictionary. | 266 // reasonable dictionary. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 238 } | 280 } |
| 239 | 281 |
| 240 void SdchOwner::OnClearDictionaries(net::SdchManager* manager) { | 282 void SdchOwner::OnClearDictionaries(net::SdchManager* manager) { |
| 241 total_dictionary_bytes_ = 0; | 283 total_dictionary_bytes_ = 0; |
| 242 local_dictionary_info_.clear(); | 284 local_dictionary_info_.clear(); |
| 243 fetcher_.Cancel(); | 285 fetcher_.Cancel(); |
| 244 } | 286 } |
| 245 | 287 |
| 246 void SdchOwner::SetClockForTesting(scoped_ptr<base::Clock> clock) { | 288 void SdchOwner::SetClockForTesting(scoped_ptr<base::Clock> clock) { |
| 247 clock_ = clock.Pass(); | 289 clock_ = clock.Pass(); |
| 290 |
| 291 #if defined(OS_CHROMEOS) |
| 292 // For debugging http://crbug.com/454198; remove when resolved. |
| 293 CHECK_EQ(0u, destroyed_); |
| 294 CHECK(clock_.get()); |
| 295 #endif |
| 248 } | 296 } |
| 249 | 297 |
| 250 void SdchOwner::OnMemoryPressure( | 298 void SdchOwner::OnMemoryPressure( |
| 251 base::MemoryPressureListener::MemoryPressureLevel level) { | 299 base::MemoryPressureListener::MemoryPressureLevel level) { |
| 252 DCHECK_NE(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, level); | 300 DCHECK_NE(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, level); |
| 253 | 301 |
| 254 for (auto it = local_dictionary_info_.begin(); | 302 for (auto it = local_dictionary_info_.begin(); |
| 255 it != local_dictionary_info_.end(); ++it) { | 303 it != local_dictionary_info_.end(); ++it) { |
| 256 RecordDictionaryEviction(it->second.use_count, | 304 RecordDictionaryEviction(it->second.use_count, |
| 257 DICTIONARY_FATE_EVICT_FOR_MEMORY); | 305 DICTIONARY_FATE_EVICT_FOR_MEMORY); |
| 258 } | 306 } |
| 259 | 307 |
| 260 // TODO(rdsmith): Make a distinction between moderate and critical | 308 // TODO(rdsmith): Make a distinction between moderate and critical |
| 261 // memory pressure. | 309 // memory pressure. |
| 262 manager_->ClearData(); | 310 manager_->ClearData(); |
| 263 } | 311 } |
| 264 | 312 |
| 265 } // namespace net | 313 } // namespace net |
| OLD | NEW |