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 #include "net/nqe/network_qualities_prefs_manager.h" | 5 #include "net/nqe/network_qualities_prefs_manager.h" |
6 | 6 |
| 7 #include <string> |
7 #include <utility> | 8 #include <utility> |
8 | 9 |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "base/sequenced_task_runner.h" | 11 #include "base/sequenced_task_runner.h" |
11 #include "base/threading/thread_checker.h" | 12 #include "base/threading/thread_checker.h" |
12 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
13 #include "base/values.h" | |
14 #include "net/nqe/cached_network_quality.h" | |
15 #include "net/nqe/network_quality_estimator.h" | 14 #include "net/nqe/network_quality_estimator.h" |
16 | 15 |
17 namespace net { | 16 namespace net { |
18 | 17 |
| 18 namespace { |
| 19 |
| 20 // Maximum size of the prefs that hold the qualities of different networks. |
| 21 static const size_t kMaxCacheSize = 3u; |
| 22 |
| 23 // Parses |value| into a map of NetworkIDs and CachedNetworkQualities, |
| 24 // and returns the map. |
| 25 ParsedPrefs ConvertDictionaryValueToMap(const base::DictionaryValue* value) { |
| 26 ParsedPrefs read_prefs; |
| 27 |
| 28 DCHECK_GE(kMaxCacheSize, value->size()); |
| 29 |
| 30 for (base::DictionaryValue::Iterator it(*value); !it.IsAtEnd(); |
| 31 it.Advance()) { |
| 32 nqe::internal::NetworkID network_id = |
| 33 nqe::internal::NetworkID::FromString(it.key()); |
| 34 |
| 35 std::string effective_connection_type_string; |
| 36 bool effective_connection_type_available = |
| 37 it.value().GetAsString(&effective_connection_type_string); |
| 38 DCHECK(effective_connection_type_available); |
| 39 |
| 40 EffectiveConnectionType effective_connection_type = |
| 41 EFFECTIVE_CONNECTION_TYPE_UNKNOWN; |
| 42 effective_connection_type_available = GetEffectiveConnectionTypeForName( |
| 43 effective_connection_type_string, &effective_connection_type); |
| 44 DCHECK(effective_connection_type_available); |
| 45 |
| 46 nqe::internal::CachedNetworkQuality cached_network_quality( |
| 47 effective_connection_type); |
| 48 read_prefs[network_id] = cached_network_quality; |
| 49 } |
| 50 return read_prefs; |
| 51 } |
| 52 |
| 53 } // namespace |
| 54 |
19 NetworkQualitiesPrefsManager::NetworkQualitiesPrefsManager( | 55 NetworkQualitiesPrefsManager::NetworkQualitiesPrefsManager( |
20 std::unique_ptr<PrefDelegate> pref_delegate) | 56 std::unique_ptr<PrefDelegate> pref_delegate) |
21 : pref_delegate_(std::move(pref_delegate)), | 57 : pref_delegate_(std::move(pref_delegate)), |
22 pref_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 58 pref_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 59 prefs_(pref_delegate_->GetDictionaryValue().CreateDeepCopy()), |
23 network_quality_estimator_(nullptr), | 60 network_quality_estimator_(nullptr), |
| 61 read_prefs_startup_(ConvertDictionaryValueToMap(prefs_.get())), |
24 pref_weak_ptr_factory_(this) { | 62 pref_weak_ptr_factory_(this) { |
25 DCHECK(pref_delegate_); | 63 DCHECK(pref_delegate_); |
| 64 DCHECK_GE(kMaxCacheSize, prefs_->size()); |
26 | 65 |
27 pref_weak_ptr_ = pref_weak_ptr_factory_.GetWeakPtr(); | 66 pref_weak_ptr_ = pref_weak_ptr_factory_.GetWeakPtr(); |
28 } | 67 } |
29 | 68 |
30 NetworkQualitiesPrefsManager::~NetworkQualitiesPrefsManager() { | 69 NetworkQualitiesPrefsManager::~NetworkQualitiesPrefsManager() { |
| 70 if (!network_task_runner_) |
| 71 return; |
31 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); | 72 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
32 if (network_quality_estimator_) | 73 if (network_quality_estimator_) |
33 network_quality_estimator_->RemoveNetworkQualitiesCacheObserver(this); | 74 network_quality_estimator_->RemoveNetworkQualitiesCacheObserver(this); |
34 } | 75 } |
35 | 76 |
36 void NetworkQualitiesPrefsManager::InitializeOnNetworkThread( | 77 void NetworkQualitiesPrefsManager::InitializeOnNetworkThread( |
37 NetworkQualityEstimator* network_quality_estimator) { | 78 NetworkQualityEstimator* network_quality_estimator) { |
38 DCHECK(!network_task_runner_); | 79 DCHECK(!network_task_runner_); |
39 DCHECK(network_quality_estimator); | 80 DCHECK(network_quality_estimator); |
40 | 81 |
41 network_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 82 network_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
42 network_quality_estimator_ = network_quality_estimator; | 83 network_quality_estimator_ = network_quality_estimator; |
43 network_quality_estimator_->AddNetworkQualitiesCacheObserver(this); | 84 network_quality_estimator_->AddNetworkQualitiesCacheObserver(this); |
| 85 |
| 86 // Notify network quality estimator of the read prefs. |
| 87 network_quality_estimator_->OnPrefsRead(read_prefs_startup_); |
44 } | 88 } |
45 | 89 |
46 void NetworkQualitiesPrefsManager::OnChangeInCachedNetworkQuality( | 90 void NetworkQualitiesPrefsManager::OnChangeInCachedNetworkQuality( |
47 const nqe::internal::NetworkID& network_id, | 91 const nqe::internal::NetworkID& network_id, |
48 const nqe::internal::CachedNetworkQuality& cached_network_quality) { | 92 const nqe::internal::CachedNetworkQuality& cached_network_quality) { |
49 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); | 93 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
50 | 94 |
51 // Notify |this| on the pref thread. | 95 // Notify |this| on the pref thread. |
52 pref_task_runner_->PostTask( | 96 pref_task_runner_->PostTask( |
53 FROM_HERE, | 97 FROM_HERE, |
54 base::Bind(&NetworkQualitiesPrefsManager:: | 98 base::Bind(&NetworkQualitiesPrefsManager:: |
55 OnChangeInCachedNetworkQualityOnPrefThread, | 99 OnChangeInCachedNetworkQualityOnPrefThread, |
56 pref_weak_ptr_, network_id, cached_network_quality)); | 100 pref_weak_ptr_, network_id, cached_network_quality)); |
57 } | 101 } |
58 | 102 |
59 void NetworkQualitiesPrefsManager::ShutdownOnPrefThread() { | 103 void NetworkQualitiesPrefsManager::ShutdownOnPrefThread() { |
60 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); | 104 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); |
| 105 pref_weak_ptr_factory_.InvalidateWeakPtrs(); |
61 pref_delegate_.reset(); | 106 pref_delegate_.reset(); |
62 } | 107 } |
63 | 108 |
64 void NetworkQualitiesPrefsManager::OnChangeInCachedNetworkQualityOnPrefThread( | 109 void NetworkQualitiesPrefsManager::OnChangeInCachedNetworkQualityOnPrefThread( |
65 const nqe::internal::NetworkID& network_id, | 110 const nqe::internal::NetworkID& network_id, |
66 const nqe::internal::CachedNetworkQuality& cached_network_quality) { | 111 const nqe::internal::CachedNetworkQuality& cached_network_quality) { |
67 // The prefs can only be written on the pref thread. | 112 // The prefs can only be written on the pref thread. |
68 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); | 113 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); |
| 114 DCHECK_GE(kMaxCacheSize, prefs_->size()); |
69 | 115 |
70 base::DictionaryValue dictionary_value; | 116 std::string network_id_string = network_id.ToString(); |
71 dictionary_value.SetString( | 117 |
72 network_id.ToString(), | 118 // If the network ID contains a period, then return early since the dictionary |
73 GetNameForEffectiveConnectionType( | 119 // prefs cannot contain period in the path. |
74 cached_network_quality.effective_connection_type())); | 120 if (network_id_string.find(".") != std::string::npos) |
| 121 return; |
| 122 |
| 123 prefs_->SetString(network_id_string, |
| 124 GetNameForEffectiveConnectionType( |
| 125 cached_network_quality.effective_connection_type())); |
| 126 |
| 127 if (prefs_->size() > kMaxCacheSize) { |
| 128 // Delete one value that has key different than |network_id|. |
| 129 DCHECK_EQ(kMaxCacheSize + 1, prefs_->size()); |
| 130 for (base::DictionaryValue::Iterator it(*(prefs_.get())); !it.IsAtEnd(); |
| 131 it.Advance()) { |
| 132 const nqe::internal::NetworkID it_network_id = |
| 133 nqe::internal::NetworkID::FromString(it.key()); |
| 134 if (it_network_id != network_id) { |
| 135 prefs_->RemovePath(it.key(), nullptr); |
| 136 break; |
| 137 } |
| 138 } |
| 139 } |
| 140 DCHECK_GE(kMaxCacheSize, prefs_->size()); |
75 | 141 |
76 // Notify the pref delegate so that it updates the prefs on the disk. | 142 // Notify the pref delegate so that it updates the prefs on the disk. |
77 pref_delegate_->SetDictionaryValue(dictionary_value); | 143 pref_delegate_->SetDictionaryValue(*(prefs_.get())); |
| 144 } |
| 145 |
| 146 ParsedPrefs NetworkQualitiesPrefsManager::ForceReadPrefsForTesting() const { |
| 147 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); |
| 148 std::unique_ptr<base::DictionaryValue> value( |
| 149 pref_delegate_->GetDictionaryValue().CreateDeepCopy()); |
| 150 return ConvertDictionaryValueToMap(value.get()); |
78 } | 151 } |
79 | 152 |
80 } // namespace net | 153 } // namespace net |
OLD | NEW |