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 <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/rand_util.h" | |
| 11 #include "base/sequenced_task_runner.h" | 12 #include "base/sequenced_task_runner.h" |
| 12 #include "base/threading/thread_checker.h" | 13 #include "base/threading/thread_checker.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "net/nqe/network_quality_estimator.h" | 15 #include "net/nqe/network_quality_estimator.h" |
| 15 | 16 |
| 16 namespace net { | 17 namespace net { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 // Maximum size of the prefs that hold the qualities of different networks. | 21 // Maximum size of the prefs that hold the qualities of different networks. |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 49 } | 50 } |
| 50 return read_prefs; | 51 return read_prefs; |
| 51 } | 52 } |
| 52 | 53 |
| 53 } // namespace | 54 } // namespace |
| 54 | 55 |
| 55 NetworkQualitiesPrefsManager::NetworkQualitiesPrefsManager( | 56 NetworkQualitiesPrefsManager::NetworkQualitiesPrefsManager( |
| 56 std::unique_ptr<PrefDelegate> pref_delegate) | 57 std::unique_ptr<PrefDelegate> pref_delegate) |
| 57 : pref_delegate_(std::move(pref_delegate)), | 58 : pref_delegate_(std::move(pref_delegate)), |
| 58 pref_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 59 pref_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 59 prefs_(pref_delegate_->GetDictionaryValue().CreateDeepCopy()), | 60 prefs_(pref_delegate_->GetDictionaryValue()), |
| 60 network_quality_estimator_(nullptr), | 61 network_quality_estimator_(nullptr), |
| 61 read_prefs_startup_(ConvertDictionaryValueToMap(prefs_.get())), | 62 read_prefs_startup_(ConvertDictionaryValueToMap(prefs_.get())), |
| 62 pref_weak_ptr_factory_(this) { | 63 pref_weak_ptr_factory_(this) { |
| 63 DCHECK(pref_delegate_); | 64 DCHECK(pref_delegate_); |
| 64 DCHECK_GE(kMaxCacheSize, prefs_->size()); | 65 DCHECK_GE(kMaxCacheSize, prefs_->size()); |
| 65 | 66 |
| 66 pref_weak_ptr_ = pref_weak_ptr_factory_.GetWeakPtr(); | 67 pref_weak_ptr_ = pref_weak_ptr_factory_.GetWeakPtr(); |
| 67 } | 68 } |
| 68 | 69 |
| 69 NetworkQualitiesPrefsManager::~NetworkQualitiesPrefsManager() { | 70 NetworkQualitiesPrefsManager::~NetworkQualitiesPrefsManager() { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 125 // If the network ID contains a period, then return early since the dictionary | 126 // If the network ID contains a period, then return early since the dictionary |
| 126 // prefs cannot contain period in the path. | 127 // prefs cannot contain period in the path. |
| 127 if (network_id_string.find(".") != std::string::npos) | 128 if (network_id_string.find(".") != std::string::npos) |
| 128 return; | 129 return; |
| 129 | 130 |
| 130 prefs_->SetString(network_id_string, | 131 prefs_->SetString(network_id_string, |
| 131 GetNameForEffectiveConnectionType( | 132 GetNameForEffectiveConnectionType( |
| 132 cached_network_quality.effective_connection_type())); | 133 cached_network_quality.effective_connection_type())); |
| 133 | 134 |
| 134 if (prefs_->size() > kMaxCacheSize) { | 135 if (prefs_->size() > kMaxCacheSize) { |
| 135 // Delete one value that has key different than |network_id|. | 136 // Delete one randomly selected value that has key different than |
|
bengr
2016/12/15 23:51:26
has key different than -> has a key that is differ
tbansal1
2016/12/16 18:07:15
Done.
| |
| 137 // |network_id|. | |
| 136 DCHECK_EQ(kMaxCacheSize + 1, prefs_->size()); | 138 DCHECK_EQ(kMaxCacheSize + 1, prefs_->size()); |
| 139 // Generate a random number between 0 and |kMaxCacheSize| -1 (both | |
| 140 // inclusive) since the number of network IDs in |prefs_| other than | |
| 141 // |network_id| is |kMaxCacheSize|. | |
| 142 int index_to_delete = base::RandInt(0, kMaxCacheSize - 1); | |
| 143 | |
| 137 for (base::DictionaryValue::Iterator it(*prefs_); !it.IsAtEnd(); | 144 for (base::DictionaryValue::Iterator it(*prefs_); !it.IsAtEnd(); |
|
bengr
2016/12/15 23:51:26
How about adding this comment:
// Delete the kth
tbansal1
2016/12/16 18:07:15
Done.
| |
| 138 it.Advance()) { | 145 it.Advance()) { |
| 139 const nqe::internal::NetworkID it_network_id = | 146 if (nqe::internal::NetworkID::FromString(it.key()) == network_id) |
| 140 nqe::internal::NetworkID::FromString(it.key()); | 147 continue; |
| 141 if (it_network_id != network_id) { | 148 |
| 149 if (index_to_delete == 0) { | |
| 142 prefs_->RemovePath(it.key(), nullptr); | 150 prefs_->RemovePath(it.key(), nullptr); |
| 143 break; | 151 break; |
| 144 } | 152 } |
| 153 index_to_delete--; | |
| 145 } | 154 } |
| 146 } | 155 } |
| 147 DCHECK_GE(kMaxCacheSize, prefs_->size()); | 156 DCHECK_GE(kMaxCacheSize, prefs_->size()); |
| 148 | 157 |
| 149 // Notify the pref delegate so that it updates the prefs on the disk. | 158 // Notify the pref delegate so that it updates the prefs on the disk. |
| 150 pref_delegate_->SetDictionaryValue(*prefs_); | 159 pref_delegate_->SetDictionaryValue(*prefs_); |
| 151 } | 160 } |
| 152 | 161 |
| 153 ParsedPrefs NetworkQualitiesPrefsManager::ForceReadPrefsForTesting() const { | 162 ParsedPrefs NetworkQualitiesPrefsManager::ForceReadPrefsForTesting() const { |
| 154 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); | 163 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); |
| 155 std::unique_ptr<base::DictionaryValue> value( | 164 std::unique_ptr<base::DictionaryValue> value( |
| 156 pref_delegate_->GetDictionaryValue().CreateDeepCopy()); | 165 pref_delegate_->GetDictionaryValue()); |
| 157 return ConvertDictionaryValueToMap(value.get()); | 166 return ConvertDictionaryValueToMap(value.get()); |
| 158 } | 167 } |
| 159 | 168 |
| 160 } // namespace net | 169 } // namespace net |
| OLD | NEW |