OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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. |
| 4 |
| 5 #include "remoting/jingle_glue/iq_request.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "base/scoped_ptr.h" |
| 10 #include "remoting/jingle_glue/jingle_client.h" |
| 11 #include "talk/xmpp/constants.h" |
| 12 #include "talk/xmpp/xmppengine.h" |
| 13 |
| 14 namespace remoting { |
| 15 |
| 16 IqRequest::IqRequest(JingleClient* jingle_client) |
| 17 : jingle_client_(jingle_client), |
| 18 cookie_(NULL) { |
| 19 DCHECK(jingle_client_ != NULL); |
| 20 DCHECK(MessageLoop::current() == jingle_client_->message_loop()); |
| 21 } |
| 22 |
| 23 IqRequest::~IqRequest() { |
| 24 DCHECK(MessageLoop::current() == jingle_client_->message_loop()); |
| 25 Unregister(); |
| 26 } |
| 27 |
| 28 void IqRequest::SendIq(const std::string& type, |
| 29 const std::string& addressee, |
| 30 buzz::XmlElement* iq_body) { |
| 31 DCHECK(MessageLoop::current() == jingle_client_->message_loop()); |
| 32 |
| 33 // Unregister the handler if it is already registered. |
| 34 Unregister(); |
| 35 |
| 36 DCHECK(type.length() > 0); |
| 37 DCHECK(addressee.length() > 0); |
| 38 |
| 39 buzz::XmppClient* xmpp_client = jingle_client_->xmpp_client(); |
| 40 DCHECK(xmpp_client); // Expect that connection is active. |
| 41 |
| 42 scoped_ptr<buzz::XmlElement> stanza(MakeIqStanza(type, addressee, iq_body, |
| 43 xmpp_client->NextId())); |
| 44 |
| 45 xmpp_client->engine()->SendIq(stanza.get(), this, &cookie_); |
| 46 } |
| 47 |
| 48 // static |
| 49 buzz::XmlElement* IqRequest::MakeIqStanza(const std::string& type, |
| 50 const std::string& addressee, |
| 51 buzz::XmlElement* iq_body, |
| 52 const std::string& id) { |
| 53 buzz::XmlElement* stanza = new buzz::XmlElement(buzz::QN_IQ); |
| 54 stanza->AddAttr(buzz::QN_TYPE, type); |
| 55 stanza->AddAttr(buzz::QN_TO, addressee); |
| 56 stanza->AddAttr(buzz::QN_ID, id); |
| 57 stanza->AddElement(iq_body); |
| 58 return stanza; |
| 59 } |
| 60 |
| 61 void IqRequest::Unregister() { |
| 62 if (cookie_) { |
| 63 buzz::XmppClient* xmpp_client = jingle_client_->xmpp_client(); |
| 64 // No need to unregister the handler if the client has been destroyed. |
| 65 if (xmpp_client) { |
| 66 xmpp_client->engine()->RemoveIqHandler(cookie_, NULL); |
| 67 } |
| 68 cookie_ = NULL; |
| 69 } |
| 70 } |
| 71 |
| 72 void IqRequest::IqResponse(buzz::XmppIqCookie cookie, |
| 73 const buzz::XmlElement* stanza) { |
| 74 if (callback_.get() != NULL) { |
| 75 callback_->Run(stanza); |
| 76 } |
| 77 } |
| 78 |
| 79 } // namespace remoting |
OLD | NEW |