Roll src/third_party/WebKit 787a07c:716df21 (svn 201034:201036)
[chromium-blink-merge.git] / remoting / signaling / fake_signal_strategy.cc
blob996845ce8891749ce8e83b9ef785252036054a25
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"
7 #include "base/bind.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"
17 namespace remoting {
19 // static
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()),
30 jid_(jid),
31 last_id_(0),
32 weak_factory_(this) {
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,
46 main_thread_,
47 weak_factory_.GetWeakPtr());
48 if (peer->main_thread_->BelongsToCurrentThread()) {
49 peer->SetPeerCallback(peer_callback);
50 } else {
51 peer->main_thread_->PostTask(
52 FROM_HERE,
53 base::Bind(&FakeSignalStrategy::SetPeerCallback,
54 base::Unretained(peer),
55 peer_callback));
59 void FakeSignalStrategy::SetLocalJid(const std::string& jid) {
60 DCHECK(CalledOnValidThread());
61 jid_ = jid;
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 {
77 return CONNECTED;
80 SignalStrategy::Error FakeSignalStrategy::GetError() const {
81 return OK;
84 std::string FakeSignalStrategy::GetLocalJid() const {
85 DCHECK(CalledOnValidThread());
86 return jid_;
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)),
108 send_delay_);
109 } else {
110 peer_callback_.Run(stanza.Pass());
112 return true;
113 } else {
114 return false;
118 std::string FakeSignalStrategy::GetNextId() {
119 ++last_id_;
120 return base::IntToString(last_id_);
123 // static
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();
145 return;
148 base::ObserverListBase<Listener>::Iterator it(&listeners_);
149 Listener* listener;
150 while ((listener = it.GetNext()) != nullptr) {
151 if (listener->OnSignalStrategyIncomingStanza(stanza_ptr))
152 break;
156 void FakeSignalStrategy::SetPeerCallback(const PeerCallback& peer_callback) {
157 peer_callback_ = peer_callback;
160 } // namespace remoting