network_quality_store.cc 7.4 KB

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