| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "net/socket/socket_bio_adapter.h" | 5 #include "net/socket/socket_bio_adapter.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/feature_list.h" |
| 12 #include "base/location.h" | 13 #include "base/location.h" |
| 13 #include "base/logging.h" | 14 #include "base/logging.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 15 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
| 16 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
| 18 #include "net/socket/socket.h" |
| 17 #include "net/socket/stream_socket.h" | 19 #include "net/socket/stream_socket.h" |
| 18 #include "net/ssl/openssl_ssl_util.h" | 20 #include "net/ssl/openssl_ssl_util.h" |
| 19 #include "third_party/boringssl/src/include/openssl/bio.h" | 21 #include "third_party/boringssl/src/include/openssl/bio.h" |
| 20 | 22 |
| 21 namespace net { | 23 namespace net { |
| 22 | 24 |
| 23 SocketBIOAdapter::SocketBIOAdapter(StreamSocket* socket, | 25 SocketBIOAdapter::SocketBIOAdapter(StreamSocket* socket, |
| 24 int read_buffer_capacity, | 26 int read_buffer_capacity, |
| 25 int write_buffer_capacity, | 27 int write_buffer_capacity, |
| 26 Delegate* delegate) | 28 Delegate* delegate) |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 | 82 |
| 81 if (read_result_ == 0) { | 83 if (read_result_ == 0) { |
| 82 // Instantiate the read buffer and read from the socket. Although only |len| | 84 // Instantiate the read buffer and read from the socket. Although only |len| |
| 83 // bytes were requested, intentionally read to the full buffer size. The SSL | 85 // bytes were requested, intentionally read to the full buffer size. The SSL |
| 84 // layer reads the record header and body in separate reads to avoid | 86 // layer reads the record header and body in separate reads to avoid |
| 85 // overreading, but issuing one is more efficient. SSL sockets are not | 87 // overreading, but issuing one is more efficient. SSL sockets are not |
| 86 // reused after shutdown for non-SSL traffic, so overreading is fine. | 88 // reused after shutdown for non-SSL traffic, so overreading is fine. |
| 87 DCHECK(!read_buffer_); | 89 DCHECK(!read_buffer_); |
| 88 DCHECK_EQ(0, read_offset_); | 90 DCHECK_EQ(0, read_offset_); |
| 89 read_buffer_ = new IOBuffer(read_buffer_capacity_); | 91 read_buffer_ = new IOBuffer(read_buffer_capacity_); |
| 90 int result = socket_->Read(read_buffer_.get(), read_buffer_capacity_, | 92 int result = ERR_READ_IF_READY_NOT_IMPLEMENTED; |
| 91 read_callback_); | 93 if (base::FeatureList::IsEnabled(Socket::kReadIfReadyExperiment)) { |
| 94 result = socket_->ReadIfReady( |
| 95 read_buffer_.get(), read_buffer_capacity_, |
| 96 base::Bind(&SocketBIOAdapter::OnSocketReadIfReadyComplete, |
| 97 weak_factory_.GetWeakPtr())); |
| 98 if (result == ERR_IO_PENDING) |
| 99 read_buffer_ = nullptr; |
| 100 } |
| 101 if (result == ERR_READ_IF_READY_NOT_IMPLEMENTED) { |
| 102 result = socket_->Read(read_buffer_.get(), read_buffer_capacity_, |
| 103 read_callback_); |
| 104 } |
| 92 if (result == ERR_IO_PENDING) { | 105 if (result == ERR_IO_PENDING) { |
| 93 read_result_ = ERR_IO_PENDING; | 106 read_result_ = ERR_IO_PENDING; |
| 94 } else { | 107 } else { |
| 95 HandleSocketReadResult(result); | 108 HandleSocketReadResult(result); |
| 96 } | 109 } |
| 97 } | 110 } |
| 98 | 111 |
| 99 // There is a pending Read(). Inform the caller to retry when it completes. | 112 // There is a pending Read(). Inform the caller to retry when it completes. |
| 100 if (read_result_ == ERR_IO_PENDING) { | 113 if (read_result_ == ERR_IO_PENDING) { |
| 101 BIO_set_retry_read(bio()); | 114 BIO_set_retry_read(bio()); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 read_buffer_ = nullptr; | 152 read_buffer_ = nullptr; |
| 140 } | 153 } |
| 141 | 154 |
| 142 void SocketBIOAdapter::OnSocketReadComplete(int result) { | 155 void SocketBIOAdapter::OnSocketReadComplete(int result) { |
| 143 DCHECK_EQ(ERR_IO_PENDING, read_result_); | 156 DCHECK_EQ(ERR_IO_PENDING, read_result_); |
| 144 | 157 |
| 145 HandleSocketReadResult(result); | 158 HandleSocketReadResult(result); |
| 146 delegate_->OnReadReady(); | 159 delegate_->OnReadReady(); |
| 147 } | 160 } |
| 148 | 161 |
| 162 void SocketBIOAdapter::OnSocketReadIfReadyComplete(int result) { |
| 163 DCHECK_EQ(ERR_IO_PENDING, read_result_); |
| 164 DCHECK_GE(OK, result); |
| 165 |
| 166 // Do not use HandleSocketReadResult() because result == OK doesn't mean EOF. |
| 167 read_result_ = result; |
| 168 |
| 169 delegate_->OnReadReady(); |
| 170 } |
| 171 |
| 149 int SocketBIOAdapter::BIOWrite(const char* in, int len) { | 172 int SocketBIOAdapter::BIOWrite(const char* in, int len) { |
| 150 if (len <= 0) | 173 if (len <= 0) |
| 151 return len; | 174 return len; |
| 152 | 175 |
| 153 // If the write buffer is not empty, there must be a pending Write() to flush | 176 // If the write buffer is not empty, there must be a pending Write() to flush |
| 154 // it. | 177 // it. |
| 155 DCHECK(write_buffer_used_ == 0 || write_error_ == ERR_IO_PENDING); | 178 DCHECK(write_buffer_used_ == 0 || write_error_ == ERR_IO_PENDING); |
| 156 | 179 |
| 157 // If a previous Write() failed, report the error. | 180 // If a previous Write() failed, report the error. |
| 158 if (write_error_ != OK && write_error_ != ERR_IO_PENDING) { | 181 if (write_error_ != OK && write_error_ != ERR_IO_PENDING) { |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 SocketBIOAdapter::BIOReadWrapper, | 364 SocketBIOAdapter::BIOReadWrapper, |
| 342 nullptr, // puts | 365 nullptr, // puts |
| 343 nullptr, // gets | 366 nullptr, // gets |
| 344 SocketBIOAdapter::BIOCtrlWrapper, | 367 SocketBIOAdapter::BIOCtrlWrapper, |
| 345 nullptr, // create | 368 nullptr, // create |
| 346 nullptr, // destroy | 369 nullptr, // destroy |
| 347 nullptr, // callback_ctrl | 370 nullptr, // callback_ctrl |
| 348 }; | 371 }; |
| 349 | 372 |
| 350 } // namespace net | 373 } // namespace net |
| OLD | NEW |