OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2017 The Chromium Authors. All rights reserved. | |
nasko
2017/05/08 18:27:45
No "(c)".
tbansal1
2017/05/08 19:50:18
Done.
| |
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 "content/browser/net/network_quality_observer_impl.h" | |
6 | |
7 #include "base/memory/ptr_util.h" | |
8 #include "content/browser/renderer_host/render_process_host_impl.h" | |
9 #include "content/common/view_messages.h" | |
10 #include "content/public/browser/browser_thread.h" | |
11 #include "content/public/browser/notification_observer.h" | |
12 #include "content/public/browser/notification_registrar.h" | |
13 #include "content/public/browser/notification_service.h" | |
14 #include "content/public/browser/notification_types.h" | |
15 #include "content/public/browser/render_process_host.h" | |
16 | |
17 namespace { | |
18 | |
19 // Returns true if the |current_value| is meaningfully different from the | |
20 // |past_value|. | |
21 bool MetricChangedMeaningfully(int32_t past_value, int32_t current_value) { | |
22 if ((past_value == net::nqe::internal::INVALID_RTT_THROUGHPUT) != | |
23 (current_value == net::nqe::internal::INVALID_RTT_THROUGHPUT)) { | |
24 return true; | |
25 } | |
26 | |
27 if (past_value == net::nqe::internal::INVALID_RTT_THROUGHPUT && | |
28 current_value == net::nqe::internal::INVALID_RTT_THROUGHPUT) { | |
29 return false; | |
30 } | |
31 | |
32 // Metric has changed meaningfully only if (i) the difference between the two | |
33 // values exceed the threshold; and, (ii) the ratio of the values also exceeds | |
34 // the threshold. | |
35 static const int kMinDifferenceInMetrics = 100; | |
36 static const float kMinRatio = 1.2f; | |
37 | |
38 if (std::abs(past_value - current_value) < kMinDifferenceInMetrics) { | |
39 // The absolute change in the value is not sufficient enough. | |
40 return false; | |
41 } | |
42 | |
43 if (past_value < (kMinRatio * current_value) && | |
44 current_value < (kMinRatio * past_value)) { | |
45 // The relative change in the value is not sufficient enough. | |
46 return false; | |
47 } | |
48 | |
49 return true; | |
50 } | |
51 | |
52 } // namespace | |
53 | |
54 namespace content { | |
55 | |
56 // UIObserver observes the changes to the network quality on the UI thread, and | |
57 // notifies the renderers of the change in the network quality. | |
58 class NetworkQualityObserverImpl::UIObserver | |
59 : public content::NotificationObserver { | |
60 public: | |
61 UIObserver() {} | |
62 | |
63 ~UIObserver() override {} | |
64 | |
65 void InitOnUI() { | |
nasko
2017/05/08 18:27:45
nit: InitOnUIThread.
tbansal1
2017/05/08 19:50:19
Done.
| |
66 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
67 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_CREATED, | |
68 NotificationService::AllSources()); | |
69 } | |
70 | |
71 void OnRTTOrThroughputEstimatesComputed( | |
72 const net::nqe::internal::NetworkQuality& network_quality) { | |
73 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
74 | |
75 last_notified_network_quality_ = network_quality; | |
76 | |
77 // Notify all the existing renderers of the change in the network quality. | |
78 for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator()); | |
79 !it.IsAtEnd(); it.Advance()) { | |
80 it.GetCurrentValue()->GetRendererInterface()->OnNetworkQualityChanged( | |
81 last_notified_network_quality_.http_rtt().InMilliseconds(), | |
82 last_notified_network_quality_.transport_rtt().InMilliseconds(), | |
83 last_notified_network_quality_.downstream_throughput_kbps()); | |
84 } | |
85 } | |
86 | |
87 private: | |
88 // NotificationObserver implementation: | |
89 void Observe(int type, | |
90 const NotificationSource& source, | |
91 const NotificationDetails& details) override { | |
92 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
93 DCHECK_EQ(NOTIFICATION_RENDERER_PROCESS_CREATED, type); | |
94 | |
95 RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr(); | |
96 | |
97 // Notify the newly created renderer of the current network quality. | |
98 rph->GetRendererInterface()->OnNetworkQualityChanged( | |
99 last_notified_network_quality_.http_rtt().InMilliseconds(), | |
100 last_notified_network_quality_.transport_rtt().InMilliseconds(), | |
101 last_notified_network_quality_.downstream_throughput_kbps()); | |
102 } | |
103 | |
104 content::NotificationRegistrar registrar_; | |
105 | |
106 net::nqe::internal::NetworkQuality last_notified_network_quality_; | |
nasko
2017/05/08 18:27:44
nit: No need for empty lines between members, if t
tbansal1
2017/05/08 19:50:19
Done.
| |
107 | |
108 DISALLOW_COPY_AND_ASSIGN(UIObserver); | |
109 }; | |
110 | |
111 NetworkQualityObserverImpl::NetworkQualityObserverImpl( | |
112 net::NetworkQualityEstimator* network_quality_estimator) | |
113 : network_quality_estimator_(network_quality_estimator) { | |
114 network_quality_estimator_->AddRTTAndThroughputEstimatesObserver(this); | |
115 | |
116 ui_oberver_ = base::WrapUnique(new UIObserver()); | |
RyanSturm
2017/05/08 17:37:20
base::MakeUnique
tbansal1
2017/05/08 19:50:18
Done.
| |
117 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
118 base::BindOnce(&UIObserver::InitOnUI, | |
119 base::Unretained(ui_oberver_.get()))); | |
120 } | |
121 | |
122 NetworkQualityObserverImpl::~NetworkQualityObserverImpl() { | |
123 DCHECK(thread_checker_.CalledOnValidThread()); | |
124 network_quality_estimator_->RemoveRTTAndThroughputEstimatesObserver(this); | |
125 | |
126 if (!ui_oberver_) | |
127 return; | |
128 | |
129 // |ui_oberver_| must be deleted on UI thread. | |
130 bool deleted = BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, | |
131 ui_oberver_.release()); | |
RyanSturm
2017/05/08 17:37:20
note: Seems odd in general that there is a pattern
tbansal1
2017/05/08 19:50:18
Yes, it is okay to use a raw pointer as well. Usin
| |
132 DCHECK(deleted); | |
133 // Silence unused variable warning in release builds. | |
RyanSturm
2017/05/08 17:37:20
Not sure you really need this DCHECK. I don't know
tbansal1
2017/05/08 19:50:19
Done.
| |
134 (void)deleted; | |
135 } | |
136 | |
137 void NetworkQualityObserverImpl::OnRTTOrThroughputEstimatesComputed( | |
138 base::TimeDelta http_rtt, | |
139 base::TimeDelta transport_rtt, | |
140 int32_t downstream_throughput_kbps) { | |
141 DCHECK(thread_checker_.CalledOnValidThread()); | |
142 | |
143 // Check if any of the network quality metrics changed meaningfully. | |
144 bool http_rtt_changed = MetricChangedMeaningfully( | |
145 last_notified_network_quality_.http_rtt().InMilliseconds(), | |
146 http_rtt.InMilliseconds()); | |
147 | |
148 bool transport_rtt_changed = MetricChangedMeaningfully( | |
149 last_notified_network_quality_.transport_rtt().InMilliseconds(), | |
150 transport_rtt.InMilliseconds()); | |
151 bool kbps_changed = MetricChangedMeaningfully( | |
152 last_notified_network_quality_.downstream_throughput_kbps(), | |
153 downstream_throughput_kbps); | |
154 | |
155 if (!http_rtt_changed && !transport_rtt_changed && !kbps_changed) { | |
156 // Return since none of the metrics changed meaningfully. This reduces | |
157 // the number of notifications to the different renderers every time | |
158 // the network quality is recomputed. | |
159 return; | |
160 } | |
161 | |
162 last_notified_network_quality_ = net::nqe::internal::NetworkQuality( | |
163 http_rtt, transport_rtt, downstream_throughput_kbps); | |
164 | |
165 BrowserThread::PostTask( | |
166 BrowserThread::UI, FROM_HERE, | |
167 base::BindOnce(&UIObserver::OnRTTOrThroughputEstimatesComputed, | |
168 base::Unretained(ui_oberver_.get()), | |
nasko
2017/05/08 18:27:45
Why is this Unretained? ui_observer_ is owned by t
tbansal1
2017/05/08 19:50:18
|ui_observer_| is destroyed only after the destruc
nasko
2017/05/09 20:27:47
It depends. I think what you have here is alternat
tbansal1
2017/05/09 21:37:18
Done.
| |
169 last_notified_network_quality_)); | |
170 } | |
171 | |
172 std::unique_ptr<net::NetworkQualityEstimator::RTTAndThroughputEstimatesObserver> | |
173 CreateNetworkQualityObserver( | |
174 net::NetworkQualityEstimator* network_quality_estimator) { | |
175 return base::MakeUnique<NetworkQualityObserverImpl>( | |
176 network_quality_estimator); | |
177 } | |
178 | |
179 } // namespace content | |
OLD | NEW |