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