| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "content/browser/net/network_quality_observer_impl.h" | 5 #include "content/browser/net/network_quality_observer_impl.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "content/browser/renderer_host/render_process_host_impl.h" | 8 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 9 #include "content/common/view_messages.h" | 9 #include "content/common/view_messages.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 | 51 |
| 52 } // namespace | 52 } // namespace |
| 53 | 53 |
| 54 namespace content { | 54 namespace content { |
| 55 | 55 |
| 56 // UiThreadObserver observes the changes to the network quality on the UI | 56 // UiThreadObserver observes the changes to the network quality on the UI |
| 57 // thread, and notifies the renderers of the change in the network quality. | 57 // thread, and notifies the renderers of the change in the network quality. |
| 58 class NetworkQualityObserverImpl::UiThreadObserver | 58 class NetworkQualityObserverImpl::UiThreadObserver |
| 59 : public content::NotificationObserver { | 59 : public content::NotificationObserver { |
| 60 public: | 60 public: |
| 61 UiThreadObserver() {} | 61 UiThreadObserver() |
| 62 : last_notified_type_(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN) {} |
| 62 | 63 |
| 63 ~UiThreadObserver() override { | 64 ~UiThreadObserver() override { |
| 64 registrar_.Remove(this, NOTIFICATION_RENDERER_PROCESS_CREATED, | 65 registrar_.Remove(this, NOTIFICATION_RENDERER_PROCESS_CREATED, |
| 65 NotificationService::AllSources()); | 66 NotificationService::AllSources()); |
| 66 } | 67 } |
| 67 | 68 |
| 68 void InitOnUIThread() { | 69 void InitOnUIThread() { |
| 69 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 70 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 70 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_CREATED, | 71 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_CREATED, |
| 71 NotificationService::AllSources()); | 72 NotificationService::AllSources()); |
| 72 } | 73 } |
| 73 | 74 |
| 75 void OnEffectiveConnectionTypeChanged(net::EffectiveConnectionType type) { |
| 76 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 77 |
| 78 if (last_notified_type_ == type) |
| 79 return; |
| 80 |
| 81 last_notified_type_ = type; |
| 82 |
| 83 // Notify all the existing renderers of the change in the network quality. |
| 84 for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator()); |
| 85 !it.IsAtEnd(); it.Advance()) { |
| 86 it.GetCurrentValue()->GetRendererInterface()->OnNetworkQualityChanged( |
| 87 last_notified_type_, last_notified_network_quality_.http_rtt(), |
| 88 last_notified_network_quality_.transport_rtt(), |
| 89 last_notified_network_quality_.downstream_throughput_kbps()); |
| 90 } |
| 91 } |
| 92 |
| 74 void OnRTTOrThroughputEstimatesComputed( | 93 void OnRTTOrThroughputEstimatesComputed( |
| 75 const net::nqe::internal::NetworkQuality& network_quality) { | 94 const net::nqe::internal::NetworkQuality& network_quality) { |
| 76 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 95 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 77 | 96 |
| 78 last_notified_network_quality_ = network_quality; | 97 last_notified_network_quality_ = network_quality; |
| 79 | 98 |
| 80 // Notify all the existing renderers of the change in the network quality. | 99 // Notify all the existing renderers of the change in the network quality. |
| 81 for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator()); | 100 for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator()); |
| 82 !it.IsAtEnd(); it.Advance()) { | 101 !it.IsAtEnd(); it.Advance()) { |
| 83 it.GetCurrentValue()->GetRendererInterface()->OnNetworkQualityChanged( | 102 it.GetCurrentValue()->GetRendererInterface()->OnNetworkQualityChanged( |
| 84 last_notified_network_quality_.http_rtt(), | 103 last_notified_type_, last_notified_network_quality_.http_rtt(), |
| 85 last_notified_network_quality_.transport_rtt(), | 104 last_notified_network_quality_.transport_rtt(), |
| 86 last_notified_network_quality_.downstream_throughput_kbps()); | 105 last_notified_network_quality_.downstream_throughput_kbps()); |
| 87 } | 106 } |
| 88 } | 107 } |
| 89 | 108 |
| 90 private: | 109 private: |
| 91 // NotificationObserver implementation: | 110 // NotificationObserver implementation: |
| 92 void Observe(int type, | 111 void Observe(int type, |
| 93 const NotificationSource& source, | 112 const NotificationSource& source, |
| 94 const NotificationDetails& details) override { | 113 const NotificationDetails& details) override { |
| 95 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 114 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 96 DCHECK_EQ(NOTIFICATION_RENDERER_PROCESS_CREATED, type); | 115 DCHECK_EQ(NOTIFICATION_RENDERER_PROCESS_CREATED, type); |
| 97 | 116 |
| 98 RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr(); | 117 RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr(); |
| 99 | 118 |
| 100 // Notify the newly created renderer of the current network quality. | 119 // Notify the newly created renderer of the current network quality. |
| 101 rph->GetRendererInterface()->OnNetworkQualityChanged( | 120 rph->GetRendererInterface()->OnNetworkQualityChanged( |
| 102 last_notified_network_quality_.http_rtt(), | 121 last_notified_type_, last_notified_network_quality_.http_rtt(), |
| 103 last_notified_network_quality_.transport_rtt(), | 122 last_notified_network_quality_.transport_rtt(), |
| 104 last_notified_network_quality_.downstream_throughput_kbps()); | 123 last_notified_network_quality_.downstream_throughput_kbps()); |
| 105 } | 124 } |
| 106 | 125 |
| 107 content::NotificationRegistrar registrar_; | 126 content::NotificationRegistrar registrar_; |
| 108 | 127 |
| 109 // The network quality that was last sent to the renderers. | 128 // The network quality that was last sent to the renderers. |
| 129 net::EffectiveConnectionType last_notified_type_; |
| 110 net::nqe::internal::NetworkQuality last_notified_network_quality_; | 130 net::nqe::internal::NetworkQuality last_notified_network_quality_; |
| 111 | 131 |
| 112 DISALLOW_COPY_AND_ASSIGN(UiThreadObserver); | 132 DISALLOW_COPY_AND_ASSIGN(UiThreadObserver); |
| 113 }; | 133 }; |
| 114 | 134 |
| 115 NetworkQualityObserverImpl::NetworkQualityObserverImpl( | 135 NetworkQualityObserverImpl::NetworkQualityObserverImpl( |
| 116 net::NetworkQualityEstimator* network_quality_estimator) | 136 net::NetworkQualityEstimator* network_quality_estimator) |
| 117 : network_quality_estimator_(network_quality_estimator) { | 137 : network_quality_estimator_(network_quality_estimator), |
| 138 last_notified_type_(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN) { |
| 118 network_quality_estimator_->AddRTTAndThroughputEstimatesObserver(this); | 139 network_quality_estimator_->AddRTTAndThroughputEstimatesObserver(this); |
| 140 network_quality_estimator_->AddEffectiveConnectionTypeObserver(this); |
| 119 | 141 |
| 120 ui_thread_observer_ = base::MakeUnique<UiThreadObserver>(); | 142 ui_thread_observer_ = base::MakeUnique<UiThreadObserver>(); |
| 121 BrowserThread::PostTask( | 143 BrowserThread::PostTask( |
| 122 BrowserThread::UI, FROM_HERE, | 144 BrowserThread::UI, FROM_HERE, |
| 123 base::BindOnce(&UiThreadObserver::InitOnUIThread, | 145 base::BindOnce(&UiThreadObserver::InitOnUIThread, |
| 124 base::Unretained(ui_thread_observer_.get()))); | 146 base::Unretained(ui_thread_observer_.get()))); |
| 125 } | 147 } |
| 126 | 148 |
| 127 NetworkQualityObserverImpl::~NetworkQualityObserverImpl() { | 149 NetworkQualityObserverImpl::~NetworkQualityObserverImpl() { |
| 128 DCHECK(thread_checker_.CalledOnValidThread()); | 150 DCHECK(thread_checker_.CalledOnValidThread()); |
| 129 network_quality_estimator_->RemoveRTTAndThroughputEstimatesObserver(this); | 151 network_quality_estimator_->RemoveRTTAndThroughputEstimatesObserver(this); |
| 152 network_quality_estimator_->RemoveEffectiveConnectionTypeObserver(this); |
| 130 | 153 |
| 131 DCHECK(ui_thread_observer_); | 154 DCHECK(ui_thread_observer_); |
| 132 | 155 |
| 133 // If possible, delete |ui_thread_observer_| on UI thread. | 156 // If possible, delete |ui_thread_observer_| on UI thread. |
| 134 UiThreadObserver* ui_thread_observer_ptr = ui_thread_observer_.release(); | 157 UiThreadObserver* ui_thread_observer_ptr = ui_thread_observer_.release(); |
| 135 bool posted = BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, | 158 bool posted = BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, |
| 136 ui_thread_observer_ptr); | 159 ui_thread_observer_ptr); |
| 137 | 160 |
| 138 if (!posted) | 161 if (!posted) |
| 139 delete ui_thread_observer_ptr; | 162 delete ui_thread_observer_ptr; |
| 140 } | 163 } |
| 141 | 164 |
| 165 void NetworkQualityObserverImpl::OnEffectiveConnectionTypeChanged( |
| 166 net::EffectiveConnectionType type) { |
| 167 DCHECK(thread_checker_.CalledOnValidThread()); |
| 168 |
| 169 if (last_notified_type_ == type) |
| 170 return; |
| 171 |
| 172 last_notified_type_ = type; |
| 173 |
| 174 BrowserThread::PostTask( |
| 175 BrowserThread::UI, FROM_HERE, |
| 176 base::BindOnce(&UiThreadObserver::OnEffectiveConnectionTypeChanged, |
| 177 base::Unretained(ui_thread_observer_.get()), |
| 178 last_notified_type_)); |
| 179 } |
| 180 |
| 142 void NetworkQualityObserverImpl::OnRTTOrThroughputEstimatesComputed( | 181 void NetworkQualityObserverImpl::OnRTTOrThroughputEstimatesComputed( |
| 143 base::TimeDelta http_rtt, | 182 base::TimeDelta http_rtt, |
| 144 base::TimeDelta transport_rtt, | 183 base::TimeDelta transport_rtt, |
| 145 int32_t downstream_throughput_kbps) { | 184 int32_t downstream_throughput_kbps) { |
| 146 DCHECK(thread_checker_.CalledOnValidThread()); | 185 DCHECK(thread_checker_.CalledOnValidThread()); |
| 147 | 186 |
| 148 // Check if any of the network quality metrics changed meaningfully. | 187 // Check if any of the network quality metrics changed meaningfully. |
| 149 bool http_rtt_changed = MetricChangedMeaningfully( | 188 bool http_rtt_changed = MetricChangedMeaningfully( |
| 150 last_notified_network_quality_.http_rtt().InMilliseconds(), | 189 last_notified_network_quality_.http_rtt().InMilliseconds(), |
| 151 http_rtt.InMilliseconds()); | 190 http_rtt.InMilliseconds()); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 175 } | 214 } |
| 176 | 215 |
| 177 std::unique_ptr<net::NetworkQualityEstimator::RTTAndThroughputEstimatesObserver> | 216 std::unique_ptr<net::NetworkQualityEstimator::RTTAndThroughputEstimatesObserver> |
| 178 CreateNetworkQualityObserver( | 217 CreateNetworkQualityObserver( |
| 179 net::NetworkQualityEstimator* network_quality_estimator) { | 218 net::NetworkQualityEstimator* network_quality_estimator) { |
| 180 return base::MakeUnique<NetworkQualityObserverImpl>( | 219 return base::MakeUnique<NetworkQualityObserverImpl>( |
| 181 network_quality_estimator); | 220 network_quality_estimator); |
| 182 } | 221 } |
| 183 | 222 |
| 184 } // namespace content | 223 } // namespace content |
| OLD | NEW |