OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_NQE_NETWORK_QUALITIES_PREFS_MANAGER_H_ | 5 #ifndef NET_NQE_NETWORK_QUALITIES_PREFS_MANAGER_H_ |
6 #define NET_NQE_NETWORK_QUALITIES_PREFS_MANAGER_H_ | 6 #define NET_NQE_NETWORK_QUALITIES_PREFS_MANAGER_H_ |
7 | 7 |
| 8 #include <map> |
8 #include <memory> | 9 #include <memory> |
9 | 10 |
10 #include "base/macros.h" | 11 #include "base/macros.h" |
11 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/values.h" |
12 #include "net/base/net_export.h" | 14 #include "net/base/net_export.h" |
| 15 #include "net/nqe/cached_network_quality.h" |
13 #include "net/nqe/effective_connection_type.h" | 16 #include "net/nqe/effective_connection_type.h" |
14 #include "net/nqe/network_id.h" | 17 #include "net/nqe/network_id.h" |
15 #include "net/nqe/network_quality_store.h" | 18 #include "net/nqe/network_quality_store.h" |
16 | 19 |
17 namespace base { | 20 namespace base { |
18 class DictionaryValue; | |
19 class SequencedTaskRunner; | 21 class SequencedTaskRunner; |
20 } | 22 } |
21 | 23 |
22 namespace net { | 24 namespace net { |
23 namespace nqe { | |
24 namespace internal { | |
25 class CachedNetworkQuality; | |
26 } | |
27 } | |
28 class NetworkQualityEstimator; | 25 class NetworkQualityEstimator; |
29 | 26 |
30 typedef base::Callback<void( | 27 typedef base::Callback<void( |
31 const nqe::internal::NetworkID& network_id, | 28 const nqe::internal::NetworkID& network_id, |
32 const nqe::internal::CachedNetworkQuality& cached_network_quality)> | 29 const nqe::internal::CachedNetworkQuality& cached_network_quality)> |
33 OnChangeInCachedNetworkQualityCallback; | 30 OnChangeInCachedNetworkQualityCallback; |
34 | 31 |
| 32 typedef std::map<nqe::internal::NetworkID, nqe::internal::CachedNetworkQuality> |
| 33 ParsedPrefs; |
| 34 |
35 // Using the provided PrefDelegate, NetworkQualitiesPrefsManager creates and | 35 // Using the provided PrefDelegate, NetworkQualitiesPrefsManager creates and |
36 // updates network quality information that is stored in prefs. Instances of | 36 // updates network quality information that is stored in prefs. Instances of |
37 // this class must be constructed on the pref thread, and should later be moved | 37 // this class must be constructed on the pref thread, and should later be moved |
38 // to the network thread by calling InitializeOnNetworkThread. | 38 // to the network thread by calling InitializeOnNetworkThread. |
39 // | 39 // |
40 // This class interacts with both the pref thread and the network thread, and | 40 // This class interacts with both the pref thread and the network thread, and |
41 // propagates network quality pref changes from the network thread to the | 41 // propagates network quality pref changes from the network thread to the |
42 // provided pref delegate on the pref thread. | 42 // provided pref delegate on the pref thread. |
43 // | 43 // |
44 // ShutdownOnPrefThread must be called from the pref thread before destruction. | 44 // ShutdownOnPrefThread must be called from the pref thread before destruction. |
45 class NET_EXPORT NetworkQualitiesPrefsManager | 45 class NET_EXPORT NetworkQualitiesPrefsManager |
46 : public nqe::internal::NetworkQualityStore::NetworkQualitiesCacheObserver { | 46 : public nqe::internal::NetworkQualityStore::NetworkQualitiesCacheObserver { |
47 public: | 47 public: |
48 // Provides an interface that must be implemented by the embedder. | 48 // Provides an interface that must be implemented by the embedder. |
49 class NET_EXPORT PrefDelegate { | 49 class NET_EXPORT PrefDelegate { |
50 public: | 50 public: |
| 51 virtual ~PrefDelegate() {} |
| 52 |
51 // Sets the persistent pref to the given value. | 53 // Sets the persistent pref to the given value. |
52 virtual void SetDictionaryValue(const base::DictionaryValue& value) = 0; | 54 virtual void SetDictionaryValue(const base::DictionaryValue& value) = 0; |
| 55 |
| 56 // Returns the peristent prefs. |
| 57 virtual const base::DictionaryValue& GetDictionaryValue() = 0; |
53 }; | 58 }; |
54 | 59 |
55 // Creates an instance of the NetworkQualitiesPrefsManager. Ownership of | 60 // Creates an instance of the NetworkQualitiesPrefsManager. Ownership of |
56 // |pref_delegate| is taken by this class. Must be constructed on the pref | 61 // |pref_delegate| is taken by this class. Must be constructed on the pref |
57 // thread, and then moved to network thread. | 62 // thread, and then moved to network thread. |
58 explicit NetworkQualitiesPrefsManager( | 63 explicit NetworkQualitiesPrefsManager( |
59 std::unique_ptr<PrefDelegate> pref_delegate); | 64 std::unique_ptr<PrefDelegate> pref_delegate); |
60 ~NetworkQualitiesPrefsManager() override; | 65 ~NetworkQualitiesPrefsManager() override; |
61 | 66 |
62 // Initialize on the Network thread. | 67 // Initialize on the Network thread. |
63 void InitializeOnNetworkThread( | 68 void InitializeOnNetworkThread( |
64 NetworkQualityEstimator* network_quality_estimator); | 69 NetworkQualityEstimator* network_quality_estimator); |
65 | 70 |
66 // Prepare for shutdown. Must be called on the pref thread before destruction. | 71 // Prepare for shutdown. Must be called on the pref thread before destruction. |
67 void ShutdownOnPrefThread(); | 72 void ShutdownOnPrefThread(); |
68 | 73 |
| 74 // Reads the prefs again, parses them into a map of NetworkIDs and |
| 75 // CachedNetworkQualities, and returns the map. |
| 76 ParsedPrefs ForceReadPrefsForTesting() const; |
| 77 |
69 private: | 78 private: |
70 // Pref thread members: | 79 // Pref thread members: |
71 // Called on pref thread when there is a change in the cached network quality. | 80 // Called on pref thread when there is a change in the cached network quality. |
72 void OnChangeInCachedNetworkQualityOnPrefThread( | 81 void OnChangeInCachedNetworkQualityOnPrefThread( |
73 const nqe::internal::NetworkID& network_id, | 82 const nqe::internal::NetworkID& network_id, |
74 const nqe::internal::CachedNetworkQuality& cached_network_quality); | 83 const nqe::internal::CachedNetworkQuality& cached_network_quality); |
75 | 84 |
76 // Responsible for writing the persistent prefs to the disk. | 85 // Responsible for writing the persistent prefs to the disk. |
77 std::unique_ptr<PrefDelegate> pref_delegate_; | 86 std::unique_ptr<PrefDelegate> pref_delegate_; |
78 | 87 |
79 scoped_refptr<base::SequencedTaskRunner> pref_task_runner_; | 88 scoped_refptr<base::SequencedTaskRunner> pref_task_runner_; |
80 | 89 |
| 90 // Current prefs on the disk. Should be accessed only on the pref thread. |
| 91 std::unique_ptr<base::DictionaryValue> prefs_; |
| 92 |
81 // Should be accessed only on the pref thread. | 93 // Should be accessed only on the pref thread. |
82 base::WeakPtr<NetworkQualitiesPrefsManager> pref_weak_ptr_; | 94 base::WeakPtr<NetworkQualitiesPrefsManager> pref_weak_ptr_; |
83 | 95 |
84 // Network thread members: | 96 // Network thread members: |
85 // nqe::internal::NetworkQualityStore::NetworkQualitiesCacheObserver | 97 // nqe::internal::NetworkQualityStore::NetworkQualitiesCacheObserver |
86 // implementation: | 98 // implementation: |
87 void OnChangeInCachedNetworkQuality( | 99 void OnChangeInCachedNetworkQuality( |
88 const nqe::internal::NetworkID& network_id, | 100 const nqe::internal::NetworkID& network_id, |
89 const nqe::internal::CachedNetworkQuality& cached_network_quality) | 101 const nqe::internal::CachedNetworkQuality& cached_network_quality) |
90 override; | 102 override; |
91 | 103 |
92 NetworkQualityEstimator* network_quality_estimator_; | 104 NetworkQualityEstimator* network_quality_estimator_; |
93 | 105 |
94 scoped_refptr<base::SequencedTaskRunner> network_task_runner_; | 106 scoped_refptr<base::SequencedTaskRunner> network_task_runner_; |
95 | 107 |
| 108 // Network quality prefs read from the disk at the time of startup. Can be |
| 109 // accessed on any thread. |
| 110 const ParsedPrefs read_prefs_startup_; |
| 111 |
96 // Used to get |weak_ptr_| to self on the pref thread. | 112 // Used to get |weak_ptr_| to self on the pref thread. |
97 base::WeakPtrFactory<NetworkQualitiesPrefsManager> pref_weak_ptr_factory_; | 113 base::WeakPtrFactory<NetworkQualitiesPrefsManager> pref_weak_ptr_factory_; |
98 | 114 |
99 DISALLOW_COPY_AND_ASSIGN(NetworkQualitiesPrefsManager); | 115 DISALLOW_COPY_AND_ASSIGN(NetworkQualitiesPrefsManager); |
100 }; | 116 }; |
101 | 117 |
102 } // namespace net | 118 } // namespace net |
103 | 119 |
104 #endif // NET_NQE_NETWORK_QUALITIES_PREFS_MANAGER_H_ | 120 #endif // NET_NQE_NETWORK_QUALITIES_PREFS_MANAGER_H_ |
OLD | NEW |