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_.emplace(network_id, cached_network_quality);
56 DCHECK_LE(cached_network_qualities_.size(),
57 static_cast<size_t>(kMaximumNetworkQualityCacheSize));
58
59 for (auto& observer : network_qualities_cache_observer_list_)
60 observer.OnChangeInCachedNetworkQuality(network_id, cached_network_quality);
61 }
62
GetById(const nqe::internal::NetworkID & network_id,nqe::internal::CachedNetworkQuality * cached_network_quality) const63 bool NetworkQualityStore::GetById(
64 const nqe::internal::NetworkID& network_id,
65 nqe::internal::CachedNetworkQuality* cached_network_quality) const {
66 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
67
68 // First check if an exact match can be found.
69 for (const auto& cached_quality : cached_network_qualities_) {
70 if (network_id.type != cached_quality.first.type ||
71 network_id.id != cached_quality.first.id) {
72 // The |type| and |id| must match.
73 continue;
74 }
75
76 // Check for an exact match, and return immediately if one is found.
77 // It's possible that the current network does not have signal strength
78 // available. In that case, return the cached network quality when the
79 // signal strength was unavailable.
80 if (network_id.signal_strength == cached_quality.first.signal_strength) {
81 *cached_network_quality = cached_quality.second;
82 return true;
83 }
84 }
85
86 // Handle the case when current network does not have signal strength
87 // available. Return the cached network quality that corresponds to the
88 // highest signal strength. This ensures that the method returns the fastest
89 // network quality possible for the current network, and serves as a
90 // conservative estimate.
91 if (network_id.signal_strength == INT32_MIN) {
92 auto matching_it = cached_network_qualities_.end();
93
94 for (auto it = cached_network_qualities_.begin();
95 it != cached_network_qualities_.end(); ++it) {
96 if (network_id.type != it->first.type || network_id.id != it->first.id) {
97 // The |type| and |id| must match.
98 continue;
99 }
100
101 // The cached network must have signal strength available. If the cached
102 // signal strength is unavailable, then this case would have been handled
103 // above.
104 DCHECK_NE(INT32_MIN, it->first.signal_strength);
105
106 if (matching_it == cached_network_qualities_.end() ||
107 it->first.signal_strength > matching_it->first.signal_strength) {
108 matching_it = it;
109 }
110 }
111
112 if (matching_it == cached_network_qualities_.end())
113 return false;
114
115 *cached_network_quality = matching_it->second;
116 return true;
117 }
118
119 // Finally, handle the case where the current network has a valid signal
120 // strength, but there is no exact match.
121
122 // |matching_it| points to the entry that has the same connection type and
123 // id as |network_id|, and has the signal strength closest to the signal
124 // stength of |network_id|.
125 auto matching_it = cached_network_qualities_.end();
126 int matching_it_diff_signal_strength = INT32_MAX;
127
128 // Find the closest estimate.
129 for (auto it = cached_network_qualities_.begin();
130 it != cached_network_qualities_.end(); ++it) {
131 if (network_id.type != it->first.type || network_id.id != it->first.id) {
132 // The |type| and |id| must match.
133 continue;
134 }
135
136 DCHECK_LE(0, network_id.signal_strength);
137
138 // Determine if the signal strength of |network_id| is closer to the
139 // signal strength of the network at |it| then that of the network at
140 // |matching_it|.
141 int diff_signal_strength;
142 if (it->first.signal_strength == INT32_MIN) {
143 // Current network has signal strength available. However, the persisted
144 // network does not. Set the |diff_signal_strength| to INT32_MAX. This
145 // ensures that if an entry with a valid signal strength is found later
146 // during iteration, then that entry will be used. If no entry with valid
147 // signal strength is found, then this entry will be used.
148 diff_signal_strength = INT32_MAX;
149 } else {
150 diff_signal_strength =
151 std::abs(network_id.signal_strength - it->first.signal_strength);
152 }
153
154 if (matching_it == cached_network_qualities_.end() ||
155 diff_signal_strength < matching_it_diff_signal_strength) {
156 matching_it = it;
157 matching_it_diff_signal_strength = diff_signal_strength;
158 }
159 }
160
161 if (matching_it == cached_network_qualities_.end())
162 return false;
163
164 *cached_network_quality = matching_it->second;
165 return true;
166 }
167
AddNetworkQualitiesCacheObserver(NetworkQualitiesCacheObserver * observer)168 void NetworkQualityStore::AddNetworkQualitiesCacheObserver(
169 NetworkQualitiesCacheObserver* observer) {
170 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
171 network_qualities_cache_observer_list_.AddObserver(observer);
172
173 // Notify the |observer| on the next message pump since |observer| may not
174 // be completely set up for receiving the callbacks.
175 base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
176 FROM_HERE,
177 base::BindOnce(&NetworkQualityStore::NotifyCacheObserverIfPresent,
178 weak_ptr_factory_.GetWeakPtr(),
179 base::UnsafeDangling(observer)));
180 }
181
RemoveNetworkQualitiesCacheObserver(NetworkQualitiesCacheObserver * observer)182 void NetworkQualityStore::RemoveNetworkQualitiesCacheObserver(
183 NetworkQualitiesCacheObserver* observer) {
184 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
185 network_qualities_cache_observer_list_.RemoveObserver(observer);
186 }
187
NotifyCacheObserverIfPresent(MayBeDangling<NetworkQualitiesCacheObserver> observer) const188 void NetworkQualityStore::NotifyCacheObserverIfPresent(
189 MayBeDangling<NetworkQualitiesCacheObserver> observer) const {
190 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
191
192 if (!network_qualities_cache_observer_list_.HasObserver(observer))
193 return;
194 for (const auto& it : cached_network_qualities_)
195 observer->OnChangeInCachedNetworkQuality(it.first, it.second);
196 }
197
198 } // namespace net::nqe::internal
199