Instrumenting internals of AccountIdFetcher::Start to locate the source of jankiness.
[chromium-blink-merge.git] / remoting / signaling / fake_signal_strategy.cc
blob62c4ead72e78de6d8adfdc3ec808da7d087b6928
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::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 {
72 return CONNECTED;
75 SignalStrategy::Error FakeSignalStrategy::GetError() const {
76 return OK;
79 std::string FakeSignalStrategy::GetLocalJid() const {
80 DCHECK(CalledOnValidThread());
81 return jid_;
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 peer_callback_.Run(stanza.Pass());
101 return true;
102 } else {
103 return false;
107 std::string FakeSignalStrategy::GetNextId() {
108 ++last_id_;
109 return base::IntToString(last_id_);
112 // static
113 void FakeSignalStrategy::DeliverMessageOnThread(
114 scoped_refptr<base::SingleThreadTaskRunner> thread,
115 base::WeakPtr<FakeSignalStrategy> target,
116 scoped_ptr<buzz::XmlElement> stanza) {
117 thread->PostTask(FROM_HERE,
118 base::Bind(&FakeSignalStrategy::OnIncomingMessage,
119 target, base::Passed(&stanza)));
122 void FakeSignalStrategy::OnIncomingMessage(
123 scoped_ptr<buzz::XmlElement> stanza) {
124 DCHECK(CalledOnValidThread());
126 buzz::XmlElement* stanza_ptr = stanza.get();
127 received_messages_.push_back(stanza.release());
129 const std::string& to_field = stanza_ptr->Attr(buzz::QN_TO);
130 if (to_field != jid_) {
131 LOG(WARNING) << "Dropping stanza that is addressed to " << to_field
132 << ". Local jid: " << jid_
133 << ". Message content: " << stanza_ptr->Str();
134 return;
137 ObserverListBase<Listener>::Iterator it(listeners_);
138 Listener* listener;
139 while ((listener = it.GetNext()) != NULL) {
140 if (listener->OnSignalStrategyIncomingStanza(stanza_ptr))
141 break;
145 void FakeSignalStrategy::SetPeerCallback(const PeerCallback& peer_callback) {
146 peer_callback_ = peer_callback;
149 } // namespace remoting