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

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

Issue 2487883002: NQE: Use cached estimates (Closed)
Patch Set: bengr comments Created 4 years 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 #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
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
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 a key that is different from
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();
138 it.Advance()) { 145 it.Advance()) {
139 const nqe::internal::NetworkID it_network_id = 146 // Delete the kth element in the dictionary, not including the element
140 nqe::internal::NetworkID::FromString(it.key()); 147 // that represents the current network. k == |index_to_delete|.
141 if (it_network_id != network_id) { 148 if (nqe::internal::NetworkID::FromString(it.key()) == network_id)
149 continue;
150
151 if (index_to_delete == 0) {
142 prefs_->RemovePath(it.key(), nullptr); 152 prefs_->RemovePath(it.key(), nullptr);
143 break; 153 break;
144 } 154 }
155 index_to_delete--;
145 } 156 }
146 } 157 }
147 DCHECK_GE(kMaxCacheSize, prefs_->size()); 158 DCHECK_GE(kMaxCacheSize, prefs_->size());
148 159
149 // Notify the pref delegate so that it updates the prefs on the disk. 160 // Notify the pref delegate so that it updates the prefs on the disk.
150 pref_delegate_->SetDictionaryValue(*prefs_); 161 pref_delegate_->SetDictionaryValue(*prefs_);
151 } 162 }
152 163
153 ParsedPrefs NetworkQualitiesPrefsManager::ForceReadPrefsForTesting() const { 164 ParsedPrefs NetworkQualitiesPrefsManager::ForceReadPrefsForTesting() const {
154 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); 165 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread());
155 std::unique_ptr<base::DictionaryValue> value( 166 std::unique_ptr<base::DictionaryValue> value(
156 pref_delegate_->GetDictionaryValue().CreateDeepCopy()); 167 pref_delegate_->GetDictionaryValue());
157 return ConvertDictionaryValueToMap(value.get()); 168 return ConvertDictionaryValueToMap(value.get());
158 } 169 }
159 170
160 } // namespace net 171 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698