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/xmpp_signal_strategy.h" | 5 #include "remoting/jingle_glue/xmpp_signal_strategy.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "jingle/notifier/base/gaia_token_pre_xmpp_auth.h" | 8 #include "jingle/notifier/base/gaia_token_pre_xmpp_auth.h" |
9 #include "remoting/jingle_glue/iq_request.h" | |
10 #include "remoting/jingle_glue/jingle_thread.h" | 9 #include "remoting/jingle_glue/jingle_thread.h" |
11 #include "remoting/jingle_glue/xmpp_socket_adapter.h" | 10 #include "remoting/jingle_glue/xmpp_socket_adapter.h" |
12 #include "third_party/libjingle/source/talk/base/asyncsocket.h" | 11 #include "third_party/libjingle/source/talk/base/asyncsocket.h" |
13 #include "third_party/libjingle/source/talk/xmpp/prexmppauth.h" | 12 #include "third_party/libjingle/source/talk/xmpp/prexmppauth.h" |
14 #include "third_party/libjingle/source/talk/xmpp/saslcookiemechanism.h" | 13 #include "third_party/libjingle/source/talk/xmpp/saslcookiemechanism.h" |
15 | 14 |
16 namespace remoting { | 15 namespace remoting { |
17 | 16 |
18 XmppSignalStrategy::XmppSignalStrategy(JingleThread* jingle_thread, | 17 XmppSignalStrategy::XmppSignalStrategy(JingleThread* jingle_thread, |
19 const std::string& username, | 18 const std::string& username, |
20 const std::string& auth_token, | 19 const std::string& auth_token, |
21 const std::string& auth_token_service) | 20 const std::string& auth_token_service) |
22 : thread_(jingle_thread), | 21 : thread_(jingle_thread), |
23 username_(username), | 22 username_(username), |
24 auth_token_(auth_token), | 23 auth_token_(auth_token), |
25 auth_token_service_(auth_token_service), | 24 auth_token_service_(auth_token_service), |
26 xmpp_client_(NULL), | 25 xmpp_client_(NULL), |
27 observer_(NULL), | 26 observer_(NULL) { |
28 listener_(NULL) { | |
29 } | 27 } |
30 | 28 |
31 XmppSignalStrategy::~XmppSignalStrategy() { | 29 XmppSignalStrategy::~XmppSignalStrategy() { |
32 DCHECK(listener_ == NULL); | 30 DCHECK(listeners_.empty()); |
33 Close(); | 31 Close(); |
34 } | 32 } |
35 | 33 |
36 void XmppSignalStrategy::Init(StatusObserver* observer) { | 34 void XmppSignalStrategy::Init(StatusObserver* observer) { |
37 observer_ = observer; | 35 observer_ = observer; |
38 | 36 |
39 buzz::Jid login_jid(username_); | 37 buzz::Jid login_jid(username_); |
40 | 38 |
41 buzz::XmppClientSettings settings; | 39 buzz::XmppClientSettings settings; |
42 settings.set_user(login_jid.node()); | 40 settings.set_user(login_jid.node()); |
(...skipping 19 matching lines...) Expand all Loading... |
62 xmpp_client_->engine()->RemoveStanzaHandler(this); | 60 xmpp_client_->engine()->RemoveStanzaHandler(this); |
63 | 61 |
64 xmpp_client_->Disconnect(); | 62 xmpp_client_->Disconnect(); |
65 | 63 |
66 // |xmpp_client_| should be set to NULL in OnConnectionStateChanged() | 64 // |xmpp_client_| should be set to NULL in OnConnectionStateChanged() |
67 // in response to Disconnect() call above. | 65 // in response to Disconnect() call above. |
68 DCHECK(xmpp_client_ == NULL); | 66 DCHECK(xmpp_client_ == NULL); |
69 } | 67 } |
70 } | 68 } |
71 | 69 |
72 void XmppSignalStrategy::SetListener(Listener* listener) { | 70 void XmppSignalStrategy::AddListener(Listener* listener) { |
73 // Don't overwrite an listener without explicitly going | 71 DCHECK(std::find(listeners_.begin(), listeners_.end(), listener) == |
74 // through "NULL" first. | 72 listeners_.end()); |
75 DCHECK(listener_ == NULL || listener == NULL); | 73 listeners_.push_back(listener); |
76 listener_ = listener; | |
77 } | 74 } |
78 | 75 |
79 void XmppSignalStrategy::SendStanza(buzz::XmlElement* stanza) { | 76 void XmppSignalStrategy::RemoveListener(Listener* listener) { |
| 77 std::vector<Listener*>::iterator it = |
| 78 std::find(listeners_.begin(), listeners_.end(), listener); |
| 79 CHECK(it != listeners_.end()); |
| 80 listeners_.erase(it); |
| 81 } |
| 82 |
| 83 bool XmppSignalStrategy::SendStanza(buzz::XmlElement* stanza) { |
80 if (!xmpp_client_) { | 84 if (!xmpp_client_) { |
81 LOG(INFO) << "Dropping signalling message because XMPP " | 85 LOG(INFO) << "Dropping signalling message because XMPP " |
82 "connection has been terminated."; | 86 "connection has been terminated."; |
83 return; | 87 delete stanza; |
| 88 return false; |
84 } | 89 } |
85 xmpp_client_->SendStanza(stanza); | 90 |
| 91 buzz::XmppReturnStatus status = xmpp_client_->SendStanza(stanza); |
| 92 return status == buzz::XMPP_RETURN_OK || status == buzz::XMPP_RETURN_PENDING; |
86 } | 93 } |
87 | 94 |
88 std::string XmppSignalStrategy::GetNextId() { | 95 std::string XmppSignalStrategy::GetNextId() { |
89 if (!xmpp_client_) { | 96 if (!xmpp_client_) { |
90 // If the connection has been terminated then it doesn't matter | 97 // If the connection has been terminated then it doesn't matter |
91 // what Id we return. | 98 // what Id we return. |
92 return ""; | 99 return ""; |
93 } | 100 } |
94 return xmpp_client_->NextId(); | 101 return xmpp_client_->NextId(); |
95 } | 102 } |
96 | 103 |
97 IqRequest* XmppSignalStrategy::CreateIqRequest() { | |
98 return new IqRequest(this, &iq_registry_); | |
99 } | |
100 | |
101 bool XmppSignalStrategy::HandleStanza(const buzz::XmlElement* stanza) { | 104 bool XmppSignalStrategy::HandleStanza(const buzz::XmlElement* stanza) { |
102 if (listener_ && listener_->OnIncomingStanza(stanza)) | 105 for (std::vector<Listener*>::iterator it = listeners_.begin(); |
103 return true; | 106 it != listeners_.end(); ++it) { |
104 return iq_registry_.OnIncomingStanza(stanza); | 107 if ((*it)->OnIncomingStanza(stanza)) |
| 108 return true; |
| 109 } |
| 110 return false; |
105 } | 111 } |
106 | 112 |
107 void XmppSignalStrategy::OnConnectionStateChanged( | 113 void XmppSignalStrategy::OnConnectionStateChanged( |
108 buzz::XmppEngine::State state) { | 114 buzz::XmppEngine::State state) { |
109 switch (state) { | 115 switch (state) { |
110 case buzz::XmppEngine::STATE_START: | 116 case buzz::XmppEngine::STATE_START: |
111 observer_->OnStateChange(StatusObserver::START); | 117 observer_->OnStateChange(StatusObserver::START); |
112 break; | 118 break; |
113 case buzz::XmppEngine::STATE_OPENING: | 119 case buzz::XmppEngine::STATE_OPENING: |
114 observer_->OnStateChange(StatusObserver::CONNECTING); | 120 observer_->OnStateChange(StatusObserver::CONNECTING); |
(...skipping 24 matching lines...) Expand all Loading... |
139 } | 145 } |
140 | 146 |
141 return new notifier::GaiaTokenPreXmppAuth( | 147 return new notifier::GaiaTokenPreXmppAuth( |
142 jid.Str(), | 148 jid.Str(), |
143 settings.auth_cookie(), | 149 settings.auth_cookie(), |
144 settings.token_service(), | 150 settings.token_service(), |
145 mechanism); | 151 mechanism); |
146 } | 152 } |
147 | 153 |
148 } // namespace remoting | 154 } // namespace remoting |
OLD | NEW |