WebKit Roll 97377:97400.
[chromium-blink-merge.git] / remoting / jingle_glue / fake_signal_strategy.cc
blobbe95819b334a07c500cd69868bf59ac2f9c1ff36
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"
7 #include "base/bind.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"
15 namespace remoting {
17 // static
18 void FakeSignalStrategy::Connect(FakeSignalStrategy* peer1,
19 FakeSignalStrategy* peer2) {
20 peer1->peer_ = peer2;
21 peer2->peer_ = peer1;
24 FakeSignalStrategy::FakeSignalStrategy(const std::string& jid)
25 : jid_(jid),
26 peer_(NULL),
27 listener_(NULL),
28 last_id_(0),
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());
49 listener_ = NULL;
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);
58 listener_ = listener;
61 void FakeSignalStrategy::SendStanza(buzz::XmlElement* stanza) {
62 DCHECK(CalledOnValidThread());
64 stanza->SetAttr(buzz::QN_FROM, jid_);
66 if (peer_) {
67 peer_->OnIncomingMessage(stanza);
68 } else {
69 delete stanza;
73 std::string FakeSignalStrategy::GetNextId() {
74 ++last_id_;
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();
99 return;
102 if (listener_)
103 listener_->OnIncomingStanza(stanza);
104 iq_registry_.OnIncomingStanza(stanza);
106 pending_messages_.pop();
107 delete stanza;
111 } // namespace remoting