| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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 #ifndef CHROME_BROWSER_SYNC_NOTIFIER_COMMUNICATOR_SSL_SOCKET_ADAPTER_H_ | |
| 6 #define CHROME_BROWSER_SYNC_NOTIFIER_COMMUNICATOR_SSL_SOCKET_ADAPTER_H_ | |
| 7 | |
| 8 #include "base/scoped_ptr.h" | |
| 9 #include "net/base/completion_callback.h" | |
| 10 #include "net/base/io_buffer.h" | |
| 11 #include "net/base/net_errors.h" | |
| 12 #include "net/base/net_log.h" | |
| 13 #include "net/socket/client_socket.h" | |
| 14 #include "net/socket/ssl_client_socket.h" | |
| 15 #include "talk/base/asyncsocket.h" | |
| 16 #include "talk/base/ssladapter.h" | |
| 17 | |
| 18 namespace notifier { | |
| 19 | |
| 20 class SSLSocketAdapter; | |
| 21 | |
| 22 // This class provides a wrapper to libjingle's talk_base::AsyncSocket that | |
| 23 // implements Chromium's net::ClientSocket interface. It's used by | |
| 24 // SSLSocketAdapter to enable Chromium's SSL implementation to work over | |
| 25 // libjingle's socket class. | |
| 26 class TransportSocket : public net::ClientSocket, public sigslot::has_slots<> { | |
| 27 public: | |
| 28 TransportSocket(talk_base::AsyncSocket* socket, | |
| 29 SSLSocketAdapter *ssl_adapter); | |
| 30 | |
| 31 void set_addr(const talk_base::SocketAddress& addr) { | |
| 32 addr_ = addr; | |
| 33 } | |
| 34 | |
| 35 // net::ClientSocket implementation | |
| 36 | |
| 37 virtual int Connect(net::CompletionCallback* callback); | |
| 38 virtual void Disconnect(); | |
| 39 virtual bool IsConnected() const; | |
| 40 virtual bool IsConnectedAndIdle() const; | |
| 41 virtual int GetPeerAddress(net::AddressList* address) const; | |
| 42 virtual const net::BoundNetLog& NetLog() const { return net_log_; } | |
| 43 | |
| 44 // net::Socket implementation | |
| 45 | |
| 46 virtual int Read(net::IOBuffer* buf, int buf_len, | |
| 47 net::CompletionCallback* callback); | |
| 48 virtual int Write(net::IOBuffer* buf, int buf_len, | |
| 49 net::CompletionCallback* callback); | |
| 50 virtual bool SetReceiveBufferSize(int32 size); | |
| 51 virtual bool SetSendBufferSize(int32 size); | |
| 52 | |
| 53 private: | |
| 54 friend class SSLSocketAdapter; | |
| 55 | |
| 56 void OnConnectEvent(talk_base::AsyncSocket * socket); | |
| 57 bool OnReadEvent(talk_base::AsyncSocket * socket); | |
| 58 bool OnWriteEvent(talk_base::AsyncSocket * socket); | |
| 59 | |
| 60 net::CompletionCallback* connect_callback_; | |
| 61 net::CompletionCallback* read_callback_; | |
| 62 net::CompletionCallback* write_callback_; | |
| 63 | |
| 64 scoped_refptr<net::IOBuffer> read_buffer_; | |
| 65 int read_buffer_len_; | |
| 66 scoped_refptr<net::IOBuffer> write_buffer_; | |
| 67 int write_buffer_len_; | |
| 68 | |
| 69 net::BoundNetLog net_log_; | |
| 70 | |
| 71 talk_base::AsyncSocket *socket_; | |
| 72 talk_base::SocketAddress addr_; | |
| 73 | |
| 74 DISALLOW_COPY_AND_ASSIGN(TransportSocket); | |
| 75 }; | |
| 76 | |
| 77 // This provides a talk_base::AsyncSocketAdapter interface around Chromium's | |
| 78 // net::SSLClientSocket class. This allows notifier to use Chromium's SSL | |
| 79 // implementation instead of OpenSSL. | |
| 80 class SSLSocketAdapter : public talk_base::SSLAdapter { | |
| 81 public: | |
| 82 explicit SSLSocketAdapter(talk_base::AsyncSocket* socket); | |
| 83 | |
| 84 // StartSSL returns 0 if successful, or non-zero on failure. | |
| 85 // If StartSSL is called while the socket is closed or connecting, the SSL | |
| 86 // negotiation will begin as soon as the socket connects. | |
| 87 // | |
| 88 // restartable is not implemented, and must be set to false. | |
| 89 virtual int StartSSL(const char* hostname, bool restartable); | |
| 90 | |
| 91 // Create the default SSL adapter for this platform. | |
| 92 static SSLSocketAdapter* Create(AsyncSocket* socket); | |
| 93 | |
| 94 virtual int Send(const void* pv, size_t cb); | |
| 95 virtual int Recv(void* pv, size_t cb); | |
| 96 | |
| 97 private: | |
| 98 friend class TransportSocket; | |
| 99 | |
| 100 enum State { | |
| 101 STATE_NONE, | |
| 102 STATE_READ, | |
| 103 STATE_READ_COMPLETE, | |
| 104 STATE_WRITE, | |
| 105 STATE_WRITE_COMPLETE, | |
| 106 STATE_SSL_WAIT | |
| 107 }; | |
| 108 | |
| 109 void OnConnected(int result); | |
| 110 void OnIO(int result); | |
| 111 | |
| 112 void OnReadEvent(talk_base::AsyncSocket * socket); | |
| 113 void OnWriteEvent(talk_base::AsyncSocket * socket); | |
| 114 void OnConnectEvent(talk_base::AsyncSocket * socket); | |
| 115 | |
| 116 int BeginSSL(); | |
| 117 | |
| 118 bool ignore_bad_cert_; | |
| 119 std::string hostname_; | |
| 120 TransportSocket* transport_socket_; | |
| 121 scoped_ptr<net::SSLClientSocket> ssl_socket_; | |
| 122 net::CompletionCallbackImpl<SSLSocketAdapter> connected_callback_; | |
| 123 net::CompletionCallbackImpl<SSLSocketAdapter> io_callback_; | |
| 124 bool ssl_connected_; | |
| 125 State state_; | |
| 126 scoped_refptr<net::IOBuffer> transport_buf_; | |
| 127 int data_transferred_; | |
| 128 | |
| 129 DISALLOW_COPY_AND_ASSIGN(SSLSocketAdapter); | |
| 130 }; | |
| 131 | |
| 132 } // namespace notifier | |
| 133 | |
| 134 #endif // CHROME_BROWSER_SYNC_NOTIFIER_COMMUNICATOR_SSL_SOCKET_ADAPTER_H_ | |
| OLD | NEW |