OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "net/nqe/network_qualities_prefs_manager.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/sequenced_task_runner.h" | |
11 #include "base/threading/thread_checker.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" | |
16 | |
17 namespace net { | |
18 | |
19 NetworkQualitiesPrefsManager::NetworkQualitiesPrefsManager( | |
bengr
2016/09/22 23:35:46
Is the NetworkQualitiesPrefsManager a Pref thread
tbansal1
2016/09/23 17:10:32
It will be created and destroyed by UI_NQE keyed s
| |
20 std::unique_ptr<PrefDelegate> pref_delegate) | |
21 : pref_delegate_(std::move(pref_delegate)), | |
22 pref_task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
23 network_quality_estimator_(nullptr), | |
24 pref_weak_ptr_factory_(this) { | |
25 DCHECK(pref_delegate_); | |
26 | |
27 pref_weak_ptr_ = pref_weak_ptr_factory_.GetWeakPtr(); | |
28 } | |
29 | |
30 NetworkQualitiesPrefsManager::~NetworkQualitiesPrefsManager() { | |
31 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); | |
32 network_quality_estimator_->RemoveNetworkQualitiesCacheObserver(this); | |
bengr
2016/09/22 23:35:46
This seems unsafe, e.g., if called before Initiali
tbansal1
2016/09/23 17:10:32
Done.
| |
33 } | |
34 | |
35 void NetworkQualitiesPrefsManager::InitializeOnNetworkThread( | |
36 NetworkQualityEstimator* network_quality_estimator) { | |
37 DCHECK(!network_task_runner_); | |
38 DCHECK(network_quality_estimator); | |
39 | |
40 network_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | |
41 | |
bengr
2016/09/22 23:35:46
nit: I'd remove the blank lines on 41 and 43.
tbansal1
2016/09/23 17:10:32
Done.
| |
42 network_quality_estimator_ = network_quality_estimator; | |
43 | |
44 network_quality_estimator_->AddNetworkQualitiesCacheObserver(this); | |
45 } | |
46 | |
47 void NetworkQualitiesPrefsManager::OnChangeInCachedNetworkQuality( | |
48 const nqe::internal::NetworkID& network_id, | |
49 const nqe::internal::CachedNetworkQuality& cached_network_quality) { | |
50 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); | |
51 | |
52 // Notify |this| on the pref thread. | |
53 pref_task_runner_->PostTask( | |
54 FROM_HERE, | |
55 base::Bind(&NetworkQualitiesPrefsManager:: | |
56 OnChangeInCachedNetworkQualityOnPrefThread, | |
57 pref_weak_ptr_, network_id, cached_network_quality)); | |
58 } | |
59 | |
60 void NetworkQualitiesPrefsManager::ShutdownOnPrefThread() { | |
61 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); | |
62 pref_delegate_.reset(); | |
63 } | |
64 | |
65 void NetworkQualitiesPrefsManager::OnChangeInCachedNetworkQualityOnPrefThread( | |
66 const nqe::internal::NetworkID& network_id, | |
67 const nqe::internal::CachedNetworkQuality& cached_network_quality) { | |
68 // The prefs can only be written on the pref thread. | |
69 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); | |
70 | |
71 base::DictionaryValue dictionary_value; | |
72 dictionary_value.SetString( | |
73 network_id.ToString(), | |
74 GetNameForEffectiveConnectionType( | |
75 cached_network_quality.effective_connection_type())); | |
76 | |
77 // Notify the pref delegate so that it updates the prefs on the disk. | |
bengr
2016/09/22 23:35:46
Do all pref delegates update to disk? I'd remove "
tbansal1
2016/09/23 17:10:32
Yes. They all update to disk.
| |
78 pref_delegate_->SetDictionaryValue(dictionary_value); | |
79 } | |
80 | |
81 } // namespace net | |
OLD | NEW |