Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(147)

Side by Side Diff: net/nqe/network_qualities_prefs_manager.h

Issue 2369673004: Wire NQE Prefs to Profile (Closed)
Patch Set: Addressed ryansturm comments Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
35 // Using the provided PrefDelegate, NetworkQualitiesPrefsManager creates and 32 // Using the provided PrefDelegate, NetworkQualitiesPrefsManager creates and
36 // updates network quality information that is stored in prefs. Instances of 33 // 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 34 // this class must be constructed on the pref thread, and should later be moved
38 // to the network thread by calling InitializeOnNetworkThread. 35 // to the network thread by calling InitializeOnNetworkThread.
39 // 36 //
40 // This class interacts with both the pref thread and the network thread, and 37 // 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 38 // propagates network quality pref changes from the network thread to the
42 // provided pref delegate on the pref thread. 39 // provided pref delegate on the pref thread.
43 // 40 //
44 // ShutdownOnPrefThread must be called from the pref thread before destruction. 41 // ShutdownOnPrefThread must be called from the pref thread before destruction.
45 class NET_EXPORT NetworkQualitiesPrefsManager 42 class NET_EXPORT NetworkQualitiesPrefsManager
46 : public nqe::internal::NetworkQualityStore::NetworkQualitiesCacheObserver { 43 : public nqe::internal::NetworkQualityStore::NetworkQualitiesCacheObserver {
47 public: 44 public:
48 // Provides an interface that must be implemented by the embedder. 45 // Provides an interface that must be implemented by the embedder.
49 class NET_EXPORT PrefDelegate { 46 class NET_EXPORT PrefDelegate {
50 public: 47 public:
48 virtual ~PrefDelegate() {}
49
51 // Sets the persistent pref to the given value. 50 // Sets the persistent pref to the given value.
52 virtual void SetDictionaryValue(const base::DictionaryValue& value) = 0; 51 virtual void SetDictionaryValue(const base::DictionaryValue& value) = 0;
52
53 // Returns the peristent prefs.
54 virtual const base::DictionaryValue& GetDictionaryValue() = 0;
53 }; 55 };
54 56
55 // Creates an instance of the NetworkQualitiesPrefsManager. Ownership of 57 // Creates an instance of the NetworkQualitiesPrefsManager. Ownership of
56 // |pref_delegate| is taken by this class. Must be constructed on the pref 58 // |pref_delegate| is taken by this class. Must be constructed on the pref
57 // thread, and then moved to network thread. 59 // thread, and then moved to network thread.
58 explicit NetworkQualitiesPrefsManager( 60 explicit NetworkQualitiesPrefsManager(
59 std::unique_ptr<PrefDelegate> pref_delegate); 61 std::unique_ptr<PrefDelegate> pref_delegate);
60 ~NetworkQualitiesPrefsManager() override; 62 ~NetworkQualitiesPrefsManager() override;
61 63
62 // Initialize on the Network thread. 64 // Initialize on the Network thread.
63 void InitializeOnNetworkThread( 65 void InitializeOnNetworkThread(
64 NetworkQualityEstimator* network_quality_estimator); 66 NetworkQualityEstimator* network_quality_estimator);
65 67
66 // Prepare for shutdown. Must be called on the pref thread before destruction. 68 // Prepare for shutdown. Must be called on the pref thread before destruction.
67 void ShutdownOnPrefThread(); 69 void ShutdownOnPrefThread();
68 70
71 // Reads the prefs again, parses them into a map of NetworkIDs and
72 // CachedNetworkQualities, and returns the map.
73 std::map<nqe::internal::NetworkID, nqe::internal::CachedNetworkQuality>
bengr 2016/10/14 22:37:13 It might be a little more readable to typedef this
tbansal1 2016/10/14 23:25:53 Done.
74 ForceReadPrefsForTesting() const;
75
69 private: 76 private:
70 // Pref thread members: 77 // Pref thread members:
71 // Called on pref thread when there is a change in the cached network quality. 78 // Called on pref thread when there is a change in the cached network quality.
72 void OnChangeInCachedNetworkQualityOnPrefThread( 79 void OnChangeInCachedNetworkQualityOnPrefThread(
73 const nqe::internal::NetworkID& network_id, 80 const nqe::internal::NetworkID& network_id,
74 const nqe::internal::CachedNetworkQuality& cached_network_quality); 81 const nqe::internal::CachedNetworkQuality& cached_network_quality);
75 82
76 // Responsible for writing the persistent prefs to the disk. 83 // Responsible for writing the persistent prefs to the disk.
77 std::unique_ptr<PrefDelegate> pref_delegate_; 84 std::unique_ptr<PrefDelegate> pref_delegate_;
78 85
79 scoped_refptr<base::SequencedTaskRunner> pref_task_runner_; 86 scoped_refptr<base::SequencedTaskRunner> pref_task_runner_;
80 87
88 // Current prefs on the disk. Should be accessed only on the pref thread.
89 std::unique_ptr<base::DictionaryValue> prefs_;
90
81 // Should be accessed only on the pref thread. 91 // Should be accessed only on the pref thread.
82 base::WeakPtr<NetworkQualitiesPrefsManager> pref_weak_ptr_; 92 base::WeakPtr<NetworkQualitiesPrefsManager> pref_weak_ptr_;
83 93
84 // Network thread members: 94 // Network thread members:
85 // nqe::internal::NetworkQualityStore::NetworkQualitiesCacheObserver 95 // nqe::internal::NetworkQualityStore::NetworkQualitiesCacheObserver
86 // implementation: 96 // implementation:
87 void OnChangeInCachedNetworkQuality( 97 void OnChangeInCachedNetworkQuality(
88 const nqe::internal::NetworkID& network_id, 98 const nqe::internal::NetworkID& network_id,
89 const nqe::internal::CachedNetworkQuality& cached_network_quality) 99 const nqe::internal::CachedNetworkQuality& cached_network_quality)
90 override; 100 override;
91 101
92 NetworkQualityEstimator* network_quality_estimator_; 102 NetworkQualityEstimator* network_quality_estimator_;
93 103
94 scoped_refptr<base::SequencedTaskRunner> network_task_runner_; 104 scoped_refptr<base::SequencedTaskRunner> network_task_runner_;
95 105
106 // Network quality prefs read from the disk at the time of startup. Can be
107 // accessed on any thread.
108 const std::map<nqe::internal::NetworkID, nqe::internal::CachedNetworkQuality>
109 read_prefs_startup_;
110
96 // Used to get |weak_ptr_| to self on the pref thread. 111 // Used to get |weak_ptr_| to self on the pref thread.
97 base::WeakPtrFactory<NetworkQualitiesPrefsManager> pref_weak_ptr_factory_; 112 base::WeakPtrFactory<NetworkQualitiesPrefsManager> pref_weak_ptr_factory_;
98 113
99 DISALLOW_COPY_AND_ASSIGN(NetworkQualitiesPrefsManager); 114 DISALLOW_COPY_AND_ASSIGN(NetworkQualitiesPrefsManager);
100 }; 115 };
101 116
102 } // namespace net 117 } // namespace net
103 118
104 #endif // NET_NQE_NETWORK_QUALITIES_PREFS_MANAGER_H_ 119 #endif // NET_NQE_NETWORK_QUALITIES_PREFS_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698