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 "chromecast/net/connectivity_checker_impl.h" | 5 #include "chromecast/net/connectivity_checker_impl.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "chromecast/base/metrics/cast_metrics_helper.h" | 14 #include "chromecast/base/metrics/cast_metrics_helper.h" |
15 #include "chromecast/chromecast_features.h" | 15 #include "chromecast/chromecast_features.h" |
16 #include "chromecast/net/net_switches.h" | 16 #include "chromecast/net/net_switches.h" |
17 #include "net/base/request_priority.h" | 17 #include "net/base/request_priority.h" |
18 #include "net/http/http_response_headers.h" | 18 #include "net/http/http_response_headers.h" |
19 #include "net/http/http_response_info.h" | 19 #include "net/http/http_response_info.h" |
20 #include "net/http/http_status_code.h" | 20 #include "net/http/http_status_code.h" |
21 #include "net/proxy/proxy_config.h" | |
22 #include "net/proxy/proxy_config_service_fixed.h" | |
23 #include "net/socket/ssl_client_socket.h" | 21 #include "net/socket/ssl_client_socket.h" |
24 #include "net/url_request/url_request_context.h" | 22 #include "net/url_request/url_request_context.h" |
25 #include "net/url_request/url_request_context_builder.h" | 23 #include "net/url_request/url_request_context_builder.h" |
| 24 #include "net/url_request/url_request_context_getter.h" |
26 | 25 |
27 namespace chromecast { | 26 namespace chromecast { |
28 | 27 |
29 namespace { | 28 namespace { |
30 | 29 |
31 // How often connectivity checks are performed in seconds while not connected. | 30 // How often connectivity checks are performed in seconds while not connected. |
32 const unsigned int kConnectivityPeriodSeconds = 1; | 31 const unsigned int kConnectivityPeriodSeconds = 1; |
33 | 32 |
34 #if BUILDFLAG(IS_CAST_AUDIO_ONLY) | 33 #if BUILDFLAG(IS_CAST_AUDIO_ONLY) |
35 // How often connectivity checks are performed in seconds while connected. | 34 // How often connectivity checks are performed in seconds while connected. |
(...skipping 15 matching lines...) Expand all Loading... |
51 // Histogram "Cast.Network.Down.Duration.In.Seconds" shows 40% of network | 50 // Histogram "Cast.Network.Down.Duration.In.Seconds" shows 40% of network |
52 // downtime is less than 3 seconds. | 51 // downtime is less than 3 seconds. |
53 const char kNetworkChangedDelayInSeconds = 3; | 52 const char kNetworkChangedDelayInSeconds = 3; |
54 | 53 |
55 const char kMetricNameNetworkConnectivityCheckingErrorType[] = | 54 const char kMetricNameNetworkConnectivityCheckingErrorType[] = |
56 "Network.ConnectivityChecking.ErrorType"; | 55 "Network.ConnectivityChecking.ErrorType"; |
57 | 56 |
58 } // namespace | 57 } // namespace |
59 | 58 |
60 ConnectivityCheckerImpl::ConnectivityCheckerImpl( | 59 ConnectivityCheckerImpl::ConnectivityCheckerImpl( |
61 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) | 60 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 61 net::URLRequestContextGetter* url_request_context_getter) |
62 : ConnectivityChecker(), | 62 : ConnectivityChecker(), |
63 task_runner_(task_runner), | 63 task_runner_(task_runner), |
64 connected_(false), | 64 connected_(false), |
65 connection_type_(net::NetworkChangeNotifier::CONNECTION_NONE), | 65 connection_type_(net::NetworkChangeNotifier::CONNECTION_NONE), |
66 check_errors_(0), | 66 check_errors_(0), |
67 network_changed_pending_(false) { | 67 network_changed_pending_(false) { |
68 DCHECK(task_runner_.get()); | 68 DCHECK(task_runner_.get()); |
| 69 |
69 task_runner->PostTask(FROM_HERE, | 70 task_runner->PostTask(FROM_HERE, |
70 base::Bind(&ConnectivityCheckerImpl::Initialize, this)); | 71 base::Bind(&ConnectivityCheckerImpl::Initialize, this, |
| 72 url_request_context_getter)); |
71 } | 73 } |
72 | 74 |
73 void ConnectivityCheckerImpl::Initialize() { | 75 void ConnectivityCheckerImpl::Initialize( |
| 76 net::URLRequestContextGetter* url_request_context_getter) { |
74 DCHECK(task_runner_->BelongsToCurrentThread()); | 77 DCHECK(task_runner_->BelongsToCurrentThread()); |
75 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 78 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
76 base::CommandLine::StringType check_url_str = | 79 base::CommandLine::StringType check_url_str = |
77 command_line->GetSwitchValueNative(switches::kConnectivityCheckUrl); | 80 command_line->GetSwitchValueNative(switches::kConnectivityCheckUrl); |
78 connectivity_check_url_.reset(new GURL( | 81 connectivity_check_url_.reset(new GURL( |
79 check_url_str.empty() ? kDefaultConnectivityCheckUrl : check_url_str)); | 82 check_url_str.empty() ? kDefaultConnectivityCheckUrl : check_url_str)); |
80 | 83 |
81 net::URLRequestContextBuilder builder; | 84 url_request_context_ = url_request_context_getter->GetURLRequestContext(); |
82 builder.set_proxy_config_service( | |
83 base::MakeUnique<net::ProxyConfigServiceFixed>( | |
84 net::ProxyConfig::CreateDirect())); | |
85 builder.DisableHttpCache(); | |
86 url_request_context_ = builder.Build(); | |
87 | 85 |
88 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); | 86 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); |
89 task_runner_->PostTask(FROM_HERE, | 87 task_runner_->PostTask(FROM_HERE, |
90 base::Bind(&ConnectivityCheckerImpl::Check, this)); | 88 base::Bind(&ConnectivityCheckerImpl::Check, this)); |
91 } | 89 } |
92 | 90 |
93 ConnectivityCheckerImpl::~ConnectivityCheckerImpl() { | 91 ConnectivityCheckerImpl::~ConnectivityCheckerImpl() { |
94 DCHECK(task_runner_.get()); | 92 DCHECK(task_runner_.get()); |
95 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); | 93 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); |
96 task_runner_->DeleteSoon(FROM_HERE, url_request_.release()); | 94 task_runner_->DeleteSoon(FROM_HERE, url_request_.release()); |
97 task_runner_->DeleteSoon(FROM_HERE, url_request_context_.release()); | |
98 } | 95 } |
99 | 96 |
100 bool ConnectivityCheckerImpl::Connected() const { | 97 bool ConnectivityCheckerImpl::Connected() const { |
101 base::AutoLock auto_lock(connected_lock_); | 98 base::AutoLock auto_lock(connected_lock_); |
102 return connected_; | 99 return connected_; |
103 } | 100 } |
104 | 101 |
105 void ConnectivityCheckerImpl::SetConnected(bool connected) { | 102 void ConnectivityCheckerImpl::SetConnected(bool connected) { |
106 DCHECK(task_runner_->BelongsToCurrentThread()); | 103 DCHECK(task_runner_->BelongsToCurrentThread()); |
107 if (connected_ == connected) | 104 if (connected_ == connected) |
108 return; | 105 return; |
109 { | 106 { |
110 base::AutoLock auto_lock(connected_lock_); | 107 base::AutoLock auto_lock(connected_lock_); |
111 connected_ = connected; | 108 connected_ = connected; |
112 } | 109 } |
113 Notify(connected); | 110 Notify(connected); |
114 LOG(INFO) << "Global connection is: " << (connected ? "Up" : "Down"); | 111 LOG(INFO) << "Global connection is: " << (connected ? "Up" : "Down"); |
115 } | 112 } |
116 | 113 |
117 void ConnectivityCheckerImpl::Check() { | 114 void ConnectivityCheckerImpl::Check() { |
118 task_runner_->PostTask(FROM_HERE, | 115 task_runner_->PostTask(FROM_HERE, |
119 base::Bind(&ConnectivityCheckerImpl::CheckInternal, this)); | 116 base::Bind(&ConnectivityCheckerImpl::CheckInternal, this)); |
120 } | 117 } |
121 | 118 |
122 void ConnectivityCheckerImpl::CheckInternal() { | 119 void ConnectivityCheckerImpl::CheckInternal() { |
123 DCHECK(task_runner_->BelongsToCurrentThread()); | 120 DCHECK(task_runner_->BelongsToCurrentThread()); |
124 DCHECK(url_request_context_.get()); | 121 DCHECK(url_request_context_); |
125 | 122 |
126 // Don't check connectivity if network is offline, because Internet could be | 123 // Don't check connectivity if network is offline, because Internet could be |
127 // accessible via netifs ignored. | 124 // accessible via netifs ignored. |
128 if (net::NetworkChangeNotifier::IsOffline()) | 125 if (net::NetworkChangeNotifier::IsOffline()) |
129 return; | 126 return; |
130 | 127 |
131 // If url_request_ is non-null, there is already a check going on. Don't | 128 // If url_request_ is non-null, there is already a check going on. Don't |
132 // start another. | 129 // start another. |
133 if (url_request_.get()) | 130 if (url_request_.get()) |
134 return; | 131 return; |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 void ConnectivityCheckerImpl::Cancel() { | 248 void ConnectivityCheckerImpl::Cancel() { |
252 DCHECK(task_runner_->BelongsToCurrentThread()); | 249 DCHECK(task_runner_->BelongsToCurrentThread()); |
253 if (!url_request_.get()) | 250 if (!url_request_.get()) |
254 return; | 251 return; |
255 VLOG(2) << "Cancel connectivity check in progress"; | 252 VLOG(2) << "Cancel connectivity check in progress"; |
256 url_request_.reset(nullptr); // URLRequest::Cancel() is called in destructor. | 253 url_request_.reset(nullptr); // URLRequest::Cancel() is called in destructor. |
257 timeout_.Cancel(); | 254 timeout_.Cancel(); |
258 } | 255 } |
259 | 256 |
260 } // namespace chromecast | 257 } // namespace chromecast |
OLD | NEW |