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.h" | 5 #include "chromecast/net/connectivity_checker.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "chromecast/net/net_switches.h" | 10 #include "chromecast/net/net_switches.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 // to offline. | 28 // to offline. |
29 const unsigned int kNumErrorsToNotifyOffline = 3; | 29 const unsigned int kNumErrorsToNotifyOffline = 3; |
30 | 30 |
31 // Default url for connectivity checking. | 31 // Default url for connectivity checking. |
32 const char kDefaultConnectivityCheckUrl[] = | 32 const char kDefaultConnectivityCheckUrl[] = |
33 "https://clients3.google.com/generate_204"; | 33 "https://clients3.google.com/generate_204"; |
34 | 34 |
35 } // namespace | 35 } // namespace |
36 | 36 |
37 ConnectivityChecker::ConnectivityChecker( | 37 ConnectivityChecker::ConnectivityChecker( |
38 const scoped_refptr<base::MessageLoopProxy>& loop_proxy) | 38 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) |
39 : connectivity_observer_list_( | 39 : connectivity_observer_list_( |
40 new ObserverListThreadSafe<ConnectivityObserver>()), | 40 new ObserverListThreadSafe<ConnectivityObserver>()), |
41 loop_proxy_(loop_proxy), | 41 task_runner_(task_runner), |
42 connected_(false), | 42 connected_(false), |
43 check_errors_(0) { | 43 check_errors_(0) { |
44 DCHECK(loop_proxy_.get()); | 44 DCHECK(task_runner_.get()); |
45 loop_proxy->PostTask(FROM_HERE, | 45 task_runner->PostTask(FROM_HERE, |
46 base::Bind(&ConnectivityChecker::Initialize, this)); | 46 base::Bind(&ConnectivityChecker::Initialize, this)); |
47 } | 47 } |
48 | 48 |
49 void ConnectivityChecker::Initialize() { | 49 void ConnectivityChecker::Initialize() { |
50 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 50 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
51 base::CommandLine::StringType check_url_str = | 51 base::CommandLine::StringType check_url_str = |
52 command_line->GetSwitchValueNative(switches::kConnectivityCheckUrl); | 52 command_line->GetSwitchValueNative(switches::kConnectivityCheckUrl); |
53 connectivity_check_url_.reset(new GURL( | 53 connectivity_check_url_.reset(new GURL( |
54 check_url_str.empty() ? kDefaultConnectivityCheckUrl : check_url_str)); | 54 check_url_str.empty() ? kDefaultConnectivityCheckUrl : check_url_str)); |
55 | 55 |
56 net::URLRequestContextBuilder builder; | 56 net::URLRequestContextBuilder builder; |
57 builder.set_proxy_config_service( | 57 builder.set_proxy_config_service( |
58 new net::ProxyConfigServiceFixed(net::ProxyConfig::CreateDirect())); | 58 new net::ProxyConfigServiceFixed(net::ProxyConfig::CreateDirect())); |
59 builder.DisableHttpCache(); | 59 builder.DisableHttpCache(); |
60 url_request_context_.reset(builder.Build()); | 60 url_request_context_.reset(builder.Build()); |
61 | 61 |
62 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); | 62 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); |
63 net::NetworkChangeNotifier::AddIPAddressObserver(this); | 63 net::NetworkChangeNotifier::AddIPAddressObserver(this); |
64 loop_proxy_->PostTask(FROM_HERE, | 64 task_runner_->PostTask(FROM_HERE, |
65 base::Bind(&ConnectivityChecker::Check, this)); | 65 base::Bind(&ConnectivityChecker::Check, this)); |
66 } | 66 } |
67 | 67 |
68 ConnectivityChecker::~ConnectivityChecker() { | 68 ConnectivityChecker::~ConnectivityChecker() { |
69 DCHECK(loop_proxy_.get()); | 69 DCHECK(task_runner_.get()); |
70 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); | 70 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); |
71 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); | 71 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
72 loop_proxy_->DeleteSoon(FROM_HERE, url_request_context_.release()); | 72 task_runner_->DeleteSoon(FROM_HERE, url_request_context_.release()); |
73 loop_proxy_->DeleteSoon(FROM_HERE, url_request_.release()); | 73 task_runner_->DeleteSoon(FROM_HERE, url_request_.release()); |
74 } | 74 } |
75 | 75 |
76 void ConnectivityChecker::AddConnectivityObserver( | 76 void ConnectivityChecker::AddConnectivityObserver( |
77 ConnectivityObserver* observer) { | 77 ConnectivityObserver* observer) { |
78 connectivity_observer_list_->AddObserver(observer); | 78 connectivity_observer_list_->AddObserver(observer); |
79 } | 79 } |
80 | 80 |
81 void ConnectivityChecker::RemoveConnectivityObserver( | 81 void ConnectivityChecker::RemoveConnectivityObserver( |
82 ConnectivityObserver* observer) { | 82 ConnectivityObserver* observer) { |
83 connectivity_observer_list_->RemoveObserver(observer); | 83 connectivity_observer_list_->RemoveObserver(observer); |
84 } | 84 } |
85 | 85 |
86 bool ConnectivityChecker::Connected() const { | 86 bool ConnectivityChecker::Connected() const { |
87 return connected_; | 87 return connected_; |
88 } | 88 } |
89 | 89 |
90 void ConnectivityChecker::SetConnectivity(bool connected) { | 90 void ConnectivityChecker::SetConnectivity(bool connected) { |
91 if (connected_ == connected) | 91 if (connected_ == connected) |
92 return; | 92 return; |
93 | 93 |
94 connected_ = connected; | 94 connected_ = connected; |
95 connectivity_observer_list_->Notify( | 95 connectivity_observer_list_->Notify( |
96 FROM_HERE, &ConnectivityObserver::OnConnectivityChanged, connected); | 96 FROM_HERE, &ConnectivityObserver::OnConnectivityChanged, connected); |
97 LOG(INFO) << "Global connection is: " << (connected ? "Up" : "Down"); | 97 LOG(INFO) << "Global connection is: " << (connected ? "Up" : "Down"); |
98 } | 98 } |
99 | 99 |
100 void ConnectivityChecker::Check() { | 100 void ConnectivityChecker::Check() { |
101 if (!loop_proxy_->BelongsToCurrentThread()) { | 101 if (!task_runner_->BelongsToCurrentThread()) { |
102 loop_proxy_->PostTask(FROM_HERE, | 102 task_runner_->PostTask(FROM_HERE, |
103 base::Bind(&ConnectivityChecker::Check, this)); | 103 base::Bind(&ConnectivityChecker::Check, this)); |
104 return; | 104 return; |
105 } | 105 } |
106 DCHECK(url_request_context_.get()); | 106 DCHECK(url_request_context_.get()); |
107 | 107 |
108 // Don't check connectivity if network is offline, because Internet could be | 108 // Don't check connectivity if network is offline, because Internet could be |
109 // accessible via netifs ignored. | 109 // accessible via netifs ignored. |
110 if (net::NetworkChangeNotifier::IsOffline()) | 110 if (net::NetworkChangeNotifier::IsOffline()) |
111 return; | 111 return; |
112 | 112 |
113 // If url_request_ is non-null, there is already a check going on. Don't | 113 // If url_request_ is non-null, there is already a check going on. Don't |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 } | 167 } |
168 | 168 |
169 void ConnectivityChecker::OnUrlRequestError() { | 169 void ConnectivityChecker::OnUrlRequestError() { |
170 ++check_errors_; | 170 ++check_errors_; |
171 if (check_errors_ > kNumErrorsToNotifyOffline) { | 171 if (check_errors_ > kNumErrorsToNotifyOffline) { |
172 check_errors_ = kNumErrorsToNotifyOffline; | 172 check_errors_ = kNumErrorsToNotifyOffline; |
173 SetConnectivity(false); | 173 SetConnectivity(false); |
174 } | 174 } |
175 url_request_.reset(NULL); | 175 url_request_.reset(NULL); |
176 // Check again. | 176 // Check again. |
177 loop_proxy_->PostDelayedTask( | 177 task_runner_->PostDelayedTask( |
178 FROM_HERE, base::Bind(&ConnectivityChecker::Check, this), | 178 FROM_HERE, base::Bind(&ConnectivityChecker::Check, this), |
179 base::TimeDelta::FromSeconds(kConnectivityPeriodSeconds)); | 179 base::TimeDelta::FromSeconds(kConnectivityPeriodSeconds)); |
180 } | 180 } |
181 | 181 |
182 void ConnectivityChecker::OnReadCompleted(net::URLRequest* request, | 182 void ConnectivityChecker::OnReadCompleted(net::URLRequest* request, |
183 int bytes_read) { | 183 int bytes_read) { |
184 NOTREACHED(); | 184 NOTREACHED(); |
185 } | 185 } |
186 | 186 |
187 void ConnectivityChecker::Cancel() { | 187 void ConnectivityChecker::Cancel() { |
188 if (url_request_.get()) { | 188 if (url_request_.get()) { |
189 VLOG(2) << "Cancel connectivity check in progress"; | 189 VLOG(2) << "Cancel connectivity check in progress"; |
190 url_request_.reset(NULL); // URLRequest::Cancel() is called in destructor. | 190 url_request_.reset(NULL); // URLRequest::Cancel() is called in destructor. |
191 } | 191 } |
192 } | 192 } |
193 | 193 |
194 } // namespace chromecast | 194 } // namespace chromecast |
OLD | NEW |