1 // Copyright 2016 The Chromium Authors
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_quality_store.h"
6
7 #include "base/functional/bind.h"
8 #include "base/location.h"
9 #include "base/observer_list.h"
10 #include "base/task/single_thread_task_runner.h"
11 #include "net/base/network_change_notifier.h"
12
13 namespace net::nqe::internal {
14
NetworkQualityStore()15 NetworkQualityStore::NetworkQualityStore() {
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 <= 20,
21 "Size of the network quality cache must <= 20");
22 }
23
~NetworkQualityStore()24 NetworkQualityStore::~NetworkQualityStore() {
25 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
26 }
27
Add(const nqe::internal::NetworkID & network_id,const nqe::internal::CachedNetworkQuality & cached_network_quality)28 void NetworkQualityStore::Add(
29 const nqe::internal::NetworkID& network_id,
30 const nqe::internal::CachedNetworkQuality& cached_network_quality) {
31 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
32 DCHECK_LE(cached_network_qualities_.size(),
33 static_cast<size_t>(kMaximumNetworkQualityCacheSize));
34
35 if (cached_network_quality.effective_connection_type() ==
36 EFFECTIVE_CONNECTION_TYPE_UNKNOWN) {
37 return;
38 }
39
40 // Remove the entry from the map, if it is already present.
41 cached_network_qualities_.erase(network_id);
42
43 if (cached_network_qualities_.size() == kMaximumNetworkQualityCacheSize) {
44 // Remove the oldest entry.
45 auto oldest_entry_iterator = cached_network_qualities_.begin();
46
47 for (auto it = cached_network_qualities_.begin();
48 it != cached_network_qualities_.end(); ++it) {
49 if ((it->second).OlderThan(oldest_entry_iterator->second))
50 oldest_entry_iterator = it;
51 }
52 cached_network_qualities_.erase(oldest_entry_iterator);
53 }
54
55 cached_network_qualities_.insert(
56 std::make_pair(network_id, cached_network_quality));
57 DCHECK_LE(cached_network_qualities_.size(),
58 static_cast<size_t>(kMaximumNetworkQualityCacheSize));
59
60 for (auto& observer : network_qualities_cache_observer_list_)
61 observer.OnChangeInCachedNetworkQuality(network_id, cached_network_quality);
62 }
63
GetById(const nqe::internal::NetworkID & network_id,nqe::internal::CachedNetworkQuality * cached_network_quality) const64 bool NetworkQualityStore::GetById(
65 const nqe::internal::NetworkID& network_id,
66 nqe::internal::CachedNetworkQuality* cached_network_quality) const {
67 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
68
69 // First check if an exact match can be found.
70 for (const auto& cached_quality : cached_network_qualities_) {
71 if (network_id.type != cached_quality.first.type ||
72 network_id.id != cached_quality.first.id) {
73 // The |type| and |id| must match.
74 continue;
75 }
76
77 // Check for an exact match, and return immediately if one is found.
78 // It's possible that the current network does not have signal strength
79 // available. In that case, return the cached network quality when the
80 // signal strength was unavailable.
81 if (network_id.signal_strength == cached_quality.first.signal_strength) {
82 *cached_network_quality = cached_quality.second;
83 return true;
84 }
85 }
86
87 // Handle the case when current network does not have signal strength
88 // available. Return the cached network quality that corresponds to the
89 // highest signal strength. This ensures that the method returns the fastest
90 // network quality possible for the current network, and serves as a
91 // conservative estimate.
92 if (network_id.signal_strength == INT32_MIN) {
93 auto matching_it = cached_network_qualities_.end();
94
95 for (auto it = cached_network_qualities_.begin();
96 it != cached_network_qualities_.end(); ++it) {
97 if (network_id.type != it->first.type || network_id.id != it->first.id) {
98 // The |type| and |id| must match.
99 continue;
100 }
101
102 // The cached network must have signal strength available. If the cached
103 // signal strength is unavailable, then this case would have been handled
104 // above.
105 DCHECK_NE(INT32_MIN, it->first.signal_strength);
106
107 if (matching_it == cached_network_qualities_.end() ||
108 it->first.signal_strength > matching_it->first.signal_strength) {
109 matching_it = it;
110 }
111 }
112
113 if (matching_it == cached_network_qualities_.end())
114 return false;
115
116 *cached_network_quality = matching_it->second;
117 return true;
118 }
119
120 // Finally, handle the case where the current network has a valid signal
121 // strength, but there is no exact match.
122
123 // |matching_it| points to the entry that has the same connection type and
124 // id as |network_id|, and has the signal strength closest to the signal
125 // stength of |network_id|.
126 auto matching_it = cached_network_qualities_.end();
127 int matching_it_diff_signal_strength = INT32_MAX;
128
129 // Find the closest estimate.
130 for (auto it = cached_network_qualities_.begin();
131 it != cached_network_qualities_.end(); ++it) {
132 if (network_id.type != it->first.type || network_id.id != it->first.id) {
133 // The |type| and |id| must match.
134 continue;
135 }
136
137 DCHECK_LE(0, network_id.signal_strength);
138
139 // Determine if the signal strength of |network_id| is closer to the
140 // signal strength of the network at |it| then that of the network at
141 // |matching_it|.
142 int diff_signal_strength =
143 std::abs(network_id.signal_strength - it->first.signal_strength);
144 if (it->first.signal_strength == INT32_MIN) {
145 // Current network has signal strength available. However, the persisted
146 // network does not. Set the |diff_signal_strength| to INT32_MAX. This
147 // ensures that if an entry with a valid signal strength is found later
148 // during iteration, then that entry will be used. If no entry with valid
149 // signal strength is found, then this entry will be used.
150 diff_signal_strength = INT32_MAX;
151 }
152
153 if (matching_it == cached_network_qualities_.end() ||
154 diff_signal_strength < matching_it_diff_signal_strength) {
155 matching_it = it;
156 matching_it_diff_signal_strength = diff_signal_strength;
157 }
158 }
159
160 if (matching_it == cached_network_qualities_.end())
161 return false;
162
163 *cached_network_quality = matching_it->second;
164 return true;
165 }
166
AddNetworkQualitiesCacheObserver(NetworkQualitiesCacheObserver * observer)167 void NetworkQualityStore::AddNetworkQualitiesCacheObserver(
168 NetworkQualitiesCacheObserver* observer) {
169 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
170 network_qualities_cache_observer_list_.AddObserver(observer);
171
172 // Notify the |observer| on the next message pump since |observer| may not
173 // be completely set up for receiving the callbacks.
174 base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
175 FROM_HERE,
176 base::BindOnce(&NetworkQualityStore::NotifyCacheObserverIfPresent,
177 weak_ptr_factory_.GetWeakPtr(),
178 base::UnsafeDangling(observer)));
179 }
180
RemoveNetworkQualitiesCacheObserver(NetworkQualitiesCacheObserver * observer)181 void NetworkQualityStore::RemoveNetworkQualitiesCacheObserver(
182 NetworkQualitiesCacheObserver* observer) {
183 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
184 network_qualities_cache_observer_list_.RemoveObserver(observer);
185 }
186
NotifyCacheObserverIfPresent(MayBeDangling<NetworkQualitiesCacheObserver> observer) const187 void NetworkQualityStore::NotifyCacheObserverIfPresent(
188 MayBeDangling<NetworkQualitiesCacheObserver> observer) const {
189 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
190
191 if (!network_qualities_cache_observer_list_.HasObserver(observer))
192 return;
193 for (const auto& it : cached_network_qualities_)
194 observer->OnChangeInCachedNetworkQuality(it.first, it.second);
195 }
196
197 } // namespace net::nqe::internal
198