OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "google_apis/gcm/engine/connection_factory_impl.h" | 5 #include "google_apis/gcm/engine/connection_factory_impl.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "google_apis/gcm/engine/connection_handler_impl.h" | 8 #include "google_apis/gcm/engine/connection_handler_impl.h" |
9 #include "google_apis/gcm/protocol/mcs.pb.h" | 9 #include "google_apis/gcm/protocol/mcs.pb.h" |
10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 weak_ptr_factory_.GetWeakPtr()), | 110 weak_ptr_factory_.GetWeakPtr()), |
111 NextRetryAttempt() - base::TimeTicks::Now()); | 111 NextRetryAttempt() - base::TimeTicks::Now()); |
112 return; | 112 return; |
113 } | 113 } |
114 | 114 |
115 DVLOG(1) << "Attempting connection to MCS endpoint."; | 115 DVLOG(1) << "Attempting connection to MCS endpoint."; |
116 ConnectImpl(); | 116 ConnectImpl(); |
117 } | 117 } |
118 | 118 |
119 bool ConnectionFactoryImpl::IsEndpointReachable() const { | 119 bool ConnectionFactoryImpl::IsEndpointReachable() const { |
120 return connection_handler_ && connection_handler_->CanSendMessage(); | 120 return connection_handler_ && |
| 121 connection_handler_->CanSendMessage() && |
| 122 !connecting_; |
121 } | 123 } |
122 | 124 |
123 void ConnectionFactoryImpl::SignalConnectionReset() { | 125 void ConnectionFactoryImpl::SignalConnectionReset() { |
124 if (connecting_) | 126 if (connecting_) |
125 return; // Already attempting to reconnect. | 127 return; // Already attempting to reconnect. |
126 | 128 |
127 if (!backoff_reset_time_.is_null() && | 129 if (!backoff_reset_time_.is_null() && |
128 base::TimeTicks::Now() - backoff_reset_time_ <= | 130 base::TimeTicks::Now() - backoff_reset_time_ <= |
129 base::TimeDelta::FromSeconds(kConnectionResetWindowSecs)) { | 131 base::TimeDelta::FromSeconds(kConnectionResetWindowSecs)) { |
130 backoff_entry_.swap(previous_backoff_); | 132 backoff_entry_.swap(previous_backoff_); |
(...skipping 26 matching lines...) Expand all Loading... |
157 void ConnectionFactoryImpl::OnIPAddressChanged() { | 159 void ConnectionFactoryImpl::OnIPAddressChanged() { |
158 DVLOG(1) << "IP Address changed, resetting backoff."; | 160 DVLOG(1) << "IP Address changed, resetting backoff."; |
159 backoff_entry_->Reset(); | 161 backoff_entry_->Reset(); |
160 // Connect(..) should be retrying with backoff already if a connection is | 162 // Connect(..) should be retrying with backoff already if a connection is |
161 // necessary, so no need to call again. | 163 // necessary, so no need to call again. |
162 } | 164 } |
163 | 165 |
164 void ConnectionFactoryImpl::ConnectImpl() { | 166 void ConnectionFactoryImpl::ConnectImpl() { |
165 DCHECK(!IsEndpointReachable()); | 167 DCHECK(!IsEndpointReachable()); |
166 | 168 |
| 169 if (socket_handle_.socket() && socket_handle_.socket()->IsConnected()) |
| 170 socket_handle_.socket()->Disconnect(); |
| 171 socket_handle_.Reset(); |
| 172 |
167 // TODO(zea): resolve proxies. | 173 // TODO(zea): resolve proxies. |
168 net::ProxyInfo proxy_info; | 174 net::ProxyInfo proxy_info; |
169 proxy_info.UseDirect(); | 175 proxy_info.UseDirect(); |
170 net::SSLConfig ssl_config; | 176 net::SSLConfig ssl_config; |
171 network_session_->ssl_config_service()->GetSSLConfig(&ssl_config); | 177 network_session_->ssl_config_service()->GetSSLConfig(&ssl_config); |
172 | 178 |
173 int status = net::InitSocketHandleForTlsConnect( | 179 int status = net::InitSocketHandleForTlsConnect( |
174 net::HostPortPair::FromURL(mcs_endpoint_), | 180 net::HostPortPair::FromURL(mcs_endpoint_), |
175 network_session_.get(), | 181 network_session_.get(), |
176 proxy_info, | 182 proxy_info, |
177 ssl_config, | 183 ssl_config, |
178 ssl_config, | 184 ssl_config, |
179 net::kPrivacyModeDisabled, | 185 net::kPrivacyModeDisabled, |
180 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_SOCKET), | 186 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_SOCKET), |
181 &socket_handle_, | 187 &socket_handle_, |
182 base::Bind(&ConnectionFactoryImpl::OnConnectDone, | 188 base::Bind(&ConnectionFactoryImpl::OnConnectDone, |
183 weak_ptr_factory_.GetWeakPtr())); | 189 weak_ptr_factory_.GetWeakPtr())); |
184 if (status != net::ERR_IO_PENDING) | 190 if (status != net::ERR_IO_PENDING) |
185 OnConnectDone(status); | 191 OnConnectDone(status); |
186 } | 192 } |
187 | 193 |
188 void ConnectionFactoryImpl::InitHandler() { | 194 void ConnectionFactoryImpl::InitHandler() { |
189 // May be null in tests. | 195 // May be null in tests. |
190 mcs_proto::LoginRequest login_request; | 196 mcs_proto::LoginRequest login_request; |
191 if (!request_builder_.is_null()) { | 197 if (!request_builder_.is_null()) { |
192 request_builder_.Run(&login_request); | 198 request_builder_.Run(&login_request); |
193 DCHECK(login_request.IsInitialized()); | 199 DCHECK(login_request.IsInitialized()); |
194 } | 200 } |
195 | 201 |
196 connection_handler_->Init(login_request, socket_handle_.PassSocket()); | 202 connection_handler_->Init(login_request, socket_handle_.socket()); |
197 } | 203 } |
198 | 204 |
199 scoped_ptr<net::BackoffEntry> ConnectionFactoryImpl::CreateBackoffEntry( | 205 scoped_ptr<net::BackoffEntry> ConnectionFactoryImpl::CreateBackoffEntry( |
200 const net::BackoffEntry::Policy* const policy) { | 206 const net::BackoffEntry::Policy* const policy) { |
201 return scoped_ptr<net::BackoffEntry>(new net::BackoffEntry(policy)); | 207 return scoped_ptr<net::BackoffEntry>(new net::BackoffEntry(policy)); |
202 } | 208 } |
203 | 209 |
204 void ConnectionFactoryImpl::OnConnectDone(int result) { | 210 void ConnectionFactoryImpl::OnConnectDone(int result) { |
205 if (result != net::OK) { | 211 if (result != net::OK) { |
206 LOG(ERROR) << "Failed to connect to MCS endpoint with error " << result; | 212 LOG(ERROR) << "Failed to connect to MCS endpoint with error " << result; |
(...skipping 16 matching lines...) Expand all Loading... |
223 return; | 229 return; |
224 } | 230 } |
225 // TODO(zea): Consider how to handle errors that may require some sort of | 231 // TODO(zea): Consider how to handle errors that may require some sort of |
226 // user intervention (login page, etc.). | 232 // user intervention (login page, etc.). |
227 LOG(ERROR) << "Connection reset with error " << result; | 233 LOG(ERROR) << "Connection reset with error " << result; |
228 backoff_entry_->InformOfRequest(false); | 234 backoff_entry_->InformOfRequest(false); |
229 Connect(); | 235 Connect(); |
230 } | 236 } |
231 | 237 |
232 } // namespace gcm | 238 } // namespace gcm |
OLD | NEW |