OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // TODO(ukai): code is similar with http_network_transaction.cc. We should | 5 // TODO(ukai): code is similar with http_network_transaction.cc. We should |
6 // think about ways to share code, if possible. | 6 // think about ways to share code, if possible. |
7 | 7 |
8 #include "net/socket_stream/socket_stream.h" | 8 #include "net/socket_stream/socket_stream.h" |
9 | 9 |
10 #include <set> | 10 #include <set> |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 void SocketStream::ResponseHeaders::Realloc(size_t new_size) { | 83 void SocketStream::ResponseHeaders::Realloc(size_t new_size) { |
84 headers_.reset(static_cast<char*>(realloc(headers_.release(), new_size))); | 84 headers_.reset(static_cast<char*>(realloc(headers_.release(), new_size))); |
85 } | 85 } |
86 | 86 |
87 SocketStream::ResponseHeaders::~ResponseHeaders() { data_ = NULL; } | 87 SocketStream::ResponseHeaders::~ResponseHeaders() { data_ = NULL; } |
88 | 88 |
89 SocketStream::SocketStream(const GURL& url, Delegate* delegate) | 89 SocketStream::SocketStream(const GURL& url, Delegate* delegate) |
90 : delegate_(delegate), | 90 : delegate_(delegate), |
91 url_(url), | 91 url_(url), |
92 max_pending_send_allowed_(kMaxPendingSendAllowed), | 92 max_pending_send_allowed_(kMaxPendingSendAllowed), |
| 93 context_(NULL), |
93 next_state_(STATE_NONE), | 94 next_state_(STATE_NONE), |
94 factory_(ClientSocketFactory::GetDefaultFactory()), | 95 factory_(ClientSocketFactory::GetDefaultFactory()), |
95 proxy_mode_(kDirectConnection), | 96 proxy_mode_(kDirectConnection), |
96 proxy_url_(url), | 97 proxy_url_(url), |
97 pac_request_(NULL), | 98 pac_request_(NULL), |
98 privacy_mode_(kPrivacyModeDisabled), | 99 privacy_mode_(kPrivacyModeDisabled), |
99 // Unretained() is required; without it, Bind() creates a circular | 100 // Unretained() is required; without it, Bind() creates a circular |
100 // dependency and the SocketStream object will not be freed. | 101 // dependency and the SocketStream object will not be freed. |
101 io_callback_(base::Bind(&SocketStream::OnIOCompleted, | 102 io_callback_(base::Bind(&SocketStream::OnIOCompleted, |
102 base::Unretained(this))), | 103 base::Unretained(this))), |
(...skipping 20 matching lines...) Expand all Loading... |
123 | 124 |
124 void SocketStream::SetUserData(const void* key, UserData* data) { | 125 void SocketStream::SetUserData(const void* key, UserData* data) { |
125 user_data_[key] = linked_ptr<UserData>(data); | 126 user_data_[key] = linked_ptr<UserData>(data); |
126 } | 127 } |
127 | 128 |
128 bool SocketStream::is_secure() const { | 129 bool SocketStream::is_secure() const { |
129 return url_.SchemeIs("wss"); | 130 return url_.SchemeIs("wss"); |
130 } | 131 } |
131 | 132 |
132 void SocketStream::set_context(URLRequestContext* context) { | 133 void SocketStream::set_context(URLRequestContext* context) { |
133 const URLRequestContext* prev_context = context_.get(); | 134 const URLRequestContext* prev_context = context_; |
134 | 135 |
135 if (context) { | 136 context_ = context; |
136 context_ = context->AsWeakPtr(); | |
137 } else { | |
138 context_.reset(); | |
139 } | |
140 | 137 |
141 if (prev_context != context) { | 138 if (prev_context != context) { |
142 if (prev_context && pac_request_) { | 139 if (prev_context && pac_request_) { |
143 prev_context->proxy_service()->CancelPacRequest(pac_request_); | 140 prev_context->proxy_service()->CancelPacRequest(pac_request_); |
144 pac_request_ = NULL; | 141 pac_request_ = NULL; |
145 } | 142 } |
146 | 143 |
147 net_log_.EndEvent(NetLog::TYPE_REQUEST_ALIVE); | 144 net_log_.EndEvent(NetLog::TYPE_REQUEST_ALIVE); |
148 net_log_ = BoundNetLog(); | 145 net_log_ = BoundNetLog(); |
149 | 146 |
150 if (context) { | 147 if (context) { |
151 net_log_ = BoundNetLog::Make( | 148 net_log_ = BoundNetLog::Make( |
152 context->net_log(), | 149 context->net_log(), |
153 NetLog::SOURCE_SOCKET_STREAM); | 150 NetLog::SOURCE_SOCKET_STREAM); |
154 | 151 |
155 net_log_.BeginEvent(NetLog::TYPE_REQUEST_ALIVE); | 152 net_log_.BeginEvent(NetLog::TYPE_REQUEST_ALIVE); |
156 } | 153 } |
157 } | 154 } |
158 } | 155 } |
159 | 156 |
160 void SocketStream::CheckPrivacyMode() { | 157 void SocketStream::CheckPrivacyMode() { |
161 if (context_.get() && context_->network_delegate()) { | 158 if (context_ && context_->network_delegate()) { |
162 bool enable = context_->network_delegate()->CanEnablePrivacyMode(url_, | 159 bool enable = context_->network_delegate()->CanEnablePrivacyMode(url_, |
163 url_); | 160 url_); |
164 privacy_mode_ = enable ? kPrivacyModeEnabled : kPrivacyModeDisabled; | 161 privacy_mode_ = enable ? kPrivacyModeEnabled : kPrivacyModeDisabled; |
165 // Disable Channel ID if privacy mode is enabled. | 162 // Disable Channel ID if privacy mode is enabled. |
166 if (enable) | 163 if (enable) |
167 server_ssl_config_.channel_id_enabled = false; | 164 server_ssl_config_.channel_id_enabled = false; |
168 } | 165 } |
169 } | 166 } |
170 | 167 |
171 void SocketStream::Connect() { | 168 void SocketStream::Connect() { |
172 DCHECK(base::MessageLoop::current()) | 169 DCHECK(base::MessageLoop::current()) |
173 << "The current base::MessageLoop must exist"; | 170 << "The current base::MessageLoop must exist"; |
174 DCHECK_EQ(base::MessageLoop::TYPE_IO, base::MessageLoop::current()->type()) | 171 DCHECK_EQ(base::MessageLoop::TYPE_IO, base::MessageLoop::current()->type()) |
175 << "The current base::MessageLoop must be TYPE_IO"; | 172 << "The current base::MessageLoop must be TYPE_IO"; |
176 if (context_.get()) { | 173 if (context_) { |
177 context_->ssl_config_service()->GetSSLConfig(&server_ssl_config_); | 174 context_->ssl_config_service()->GetSSLConfig(&server_ssl_config_); |
178 proxy_ssl_config_ = server_ssl_config_; | 175 proxy_ssl_config_ = server_ssl_config_; |
179 } | 176 } |
180 CheckPrivacyMode(); | 177 CheckPrivacyMode(); |
181 | 178 |
182 DCHECK_EQ(next_state_, STATE_NONE); | 179 DCHECK_EQ(next_state_, STATE_NONE); |
183 | 180 |
184 AddRef(); // Released in Finish() | 181 AddRef(); // Released in Finish() |
185 // Open a connection asynchronously, so that delegate won't be called | 182 // Open a connection asynchronously, so that delegate won't be called |
186 // back before returning Connect(). | 183 // back before returning Connect(). |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
444 waiting_for_write_completion_ = false; | 441 waiting_for_write_completion_ = false; |
445 if (result > 0) { | 442 if (result > 0) { |
446 DidSendData(result); | 443 DidSendData(result); |
447 result = OK; | 444 result = OK; |
448 } | 445 } |
449 DoLoop(result); | 446 DoLoop(result); |
450 } | 447 } |
451 | 448 |
452 void SocketStream::DoLoop(int result) { | 449 void SocketStream::DoLoop(int result) { |
453 // If context was not set, close immediately. | 450 // If context was not set, close immediately. |
454 if (!context_.get()) | 451 if (!context_) |
455 next_state_ = STATE_CLOSE; | 452 next_state_ = STATE_CLOSE; |
456 | 453 |
457 if (next_state_ == STATE_NONE) | 454 if (next_state_ == STATE_NONE) |
458 return; | 455 return; |
459 | 456 |
460 do { | 457 do { |
461 State state = next_state_; | 458 State state = next_state_; |
462 next_state_ = STATE_NONE; | 459 next_state_ = STATE_NONE; |
463 switch (state) { | 460 switch (state) { |
464 case STATE_BEFORE_CONNECT: | 461 case STATE_BEFORE_CONNECT: |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
570 // record the error. In next iteration, it will close the connection. | 567 // record the error. In next iteration, it will close the connection. |
571 if (state != STATE_READ_WRITE && result < ERR_IO_PENDING) { | 568 if (state != STATE_READ_WRITE && result < ERR_IO_PENDING) { |
572 net_log_.EndEventWithNetErrorCode( | 569 net_log_.EndEventWithNetErrorCode( |
573 NetLog::TYPE_SOCKET_STREAM_CONNECT, result); | 570 NetLog::TYPE_SOCKET_STREAM_CONNECT, result); |
574 } | 571 } |
575 } while (result != ERR_IO_PENDING); | 572 } while (result != ERR_IO_PENDING); |
576 } | 573 } |
577 | 574 |
578 int SocketStream::DoBeforeConnect() { | 575 int SocketStream::DoBeforeConnect() { |
579 next_state_ = STATE_BEFORE_CONNECT_COMPLETE; | 576 next_state_ = STATE_BEFORE_CONNECT_COMPLETE; |
580 if (!context_.get() || !context_->network_delegate()) { | 577 if (!context_ || !context_->network_delegate()) |
581 // TODO(yhirano): This should not be OK. | |
582 return OK; | 578 return OK; |
583 } | |
584 | 579 |
585 int result = context_->network_delegate()->NotifyBeforeSocketStreamConnect( | 580 int result = context_->network_delegate()->NotifyBeforeSocketStreamConnect( |
586 this, io_callback_); | 581 this, io_callback_); |
587 if (result != OK && result != ERR_IO_PENDING) | 582 if (result != OK && result != ERR_IO_PENDING) |
588 next_state_ = STATE_CLOSE; | 583 next_state_ = STATE_CLOSE; |
589 | 584 |
590 return result; | 585 return result; |
591 } | 586 } |
592 | 587 |
593 int SocketStream::DoBeforeConnectComplete(int result) { | 588 int SocketStream::DoBeforeConnectComplete(int result) { |
(...skipping 693 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1287 | 1282 |
1288 next_state_ = STATE_TCP_CONNECT; | 1283 next_state_ = STATE_TCP_CONNECT; |
1289 DoLoop(OK); | 1284 DoLoop(OK); |
1290 } | 1285 } |
1291 | 1286 |
1292 int SocketStream::HandleCertificateError(int result) { | 1287 int SocketStream::HandleCertificateError(int result) { |
1293 DCHECK(IsCertificateError(result)); | 1288 DCHECK(IsCertificateError(result)); |
1294 SSLClientSocket* ssl_socket = static_cast<SSLClientSocket*>(socket_.get()); | 1289 SSLClientSocket* ssl_socket = static_cast<SSLClientSocket*>(socket_.get()); |
1295 DCHECK(ssl_socket); | 1290 DCHECK(ssl_socket); |
1296 | 1291 |
1297 if (!context_.get()) | 1292 if (!context_) |
1298 return result; | 1293 return result; |
1299 | 1294 |
1300 if (SSLClientSocket::IgnoreCertError(result, LOAD_IGNORE_ALL_CERT_ERRORS)) { | 1295 if (SSLClientSocket::IgnoreCertError(result, LOAD_IGNORE_ALL_CERT_ERRORS)) { |
1301 const HttpNetworkSession::Params* session_params = | 1296 const HttpNetworkSession::Params* session_params = |
1302 context_->GetNetworkSessionParams(); | 1297 context_->GetNetworkSessionParams(); |
1303 if (session_params && session_params->ignore_certificate_errors) | 1298 if (session_params && session_params->ignore_certificate_errors) |
1304 return OK; | 1299 return OK; |
1305 } | 1300 } |
1306 | 1301 |
1307 if (!delegate_) | 1302 if (!delegate_) |
1308 return result; | 1303 return result; |
1309 | 1304 |
1310 SSLInfo ssl_info; | 1305 SSLInfo ssl_info; |
1311 ssl_socket->GetSSLInfo(&ssl_info); | 1306 ssl_socket->GetSSLInfo(&ssl_info); |
1312 | 1307 |
1313 TransportSecurityState::DomainState domain_state; | 1308 TransportSecurityState::DomainState domain_state; |
1314 const bool fatal = context_->transport_security_state() && | 1309 const bool fatal = context_->transport_security_state() && |
1315 context_->transport_security_state()->GetDomainState(url_.host(), | 1310 context_->transport_security_state()->GetDomainState(url_.host(), |
1316 SSLConfigService::IsSNIAvailable(context_->ssl_config_service()), | 1311 SSLConfigService::IsSNIAvailable(context_->ssl_config_service()), |
1317 &domain_state) && | 1312 &domain_state) && |
1318 domain_state.ShouldSSLErrorsBeFatal(); | 1313 domain_state.ShouldSSLErrorsBeFatal(); |
1319 | 1314 |
1320 delegate_->OnSSLCertificateError(this, ssl_info, fatal); | 1315 delegate_->OnSSLCertificateError(this, ssl_info, fatal); |
1321 return ERR_IO_PENDING; | 1316 return ERR_IO_PENDING; |
1322 } | 1317 } |
1323 | 1318 |
1324 } // namespace net | 1319 } // namespace net |
OLD | NEW |