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