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_QUALITIES_PREFS_MANAGER_H_ 6 #define NET_NQE_NETWORK_QUALITIES_PREFS_MANAGER_H_ 7 8 #include <map> 9 #include <memory> 10 11 #include "base/memory/raw_ptr.h" 12 #include "base/memory/weak_ptr.h" 13 #include "base/sequence_checker.h" 14 #include "base/values.h" 15 #include "net/base/net_export.h" 16 #include "net/nqe/cached_network_quality.h" 17 #include "net/nqe/effective_connection_type.h" 18 #include "net/nqe/network_id.h" 19 #include "net/nqe/network_quality_store.h" 20 21 namespace net { 22 class NetworkQualityEstimator; 23 24 typedef std::map<nqe::internal::NetworkID, nqe::internal::CachedNetworkQuality> 25 ParsedPrefs; 26 27 // Using the provided PrefDelegate, NetworkQualitiesPrefsManager creates and 28 // updates network quality information that is stored in prefs. 29 class NET_EXPORT NetworkQualitiesPrefsManager 30 : public nqe::internal::NetworkQualityStore::NetworkQualitiesCacheObserver { 31 public: 32 // Provides an interface that must be implemented by the embedder. 33 class NET_EXPORT PrefDelegate { 34 public: 35 virtual ~PrefDelegate() = default; 36 37 // Sets the persistent pref to the given value. 38 virtual void SetDictionaryValue(const base::Value::Dict& dict) = 0; 39 40 // Returns a copy of the persistent prefs. 41 virtual base::Value::Dict GetDictionaryValue() = 0; 42 }; 43 44 // Creates an instance of the NetworkQualitiesPrefsManager. Ownership of 45 // |pref_delegate| is taken by this class. 46 explicit NetworkQualitiesPrefsManager( 47 std::unique_ptr<PrefDelegate> pref_delegate); 48 49 NetworkQualitiesPrefsManager(const NetworkQualitiesPrefsManager&) = delete; 50 NetworkQualitiesPrefsManager& operator=(const NetworkQualitiesPrefsManager&) = 51 delete; 52 53 ~NetworkQualitiesPrefsManager() override; 54 55 // Initialize on the Network thread. Must be called after pref service has 56 // been initialized, and prefs are ready for reading. 57 void InitializeOnNetworkThread( 58 NetworkQualityEstimator* network_quality_estimator); 59 60 // Prepare for shutdown. 61 void ShutdownOnPrefSequence(); 62 63 // Clear the network quality estimator prefs. 64 void ClearPrefs(); 65 66 // Reads the prefs again, parses them into a map of NetworkIDs and 67 // CachedNetworkQualities, and returns the map. 68 ParsedPrefs ForceReadPrefsForTesting() const; 69 70 private: 71 // Responsible for writing the persistent prefs to the disk. 72 std::unique_ptr<PrefDelegate> pref_delegate_; 73 74 // Current prefs on the disk. 75 base::Value::Dict prefs_; 76 77 // nqe::internal::NetworkQualityStore::NetworkQualitiesCacheObserver 78 // implementation: 79 void OnChangeInCachedNetworkQuality( 80 const nqe::internal::NetworkID& network_id, 81 const nqe::internal::CachedNetworkQuality& cached_network_quality) 82 override; 83 84 raw_ptr<NetworkQualityEstimator> network_quality_estimator_ = nullptr; 85 86 // Network quality prefs read from the disk at the time of startup. 87 ParsedPrefs read_prefs_startup_; 88 89 SEQUENCE_CHECKER(sequence_checker_); 90 }; 91 92 } // namespace net 93 94 #endif // NET_NQE_NETWORK_QUALITIES_PREFS_MANAGER_H_ 95