| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef REMOTING_PROTOCOL_FAKE_SESSION_H_ | 5 #ifndef REMOTING_PROTOCOL_FAKE_SESSION_H_ |
| 6 #define REMOTING_PROTOCOL_FAKE_SESSION_H_ | 6 #define REMOTING_PROTOCOL_FAKE_SESSION_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "remoting/protocol/fake_stream_socket.h" |
| 14 #include "net/base/completion_callback.h" | |
| 15 #include "net/socket/socket.h" | |
| 16 #include "net/socket/stream_socket.h" | |
| 17 #include "remoting/protocol/session.h" | 14 #include "remoting/protocol/session.h" |
| 18 #include "remoting/protocol/stream_channel_factory.h" | |
| 19 | |
| 20 namespace base { | |
| 21 class MessageLoop; | |
| 22 } | |
| 23 | 15 |
| 24 namespace remoting { | 16 namespace remoting { |
| 25 namespace protocol { | 17 namespace protocol { |
| 26 | 18 |
| 27 extern const char kTestJid[]; | 19 extern const char kTestJid[]; |
| 28 | 20 |
| 29 // FakeSocket implement net::Socket interface for FakeConnection. All data | 21 // FakeSession is a dummy protocol::Session that uses FakeStreamSocket for all |
| 30 // written to FakeSocket is stored in a buffer returned by written_data(). | |
| 31 // Read() reads data from another buffer that can be set with AppendInputData(). | |
| 32 // Pending reads are supported, so if there is a pending read AppendInputData() | |
| 33 // calls the read callback. | |
| 34 // | |
| 35 // Two fake sockets can be connected to each other using the | |
| 36 // PairWith() method, e.g.: a->PairWith(b). After this all data | |
| 37 // written to |a| can be read from |b| and vica versa. Two connected | |
| 38 // sockets |a| and |b| must be created and used on the same thread. | |
| 39 class FakeSocket : public net::StreamSocket { | |
| 40 public: | |
| 41 FakeSocket(); | |
| 42 virtual ~FakeSocket(); | |
| 43 | |
| 44 const std::string& written_data() const { return written_data_; } | |
| 45 | |
| 46 void set_write_limit(int write_limit) { write_limit_ = write_limit; } | |
| 47 void set_async_write(bool async_write) { async_write_ = async_write; } | |
| 48 void set_next_write_error(int error) { next_write_error_ = error; } | |
| 49 void set_next_read_error(int error) { next_read_error_ = error; } | |
| 50 void AppendInputData(const std::vector<char>& data); | |
| 51 void PairWith(FakeSocket* peer_socket); | |
| 52 int input_pos() const { return input_pos_; } | |
| 53 bool read_pending() const { return read_pending_; } | |
| 54 | |
| 55 // net::Socket implementation. | |
| 56 virtual int Read(net::IOBuffer* buf, int buf_len, | |
| 57 const net::CompletionCallback& callback) OVERRIDE; | |
| 58 virtual int Write(net::IOBuffer* buf, int buf_len, | |
| 59 const net::CompletionCallback& callback) OVERRIDE; | |
| 60 | |
| 61 virtual int SetReceiveBufferSize(int32 size) OVERRIDE; | |
| 62 virtual int SetSendBufferSize(int32 size) OVERRIDE; | |
| 63 | |
| 64 // net::StreamSocket interface. | |
| 65 virtual int Connect(const net::CompletionCallback& callback) OVERRIDE; | |
| 66 virtual void Disconnect() OVERRIDE; | |
| 67 virtual bool IsConnected() const OVERRIDE; | |
| 68 virtual bool IsConnectedAndIdle() const OVERRIDE; | |
| 69 virtual int GetPeerAddress(net::IPEndPoint* address) const OVERRIDE; | |
| 70 virtual int GetLocalAddress(net::IPEndPoint* address) const OVERRIDE; | |
| 71 virtual const net::BoundNetLog& NetLog() const OVERRIDE; | |
| 72 virtual void SetSubresourceSpeculation() OVERRIDE; | |
| 73 virtual void SetOmniboxSpeculation() OVERRIDE; | |
| 74 virtual bool WasEverUsed() const OVERRIDE; | |
| 75 virtual bool UsingTCPFastOpen() const OVERRIDE; | |
| 76 virtual bool WasNpnNegotiated() const OVERRIDE; | |
| 77 virtual net::NextProto GetNegotiatedProtocol() const OVERRIDE; | |
| 78 virtual bool GetSSLInfo(net::SSLInfo* ssl_info) OVERRIDE; | |
| 79 | |
| 80 private: | |
| 81 void DoAsyncWrite(scoped_refptr<net::IOBuffer> buf, int buf_len, | |
| 82 const net::CompletionCallback& callback); | |
| 83 void DoWrite(net::IOBuffer* buf, int buf_len); | |
| 84 | |
| 85 bool async_write_; | |
| 86 bool write_pending_; | |
| 87 int write_limit_; | |
| 88 int next_write_error_; | |
| 89 | |
| 90 int next_read_error_; | |
| 91 bool read_pending_; | |
| 92 scoped_refptr<net::IOBuffer> read_buffer_; | |
| 93 int read_buffer_size_; | |
| 94 net::CompletionCallback read_callback_; | |
| 95 base::WeakPtr<FakeSocket> peer_socket_; | |
| 96 | |
| 97 std::string written_data_; | |
| 98 std::string input_data_; | |
| 99 int input_pos_; | |
| 100 | |
| 101 net::BoundNetLog net_log_; | |
| 102 | |
| 103 base::MessageLoop* message_loop_; | |
| 104 base::WeakPtrFactory<FakeSocket> weak_factory_; | |
| 105 | |
| 106 DISALLOW_COPY_AND_ASSIGN(FakeSocket); | |
| 107 }; | |
| 108 | |
| 109 // FakeUdpSocket is similar to FakeSocket but behaves as UDP socket. All written | |
| 110 // packets are stored separetely in written_packets(). AppendInputPacket() adds | |
| 111 // one packet that will be returned by Read(). | |
| 112 class FakeUdpSocket : public net::Socket { | |
| 113 public: | |
| 114 FakeUdpSocket(); | |
| 115 virtual ~FakeUdpSocket(); | |
| 116 | |
| 117 const std::vector<std::string>& written_packets() const { | |
| 118 return written_packets_; | |
| 119 } | |
| 120 | |
| 121 void AppendInputPacket(const char* data, int data_size); | |
| 122 int input_pos() const { return input_pos_; } | |
| 123 | |
| 124 // net::Socket implementation. | |
| 125 virtual int Read(net::IOBuffer* buf, int buf_len, | |
| 126 const net::CompletionCallback& callback) OVERRIDE; | |
| 127 virtual int Write(net::IOBuffer* buf, int buf_len, | |
| 128 const net::CompletionCallback& callback) OVERRIDE; | |
| 129 | |
| 130 virtual int SetReceiveBufferSize(int32 size) OVERRIDE; | |
| 131 virtual int SetSendBufferSize(int32 size) OVERRIDE; | |
| 132 | |
| 133 private: | |
| 134 bool read_pending_; | |
| 135 scoped_refptr<net::IOBuffer> read_buffer_; | |
| 136 int read_buffer_size_; | |
| 137 net::CompletionCallback read_callback_; | |
| 138 | |
| 139 std::vector<std::string> written_packets_; | |
| 140 std::vector<std::string> input_packets_; | |
| 141 int input_pos_; | |
| 142 | |
| 143 base::MessageLoop* message_loop_; | |
| 144 | |
| 145 DISALLOW_COPY_AND_ASSIGN(FakeUdpSocket); | |
| 146 }; | |
| 147 | |
| 148 // FakeSession is a dummy protocol::Session that uses FakeSocket for all | |
| 149 // channels. | 22 // channels. |
| 150 class FakeSession : public Session, | 23 class FakeSession : public Session { |
| 151 public StreamChannelFactory { | |
| 152 public: | 24 public: |
| 153 FakeSession(); | 25 FakeSession(); |
| 154 virtual ~FakeSession(); | 26 virtual ~FakeSession(); |
| 155 | 27 |
| 156 EventHandler* event_handler() { return event_handler_; } | 28 EventHandler* event_handler() { return event_handler_; } |
| 157 | 29 |
| 158 void set_async_creation(bool async_creation) { | |
| 159 async_creation_ = async_creation; | |
| 160 } | |
| 161 | |
| 162 void set_error(ErrorCode error) { error_ = error; } | 30 void set_error(ErrorCode error) { error_ = error; } |
| 163 | 31 |
| 164 bool is_closed() const { return closed_; } | 32 bool is_closed() const { return closed_; } |
| 165 | 33 |
| 166 FakeSocket* GetStreamChannel(const std::string& name); | 34 FakeStreamChannelFactory& fake_channel_factory() { return channel_factory_; } |
| 167 FakeUdpSocket* GetDatagramChannel(const std::string& name); | |
| 168 | 35 |
| 169 // Session interface. | 36 // Session interface. |
| 170 virtual void SetEventHandler(EventHandler* event_handler) OVERRIDE; | 37 virtual void SetEventHandler(EventHandler* event_handler) OVERRIDE; |
| 171 virtual ErrorCode error() OVERRIDE; | 38 virtual ErrorCode error() OVERRIDE; |
| 172 virtual const std::string& jid() OVERRIDE; | 39 virtual const std::string& jid() OVERRIDE; |
| 173 virtual const CandidateSessionConfig* candidate_config() OVERRIDE; | 40 virtual const CandidateSessionConfig* candidate_config() OVERRIDE; |
| 174 virtual const SessionConfig& config() OVERRIDE; | 41 virtual const SessionConfig& config() OVERRIDE; |
| 175 virtual void set_config(const SessionConfig& config) OVERRIDE; | 42 virtual void set_config(const SessionConfig& config) OVERRIDE; |
| 176 virtual StreamChannelFactory* GetTransportChannelFactory() OVERRIDE; | 43 virtual StreamChannelFactory* GetTransportChannelFactory() OVERRIDE; |
| 177 virtual StreamChannelFactory* GetMultiplexedChannelFactory() OVERRIDE; | 44 virtual StreamChannelFactory* GetMultiplexedChannelFactory() OVERRIDE; |
| 178 virtual void Close() OVERRIDE; | 45 virtual void Close() OVERRIDE; |
| 179 | 46 |
| 180 // StreamChannelFactory interface. | |
| 181 virtual void CreateChannel(const std::string& name, | |
| 182 const ChannelCreatedCallback& callback) OVERRIDE; | |
| 183 virtual void CancelChannelCreation(const std::string& name) OVERRIDE; | |
| 184 | |
| 185 public: | 47 public: |
| 186 void NotifyChannelCreated(const std::string& name, | |
| 187 const ChannelCreatedCallback& callback); | |
| 188 | |
| 189 EventHandler* event_handler_; | 48 EventHandler* event_handler_; |
| 190 scoped_ptr<const CandidateSessionConfig> candidate_config_; | 49 scoped_ptr<const CandidateSessionConfig> candidate_config_; |
| 191 SessionConfig config_; | 50 SessionConfig config_; |
| 192 base::MessageLoop* message_loop_; | |
| 193 | 51 |
| 194 bool async_creation_; | 52 FakeStreamChannelFactory channel_factory_; |
| 195 | |
| 196 std::map<std::string, FakeSocket*> stream_channels_; | |
| 197 std::map<std::string, FakeUdpSocket*> datagram_channels_; | |
| 198 | 53 |
| 199 std::string jid_; | 54 std::string jid_; |
| 200 | 55 |
| 201 ErrorCode error_; | 56 ErrorCode error_; |
| 202 bool closed_; | 57 bool closed_; |
| 203 | 58 |
| 204 base::WeakPtrFactory<FakeSession> weak_factory_; | |
| 205 | |
| 206 DISALLOW_COPY_AND_ASSIGN(FakeSession); | 59 DISALLOW_COPY_AND_ASSIGN(FakeSession); |
| 207 }; | 60 }; |
| 208 | 61 |
| 209 } // namespace protocol | 62 } // namespace protocol |
| 210 } // namespace remoting | 63 } // namespace remoting |
| 211 | 64 |
| 212 #endif // REMOTING_PROTOCOL_FAKE_SESSION_H_ | 65 #endif // REMOTING_PROTOCOL_FAKE_SESSION_H_ |
| OLD | NEW |