1 // Copyright (c) 2011 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/jingle_glue/fake_signal_strategy.h"
8 #include "base/logging.h"
9 #include "base/message_loop.h"
10 #include "base/stl_util.h"
11 #include "base/string_number_conversions.h"
12 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h"
13 #include "third_party/libjingle/source/talk/xmpp/constants.h"
18 void FakeSignalStrategy::Connect(FakeSignalStrategy
* peer1
,
19 FakeSignalStrategy
* peer2
) {
24 FakeSignalStrategy::FakeSignalStrategy(const std::string
& jid
)
29 ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)) {
33 FakeSignalStrategy::~FakeSignalStrategy() {
34 while (!pending_messages_
.empty()) {
35 delete pending_messages_
.front();
36 pending_messages_
.pop();
40 void FakeSignalStrategy::Init(StatusObserver
* observer
) {
41 observer
->OnStateChange(StatusObserver::START
);
42 observer
->OnStateChange(StatusObserver::CONNECTING
);
43 observer
->OnJidChange(jid_
);
44 observer
->OnStateChange(StatusObserver::CONNECTED
);
47 void FakeSignalStrategy::Close() {
48 DCHECK(CalledOnValidThread());
52 void FakeSignalStrategy::SetListener(Listener
* listener
) {
53 DCHECK(CalledOnValidThread());
55 // Don't overwrite an listener without explicitly going
56 // through "NULL" first.
57 DCHECK(listener_
== NULL
|| listener
== NULL
);
61 void FakeSignalStrategy::SendStanza(buzz::XmlElement
* stanza
) {
62 DCHECK(CalledOnValidThread());
64 stanza
->SetAttr(buzz::QN_FROM
, jid_
);
67 peer_
->OnIncomingMessage(stanza
);
73 std::string
FakeSignalStrategy::GetNextId() {
75 return base::IntToString(last_id_
);
78 IqRequest
* FakeSignalStrategy::CreateIqRequest() {
79 DCHECK(CalledOnValidThread());
81 return new JavascriptIqRequest(this, &iq_registry_
);
84 void FakeSignalStrategy::OnIncomingMessage(buzz::XmlElement
* stanza
) {
85 pending_messages_
.push(stanza
);
86 MessageLoop::current()->PostTask(
87 FROM_HERE
, task_factory_
.NewRunnableMethod(
88 &FakeSignalStrategy::DeliverIncomingMessages
));
91 void FakeSignalStrategy::DeliverIncomingMessages() {
92 while (!pending_messages_
.empty()) {
93 buzz::XmlElement
* stanza
= pending_messages_
.front();
94 const std::string
& to_field
= stanza
->Attr(buzz::QN_TO
);
95 if (to_field
!= jid_
) {
96 LOG(WARNING
) << "Dropping stanza that is addressed to " << to_field
97 << ". Local jid: " << jid_
98 << ". Message content: " << stanza
->Str();
103 listener_
->OnIncomingStanza(stanza
);
104 iq_registry_
.OnIncomingStanza(stanza
);
106 pending_messages_
.pop();
111 } // namespace remoting