network_qualities_prefs_manager.h 3.1 KB

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