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

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

Issue 2369673004: Wire NQE Prefs to Profile (Closed)
Patch Set: Addressed ryansturm comments Created 4 years, 2 months 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 <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;
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());
114
115 std::string network_id_string = network_id.ToString();
116
117 // If the network ID contains a period, then return since the dictionary prefs
RyanSturm 2016/10/14 18:36:20 I don't think UTF-8 actually fixes this because '.
tbansal1 2016/10/14 21:15:10 Acknowledged.
118 // cannot contain period in the path.
119 // TODO(tbansal): Investigate a better way of storing network IDs with period
120 // in them.
121 if (network_id_string.find(".") != std::string::npos)
122 return;
69 123
70 base::DictionaryValue dictionary_value; 124 base::DictionaryValue dictionary_value;
71 dictionary_value.SetString( 125 dictionary_value.SetString(
72 network_id.ToString(), 126 network_id_string,
73 GetNameForEffectiveConnectionType( 127 GetNameForEffectiveConnectionType(
74 cached_network_quality.effective_connection_type())); 128 cached_network_quality.effective_connection_type()));
129 prefs_->MergeDictionary(&dictionary_value);
RyanSturm 2016/10/14 18:36:21 Since you are doing this in the same place as crea
tbansal1 2016/10/14 21:15:10 Done.
130
131 if (prefs_->size() > kMaxCacheSize) {
132 // Delete one value that has key different than |network_id|.
133 DCHECK_EQ(kMaxCacheSize + 1, prefs_->size());
134 for (base::DictionaryValue::Iterator it(*(prefs_.get())); !it.IsAtEnd();
135 it.Advance()) {
136 const nqe::internal::NetworkID it_network_id =
137 nqe::internal::NetworkID::FromString(it.key());
138 if (it_network_id != network_id) {
139 prefs_->RemovePath(it.key(), nullptr);
140 break;
141 }
142 }
143 }
144 DCHECK_GE(kMaxCacheSize, prefs_->size());
75 145
76 // Notify the pref delegate so that it updates the prefs on the disk. 146 // Notify the pref delegate so that it updates the prefs on the disk.
77 pref_delegate_->SetDictionaryValue(dictionary_value); 147 pref_delegate_->SetDictionaryValue(*(prefs_.get()));
148 }
149
150 std::map<nqe::internal::NetworkID, nqe::internal::CachedNetworkQuality>
151 NetworkQualitiesPrefsManager::ForceReadPrefsForTesting() const {
152 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread());
153 std::unique_ptr<base::DictionaryValue> value(
154 pref_delegate_->GetDictionaryValue().CreateDeepCopy());
155 return ConvertDictionaryValueToMap(value.get());
78 } 156 }
79 157
80 } // namespace net 158 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698