Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/http/bidirectional_stream.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/location.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/thread_task_runner_handle.h" | |
| 12 #include "base/time/time.h" | |
| 13 #include "base/timer/timer.h" | |
| 14 #include "net/base/load_flags.h" | |
| 15 #include "net/base/net_errors.h" | |
| 16 #include "net/http/bidirectional_stream_request_info.h" | |
| 17 #include "net/http/http_network_session.h" | |
| 18 #include "net/http/http_stream.h" | |
| 19 #include "net/ssl/ssl_cert_request_info.h" | |
| 20 #include "net/ssl/ssl_config.h" | |
| 21 #include "url/gurl.h" | |
| 22 | |
| 23 namespace net { | |
| 24 | |
| 25 BidirectionalStream::Delegate::Delegate() {} | |
| 26 | |
| 27 BidirectionalStream::Delegate::~Delegate() {} | |
| 28 | |
| 29 BidirectionalStream::BidirectionalStream( | |
| 30 scoped_ptr<BidirectionalStreamRequestInfo> request_info, | |
| 31 HttpNetworkSession* session, | |
| 32 Delegate* delegate) | |
| 33 : BidirectionalStream(std::move(request_info), | |
| 34 session, | |
| 35 delegate, | |
| 36 make_scoped_ptr(new base::Timer(false, false))) {} | |
| 37 | |
| 38 BidirectionalStream::BidirectionalStream( | |
| 39 scoped_ptr<BidirectionalStreamRequestInfo> request_info, | |
| 40 HttpNetworkSession* session, | |
| 41 Delegate* delegate, | |
| 42 scoped_ptr<base::Timer> timer) | |
| 43 : request_info_(std::move(request_info)), | |
| 44 net_log_(BoundNetLog::Make(session->net_log(), | |
| 45 NetLog::SOURCE_BIDIRECTIONAL_STREAM)), | |
| 46 delegate_(delegate), | |
| 47 timer_(std::move(timer)) { | |
| 48 DCHECK(delegate_); | |
| 49 DCHECK(request_info_); | |
| 50 | |
| 51 SSLConfig server_ssl_config; | |
| 52 session->ssl_config_service()->GetSSLConfig(&server_ssl_config); | |
| 53 session->GetAlpnProtos(&server_ssl_config.alpn_protos); | |
| 54 session->GetNpnProtos(&server_ssl_config.npn_protos); | |
| 55 | |
| 56 if (!request_info_->url.SchemeIs(url::kHttpsScheme)) { | |
| 57 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 58 FROM_HERE, | |
| 59 base::Bind(&BidirectionalStream::Delegate::OnFailed, | |
| 60 base::Unretained(delegate_), ERR_DISALLOWED_URL_SCHEME)); | |
| 61 return; | |
| 62 } | |
| 63 | |
| 64 HttpRequestInfo http_request_info; | |
| 65 http_request_info.url = request_info_->url; | |
| 66 http_request_info.method = request_info_->method; | |
| 67 http_request_info.extra_headers = request_info_->extra_headers; | |
| 68 stream_request_.reset( | |
| 69 session->http_stream_factory()->RequestBidirectionalStreamJob( | |
| 70 http_request_info, request_info_->priority, server_ssl_config, | |
| 71 server_ssl_config, this, net_log_)); | |
| 72 // Check that this call cannot fail to set a non-NULL |stream_request_|. | |
| 73 DCHECK(stream_request_); | |
| 74 // Check that HttpStreamFactory does not invoke OnBidirectionalStreamJobReady | |
| 75 // synchronously. | |
| 76 DCHECK(!stream_job_); | |
| 77 } | |
| 78 | |
| 79 BidirectionalStream::~BidirectionalStream() { | |
| 80 Cancel(); | |
| 81 } | |
| 82 | |
| 83 int BidirectionalStream::ReadData(IOBuffer* buf, int buf_len) { | |
| 84 DCHECK(stream_job_); | |
| 85 | |
| 86 return stream_job_->ReadData(buf, buf_len); | |
| 87 } | |
| 88 | |
| 89 void BidirectionalStream::SendData(IOBuffer* data, | |
| 90 int length, | |
| 91 bool end_stream) { | |
| 92 DCHECK(stream_job_); | |
| 93 | |
| 94 stream_job_->SendData(data, length, end_stream); | |
| 95 } | |
| 96 | |
| 97 void BidirectionalStream::Cancel() { | |
| 98 stream_request_.reset(); | |
| 99 if (stream_job_) { | |
| 100 stream_job_->Cancel(); | |
| 101 stream_job_.reset(); | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 NextProto BidirectionalStream::GetProtocol() const { | |
| 106 if (!stream_job_) | |
| 107 return kProtoUnknown; | |
| 108 | |
| 109 return stream_job_->GetProtocol(); | |
| 110 } | |
| 111 | |
| 112 int64_t BidirectionalStream::GetTotalReceivedBytes() const { | |
| 113 if (!stream_job_) | |
| 114 return 0; | |
| 115 | |
| 116 return stream_job_->GetTotalReceivedBytes(); | |
| 117 } | |
| 118 | |
| 119 int64_t BidirectionalStream::GetTotalSentBytes() const { | |
| 120 if (!stream_job_) | |
| 121 return 0; | |
| 122 | |
| 123 return stream_job_->GetTotalSentBytes(); | |
| 124 } | |
| 125 | |
| 126 void BidirectionalStream::OnHeadersSent() { | |
| 127 delegate_->OnHeadersSent(); | |
| 128 } | |
| 129 | |
| 130 void BidirectionalStream::OnHeadersReceived( | |
| 131 const SpdyHeaderBlock& response_headers) { | |
| 132 delegate_->OnHeadersReceived(response_headers); | |
| 133 } | |
| 134 | |
| 135 void BidirectionalStream::OnDataRead(int bytes_read) { | |
| 136 delegate_->OnDataRead(bytes_read); | |
| 137 } | |
| 138 | |
| 139 void BidirectionalStream::OnDataSent() { | |
| 140 delegate_->OnDataSent(); | |
| 141 } | |
| 142 | |
| 143 void BidirectionalStream::OnTrailersReceived(const SpdyHeaderBlock& trailers) { | |
| 144 delegate_->OnTrailersReceived(trailers); | |
| 145 } | |
| 146 | |
| 147 void BidirectionalStream::OnFailed(int status) { | |
| 148 delegate_->OnFailed(status); | |
| 149 } | |
| 150 | |
| 151 void BidirectionalStream::OnStreamReady(const SSLConfig& used_ssl_config, | |
| 152 const ProxyInfo& used_proxy_info, | |
| 153 HttpStream* stream) { | |
| 154 NOTREACHED(); | |
| 155 } | |
| 156 | |
| 157 void BidirectionalStream::OnBidirectionalStreamJobReady( | |
| 158 const SSLConfig& used_ssl_config, | |
| 159 const ProxyInfo& used_proxy_info, | |
| 160 BidirectionalStreamJob* stream) { | |
| 161 DCHECK(!stream_job_); | |
| 162 | |
| 163 stream_request_.reset(); | |
| 164 stream_job_.reset(stream); | |
| 165 stream_job_->Start(request_info_.get(), net_log_, this, timer_.Pass()); | |
|
mef
2015/12/18 21:40:00
please use std::move()
xunjieli
2015/12/18 22:19:43
Done. Good catch!
| |
| 166 } | |
| 167 | |
| 168 void BidirectionalStream::OnWebSocketHandshakeStreamReady( | |
| 169 const SSLConfig& used_ssl_config, | |
| 170 const ProxyInfo& used_proxy_info, | |
| 171 WebSocketHandshakeStreamBase* stream) { | |
| 172 NOTREACHED(); | |
| 173 } | |
| 174 | |
| 175 void BidirectionalStream::OnStreamFailed(int result, | |
| 176 const SSLConfig& used_ssl_config, | |
| 177 SSLFailureState ssl_failure_state) { | |
| 178 DCHECK_LT(result, 0); | |
| 179 DCHECK_NE(result, ERR_IO_PENDING); | |
| 180 DCHECK(stream_request_); | |
| 181 | |
| 182 delegate_->OnFailed(result); | |
| 183 } | |
| 184 | |
| 185 void BidirectionalStream::OnCertificateError(int result, | |
| 186 const SSLConfig& used_ssl_config, | |
| 187 const SSLInfo& ssl_info) { | |
| 188 DCHECK_LT(result, 0); | |
| 189 DCHECK_NE(result, ERR_IO_PENDING); | |
| 190 DCHECK(stream_request_); | |
| 191 | |
| 192 delegate_->OnFailed(result); | |
| 193 } | |
| 194 | |
| 195 void BidirectionalStream::OnNeedsProxyAuth( | |
| 196 const HttpResponseInfo& proxy_response, | |
| 197 const SSLConfig& used_ssl_config, | |
| 198 const ProxyInfo& used_proxy_info, | |
| 199 HttpAuthController* auth_controller) { | |
| 200 DCHECK(stream_request_); | |
| 201 | |
| 202 delegate_->OnFailed(ERR_PROXY_AUTH_REQUESTED); | |
| 203 } | |
| 204 | |
| 205 void BidirectionalStream::OnNeedsClientAuth(const SSLConfig& used_ssl_config, | |
| 206 SSLCertRequestInfo* cert_info) { | |
| 207 DCHECK(stream_request_); | |
| 208 | |
| 209 delegate_->OnFailed(ERR_SSL_CLIENT_AUTH_CERT_NEEDED); | |
| 210 } | |
| 211 | |
| 212 void BidirectionalStream::OnHttpsProxyTunnelResponse( | |
| 213 const HttpResponseInfo& response_info, | |
| 214 const SSLConfig& used_ssl_config, | |
| 215 const ProxyInfo& used_proxy_info, | |
| 216 HttpStream* stream) { | |
| 217 DCHECK(stream_request_); | |
| 218 | |
| 219 delegate_->OnFailed(ERR_HTTPS_PROXY_TUNNEL_RESPONSE); | |
| 220 } | |
| 221 | |
| 222 void BidirectionalStream::OnQuicBroken() {} | |
| 223 | |
| 224 } // namespace net | |
| OLD | NEW |