| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/base/socket_performance_watcher.h" | 5 #include "net/base/socket_performance_watcher.h" |
| 6 | 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/location.h" |
| 7 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/single_thread_task_runner.h" |
| 8 | 11 |
| 9 namespace net { | 12 namespace net { |
| 10 | 13 |
| 11 SocketPerformanceWatcher::SocketPerformanceWatcher( | 14 SocketPerformanceWatcher::SocketPerformanceWatcher( |
| 12 const SocketPerformanceWatcherFactory::Protocol protocol, | 15 const SocketPerformanceWatcherFactory::Protocol protocol, |
| 13 SocketPerformanceWatcherFactory* socket_performance_watcher_factory) | 16 const base::WeakPtr<SocketPerformanceWatcherFactory>& |
| 17 socket_performance_watcher_factory, |
| 18 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) |
| 14 : protocol_(protocol), | 19 : protocol_(protocol), |
| 15 socket_performance_watcher_factory_(socket_performance_watcher_factory) { | 20 rtt_notification_received_count_(0), |
| 16 DCHECK(socket_performance_watcher_factory_); | 21 socket_performance_watcher_factory_(socket_performance_watcher_factory), |
| 22 task_runner_(task_runner), |
| 23 weak_ptr_factory_(this) { |
| 24 DCHECK(task_runner_); |
| 17 | 25 |
| 18 switch (protocol) { | 26 switch (protocol) { |
| 19 case SocketPerformanceWatcherFactory::PROTOCOL_TCP: | 27 case SocketPerformanceWatcherFactory::PROTOCOL_TCP: |
| 20 case SocketPerformanceWatcherFactory::PROTOCOL_QUIC: | 28 case SocketPerformanceWatcherFactory::PROTOCOL_QUIC: |
| 21 return; | 29 return; |
| 22 default: | 30 default: |
| 23 NOTREACHED(); | 31 NOTREACHED(); |
| 24 } | 32 } |
| 25 } | 33 } |
| 26 | 34 |
| 27 SocketPerformanceWatcher::~SocketPerformanceWatcher() {} | 35 SocketPerformanceWatcher::~SocketPerformanceWatcher() {} |
| 28 | 36 |
| 29 void SocketPerformanceWatcher::OnUpdatedRTTAvailable( | 37 void SocketPerformanceWatcher::OnUpdatedRTTAvailable( |
| 30 const base::TimeDelta& rtt) const { | 38 const base::TimeDelta& rtt) { |
| 31 socket_performance_watcher_factory_->OnUpdatedRTTAvailable(protocol_, rtt); | 39 DCHECK(thread_checker_.CalledOnValidThread()); |
| 40 |
| 41 rtt_notification_received_count_++; |
| 42 |
| 43 task_runner_->PostTask( |
| 44 FROM_HERE, |
| 45 base::Bind(&SocketPerformanceWatcherFactory::OnUpdatedRTTAvailable, |
| 46 socket_performance_watcher_factory_, protocol_, rtt)); |
| 47 } |
| 48 |
| 49 bool SocketPerformanceWatcher::ShouldNotifyUpdatedRTT() const { |
| 50 DCHECK(thread_checker_.CalledOnValidThread()); |
| 51 |
| 52 // Currently using a small value until crbug.com/590300 is fixed. TCP/QUIC |
| 53 // start with a hard-coded RTT estimate, and converge to a more accurate value |
| 54 // as more packets are received. This means that SPW should receive frequent |
| 55 // RTT notifications when the TCP/QUIC connection has just started, and less |
| 56 // frequent notification afterwards. |
| 57 return rtt_notification_received_count_ < 1; |
| 58 } |
| 59 |
| 60 void SocketPerformanceWatcher::Reset() { |
| 61 DCHECK(thread_checker_.CalledOnValidThread()); |
| 62 |
| 63 rtt_notification_received_count_ = 0; |
| 64 |
| 65 task_runner_->PostTask( |
| 66 FROM_HERE, base::Bind(&SocketPerformanceWatcherFactory::OnWatcherReset, |
| 67 socket_performance_watcher_factory_)); |
| 68 } |
| 69 |
| 70 base::WeakPtr<SocketPerformanceWatcher> SocketPerformanceWatcher::GetWeakPtr() { |
| 71 DCHECK(thread_checker_.CalledOnValidThread()); |
| 72 |
| 73 return weak_ptr_factory_.GetWeakPtr(); |
| 32 } | 74 } |
| 33 | 75 |
| 34 } // namespace net | 76 } // namespace net |
| OLD | NEW |