MD Downloads: prevent search text from overlapping with the cancel search (X)
[chromium-blink-merge.git] / remoting / protocol / fake_stream_socket.h
blobc234f338aa2f902599c7c4796518bddea7abb2f3
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_
8 #include <map>
9 #include <string>
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "net/base/completion_callback.h"
14 #include "remoting/protocol/p2p_stream_socket.h"
15 #include "remoting/protocol/stream_channel_factory.h"
17 namespace base {
18 class SingleThreadTaskRunner;
21 namespace remoting {
22 namespace protocol {
24 // FakeStreamSocket implement P2PStreamSocket 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 P2PStreamSocket {
35 public:
36 FakeStreamSocket();
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
60 // unpairs them.
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 // P2PStreamSocket interface.
72 int Read(const scoped_refptr<net::IOBuffer>& buf, int buf_len,
73 const net::CompletionCallback& callback) override;
74 int Write(const scoped_refptr<net::IOBuffer>& buf, int buf_len,
75 const net::CompletionCallback& callback) override;
77 private:
78 void DoAsyncWrite(const scoped_refptr<net::IOBuffer>& buf, int buf_len,
79 const net::CompletionCallback& callback);
80 void DoWrite(const scoped_refptr<net::IOBuffer>& buf, int buf_len);
82 bool async_write_;
83 bool write_pending_;
84 int write_limit_;
85 int next_write_error_;
87 int next_read_error_;
88 scoped_refptr<net::IOBuffer> read_buffer_;
89 int read_buffer_size_;
90 net::CompletionCallback read_callback_;
91 base::WeakPtr<FakeStreamSocket> peer_socket_;
93 std::string written_data_;
94 std::string input_data_;
95 int input_pos_;
97 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
98 base::WeakPtrFactory<FakeStreamSocket> weak_factory_;
100 DISALLOW_COPY_AND_ASSIGN(FakeStreamSocket);
103 // StreamChannelFactory that creates FakeStreamSocket.
104 class FakeStreamChannelFactory : public StreamChannelFactory {
105 public:
106 FakeStreamChannelFactory();
107 ~FakeStreamChannelFactory() override;
109 void set_asynchronous_create(bool asynchronous_create) {
110 asynchronous_create_ = asynchronous_create;
113 void set_fail_create(bool fail_create) { fail_create_ = fail_create; }
115 FakeStreamSocket* GetFakeChannel(const std::string& name);
117 // ChannelFactory interface.
118 void CreateChannel(const std::string& name,
119 const ChannelCreatedCallback& callback) override;
120 void CancelChannelCreation(const std::string& name) override;
122 private:
123 void NotifyChannelCreated(scoped_ptr<FakeStreamSocket> owned_channel,
124 const std::string& name,
125 const ChannelCreatedCallback& callback);
127 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
128 bool asynchronous_create_;
129 std::map<std::string, base::WeakPtr<FakeStreamSocket> > channels_;
131 bool fail_create_;
133 base::WeakPtrFactory<FakeStreamChannelFactory> weak_factory_;
135 DISALLOW_COPY_AND_ASSIGN(FakeStreamChannelFactory);
138 } // namespace protocol
139 } // namespace remoting
141 #endif // REMOTING_PROTOCOL_FAKE_STREAM_SOCKET_H_