1 // Copyright (c) 2012 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.
5 #include "jingle/notifier/listener/send_ping_task.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "jingle/notifier/listener/xml_element_util.h"
12 #include "talk/xmpp/constants.h"
13 #include "talk/xmpp/jid.h"
14 #include "talk/xmpp/xmppclient.h"
15 #include "third_party/webrtc/libjingle/xmllite/qname.h"
16 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
20 SendPingTask::Delegate::~Delegate() {
23 SendPingTask::SendPingTask(buzz::XmppTaskParentInterface* parent,
25 : XmppTask(parent, buzz::XmppEngine::HL_SINGLE), delegate_(delegate) {
28 SendPingTask::~SendPingTask() {
31 int SendPingTask::ProcessStart() {
32 ping_task_id_ = task_id();
33 scoped_ptr<buzz::XmlElement> stanza(MakePingStanza(ping_task_id_));
34 DVLOG(1) << "Sending ping stanza " << XmlElementToString(*stanza);
35 if (SendStanza(stanza.get()) != buzz::XMPP_RETURN_OK) {
36 DLOG(WARNING) << "Could not send stanza " << XmlElementToString(*stanza);
39 return STATE_RESPONSE;
42 int SendPingTask::ProcessResponse() {
43 const buzz::XmlElement* stanza = NextStanza();
48 DVLOG(1) << "Received stanza " << XmlElementToString(*stanza);
50 std::string type = stanza->Attr(buzz::QN_TYPE);
51 if (type != buzz::STR_RESULT) {
52 DLOG(WARNING) << "No type=\"result\" attribute found in stanza "
53 << XmlElementToString(*stanza);
57 delegate_->OnPingResponseReceived();
61 bool SendPingTask::HandleStanza(const buzz::XmlElement* stanza) {
62 // MatchResponseIq() matches the given Jid with the "from" field of the given
63 // stanza, which in this case should be the empty string
64 // (signifying the server).
65 if (MatchResponseIq(stanza, buzz::Jid(buzz::STR_EMPTY), ping_task_id_)) {
72 buzz::XmlElement* SendPingTask::MakePingStanza(const std::string& task_id) {
73 buzz::XmlElement* stanza = MakeIq(buzz::STR_GET,
74 buzz::Jid(buzz::STR_EMPTY),
76 stanza->AddElement(new buzz::XmlElement(buzz::QN_PING));
80 } // namespace notifier