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/javascript_signal_strategy.h" | 5 #include "remoting/jingle_glue/javascript_signal_strategy.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
11 #include "remoting/jingle_glue/iq_request.h" | |
12 #include "remoting/jingle_glue/xmpp_proxy.h" | 11 #include "remoting/jingle_glue/xmpp_proxy.h" |
13 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" | 12 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
14 | 13 |
15 namespace remoting { | 14 namespace remoting { |
16 | 15 |
17 JavascriptSignalStrategy::JavascriptSignalStrategy(const std::string& your_jid) | 16 JavascriptSignalStrategy::JavascriptSignalStrategy(const std::string& your_jid) |
18 : your_jid_(your_jid), | 17 : your_jid_(your_jid), |
19 listener_(NULL), | |
20 last_id_(0) { | 18 last_id_(0) { |
21 } | 19 } |
22 | 20 |
23 JavascriptSignalStrategy::~JavascriptSignalStrategy() { | 21 JavascriptSignalStrategy::~JavascriptSignalStrategy() { |
24 DCHECK(listener_ == NULL); | 22 DCHECK(listeners_.empty()); |
25 Close(); | 23 Close(); |
26 } | 24 } |
27 | 25 |
28 void JavascriptSignalStrategy::AttachXmppProxy( | 26 void JavascriptSignalStrategy::AttachXmppProxy( |
29 scoped_refptr<XmppProxy> xmpp_proxy) { | 27 scoped_refptr<XmppProxy> xmpp_proxy) { |
| 28 DCHECK(CalledOnValidThread()); |
30 xmpp_proxy_ = xmpp_proxy; | 29 xmpp_proxy_ = xmpp_proxy; |
31 xmpp_proxy_->AttachCallback(AsWeakPtr()); | 30 xmpp_proxy_->AttachCallback(AsWeakPtr()); |
32 } | 31 } |
33 | 32 |
34 void JavascriptSignalStrategy::Init(StatusObserver* observer) { | 33 void JavascriptSignalStrategy::Init(StatusObserver* observer) { |
35 DCHECK(CalledOnValidThread()); | 34 DCHECK(CalledOnValidThread()); |
36 | 35 |
37 // Blast through each state since for a JavascriptSignalStrategy, we're | 36 // Blast through each state since for a JavascriptSignalStrategy, we're |
38 // already connected. | 37 // already connected. |
39 // | 38 // |
40 // TODO(ajwong): Clarify the status API contract to see if we have to actually | 39 // TODO(ajwong): Clarify the status API contract to see if we have to actually |
41 // walk through each state. | 40 // walk through each state. |
42 observer->OnStateChange(StatusObserver::START); | 41 observer->OnStateChange(StatusObserver::START); |
43 observer->OnStateChange(StatusObserver::CONNECTING); | 42 observer->OnStateChange(StatusObserver::CONNECTING); |
44 observer->OnJidChange(your_jid_); | 43 observer->OnJidChange(your_jid_); |
45 observer->OnStateChange(StatusObserver::CONNECTED); | 44 observer->OnStateChange(StatusObserver::CONNECTED); |
46 } | 45 } |
47 | 46 |
48 void JavascriptSignalStrategy::Close() { | 47 void JavascriptSignalStrategy::Close() { |
49 DCHECK(CalledOnValidThread()); | 48 DCHECK(CalledOnValidThread()); |
50 | 49 |
51 if (xmpp_proxy_) { | 50 if (xmpp_proxy_) { |
52 xmpp_proxy_->DetachCallback(); | 51 xmpp_proxy_->DetachCallback(); |
53 xmpp_proxy_ = NULL; | 52 xmpp_proxy_ = NULL; |
54 } | 53 } |
55 } | 54 } |
56 | 55 |
57 void JavascriptSignalStrategy::SetListener(Listener* listener) { | 56 void JavascriptSignalStrategy::AddListener(Listener* listener) { |
58 DCHECK(CalledOnValidThread()); | 57 DCHECK(CalledOnValidThread()); |
59 | 58 DCHECK(std::find(listeners_.begin(), listeners_.end(), listener) == |
60 // Don't overwrite an listener without explicitly going | 59 listeners_.end()); |
61 // through "NULL" first. | 60 listeners_.push_back(listener); |
62 DCHECK(listener_ == NULL || listener == NULL); | |
63 listener_ = listener; | |
64 } | 61 } |
65 | 62 |
66 void JavascriptSignalStrategy::SendStanza(buzz::XmlElement* stanza) { | 63 void JavascriptSignalStrategy::RemoveListener(Listener* listener) { |
67 DCHECK(CalledOnValidThread()); | 64 DCHECK(CalledOnValidThread()); |
| 65 std::vector<Listener*>::iterator it = |
| 66 std::find(listeners_.begin(), listeners_.end(), listener); |
| 67 CHECK(it != listeners_.end()); |
| 68 listeners_.erase(it); |
| 69 } |
68 | 70 |
| 71 bool JavascriptSignalStrategy::SendStanza(buzz::XmlElement* stanza) { |
| 72 DCHECK(CalledOnValidThread()); |
69 xmpp_proxy_->SendIq(stanza->Str()); | 73 xmpp_proxy_->SendIq(stanza->Str()); |
70 delete stanza; | 74 delete stanza; |
| 75 return true; |
71 } | 76 } |
72 | 77 |
73 std::string JavascriptSignalStrategy::GetNextId() { | 78 std::string JavascriptSignalStrategy::GetNextId() { |
| 79 DCHECK(CalledOnValidThread()); |
74 ++last_id_; | 80 ++last_id_; |
75 return base::IntToString(last_id_); | 81 return base::IntToString(last_id_); |
76 } | 82 } |
77 | 83 |
78 IqRequest* JavascriptSignalStrategy::CreateIqRequest() { | 84 void JavascriptSignalStrategy::OnIq(const std::string& stanza_str) { |
79 DCHECK(CalledOnValidThread()); | 85 DCHECK(CalledOnValidThread()); |
80 | |
81 return new IqRequest(this, &iq_registry_); | |
82 } | |
83 | |
84 void JavascriptSignalStrategy::OnIq(const std::string& stanza_str) { | |
85 scoped_ptr<buzz::XmlElement> stanza(buzz::XmlElement::ForStr(stanza_str)); | 86 scoped_ptr<buzz::XmlElement> stanza(buzz::XmlElement::ForStr(stanza_str)); |
86 | |
87 if (!stanza.get()) { | 87 if (!stanza.get()) { |
88 LOG(WARNING) << "Malformed XMPP stanza received: " << stanza_str; | 88 LOG(WARNING) << "Malformed XMPP stanza received: " << stanza_str; |
89 return; | 89 return; |
90 } | 90 } |
91 | 91 |
92 if (listener_ && listener_->OnIncomingStanza(stanza.get())) | 92 for (std::vector<Listener*>::iterator it = listeners_.begin(); |
93 return; | 93 it != listeners_.end(); ++it) { |
94 | 94 if ((*it)->OnIncomingStanza(stanza.get())) |
95 iq_registry_.OnIncomingStanza(stanza.get()); | 95 break; |
| 96 } |
96 } | 97 } |
97 | 98 |
98 } // namespace remoting | 99 } // namespace remoting |
OLD | NEW |