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