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 Write() call. Once returned the
49 // value is automatically reset to net::OK .
50 void set_next_write_error(int error
) { next_write_error_
= error
; }
52 // Appends |data| to the read buffer.
53 void AppendInputData(const std::string
& data
);
55 // Causes Read() to fail with |error| once the read buffer is exhausted. If
56 // there is a currently pending Read, it is interrupted.
57 void AppendReadError(int error
);
59 // Pairs the socket with |peer_socket|. Deleting either of the paired sockets
61 void PairWith(FakeStreamSocket
* peer_socket
);
63 // Current input position in bytes.
64 int input_pos() const { return input_pos_
; }
66 // True if a Read() call is currently pending.
67 bool read_pending() const { return !read_callback_
.is_null(); }
69 base::WeakPtr
<FakeStreamSocket
> GetWeakPtr();
71 // net::Socket implementation.
72 int Read(net::IOBuffer
* buf
,
74 const net::CompletionCallback
& callback
) override
;
75 int Write(net::IOBuffer
* buf
,
77 const net::CompletionCallback
& callback
) override
;
78 int SetReceiveBufferSize(int32 size
) override
;
79 int SetSendBufferSize(int32 size
) override
;
81 // net::StreamSocket interface.
82 int Connect(const net::CompletionCallback
& callback
) override
;
83 void Disconnect() override
;
84 bool IsConnected() const override
;
85 bool IsConnectedAndIdle() const override
;
86 int GetPeerAddress(net::IPEndPoint
* address
) const override
;
87 int GetLocalAddress(net::IPEndPoint
* address
) const override
;
88 const net::BoundNetLog
& NetLog() const override
;
89 void SetSubresourceSpeculation() override
;
90 void SetOmniboxSpeculation() override
;
91 bool WasEverUsed() const override
;
92 bool UsingTCPFastOpen() const override
;
93 bool WasNpnNegotiated() const override
;
94 net::NextProto
GetNegotiatedProtocol() const override
;
95 bool GetSSLInfo(net::SSLInfo
* ssl_info
) override
;
98 void DoAsyncWrite(scoped_refptr
<net::IOBuffer
> buf
, int buf_len
,
99 const net::CompletionCallback
& callback
);
100 void DoWrite(net::IOBuffer
* buf
, int buf_len
);
105 int next_write_error_
;
107 int next_read_error_
;
108 scoped_refptr
<net::IOBuffer
> read_buffer_
;
109 int read_buffer_size_
;
110 net::CompletionCallback read_callback_
;
111 base::WeakPtr
<FakeStreamSocket
> peer_socket_
;
113 std::string written_data_
;
114 std::string input_data_
;
117 net::BoundNetLog net_log_
;
119 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
120 base::WeakPtrFactory
<FakeStreamSocket
> weak_factory_
;
122 DISALLOW_COPY_AND_ASSIGN(FakeStreamSocket
);
125 // StreamChannelFactory that creates FakeStreamSocket.
126 class FakeStreamChannelFactory
: public StreamChannelFactory
{
128 FakeStreamChannelFactory();
129 ~FakeStreamChannelFactory() override
;
131 void set_asynchronous_create(bool asynchronous_create
) {
132 asynchronous_create_
= asynchronous_create
;
135 void set_fail_create(bool fail_create
) { fail_create_
= fail_create
; }
137 FakeStreamSocket
* GetFakeChannel(const std::string
& name
);
139 // ChannelFactory interface.
140 void CreateChannel(const std::string
& name
,
141 const ChannelCreatedCallback
& callback
) override
;
142 void CancelChannelCreation(const std::string
& name
) override
;
145 void NotifyChannelCreated(scoped_ptr
<FakeStreamSocket
> owned_channel
,
146 const std::string
& name
,
147 const ChannelCreatedCallback
& callback
);
149 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
150 bool asynchronous_create_
;
151 std::map
<std::string
, base::WeakPtr
<FakeStreamSocket
> > channels_
;
155 base::WeakPtrFactory
<FakeStreamChannelFactory
> weak_factory_
;
157 DISALLOW_COPY_AND_ASSIGN(FakeStreamChannelFactory
);
160 } // namespace protocol
161 } // namespace remoting
163 #endif // REMOTING_PROTOCOL_FAKE_STREAM_SOCKET_H_