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) { |
30 xmpp_proxy_ = xmpp_proxy; | 28 xmpp_proxy_ = xmpp_proxy; |
31 xmpp_proxy_->AttachCallback(AsWeakPtr()); | 29 xmpp_proxy_->AttachCallback(AsWeakPtr()); |
32 } | 30 } |
33 | 31 |
34 void JavascriptSignalStrategy::Init(StatusObserver* observer) { | 32 void JavascriptSignalStrategy::Init(StatusObserver* observer) { |
(...skipping 12 matching lines...) Expand all Loading... | |
47 | 45 |
48 void JavascriptSignalStrategy::Close() { | 46 void JavascriptSignalStrategy::Close() { |
49 DCHECK(CalledOnValidThread()); | 47 DCHECK(CalledOnValidThread()); |
50 | 48 |
51 if (xmpp_proxy_) { | 49 if (xmpp_proxy_) { |
52 xmpp_proxy_->DetachCallback(); | 50 xmpp_proxy_->DetachCallback(); |
53 xmpp_proxy_ = NULL; | 51 xmpp_proxy_ = NULL; |
54 } | 52 } |
55 } | 53 } |
56 | 54 |
57 void JavascriptSignalStrategy::SetListener(Listener* listener) { | 55 void JavascriptSignalStrategy::AddListener(Listener* listener) { |
58 DCHECK(CalledOnValidThread()); | 56 DCHECK(CalledOnValidThread()); |
59 | 57 DCHECK(std::find(listeners_.begin(), listeners_.end(), listener) == |
60 // Don't overwrite an listener without explicitly going | 58 listeners_.end()); |
61 // through "NULL" first. | 59 listeners_.push_back(listener); |
62 DCHECK(listener_ == NULL || listener == NULL); | |
63 listener_ = listener; | |
64 } | 60 } |
65 | 61 |
66 void JavascriptSignalStrategy::SendStanza(buzz::XmlElement* stanza) { | 62 void JavascriptSignalStrategy::RemoveListener(Listener* listener) { |
67 DCHECK(CalledOnValidThread()); | 63 DCHECK(CalledOnValidThread()); |
64 std::vector<Listener*>::iterator it = | |
65 std::find(listeners_.begin(), listeners_.end(), listener); | |
66 CHECK(it != listeners_.end()); | |
67 listeners_.erase(it); | |
68 } | |
68 | 69 |
70 bool JavascriptSignalStrategy::SendStanza(buzz::XmlElement* stanza) { | |
71 DCHECK(CalledOnValidThread()); | |
69 xmpp_proxy_->SendIq(stanza->Str()); | 72 xmpp_proxy_->SendIq(stanza->Str()); |
70 delete stanza; | 73 delete stanza; |
74 return true; | |
71 } | 75 } |
72 | 76 |
73 std::string JavascriptSignalStrategy::GetNextId() { | 77 std::string JavascriptSignalStrategy::GetNextId() { |
Wez
2011/11/03 02:09:30
CalledOnValidThread?
Sergey Ulanov
2011/11/03 02:41:31
Done.
| |
74 ++last_id_; | 78 ++last_id_; |
75 return base::IntToString(last_id_); | 79 return base::IntToString(last_id_); |
76 } | 80 } |
77 | 81 |
78 IqRequest* JavascriptSignalStrategy::CreateIqRequest() { | |
79 DCHECK(CalledOnValidThread()); | |
80 | |
81 return new IqRequest(this, &iq_registry_); | |
82 } | |
83 | |
84 void JavascriptSignalStrategy::OnIq(const std::string& stanza_str) { | 82 void JavascriptSignalStrategy::OnIq(const std::string& stanza_str) { |
Wez
2011/11/03 02:09:30
CalledOnValidThread?
Sergey Ulanov
2011/11/03 02:41:31
Done.
| |
85 scoped_ptr<buzz::XmlElement> stanza(buzz::XmlElement::ForStr(stanza_str)); | 83 scoped_ptr<buzz::XmlElement> stanza(buzz::XmlElement::ForStr(stanza_str)); |
86 | 84 |
87 if (!stanza.get()) { | 85 if (!stanza.get()) { |
88 LOG(WARNING) << "Malformed XMPP stanza received: " << stanza_str; | 86 LOG(WARNING) << "Malformed XMPP stanza received: " << stanza_str; |
89 return; | 87 return; |
90 } | 88 } |
91 | 89 |
92 if (listener_ && listener_->OnIncomingStanza(stanza.get())) | 90 for (std::vector<Listener*>::iterator it = listeners_.begin(); |
93 return; | 91 it != listeners_.end(); ++it) { |
94 | 92 if ((*it)->OnIncomingStanza(stanza.get())) |
95 iq_registry_.OnIncomingStanza(stanza.get()); | 93 break; |
94 } | |
96 } | 95 } |
97 | 96 |
98 } // namespace remoting | 97 } // namespace remoting |
OLD | NEW |