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 ~FakeStreamSocket() override
;
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 int Read(net::IOBuffer
* buf
,
71 const net::CompletionCallback
& callback
) override
;
72 int Write(net::IOBuffer
* buf
,
74 const net::CompletionCallback
& callback
) override
;
75 int SetReceiveBufferSize(int32 size
) override
;
76 int SetSendBufferSize(int32 size
) override
;
78 // net::StreamSocket interface.
79 int Connect(const net::CompletionCallback
& callback
) override
;
80 void Disconnect() override
;
81 bool IsConnected() const override
;
82 bool IsConnectedAndIdle() const override
;
83 int GetPeerAddress(net::IPEndPoint
* address
) const override
;
84 int GetLocalAddress(net::IPEndPoint
* address
) const override
;
85 const net::BoundNetLog
& NetLog() const override
;
86 void SetSubresourceSpeculation() override
;
87 void SetOmniboxSpeculation() override
;
88 bool WasEverUsed() const override
;
89 bool UsingTCPFastOpen() const override
;
90 bool WasNpnNegotiated() const override
;
91 net::NextProto
GetNegotiatedProtocol() const override
;
92 bool GetSSLInfo(net::SSLInfo
* ssl_info
) override
;
95 void DoAsyncWrite(scoped_refptr
<net::IOBuffer
> buf
, int buf_len
,
96 const net::CompletionCallback
& callback
);
97 void DoWrite(net::IOBuffer
* buf
, int buf_len
);
102 int next_write_error_
;
104 int next_read_error_
;
105 scoped_refptr
<net::IOBuffer
> read_buffer_
;
106 int read_buffer_size_
;
107 net::CompletionCallback read_callback_
;
108 base::WeakPtr
<FakeStreamSocket
> peer_socket_
;
110 std::string written_data_
;
111 std::string input_data_
;
114 net::BoundNetLog net_log_
;
116 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
117 base::WeakPtrFactory
<FakeStreamSocket
> weak_factory_
;
119 DISALLOW_COPY_AND_ASSIGN(FakeStreamSocket
);
122 // StreamChannelFactory that creates FakeStreamSocket.
123 class FakeStreamChannelFactory
: public StreamChannelFactory
{
125 FakeStreamChannelFactory();
126 ~FakeStreamChannelFactory() override
;
128 void set_asynchronous_create(bool asynchronous_create
) {
129 asynchronous_create_
= asynchronous_create
;
132 void set_fail_create(bool fail_create
) { fail_create_
= fail_create
; }
134 FakeStreamSocket
* GetFakeChannel(const std::string
& name
);
136 // ChannelFactory interface.
137 void CreateChannel(const std::string
& name
,
138 const ChannelCreatedCallback
& callback
) override
;
139 void CancelChannelCreation(const std::string
& name
) override
;
142 void NotifyChannelCreated(scoped_ptr
<FakeStreamSocket
> owned_channel
,
143 const std::string
& name
,
144 const ChannelCreatedCallback
& callback
);
146 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
147 bool asynchronous_create_
;
148 std::map
<std::string
, base::WeakPtr
<FakeStreamSocket
> > channels_
;
152 base::WeakPtrFactory
<FakeStreamChannelFactory
> weak_factory_
;
154 DISALLOW_COPY_AND_ASSIGN(FakeStreamChannelFactory
);
157 } // namespace protocol
158 } // namespace remoting
160 #endif // REMOTING_PROTOCOL_FAKE_STREAM_SOCKET_H_