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 #include "remoting/signaling/fake_signal_strategy.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/stl_util.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
15 #include "third_party/webrtc/libjingle/xmpp/constants.h"
20 void FakeSignalStrategy::Connect(FakeSignalStrategy
* peer1
,
21 FakeSignalStrategy
* peer2
) {
22 DCHECK(peer1
->main_thread_
->BelongsToCurrentThread());
23 DCHECK(peer2
->main_thread_
->BelongsToCurrentThread());
24 peer1
->ConnectTo(peer2
);
25 peer2
->ConnectTo(peer1
);
28 FakeSignalStrategy::FakeSignalStrategy(const std::string
& jid
)
29 : main_thread_(base::ThreadTaskRunnerHandle::Get()),
36 FakeSignalStrategy::~FakeSignalStrategy() {
37 while (!received_messages_
.empty()) {
38 delete received_messages_
.front();
39 received_messages_
.pop_front();
43 void FakeSignalStrategy::ConnectTo(FakeSignalStrategy
* peer
) {
44 PeerCallback peer_callback
=
45 base::Bind(&FakeSignalStrategy::DeliverMessageOnThread
,
47 weak_factory_
.GetWeakPtr());
48 if (peer
->main_thread_
->BelongsToCurrentThread()) {
49 peer
->SetPeerCallback(peer_callback
);
51 peer
->main_thread_
->PostTask(
53 base::Bind(&FakeSignalStrategy::SetPeerCallback
,
54 base::Unretained(peer
),
59 void FakeSignalStrategy::SetLocalJid(const std::string
& jid
) {
60 DCHECK(CalledOnValidThread());
64 void FakeSignalStrategy::Connect() {
65 DCHECK(CalledOnValidThread());
66 FOR_EACH_OBSERVER(Listener
, listeners_
,
67 OnSignalStrategyStateChange(CONNECTED
));
70 void FakeSignalStrategy::Disconnect() {
71 DCHECK(CalledOnValidThread());
72 FOR_EACH_OBSERVER(Listener
, listeners_
,
73 OnSignalStrategyStateChange(DISCONNECTED
));
76 SignalStrategy::State
FakeSignalStrategy::GetState() const {
80 SignalStrategy::Error
FakeSignalStrategy::GetError() const {
84 std::string
FakeSignalStrategy::GetLocalJid() const {
85 DCHECK(CalledOnValidThread());
89 void FakeSignalStrategy::AddListener(Listener
* listener
) {
90 DCHECK(CalledOnValidThread());
91 listeners_
.AddObserver(listener
);
94 void FakeSignalStrategy::RemoveListener(Listener
* listener
) {
95 DCHECK(CalledOnValidThread());
96 listeners_
.RemoveObserver(listener
);
99 bool FakeSignalStrategy::SendStanza(scoped_ptr
<buzz::XmlElement
> stanza
) {
100 DCHECK(CalledOnValidThread());
102 stanza
->SetAttr(buzz::QN_FROM
, jid_
);
104 if (!peer_callback_
.is_null()) {
105 if (send_delay_
!= base::TimeDelta()) {
106 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
107 FROM_HERE
, base::Bind(peer_callback_
, base::Passed(&stanza
)),
110 peer_callback_
.Run(stanza
.Pass());
118 std::string
FakeSignalStrategy::GetNextId() {
120 return base::IntToString(last_id_
);
124 void FakeSignalStrategy::DeliverMessageOnThread(
125 scoped_refptr
<base::SingleThreadTaskRunner
> thread
,
126 base::WeakPtr
<FakeSignalStrategy
> target
,
127 scoped_ptr
<buzz::XmlElement
> stanza
) {
128 thread
->PostTask(FROM_HERE
,
129 base::Bind(&FakeSignalStrategy::OnIncomingMessage
,
130 target
, base::Passed(&stanza
)));
133 void FakeSignalStrategy::OnIncomingMessage(
134 scoped_ptr
<buzz::XmlElement
> stanza
) {
135 DCHECK(CalledOnValidThread());
137 buzz::XmlElement
* stanza_ptr
= stanza
.get();
138 received_messages_
.push_back(stanza
.release());
140 const std::string
& to_field
= stanza_ptr
->Attr(buzz::QN_TO
);
141 if (to_field
!= jid_
) {
142 LOG(WARNING
) << "Dropping stanza that is addressed to " << to_field
143 << ". Local jid: " << jid_
144 << ". Message content: " << stanza_ptr
->Str();
148 base::ObserverListBase
<Listener
>::Iterator
it(&listeners_
);
150 while ((listener
= it
.GetNext()) != nullptr) {
151 if (listener
->OnSignalStrategyIncomingStanza(stanza_ptr
))
156 void FakeSignalStrategy::SetPeerCallback(const PeerCallback
& peer_callback
) {
157 peer_callback_
= peer_callback
;
160 } // namespace remoting