OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/jingle_glue/fake_signal_strategy.h" | 5 #include "remoting/jingle_glue/fake_signal_strategy.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
12 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" | 12 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
13 #include "third_party/libjingle/source/talk/xmpp/constants.h" | 13 #include "third_party/libjingle/source/talk/xmpp/constants.h" |
14 | 14 |
15 namespace remoting { | 15 namespace remoting { |
16 | 16 |
17 // static | 17 // static |
18 void FakeSignalStrategy::Connect(FakeSignalStrategy* peer1, | 18 void FakeSignalStrategy::Connect(FakeSignalStrategy* peer1, |
19 FakeSignalStrategy* peer2) { | 19 FakeSignalStrategy* peer2) { |
20 peer1->peer_ = peer2; | 20 peer1->peer_ = peer2; |
21 peer2->peer_ = peer1; | 21 peer2->peer_ = peer1; |
22 } | 22 } |
23 | 23 |
24 FakeSignalStrategy::FakeSignalStrategy(const std::string& jid) | 24 FakeSignalStrategy::FakeSignalStrategy(const std::string& jid) |
25 : jid_(jid), | 25 : jid_(jid), |
26 peer_(NULL), | 26 peer_(NULL), |
27 listener_(NULL), | |
28 last_id_(0), | 27 last_id_(0), |
29 ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)) { | 28 ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)) { |
30 | 29 |
31 } | 30 } |
32 | 31 |
33 FakeSignalStrategy::~FakeSignalStrategy() { | 32 FakeSignalStrategy::~FakeSignalStrategy() { |
34 while (!pending_messages_.empty()) { | 33 while (!pending_messages_.empty()) { |
35 delete pending_messages_.front(); | 34 delete pending_messages_.front(); |
36 pending_messages_.pop(); | 35 pending_messages_.pop(); |
37 } | 36 } |
| 37 DCHECK(listeners_.empty()); |
38 } | 38 } |
39 | 39 |
40 void FakeSignalStrategy::Init(StatusObserver* observer) { | 40 void FakeSignalStrategy::Init(StatusObserver* observer) { |
41 observer->OnStateChange(StatusObserver::START); | 41 observer->OnStateChange(StatusObserver::START); |
42 observer->OnStateChange(StatusObserver::CONNECTING); | 42 observer->OnStateChange(StatusObserver::CONNECTING); |
43 observer->OnJidChange(jid_); | 43 observer->OnJidChange(jid_); |
44 observer->OnStateChange(StatusObserver::CONNECTED); | 44 observer->OnStateChange(StatusObserver::CONNECTED); |
45 } | 45 } |
46 | 46 |
47 void FakeSignalStrategy::Close() { | 47 void FakeSignalStrategy::Close() { |
48 DCHECK(CalledOnValidThread()); | 48 DCHECK(CalledOnValidThread()); |
49 listener_ = NULL; | |
50 } | 49 } |
51 | 50 |
52 void FakeSignalStrategy::SetListener(Listener* listener) { | 51 void FakeSignalStrategy::AddListener(Listener* listener) { |
53 DCHECK(CalledOnValidThread()); | 52 DCHECK(CalledOnValidThread()); |
54 | 53 DCHECK(std::find(listeners_.begin(), listeners_.end(), listener) == |
55 // Don't overwrite an listener without explicitly going | 54 listeners_.end()); |
56 // through "NULL" first. | 55 listeners_.push_back(listener); |
57 DCHECK(listener_ == NULL || listener == NULL); | |
58 listener_ = listener; | |
59 } | 56 } |
60 | 57 |
61 void FakeSignalStrategy::SendStanza(buzz::XmlElement* stanza) { | 58 void FakeSignalStrategy::RemoveListener(Listener* listener) { |
| 59 DCHECK(CalledOnValidThread()); |
| 60 std::vector<Listener*>::iterator it = |
| 61 std::find(listeners_.begin(), listeners_.end(), listener); |
| 62 CHECK(it != listeners_.end()); |
| 63 listeners_.erase(it); |
| 64 } |
| 65 |
| 66 bool FakeSignalStrategy::SendStanza(buzz::XmlElement* stanza) { |
62 DCHECK(CalledOnValidThread()); | 67 DCHECK(CalledOnValidThread()); |
63 | 68 |
64 stanza->SetAttr(buzz::QN_FROM, jid_); | 69 stanza->SetAttr(buzz::QN_FROM, jid_); |
65 | 70 |
66 if (peer_) { | 71 if (peer_) { |
67 peer_->OnIncomingMessage(stanza); | 72 peer_->OnIncomingMessage(stanza); |
| 73 return true; |
68 } else { | 74 } else { |
69 delete stanza; | 75 delete stanza; |
| 76 return false; |
70 } | 77 } |
71 } | 78 } |
72 | 79 |
73 std::string FakeSignalStrategy::GetNextId() { | 80 std::string FakeSignalStrategy::GetNextId() { |
74 ++last_id_; | 81 ++last_id_; |
75 return base::IntToString(last_id_); | 82 return base::IntToString(last_id_); |
76 } | 83 } |
77 | 84 |
78 IqRequest* FakeSignalStrategy::CreateIqRequest() { | |
79 DCHECK(CalledOnValidThread()); | |
80 | |
81 return new IqRequest(this, &iq_registry_); | |
82 } | |
83 | |
84 void FakeSignalStrategy::OnIncomingMessage(buzz::XmlElement* stanza) { | 85 void FakeSignalStrategy::OnIncomingMessage(buzz::XmlElement* stanza) { |
85 pending_messages_.push(stanza); | 86 pending_messages_.push(stanza); |
86 MessageLoop::current()->PostTask( | 87 MessageLoop::current()->PostTask( |
87 FROM_HERE, task_factory_.NewRunnableMethod( | 88 FROM_HERE, task_factory_.NewRunnableMethod( |
88 &FakeSignalStrategy::DeliverIncomingMessages)); | 89 &FakeSignalStrategy::DeliverIncomingMessages)); |
89 } | 90 } |
90 | 91 |
91 void FakeSignalStrategy::DeliverIncomingMessages() { | 92 void FakeSignalStrategy::DeliverIncomingMessages() { |
92 while (!pending_messages_.empty()) { | 93 while (!pending_messages_.empty()) { |
93 buzz::XmlElement* stanza = pending_messages_.front(); | 94 buzz::XmlElement* stanza = pending_messages_.front(); |
94 const std::string& to_field = stanza->Attr(buzz::QN_TO); | 95 const std::string& to_field = stanza->Attr(buzz::QN_TO); |
95 if (to_field != jid_) { | 96 if (to_field != jid_) { |
96 LOG(WARNING) << "Dropping stanza that is addressed to " << to_field | 97 LOG(WARNING) << "Dropping stanza that is addressed to " << to_field |
97 << ". Local jid: " << jid_ | 98 << ". Local jid: " << jid_ |
98 << ". Message content: " << stanza->Str(); | 99 << ". Message content: " << stanza->Str(); |
99 return; | 100 return; |
100 } | 101 } |
101 | 102 |
102 if (listener_) | 103 for (std::vector<Listener*>::iterator it = listeners_.begin(); |
103 listener_->OnIncomingStanza(stanza); | 104 it != listeners_.end(); ++it) { |
104 iq_registry_.OnIncomingStanza(stanza); | 105 if ((*it)->OnIncomingStanza(stanza)) |
| 106 break; |
| 107 } |
105 | 108 |
106 pending_messages_.pop(); | 109 pending_messages_.pop(); |
107 delete stanza; | 110 delete stanza; |
108 } | 111 } |
109 } | 112 } |
110 | 113 |
111 } // namespace remoting | 114 } // namespace remoting |
OLD | NEW |