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 url_request_context_getter_(url_request_context_getter) { | |
68 DCHECK(task_runner_.get()); | 69 DCHECK(task_runner_.get()); |
70 | |
69 task_runner->PostTask(FROM_HERE, | 71 task_runner->PostTask(FROM_HERE, |
70 base::Bind(&ConnectivityCheckerImpl::Initialize, this)); | 72 base::Bind(&ConnectivityCheckerImpl::Initialize, this)); |
71 } | 73 } |
72 | 74 |
73 void ConnectivityCheckerImpl::Initialize() { | 75 void ConnectivityCheckerImpl::Initialize() { |
74 DCHECK(task_runner_->BelongsToCurrentThread()); | 76 DCHECK(task_runner_->BelongsToCurrentThread()); |
75 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 77 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
76 base::CommandLine::StringType check_url_str = | 78 base::CommandLine::StringType check_url_str = |
77 command_line->GetSwitchValueNative(switches::kConnectivityCheckUrl); | 79 command_line->GetSwitchValueNative(switches::kConnectivityCheckUrl); |
78 connectivity_check_url_.reset(new GURL( | 80 connectivity_check_url_.reset(new GURL( |
79 check_url_str.empty() ? kDefaultConnectivityCheckUrl : check_url_str)); | 81 check_url_str.empty() ? kDefaultConnectivityCheckUrl : check_url_str)); |
80 | 82 |
81 net::URLRequestContextBuilder builder; | 83 url_request_context_ = url_request_context_getter_->GetURLRequestContext(); |
wzhong
2017/02/07 22:00:06
Nit: can url_request_context_ initialized in const
almasrymina
2017/02/08 00:48:04
Done.
| |
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 | 84 |
88 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); | 85 net::NetworkChangeNotifier::AddNetworkChangeObserver(this); |
89 task_runner_->PostTask(FROM_HERE, | 86 task_runner_->PostTask(FROM_HERE, |
90 base::Bind(&ConnectivityCheckerImpl::Check, this)); | 87 base::Bind(&ConnectivityCheckerImpl::Check, this)); |
91 } | 88 } |
92 | 89 |
93 ConnectivityCheckerImpl::~ConnectivityCheckerImpl() { | 90 ConnectivityCheckerImpl::~ConnectivityCheckerImpl() { |
94 DCHECK(task_runner_.get()); | 91 DCHECK(task_runner_.get()); |
95 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); | 92 net::NetworkChangeNotifier::RemoveNetworkChangeObserver(this); |
96 task_runner_->DeleteSoon(FROM_HERE, url_request_.release()); | 93 task_runner_->DeleteSoon(FROM_HERE, url_request_.release()); |
97 task_runner_->DeleteSoon(FROM_HERE, url_request_context_.release()); | |
98 } | 94 } |
99 | 95 |
100 bool ConnectivityCheckerImpl::Connected() const { | 96 bool ConnectivityCheckerImpl::Connected() const { |
101 base::AutoLock auto_lock(connected_lock_); | 97 base::AutoLock auto_lock(connected_lock_); |
102 return connected_; | 98 return connected_; |
103 } | 99 } |
104 | 100 |
105 void ConnectivityCheckerImpl::SetConnected(bool connected) { | 101 void ConnectivityCheckerImpl::SetConnected(bool connected) { |
106 DCHECK(task_runner_->BelongsToCurrentThread()); | 102 DCHECK(task_runner_->BelongsToCurrentThread()); |
107 if (connected_ == connected) | 103 if (connected_ == connected) |
108 return; | 104 return; |
109 { | 105 { |
110 base::AutoLock auto_lock(connected_lock_); | 106 base::AutoLock auto_lock(connected_lock_); |
111 connected_ = connected; | 107 connected_ = connected; |
112 } | 108 } |
113 Notify(connected); | 109 Notify(connected); |
114 LOG(INFO) << "Global connection is: " << (connected ? "Up" : "Down"); | 110 LOG(INFO) << "Global connection is: " << (connected ? "Up" : "Down"); |
115 } | 111 } |
116 | 112 |
117 void ConnectivityCheckerImpl::Check() { | 113 void ConnectivityCheckerImpl::Check() { |
118 task_runner_->PostTask(FROM_HERE, | 114 task_runner_->PostTask(FROM_HERE, |
119 base::Bind(&ConnectivityCheckerImpl::CheckInternal, this)); | 115 base::Bind(&ConnectivityCheckerImpl::CheckInternal, this)); |
120 } | 116 } |
121 | 117 |
122 void ConnectivityCheckerImpl::CheckInternal() { | 118 void ConnectivityCheckerImpl::CheckInternal() { |
123 DCHECK(task_runner_->BelongsToCurrentThread()); | 119 DCHECK(task_runner_->BelongsToCurrentThread()); |
124 DCHECK(url_request_context_.get()); | 120 DCHECK(url_request_context_); |
125 | 121 |
126 // Don't check connectivity if network is offline, because Internet could be | 122 // Don't check connectivity if network is offline, because Internet could be |
127 // accessible via netifs ignored. | 123 // accessible via netifs ignored. |
128 if (net::NetworkChangeNotifier::IsOffline()) | 124 if (net::NetworkChangeNotifier::IsOffline()) |
129 return; | 125 return; |
130 | 126 |
131 // If url_request_ is non-null, there is already a check going on. Don't | 127 // If url_request_ is non-null, there is already a check going on. Don't |
132 // start another. | 128 // start another. |
133 if (url_request_.get()) | 129 if (url_request_.get()) |
134 return; | 130 return; |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
251 void ConnectivityCheckerImpl::Cancel() { | 247 void ConnectivityCheckerImpl::Cancel() { |
252 DCHECK(task_runner_->BelongsToCurrentThread()); | 248 DCHECK(task_runner_->BelongsToCurrentThread()); |
253 if (!url_request_.get()) | 249 if (!url_request_.get()) |
254 return; | 250 return; |
255 VLOG(2) << "Cancel connectivity check in progress"; | 251 VLOG(2) << "Cancel connectivity check in progress"; |
256 url_request_.reset(nullptr); // URLRequest::Cancel() is called in destructor. | 252 url_request_.reset(nullptr); // URLRequest::Cancel() is called in destructor. |
257 timeout_.Cancel(); | 253 timeout_.Cancel(); |
258 } | 254 } |
259 | 255 |
260 } // namespace chromecast | 256 } // namespace chromecast |
OLD | NEW |