blob: d0fb99add582d12c75ece1bdec2df3e97592802d (
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
|
// Copyright 2014 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_SIGNALING_FAKE_SIGNAL_STRATEGY_H_
#define REMOTING_SIGNALING_FAKE_SIGNAL_STRATEGY_H_
#include <list>
#include <queue>
#include <string>
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/threading/non_thread_safe.h"
#include "remoting/signaling/iq_sender.h"
#include "remoting/signaling/signal_strategy.h"
namespace base {
class SingleThreadTaskRunner;
} // namespace base
namespace remoting {
class FakeSignalStrategy : public SignalStrategy,
public base::NonThreadSafe {
public:
// Calls ConenctTo() to connect |peer1| and |peer2|. Both |peer1| and |peer2|
// must belong to the current thread.
static void Connect(FakeSignalStrategy* peer1, FakeSignalStrategy* peer2);
FakeSignalStrategy(const std::string& jid);
~FakeSignalStrategy() override;
const std::list<buzz::XmlElement*>& received_messages() {
return received_messages_;
}
void set_send_delay(base::TimeDelta delay) {
send_delay_ = delay;
}
// Connects current FakeSignalStrategy to receive messages from |peer|.
void ConnectTo(FakeSignalStrategy* peer);
void SetLocalJid(const std::string& jid);
// SignalStrategy interface.
void Connect() override;
void Disconnect() override;
State GetState() const override;
Error GetError() const override;
std::string GetLocalJid() const override;
void AddListener(Listener* listener) override;
void RemoveListener(Listener* listener) override;
bool SendStanza(scoped_ptr<buzz::XmlElement> stanza) override;
std::string GetNextId() override;
private:
typedef base::Callback<void(scoped_ptr<buzz::XmlElement> message)>
PeerCallback;
static void DeliverMessageOnThread(
scoped_refptr<base::SingleThreadTaskRunner> thread,
base::WeakPtr<FakeSignalStrategy> target,
scoped_ptr<buzz::XmlElement> stanza);
// Called by the |peer_|. Takes ownership of |stanza|.
void OnIncomingMessage(scoped_ptr<buzz::XmlElement> stanza);
void SetPeerCallback(const PeerCallback& peer_callback);
scoped_refptr<base::SingleThreadTaskRunner> main_thread_;
std::string jid_;
PeerCallback peer_callback_;
base::ObserverList<Listener, true> listeners_;
int last_id_;
base::TimeDelta send_delay_;
// All received messages, includes thouse still in |pending_messages_|.
std::list<buzz::XmlElement*> received_messages_;
base::WeakPtrFactory<FakeSignalStrategy> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(FakeSignalStrategy);
};
} // namespace remoting
#endif // REMOTING_SIGNALING_FAKE_SIGNAL_STRATEGY_H_
|