network_quality_store.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #ifndef NET_NQE_NETWORK_QUALITY_STORE_H_
  5. #define NET_NQE_NETWORK_QUALITY_STORE_H_
  6. #include <map>
  7. #include "base/memory/weak_ptr.h"
  8. #include "base/observer_list.h"
  9. #include "base/sequence_checker.h"
  10. #include "net/base/net_export.h"
  11. #include "net/nqe/cached_network_quality.h"
  12. #include "net/nqe/effective_connection_type.h"
  13. #include "net/nqe/network_id.h"
  14. namespace net::nqe::internal {
  15. // NetworkQualityStore holds the network qualities of different networks in
  16. // memory. Entries are stored in LRU order, and older entries may be evicted.
  17. class NET_EXPORT_PRIVATE NetworkQualityStore {
  18. public:
  19. // Observes changes in the cached network qualities.
  20. class NET_EXPORT_PRIVATE NetworkQualitiesCacheObserver {
  21. public:
  22. NetworkQualitiesCacheObserver(const NetworkQualitiesCacheObserver&) =
  23. delete;
  24. NetworkQualitiesCacheObserver& operator=(
  25. const NetworkQualitiesCacheObserver&) = delete;
  26. // Notifies the observer of a change in the cached network quality. The
  27. // observer must register and unregister itself on the IO thread. All the
  28. // observers would be notified on the IO thread. |network_id| is the ID of
  29. // the network whose cached quality is being reported.
  30. virtual void OnChangeInCachedNetworkQuality(
  31. const nqe::internal::NetworkID& network_id,
  32. const nqe::internal::CachedNetworkQuality& cached_network_quality) = 0;
  33. protected:
  34. NetworkQualitiesCacheObserver() = default;
  35. virtual ~NetworkQualitiesCacheObserver() = default;
  36. };
  37. NetworkQualityStore();
  38. NetworkQualityStore(const NetworkQualityStore&) = delete;
  39. NetworkQualityStore& operator=(const NetworkQualityStore&) = delete;
  40. ~NetworkQualityStore();
  41. // Stores the network quality |cached_network_quality| of network with ID
  42. // |network_id|.
  43. void Add(const nqe::internal::NetworkID& network_id,
  44. const nqe::internal::CachedNetworkQuality& cached_network_quality);
  45. // Returns true if the network quality estimate was successfully read
  46. // for a network with ID |network_id|, and sets |cached_network_quality| to
  47. // the estimate read.
  48. bool GetById(
  49. const nqe::internal::NetworkID& network_id,
  50. nqe::internal::CachedNetworkQuality* cached_network_quality) const;
  51. // Adds and removes |observer| from the list of cache observers. The
  52. // observers are notified on the same thread on which it was added. Addition
  53. // and removal of the observer must happen on the same thread.
  54. void AddNetworkQualitiesCacheObserver(
  55. NetworkQualitiesCacheObserver* observer);
  56. void RemoveNetworkQualitiesCacheObserver(
  57. NetworkQualitiesCacheObserver* observer);
  58. // If |disable_offline_check| is set to true, the offline check is disabled
  59. // when storing the network quality.
  60. void DisableOfflineCheckForTesting(bool disable_offline_check);
  61. private:
  62. // Maximum size of the store that holds network quality estimates.
  63. // A smaller size may reduce the cache hit rate due to frequent evictions.
  64. // A larger size may affect performance.
  65. static const size_t kMaximumNetworkQualityCacheSize = 20;
  66. // Notifies |observer| of the current effective connection type if |observer|
  67. // is still registered as an observer.
  68. void NotifyCacheObserverIfPresent(
  69. NetworkQualitiesCacheObserver* observer) const;
  70. // This does not use an unordered_map or hash_map for code simplicity (the key
  71. // just implements operator<, rather than hash and equality) and because the
  72. // map is tiny.
  73. typedef std::map<nqe::internal::NetworkID,
  74. nqe::internal::CachedNetworkQuality>
  75. CachedNetworkQualities;
  76. // Data structure that stores the qualities of networks.
  77. CachedNetworkQualities cached_network_qualities_;
  78. // Observer list for changes in the cached network quality.
  79. base::ObserverList<NetworkQualitiesCacheObserver>::Unchecked
  80. network_qualities_cache_observer_list_;
  81. SEQUENCE_CHECKER(sequence_checker_);
  82. base::WeakPtrFactory<NetworkQualityStore> weak_ptr_factory_{this};
  83. };
  84. } // namespace net::nqe::internal
  85. #endif // NET_NQE_NETWORK_QUALITY_STORE_H_