OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 REMOTING_PROTOCOL_FAKE_STREAM_SOCKET_H_ |
| 6 #define REMOTING_PROTOCOL_FAKE_STREAM_SOCKET_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <string> |
| 10 |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/memory/weak_ptr.h" |
| 13 #include "net/base/completion_callback.h" |
| 14 #include "net/socket/stream_socket.h" |
| 15 #include "remoting/protocol/stream_channel_factory.h" |
| 16 |
| 17 namespace base { |
| 18 class SingleThreadTaskRunner; |
| 19 } |
| 20 |
| 21 namespace remoting { |
| 22 namespace protocol { |
| 23 |
| 24 // FakeStreamSocket implement net::StreamSocket interface. All data written to |
| 25 // FakeStreamSocket is stored in a buffer returned by written_data(). Read() |
| 26 // reads data from another buffer that can be set with AppendInputData(). |
| 27 // Pending reads are supported, so if there is a pending read AppendInputData() |
| 28 // calls the read callback. |
| 29 // |
| 30 // Two fake sockets can be connected to each other using the |
| 31 // PairWith() method, e.g.: a->PairWith(b). After this all data |
| 32 // written to |a| can be read from |b| and vice versa. Two connected |
| 33 // sockets |a| and |b| must be created and used on the same thread. |
| 34 class FakeStreamSocket : public net::StreamSocket { |
| 35 public: |
| 36 FakeStreamSocket(); |
| 37 virtual ~FakeStreamSocket(); |
| 38 |
| 39 // Returns all data written to the socket. |
| 40 const std::string& written_data() const { return written_data_; } |
| 41 |
| 42 // Sets maximum number of bytes written by each Write() call. |
| 43 void set_write_limit(int write_limit) { write_limit_ = write_limit; } |
| 44 |
| 45 // Enables asynchronous Write(). |
| 46 void set_async_write(bool async_write) { async_write_ = async_write; } |
| 47 |
| 48 // Set error codes for the next Read() and Write() calls. Once returned the |
| 49 // values are automatically reset to net::OK . |
| 50 void set_next_read_error(int error) { next_read_error_ = error; } |
| 51 void set_next_write_error(int error) { next_write_error_ = error; } |
| 52 |
| 53 // Appends |data| to the read buffer. |
| 54 void AppendInputData(const std::string& data); |
| 55 |
| 56 // Pairs the socket with |peer_socket|. Deleting either of the paired sockets |
| 57 // unpairs them. |
| 58 void PairWith(FakeStreamSocket* peer_socket); |
| 59 |
| 60 // Current input position in bytes. |
| 61 int input_pos() const { return input_pos_; } |
| 62 |
| 63 // True if a Read() call is currently pending. |
| 64 bool read_pending() const { return !read_callback_.is_null(); } |
| 65 |
| 66 base::WeakPtr<FakeStreamSocket> GetWeakPtr(); |
| 67 |
| 68 // net::Socket implementation. |
| 69 virtual int Read(net::IOBuffer* buf, int buf_len, |
| 70 const net::CompletionCallback& callback) OVERRIDE; |
| 71 virtual int Write(net::IOBuffer* buf, int buf_len, |
| 72 const net::CompletionCallback& callback) OVERRIDE; |
| 73 virtual int SetReceiveBufferSize(int32 size) OVERRIDE; |
| 74 virtual int SetSendBufferSize(int32 size) OVERRIDE; |
| 75 |
| 76 // net::StreamSocket interface. |
| 77 virtual int Connect(const net::CompletionCallback& callback) OVERRIDE; |
| 78 virtual void Disconnect() OVERRIDE; |
| 79 virtual bool IsConnected() const OVERRIDE; |
| 80 virtual bool IsConnectedAndIdle() const OVERRIDE; |
| 81 virtual int GetPeerAddress(net::IPEndPoint* address) const OVERRIDE; |
| 82 virtual int GetLocalAddress(net::IPEndPoint* address) const OVERRIDE; |
| 83 virtual const net::BoundNetLog& NetLog() const OVERRIDE; |
| 84 virtual void SetSubresourceSpeculation() OVERRIDE; |
| 85 virtual void SetOmniboxSpeculation() OVERRIDE; |
| 86 virtual bool WasEverUsed() const OVERRIDE; |
| 87 virtual bool UsingTCPFastOpen() const OVERRIDE; |
| 88 virtual bool WasNpnNegotiated() const OVERRIDE; |
| 89 virtual net::NextProto GetNegotiatedProtocol() const OVERRIDE; |
| 90 virtual bool GetSSLInfo(net::SSLInfo* ssl_info) OVERRIDE; |
| 91 |
| 92 private: |
| 93 void DoAsyncWrite(scoped_refptr<net::IOBuffer> buf, int buf_len, |
| 94 const net::CompletionCallback& callback); |
| 95 void DoWrite(net::IOBuffer* buf, int buf_len); |
| 96 |
| 97 bool async_write_; |
| 98 bool write_pending_; |
| 99 int write_limit_; |
| 100 int next_write_error_; |
| 101 |
| 102 int next_read_error_; |
| 103 scoped_refptr<net::IOBuffer> read_buffer_; |
| 104 int read_buffer_size_; |
| 105 net::CompletionCallback read_callback_; |
| 106 base::WeakPtr<FakeStreamSocket> peer_socket_; |
| 107 |
| 108 std::string written_data_; |
| 109 std::string input_data_; |
| 110 int input_pos_; |
| 111 |
| 112 net::BoundNetLog net_log_; |
| 113 |
| 114 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 115 base::WeakPtrFactory<FakeStreamSocket> weak_factory_; |
| 116 |
| 117 DISALLOW_COPY_AND_ASSIGN(FakeStreamSocket); |
| 118 }; |
| 119 |
| 120 // StreamChannelFactory that creates FakeStreamSocket. |
| 121 class FakeStreamChannelFactory : public StreamChannelFactory { |
| 122 public: |
| 123 FakeStreamChannelFactory(); |
| 124 virtual ~FakeStreamChannelFactory(); |
| 125 |
| 126 void set_asynchronous_create(bool asynchronous_create) { |
| 127 asynchronous_create_ = asynchronous_create; |
| 128 } |
| 129 |
| 130 void set_fail_create(bool fail_create) { fail_create_ = fail_create; } |
| 131 |
| 132 FakeStreamSocket* GetFakeChannel(const std::string& name); |
| 133 |
| 134 // ChannelFactory interface. |
| 135 virtual void CreateChannel(const std::string& name, |
| 136 const ChannelCreatedCallback& callback) OVERRIDE; |
| 137 virtual void CancelChannelCreation(const std::string& name) OVERRIDE; |
| 138 |
| 139 private: |
| 140 void NotifyChannelCreated(scoped_ptr<FakeStreamSocket> owned_channel, |
| 141 const std::string& name, |
| 142 const ChannelCreatedCallback& callback); |
| 143 |
| 144 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 145 bool asynchronous_create_; |
| 146 std::map<std::string, base::WeakPtr<FakeStreamSocket> > channels_; |
| 147 |
| 148 bool fail_create_; |
| 149 |
| 150 base::WeakPtrFactory<FakeStreamChannelFactory> weak_factory_; |
| 151 |
| 152 DISALLOW_COPY_AND_ASSIGN(FakeStreamChannelFactory); |
| 153 }; |
| 154 |
| 155 } // namespace protocol |
| 156 } // namespace remoting |
| 157 |
| 158 #endif // REMOTING_PROTOCOL_FAKE_STREAM_SOCKET_H_ |
OLD | NEW |