2 * Copyright 2011 The WebRTC Project Authors. All rights reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
11 #include "webrtc/libjingle/xmpp/mucroomlookuptask.h"
13 #include "webrtc/libjingle/xmpp/constants.h"
14 #include "webrtc/base/logging.h"
15 #include "webrtc/base/scoped_ptr.h"
21 MucRoomLookupTask::CreateLookupTaskForRoomName(XmppTaskParentInterface* parent,
22 const Jid& lookup_server_jid,
23 const std::string& room_name,
24 const std::string& room_domain) {
25 return new MucRoomLookupTask(parent, lookup_server_jid,
26 MakeNameQuery(room_name, room_domain));
30 MucRoomLookupTask::CreateLookupTaskForRoomJid(XmppTaskParentInterface* parent,
31 const Jid& lookup_server_jid,
32 const Jid& room_jid) {
33 return new MucRoomLookupTask(parent, lookup_server_jid,
34 MakeJidQuery(room_jid));
38 MucRoomLookupTask::CreateLookupTaskForHangoutId(XmppTaskParentInterface* parent,
39 const Jid& lookup_server_jid,
40 const std::string& hangout_id) {
41 return new MucRoomLookupTask(parent, lookup_server_jid,
42 MakeHangoutIdQuery(hangout_id));
46 MucRoomLookupTask::CreateLookupTaskForExternalId(
47 XmppTaskParentInterface* parent,
48 const Jid& lookup_server_jid,
49 const std::string& external_id,
50 const std::string& type) {
51 return new MucRoomLookupTask(parent, lookup_server_jid,
52 MakeExternalIdQuery(external_id, type));
55 MucRoomLookupTask::MucRoomLookupTask(XmppTaskParentInterface* parent,
56 const Jid& lookup_server_jid,
58 : IqTask(parent, STR_SET, lookup_server_jid, query) {
61 XmlElement* MucRoomLookupTask::MakeNameQuery(
62 const std::string& room_name, const std::string& room_domain) {
63 XmlElement* name_elem = new XmlElement(QN_SEARCH_ROOM_NAME, false);
64 name_elem->SetBodyText(room_name);
66 XmlElement* domain_elem = new XmlElement(QN_SEARCH_ROOM_DOMAIN, false);
67 domain_elem->SetBodyText(room_domain);
69 XmlElement* query = new XmlElement(QN_SEARCH_QUERY, true);
70 query->AddElement(name_elem);
71 query->AddElement(domain_elem);
75 XmlElement* MucRoomLookupTask::MakeJidQuery(const Jid& room_jid) {
76 XmlElement* jid_elem = new XmlElement(QN_SEARCH_ROOM_JID);
77 jid_elem->SetBodyText(room_jid.Str());
79 XmlElement* query = new XmlElement(QN_SEARCH_QUERY);
80 query->AddElement(jid_elem);
84 XmlElement* MucRoomLookupTask::MakeExternalIdQuery(
85 const std::string& external_id, const std::string& type) {
86 XmlElement* external_id_elem = new XmlElement(QN_SEARCH_EXTERNAL_ID);
87 external_id_elem->SetAttr(QN_TYPE, type);
88 external_id_elem->SetBodyText(external_id);
90 XmlElement* query = new XmlElement(QN_SEARCH_QUERY);
91 query->AddElement(external_id_elem);
95 // Construct a stanza to lookup the muc jid for a given hangout id. eg:
97 // <query xmlns="jabber:iq:search">
98 // <hangout-id>0b48ad092c893a53b7bfc87422caf38e93978798e</hangout-id>
100 XmlElement* MucRoomLookupTask::MakeHangoutIdQuery(
101 const std::string& hangout_id) {
102 XmlElement* hangout_id_elem = new XmlElement(QN_SEARCH_HANGOUT_ID, false);
103 hangout_id_elem->SetBodyText(hangout_id);
105 XmlElement* query = new XmlElement(QN_SEARCH_QUERY, true);
106 query->AddElement(hangout_id_elem);
110 // Handle a response like the following:
112 // <query xmlns="jabber:iq:search">
113 // <item jid="muvc-private-chat-guid@groupchat.google.com">
114 // <room-name>0b48ad092c893a53b7bfc87422caf38e93978798e</room-name>
115 // <room-domain>hangout.google.com</room-domain>
118 void MucRoomLookupTask::HandleResult(const XmlElement* stanza) {
119 const XmlElement* query_elem = stanza->FirstNamed(QN_SEARCH_QUERY);
120 if (query_elem == NULL) {
121 SignalError(this, stanza);
125 const XmlElement* item_elem = query_elem->FirstNamed(QN_SEARCH_ITEM);
126 if (item_elem == NULL) {
127 SignalError(this, stanza);
132 room.jid = Jid(item_elem->Attr(buzz::QN_JID));
133 if (!room.jid.IsValid()) {
134 SignalError(this, stanza);
138 const XmlElement* room_name_elem =
139 item_elem->FirstNamed(QN_SEARCH_ROOM_NAME);
140 if (room_name_elem != NULL) {
141 room.name = room_name_elem->BodyText();
144 const XmlElement* room_domain_elem =
145 item_elem->FirstNamed(QN_SEARCH_ROOM_DOMAIN);
146 if (room_domain_elem != NULL) {
147 room.domain = room_domain_elem->BodyText();
150 const XmlElement* hangout_id_elem =
151 item_elem->FirstNamed(QN_SEARCH_HANGOUT_ID);
152 if (hangout_id_elem != NULL) {
153 room.hangout_id = hangout_id_elem->BodyText();
156 SignalResult(this, room);