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::Connect() {
60 DCHECK(CalledOnValidThread());
61 FOR_EACH_OBSERVER(Listener
, listeners_
,
62 OnSignalStrategyStateChange(CONNECTED
));
65 void FakeSignalStrategy::Disconnect() {
66 DCHECK(CalledOnValidThread());
67 FOR_EACH_OBSERVER(Listener
, listeners_
,
68 OnSignalStrategyStateChange(DISCONNECTED
));
71 SignalStrategy::State
FakeSignalStrategy::GetState() const {
75 SignalStrategy::Error
FakeSignalStrategy::GetError() const {
79 std::string
FakeSignalStrategy::GetLocalJid() const {
80 DCHECK(CalledOnValidThread());
84 void FakeSignalStrategy::AddListener(Listener
* listener
) {
85 DCHECK(CalledOnValidThread());
86 listeners_
.AddObserver(listener
);
89 void FakeSignalStrategy::RemoveListener(Listener
* listener
) {
90 DCHECK(CalledOnValidThread());
91 listeners_
.RemoveObserver(listener
);
94 bool FakeSignalStrategy::SendStanza(scoped_ptr
<buzz::XmlElement
> stanza
) {
95 DCHECK(CalledOnValidThread());
97 stanza
->SetAttr(buzz::QN_FROM
, jid_
);
99 if (!peer_callback_
.is_null()) {
100 if (send_delay_
!= base::TimeDelta()) {
101 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
102 FROM_HERE
, base::Bind(peer_callback_
, base::Passed(&stanza
)),
105 peer_callback_
.Run(stanza
.Pass());
113 std::string
FakeSignalStrategy::GetNextId() {
115 return base::IntToString(last_id_
);
119 void FakeSignalStrategy::DeliverMessageOnThread(
120 scoped_refptr
<base::SingleThreadTaskRunner
> thread
,
121 base::WeakPtr
<FakeSignalStrategy
> target
,
122 scoped_ptr
<buzz::XmlElement
> stanza
) {
123 thread
->PostTask(FROM_HERE
,
124 base::Bind(&FakeSignalStrategy::OnIncomingMessage
,
125 target
, base::Passed(&stanza
)));
128 void FakeSignalStrategy::OnIncomingMessage(
129 scoped_ptr
<buzz::XmlElement
> stanza
) {
130 DCHECK(CalledOnValidThread());
132 buzz::XmlElement
* stanza_ptr
= stanza
.get();
133 received_messages_
.push_back(stanza
.release());
135 const std::string
& to_field
= stanza_ptr
->Attr(buzz::QN_TO
);
136 if (to_field
!= jid_
) {
137 LOG(WARNING
) << "Dropping stanza that is addressed to " << to_field
138 << ". Local jid: " << jid_
139 << ". Message content: " << stanza_ptr
->Str();
143 ObserverListBase
<Listener
>::Iterator
it(&listeners_
);
145 while ((listener
= it
.GetNext()) != nullptr) {
146 if (listener
->OnSignalStrategyIncomingStanza(stanza_ptr
))
151 void FakeSignalStrategy::SetPeerCallback(const PeerCallback
& peer_callback
) {
152 peer_callback_
= peer_callback
;
155 } // namespace remoting