| 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/dns/host_cache.h" | 5 #include "net/dns/host_cache.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 DCHECK_GT(max_entries_, size()); | 241 DCHECK_GT(max_entries_, size()); |
| 242 DCHECK_EQ(0u, entries_.count(key)); | 242 DCHECK_EQ(0u, entries_.count(key)); |
| 243 entries_.insert(std::make_pair(key, entry)); | 243 entries_.insert(std::make_pair(key, entry)); |
| 244 DCHECK_GE(max_entries_, size()); | 244 DCHECK_GE(max_entries_, size()); |
| 245 } | 245 } |
| 246 | 246 |
| 247 void HostCache::OnNetworkChange() { | 247 void HostCache::OnNetworkChange() { |
| 248 ++network_changes_; | 248 ++network_changes_; |
| 249 } | 249 } |
| 250 | 250 |
| 251 void HostCache::set_persistence_delegate(PersistenceDelegate* delegate) { |
| 252 // A PersistenceDelegate shouldn't be added if there already was one, and |
| 253 // shouldn't be removed (by setting to nullptr) if it wasn't previously there. |
| 254 DCHECK_NE(delegate == nullptr, delegate_ == nullptr); |
| 255 delegate_ = delegate; |
| 256 } |
| 257 |
| 251 void HostCache::clear() { | 258 void HostCache::clear() { |
| 252 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); | 259 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 253 RecordEraseAll(ERASE_CLEAR, base::TimeTicks::Now()); | 260 RecordEraseAll(ERASE_CLEAR, base::TimeTicks::Now()); |
| 254 | 261 |
| 255 // Don't bother scheduling a write if there's nothing to clear. | 262 // Don't bother scheduling a write if there's nothing to clear. |
| 256 if (size() == 0) | 263 if (size() == 0) |
| 257 return; | 264 return; |
| 258 | 265 |
| 259 entries_.clear(); | 266 entries_.clear(); |
| 260 if (delegate_) | 267 if (delegate_) |
| (...skipping 20 matching lines...) Expand all Loading... |
| 281 changed = true; | 288 changed = true; |
| 282 } | 289 } |
| 283 | 290 |
| 284 it = next_it; | 291 it = next_it; |
| 285 } | 292 } |
| 286 | 293 |
| 287 if (delegate_ && changed) | 294 if (delegate_ && changed) |
| 288 delegate_->ScheduleWrite(); | 295 delegate_->ScheduleWrite(); |
| 289 } | 296 } |
| 290 | 297 |
| 291 std::unique_ptr<base::ListValue> HostCache::GetAsListValue( | 298 void HostCache::GetAsListValue(base::ListValue* entry_list, |
| 292 bool include_staleness) const { | 299 bool include_staleness) const { |
| 293 std::unique_ptr<base::ListValue> entry_list(new base::ListValue()); | 300 DCHECK(entry_list); |
| 301 entry_list->Clear(); |
| 294 | 302 |
| 295 for (const auto& pair : entries_) { | 303 for (const auto& pair : entries_) { |
| 296 const Key& key = pair.first; | 304 const Key& key = pair.first; |
| 297 const Entry& entry = pair.second; | 305 const Entry& entry = pair.second; |
| 298 | 306 |
| 299 std::unique_ptr<base::DictionaryValue> entry_dict( | 307 std::unique_ptr<base::DictionaryValue> entry_dict( |
| 300 new base::DictionaryValue()); | 308 new base::DictionaryValue()); |
| 301 | 309 |
| 302 entry_dict->SetString(kHostnameKey, key.hostname); | 310 entry_dict->SetString(kHostnameKey, key.hostname); |
| 303 entry_dict->SetInteger(kAddressFamilyKey, | 311 entry_dict->SetInteger(kAddressFamilyKey, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 325 const AddressList& addresses = entry.addresses(); | 333 const AddressList& addresses = entry.addresses(); |
| 326 // Append all of the resolved addresses. | 334 // Append all of the resolved addresses. |
| 327 auto addresses_value = base::MakeUnique<base::ListValue>(); | 335 auto addresses_value = base::MakeUnique<base::ListValue>(); |
| 328 for (size_t i = 0; i < addresses.size(); ++i) | 336 for (size_t i = 0; i < addresses.size(); ++i) |
| 329 addresses_value->AppendString(addresses[i].ToStringWithoutPort()); | 337 addresses_value->AppendString(addresses[i].ToStringWithoutPort()); |
| 330 entry_dict->SetList(kAddressesKey, std::move(addresses_value)); | 338 entry_dict->SetList(kAddressesKey, std::move(addresses_value)); |
| 331 } | 339 } |
| 332 | 340 |
| 333 entry_list->Append(std::move(entry_dict)); | 341 entry_list->Append(std::move(entry_dict)); |
| 334 } | 342 } |
| 335 | |
| 336 return entry_list; | |
| 337 } | 343 } |
| 338 | 344 |
| 339 // TODO(mgersh): Add histograms to track failures. | 345 // TODO(mgersh): Add histograms to track failures. |
| 340 bool HostCache::RestoreFromListValue(const base::ListValue& old_cache) { | 346 bool HostCache::RestoreFromListValue(const base::ListValue& old_cache) { |
| 341 for (auto it = old_cache.begin(); it != old_cache.end(); it++) { | 347 for (auto it = old_cache.begin(); it != old_cache.end(); it++) { |
| 342 const base::DictionaryValue* entry_dict; | 348 const base::DictionaryValue* entry_dict; |
| 343 if (!it->GetAsDictionary(&entry_dict)) | 349 if (!it->GetAsDictionary(&entry_dict)) |
| 344 return false; | 350 return false; |
| 345 | 351 |
| 346 std::string hostname; | 352 std::string hostname; |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 530 CACHE_HISTOGRAM_TIME("EraseValid.ValidFor", -stale.expired_by); | 536 CACHE_HISTOGRAM_TIME("EraseValid.ValidFor", -stale.expired_by); |
| 531 } | 537 } |
| 532 } | 538 } |
| 533 | 539 |
| 534 void HostCache::RecordEraseAll(EraseReason reason, base::TimeTicks now) { | 540 void HostCache::RecordEraseAll(EraseReason reason, base::TimeTicks now) { |
| 535 for (const auto& it : entries_) | 541 for (const auto& it : entries_) |
| 536 RecordErase(reason, now, it.second); | 542 RecordErase(reason, now, it.second); |
| 537 } | 543 } |
| 538 | 544 |
| 539 } // namespace net | 545 } // namespace net |
| OLD | NEW |