OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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/socket/client_socket.h" |
| 13 #include "net/socket/ssl_client_socket.h" |
| 14 #include "talk/base/asyncsocket.h" |
| 15 |
| 16 namespace notifier { |
| 17 |
| 18 class SSLSocketAdapter; |
| 19 |
| 20 // This class provides a wrapper to libjingle's talk_base::AsyncSocket that |
| 21 // implements Chromium's net::ClientSocket interface. It's used by |
| 22 // SSLSocketAdapter to enable Chromium's SSL implementation to work over |
| 23 // libjingle's socket class. |
| 24 class TransportSocket : public net::ClientSocket, public sigslot::has_slots<> { |
| 25 public: |
| 26 TransportSocket(talk_base::AsyncSocket* socket, |
| 27 SSLSocketAdapter *ssl_adapter); |
| 28 |
| 29 void set_addr(const talk_base::SocketAddress& addr) { |
| 30 addr_ = addr; |
| 31 } |
| 32 |
| 33 // net::ClientSocket implementation |
| 34 |
| 35 virtual int Connect(net::CompletionCallback* callback); |
| 36 virtual void Disconnect(); |
| 37 virtual bool IsConnected() const; |
| 38 virtual bool IsConnectedAndIdle() const; |
| 39 |
| 40 #if defined(OS_LINUX) || defined(OS_MACOSX) |
| 41 virtual int GetPeerName(struct sockaddr *name, socklen_t *namelen); |
| 42 #endif |
| 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 void OnConnectEvent(talk_base::AsyncSocket * socket); |
| 55 void OnReadEvent(talk_base::AsyncSocket * socket); |
| 56 void OnWriteEvent(talk_base::AsyncSocket * socket); |
| 57 void OnCloseEvent(talk_base::AsyncSocket * socket, int err); |
| 58 |
| 59 net::CompletionCallback* connect_callback_; |
| 60 net::CompletionCallback* read_callback_; |
| 61 net::CompletionCallback* write_callback_; |
| 62 |
| 63 scoped_refptr<net::IOBuffer> read_buffer_; |
| 64 int read_buffer_len_; |
| 65 scoped_refptr<net::IOBuffer> write_buffer_; |
| 66 int write_buffer_len_; |
| 67 |
| 68 talk_base::AsyncSocket *socket_; |
| 69 talk_base::SocketAddress addr_; |
| 70 SSLSocketAdapter *ssl_adapter_; |
| 71 |
| 72 DISALLOW_COPY_AND_ASSIGN(TransportSocket); |
| 73 }; |
| 74 |
| 75 // This provides a talk_base::AsyncSocketAdapter interface around Chromium's |
| 76 // net::SSLClientSocket class. This allows notifier to use Chromium's SSL |
| 77 // implementation instead of OpenSSL. |
| 78 class SSLSocketAdapter : public talk_base::AsyncSocketAdapter { |
| 79 public: |
| 80 explicit SSLSocketAdapter(talk_base::AsyncSocket* socket); |
| 81 |
| 82 bool ignore_bad_cert() const { return ignore_bad_cert_; } |
| 83 void set_ignore_bad_cert(bool ignore) { ignore_bad_cert_ = ignore; } |
| 84 |
| 85 // StartSSL returns 0 if successful, or non-zero on failure. |
| 86 // If StartSSL is called while the socket is closed or connecting, the SSL |
| 87 // negotiation will begin as soon as the socket connects. |
| 88 // |
| 89 // restartable is not implemented, and must be set to false. |
| 90 virtual int StartSSL(const char* hostname, bool restartable); |
| 91 |
| 92 // Create the default SSL adapter for this platform. |
| 93 static SSLSocketAdapter* Create(AsyncSocket* socket); |
| 94 |
| 95 virtual int Send(const void* pv, size_t cb); |
| 96 virtual int Recv(void* pv, size_t cb); |
| 97 |
| 98 private: |
| 99 friend class TransportSocket; |
| 100 |
| 101 enum State { |
| 102 STATE_NONE, |
| 103 STATE_READ, |
| 104 STATE_READ_COMPLETE, |
| 105 STATE_WRITE, |
| 106 STATE_WRITE_COMPLETE |
| 107 }; |
| 108 |
| 109 void OnConnected(int result); |
| 110 void OnIO(int result); |
| 111 |
| 112 bool ignore_bad_cert_; |
| 113 scoped_ptr<TransportSocket> socket_; |
| 114 scoped_ptr<net::SSLClientSocket> ssl_socket_; |
| 115 net::CompletionCallbackImpl<SSLSocketAdapter> connected_callback_; |
| 116 net::CompletionCallbackImpl<SSLSocketAdapter> io_callback_; |
| 117 bool ssl_connected_; |
| 118 State state_; |
| 119 scoped_refptr<net::IOBuffer> transport_buf_; |
| 120 int data_transferred_; |
| 121 |
| 122 DISALLOW_COPY_AND_ASSIGN(SSLSocketAdapter); |
| 123 }; |
| 124 |
| 125 } // namespace notifier |
| 126 |
| 127 #endif // CHROME_BROWSER_SYNC_NOTIFIER_COMMUNICATOR_SSL_SOCKET_ADAPTER_H_ |
OLD | NEW |