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.
5 #ifndef REMOTING_PROTOCOL_FAKE_STREAM_SOCKET_H_
6 #define REMOTING_PROTOCOL_FAKE_STREAM_SOCKET_H_
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"
18 class SingleThreadTaskRunner;
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.
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 {
37 virtual ~FakeStreamSocket();
39 // Returns all data written to the socket.
40 const std::string& written_data() const { return written_data_; }
42 // Sets maximum number of bytes written by each Write() call.
43 void set_write_limit(int write_limit) { write_limit_ = write_limit; }
45 // Enables asynchronous Write().
46 void set_async_write(bool async_write) { async_write_ = async_write; }
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; }
53 // Appends |data| to the read buffer.
54 void AppendInputData(const std::string& data);
56 // Pairs the socket with |peer_socket|. Deleting either of the paired sockets
58 void PairWith(FakeStreamSocket* peer_socket);
60 // Current input position in bytes.
61 int input_pos() const { return input_pos_; }
63 // True if a Read() call is currently pending.
64 bool read_pending() const { return !read_callback_.is_null(); }
66 base::WeakPtr<FakeStreamSocket> GetWeakPtr();
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;
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;
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);
100 int next_write_error_;
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_;
108 std::string written_data_;
109 std::string input_data_;
112 net::BoundNetLog net_log_;
114 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
115 base::WeakPtrFactory<FakeStreamSocket> weak_factory_;
117 DISALLOW_COPY_AND_ASSIGN(FakeStreamSocket);
120 // StreamChannelFactory that creates FakeStreamSocket.
121 class FakeStreamChannelFactory : public StreamChannelFactory {
123 FakeStreamChannelFactory();
124 virtual ~FakeStreamChannelFactory();
126 void set_asynchronous_create(bool asynchronous_create) {
127 asynchronous_create_ = asynchronous_create;
130 void set_fail_create(bool fail_create) { fail_create_ = fail_create; }
132 FakeStreamSocket* GetFakeChannel(const std::string& name);
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;
140 void NotifyChannelCreated(scoped_ptr<FakeStreamSocket> owned_channel,
141 const std::string& name,
142 const ChannelCreatedCallback& callback);
144 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
145 bool asynchronous_create_;
146 std::map<std::string, base::WeakPtr<FakeStreamSocket> > channels_;
150 base::WeakPtrFactory<FakeStreamChannelFactory> weak_factory_;
152 DISALLOW_COPY_AND_ASSIGN(FakeStreamChannelFactory);
155 } // namespace protocol
156 } // namespace remoting
158 #endif // REMOTING_PROTOCOL_FAKE_STREAM_SOCKET_H_