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 "chrome/browser/net/nqe/ui_network_quality_estimator_service.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/message_loop/message_loop.h" | |
9 #include "base/task_runner.h" | |
10 #include "chrome/browser/browser_process.h" | |
11 #include "chrome/browser/io_thread.h" | |
12 | |
13 namespace chrome_browser_net { | |
14 | |
15 // A class that sets itself as an observer of the EffectiveconnectionType for | |
16 // the browser IO thread. It reports any change in EffectiveConnectionType back | |
17 // to the UI service. | |
18 // It is created on the UI thread, but used and deleted on the IO thread. | |
19 class UINetworkQualityEstimatorService::IONetworkQualityObserver | |
20 : public net::NetworkQualityEstimator::EffectiveConnectionTypeObserver { | |
21 public: | |
22 IONetworkQualityObserver( | |
23 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner, | |
24 base::WeakPtr<UINetworkQualityEstimatorService> service) | |
25 : ui_task_runner_(ui_task_runner), service_(service) { | |
26 DCHECK(ui_task_runner_); | |
27 // This is created on the UI thread, but used only on the UI thread. | |
tbansal1
2016/07/13 17:56:11
s/used only on the UI thread/used only on the IO t
RyanSturm
2016/07/13 18:03:45
Done.
| |
28 thread_checker_.DetachFromThread(); | |
29 } | |
30 | |
31 ~IONetworkQualityObserver() override { | |
32 DCHECK(thread_checker_.CalledOnValidThread()); | |
33 if (network_quality_estimator_) | |
34 network_quality_estimator_->RemoveEffectiveConnectionTypeObserver(this); | |
35 } | |
36 | |
37 void InitializeOnIOThread(IOThread* io_thread) { | |
38 DCHECK(thread_checker_.CalledOnValidThread()); | |
39 if (!io_thread->globals()->network_quality_estimator) | |
40 return; | |
41 network_quality_estimator_ = | |
42 io_thread->globals()->network_quality_estimator->GetWeakPtr(); | |
43 if (!network_quality_estimator_) | |
44 return; | |
45 network_quality_estimator_->AddEffectiveConnectionTypeObserver(this); | |
46 ui_task_runner_->PostTask( | |
47 FROM_HERE, | |
48 base::Bind( | |
49 &UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged, | |
50 service_, | |
51 network_quality_estimator_->GetEffectiveConnectionType())); | |
52 } | |
53 | |
54 // net::NetworkQualityEstimator::EffectiveConnectionTypeObserver | |
55 // implementation: | |
56 void OnEffectiveConnectionTypeChanged( | |
57 net::NetworkQualityEstimator::EffectiveConnectionType type) override { | |
58 DCHECK(thread_checker_.CalledOnValidThread()); | |
59 ui_task_runner_->PostTask( | |
60 FROM_HERE, | |
61 base::Bind( | |
62 &UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged, | |
63 service_, type)); | |
64 } | |
65 | |
66 private: | |
67 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; | |
68 base::WeakPtr<UINetworkQualityEstimatorService> service_; | |
69 base::WeakPtr<net::NetworkQualityEstimator> network_quality_estimator_; | |
70 base::ThreadChecker thread_checker_; | |
71 | |
72 DISALLOW_COPY_AND_ASSIGN(IONetworkQualityObserver); | |
73 }; | |
74 | |
75 UINetworkQualityEstimatorService::UINetworkQualityEstimatorService( | |
76 Profile* profile) | |
77 : type_(net::NetworkQualityEstimator::EFFECTIVE_CONNECTION_TYPE_UNKNOWN), | |
78 io_observer_(nullptr), | |
79 weak_factory_(this) {} | |
80 | |
81 UINetworkQualityEstimatorService::~UINetworkQualityEstimatorService() { | |
82 DCHECK(thread_checker_.CalledOnValidThread()); | |
83 DCHECK(io_task_runner_->DeleteSoon(FROM_HERE, io_observer_)); | |
84 } | |
85 | |
86 void UINetworkQualityEstimatorService::Initialize( | |
87 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner, | |
88 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) { | |
89 DCHECK(thread_checker_.CalledOnValidThread()); | |
90 io_task_runner_ = io_task_runner; | |
91 io_observer_ = | |
92 new IONetworkQualityObserver(ui_task_runner, weak_factory_.GetWeakPtr()); | |
93 io_task_runner_->PostTask( | |
94 FROM_HERE, base::Bind(&IONetworkQualityObserver::InitializeOnIOThread, | |
95 base::Unretained(io_observer_), | |
96 g_browser_process->io_thread())); | |
97 } | |
98 | |
99 void UINetworkQualityEstimatorService::Shutdown() { | |
100 DCHECK(thread_checker_.CalledOnValidThread()); | |
101 weak_factory_.InvalidateWeakPtrs(); | |
102 } | |
103 | |
104 void UINetworkQualityEstimatorService::EffectiveConnectionTypeChanged( | |
105 net::NetworkQualityEstimator::EffectiveConnectionType type) { | |
106 DCHECK(thread_checker_.CalledOnValidThread()); | |
107 type_ = type; | |
108 } | |
109 | |
110 net::NetworkQualityEstimator::EffectiveConnectionType | |
111 UINetworkQualityEstimatorService::type() const { | |
112 DCHECK(thread_checker_.CalledOnValidThread()); | |
113 return type_; | |
114 } | |
115 | |
116 } // namespace chrome_browser_net | |
OLD | NEW |