tls_client_socket.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2018 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. #include "services/network/tls_client_socket.h"
  5. #include <utility>
  6. #include "base/bind.h"
  7. #include "base/check.h"
  8. #include "base/memory/ptr_util.h"
  9. #include "net/base/net_errors.h"
  10. #include "net/socket/client_socket_factory.h"
  11. #include "net/socket/ssl_client_socket.h"
  12. #include "net/socket/stream_socket.h"
  13. #include "net/ssl/ssl_config.h"
  14. #include "net/ssl/ssl_config_service.h"
  15. namespace network {
  16. TLSClientSocket::TLSClientSocket(
  17. mojo::PendingRemote<mojom::SocketObserver> observer,
  18. const net::NetworkTrafficAnnotationTag& traffic_annotation)
  19. : observer_(std::move(observer)), traffic_annotation_(traffic_annotation) {}
  20. TLSClientSocket::~TLSClientSocket() {
  21. if (connect_callback_)
  22. OnTLSConnectCompleted(net::ERR_ABORTED);
  23. }
  24. void TLSClientSocket::Connect(
  25. const net::HostPortPair& host_port_pair,
  26. const net::SSLConfig& ssl_config,
  27. std::unique_ptr<net::StreamSocket> tcp_socket,
  28. net::SSLClientContext* ssl_client_context,
  29. net::ClientSocketFactory* socket_factory,
  30. mojom::TCPConnectedSocket::UpgradeToTLSCallback callback,
  31. bool send_ssl_info) {
  32. connect_callback_ = std::move(callback);
  33. send_ssl_info_ = send_ssl_info;
  34. socket_ = socket_factory->CreateSSLClientSocket(
  35. ssl_client_context, std::move(tcp_socket), host_port_pair, ssl_config);
  36. int result = socket_->Connect(base::BindOnce(
  37. &TLSClientSocket::OnTLSConnectCompleted, base::Unretained(this)));
  38. if (result != net::ERR_IO_PENDING)
  39. OnTLSConnectCompleted(result);
  40. }
  41. void TLSClientSocket::OnTLSConnectCompleted(int result) {
  42. DCHECK(!connect_callback_.is_null());
  43. mojo::ScopedDataPipeProducerHandle send_producer_handle;
  44. mojo::ScopedDataPipeConsumerHandle send_consumer_handle;
  45. if (result == net::OK) {
  46. if (mojo::CreateDataPipe(nullptr, send_producer_handle,
  47. send_consumer_handle) != MOJO_RESULT_OK) {
  48. result = net::ERR_FAILED;
  49. }
  50. }
  51. mojo::ScopedDataPipeProducerHandle receive_producer_handle;
  52. mojo::ScopedDataPipeConsumerHandle receive_consumer_handle;
  53. if (result == net::OK) {
  54. if (mojo::CreateDataPipe(nullptr, receive_producer_handle,
  55. receive_consumer_handle) != MOJO_RESULT_OK) {
  56. result = net::ERR_FAILED;
  57. }
  58. }
  59. if (result != net::OK) {
  60. socket_ = nullptr;
  61. std::move(connect_callback_)
  62. .Run(result, mojo::ScopedDataPipeConsumerHandle(),
  63. mojo::ScopedDataPipeProducerHandle(), absl::nullopt);
  64. return;
  65. }
  66. socket_data_pump_ = std::make_unique<SocketDataPump>(
  67. socket_.get(), this /*delegate*/, std::move(receive_producer_handle),
  68. std::move(send_consumer_handle), traffic_annotation_);
  69. absl::optional<net::SSLInfo> ssl_info;
  70. if (send_ssl_info_) {
  71. net::SSLInfo local;
  72. socket_->GetSSLInfo(&local);
  73. ssl_info = std::move(local);
  74. }
  75. std::move(connect_callback_)
  76. .Run(net::OK, std::move(receive_consumer_handle),
  77. std::move(send_producer_handle), std::move(ssl_info));
  78. }
  79. void TLSClientSocket::OnNetworkReadError(int net_error) {
  80. if (observer_)
  81. observer_->OnReadError(net_error);
  82. }
  83. void TLSClientSocket::OnNetworkWriteError(int net_error) {
  84. if (observer_)
  85. observer_->OnWriteError(net_error);
  86. }
  87. void TLSClientSocket::OnShutdown() {
  88. // Do nothing.
  89. }
  90. } // namespace network