Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/nqe/network_qualities_manager.h" | |
| 6 | |
| 7 #include "net/base/network_change_notifier.h" | |
| 8 | |
| 9 namespace net { | |
| 10 | |
| 11 namespace nqe { | |
| 12 | |
| 13 namespace internal { | |
| 14 | |
| 15 NetworkQualitiesManager::NetworkQualitiesManager() { | |
|
bengr
2016/07/19 01:04:35
Why not just use a map? This class seems like over
tbansal1
2016/07/20 00:08:47
It will soon become complex enough: It will intera
| |
| 16 static_assert(kMaximumNetworkQualityCacheSize > 0, | |
| 17 "Size of the network quality cache must be > 0"); | |
| 18 // This limit should not be increased unless the logic for removing the | |
| 19 // oldest cache entry is rewritten to use a doubly-linked-list LRU queue. | |
| 20 static_assert(kMaximumNetworkQualityCacheSize <= 10, | |
| 21 "Size of the network quality cache must <= 10"); | |
| 22 } | |
| 23 | |
| 24 NetworkQualitiesManager::~NetworkQualitiesManager() { | |
| 25 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 26 } | |
| 27 | |
| 28 void NetworkQualitiesManager::CacheNetworkQualityEstimate( | |
| 29 const nqe::internal::NetworkID& network_id, | |
| 30 const nqe::internal::CachedNetworkQuality& cached_network_quality) { | |
| 31 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 32 DCHECK_LE(cached_network_qualities_.size(), | |
| 33 static_cast<size_t>(kMaximumNetworkQualityCacheSize)); | |
| 34 | |
| 35 // If the network name is unavailable, caching should not be performed. | |
| 36 if (network_id.type != net::NetworkChangeNotifier::CONNECTION_ETHERNET && | |
| 37 network_id.id.empty()) { | |
| 38 return; | |
| 39 } | |
| 40 | |
| 41 // Remove the entry from the map, if it is already present. | |
| 42 cached_network_qualities_.erase(network_id); | |
| 43 | |
| 44 if (cached_network_qualities_.size() == kMaximumNetworkQualityCacheSize) { | |
| 45 // Remove the oldest entry. | |
| 46 CachedNetworkQualities::iterator oldest_entry_iterator = | |
| 47 cached_network_qualities_.begin(); | |
| 48 | |
| 49 for (CachedNetworkQualities::iterator it = | |
| 50 cached_network_qualities_.begin(); | |
| 51 it != cached_network_qualities_.end(); ++it) { | |
| 52 if ((it->second).OlderThan(oldest_entry_iterator->second)) | |
| 53 oldest_entry_iterator = it; | |
| 54 } | |
| 55 cached_network_qualities_.erase(oldest_entry_iterator); | |
| 56 } | |
| 57 | |
| 58 cached_network_qualities_.insert( | |
| 59 std::make_pair(network_id, cached_network_quality)); | |
| 60 DCHECK_LE(cached_network_qualities_.size(), | |
| 61 static_cast<size_t>(kMaximumNetworkQualityCacheSize)); | |
| 62 } | |
| 63 | |
| 64 bool NetworkQualitiesManager::GetCachedNetworkQualityEstimate( | |
| 65 const nqe::internal::NetworkID& network_id, | |
| 66 nqe::internal::CachedNetworkQuality* cached_network_quality) { | |
| 67 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 68 | |
| 69 CachedNetworkQualities::const_iterator it = | |
| 70 cached_network_qualities_.find(network_id); | |
| 71 | |
| 72 if (it == cached_network_qualities_.end()) | |
| 73 return false; | |
| 74 | |
| 75 *cached_network_quality = it->second; | |
| 76 return true; | |
| 77 } | |
| 78 | |
| 79 } // namespace internal | |
| 80 | |
| 81 } // namespace nqe | |
| 82 | |
| 83 } // namespace net | |
| OLD | NEW |