summaryrefslogtreecommitdiffstats
path: root/remoting/protocol/fake_session.h
blob: 9b216fe24844d5eed902d7d904f73fcccde29942 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef REMOTING_PROTOCOL_FAKE_SESSION_H_
#define REMOTING_PROTOCOL_FAKE_SESSION_H_

#include <vector>

#include "base/scoped_ptr.h"
#include "net/socket/socket.h"
#include "remoting/protocol/session.h"

namespace remoting {
namespace protocol {

extern const char kTestJid[];

// FakeSocket implement net::Socket interface for FakeConnection. All data
// written to FakeSocket is stored in a buffer returned by written_data().
// Read() reads data from another buffer that can be set with AppendInputData().
// Pending reads are supported, so if there is a pending read AppendInputData()
// calls the read callback.
class FakeSocket : public net::Socket {
 public:
  FakeSocket();
  virtual ~FakeSocket();

  const std::vector<char>& written_data() { return written_data_; }

  void AppendInputData(char* data, int data_size);
  int input_pos() { return input_pos_; }

  // net::Socket interface.
  virtual int Read(net::IOBuffer* buf, int buf_len,
                   net::CompletionCallback* callback);
  virtual int Write(net::IOBuffer* buf, int buf_len,
                    net::CompletionCallback* callback);

  virtual bool SetReceiveBufferSize(int32 size);
  virtual bool SetSendBufferSize(int32 size);

 private:
  bool read_pending_;
  scoped_refptr<net::IOBuffer> read_buffer_;
  int read_buffer_size_;
  net::CompletionCallback* read_callback_;

  std::vector<char> written_data_;
  std::vector<char> input_data_;
  int input_pos_;
};

// FakeSession is a dummy protocol::Session that uses FakeSocket for all
// channels.
class FakeSession : public Session {
 public:
  FakeSession();
  virtual ~FakeSession();

  StateChangeCallback* state_change_callback() { return callback_.get(); }

  void set_message_loop(MessageLoop* message_loop) {
    message_loop_ = message_loop;
  }

  bool is_closed() { return closed_; }

  virtual void SetStateChangeCallback(StateChangeCallback* callback);

  virtual FakeSocket* control_channel();
  virtual FakeSocket* event_channel();
  virtual FakeSocket* video_channel();

  virtual FakeSocket* video_rtp_channel();
  virtual FakeSocket* video_rtcp_channel();

  virtual const std::string& jid();

  virtual MessageLoop* message_loop();
  virtual const CandidateSessionConfig* candidate_config();
  virtual const SessionConfig* config();
  virtual void set_config(const SessionConfig* config);

  virtual void Close(Task* closed_task);

 public:
  scoped_ptr<StateChangeCallback> callback_;
  scoped_ptr<const CandidateSessionConfig> candidate_config_;
  scoped_ptr<const SessionConfig> config_;
  MessageLoop* message_loop_;
  FakeSocket control_channel_;
  FakeSocket event_channel_;
  FakeSocket video_channel_;
  FakeSocket video_rtp_channel_;
  FakeSocket video_rtcp_channel_;
  std::string jid_;
  bool closed_;
};

}  // namespace protocol
}  // namespace remoting

#endif  // REMOTING_PROTOCOL_FAKE_SESSION_H_