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.
7 * Module to format IQ messages so they can be displayed in the debug log.
12 /** @suppress {duplicate} */
13 var remoting = remoting || {};
18 remoting.FormatIq = function() {
24 * Verify that the only attributes on the given |node| are those specified
25 * in the |attrs| string.
27 * @param {Node} node The node to verify.
28 * @param {string} validAttrs Comma-separated list of valid attributes.
30 * @return {boolean} True if the node contains only valid attributes.
32 remoting.FormatIq.prototype.verifyAttributes = function(node, validAttrs) {
33 var attrs = ',' + validAttrs + ',';
34 var len = node.attributes.length;
35 for (var i = 0; i < len; i++) {
37 var attrNode = node.attributes[i];
38 var attr = attrNode.nodeName;
39 if (attrs.indexOf(',' + attr + ',') == -1) {
47 * Record the client and host JIDs so that we can check them against the
48 * params in the IQ packets.
50 * @param {string} clientJid The client JID string.
51 * @param {string} hostJid The host JID string.
53 remoting.FormatIq.prototype.setJids = function(clientJid, hostJid) {
54 this.clientJid = clientJid;
55 this.hostJid = hostJid;
59 * Calculate the 'pretty' version of data from the |server| node.
61 * @param {Node} server Xml node with server info.
63 * @return {?string} Formatted server string. Null if error.
65 remoting.FormatIq.prototype.calcServerString = function(server) {
66 if (!this.verifyAttributes(server, 'host,udp,tcp,tcpssl')) {
69 var host = server.getAttribute('host');
70 var udp = server.getAttribute('udp');
71 var tcp = server.getAttribute('tcp');
72 var tcpssl = server.getAttribute('tcpssl');
74 var str = "'" + host + "'";
80 str += ' tcpssl:' + tcpssl;
87 * Calc the 'pretty' version of channel data.
89 * @param {Node} channel Xml node with channel info.
91 * @return {?string} Formatted channel string. Null if error.
93 remoting.FormatIq.prototype.calcChannelString = function(channel) {
94 var name = channel.nodeName;
95 if (!this.verifyAttributes(channel, 'transport,version,codec')) {
98 var transport = channel.getAttribute('transport');
99 var version = channel.getAttribute('version');
101 var str = name + ' ' + transport + ' v' + version;
102 if (name == 'video') {
103 str += ' codec=' + channel.getAttribute('codec');
110 * Pretty print the jingleinfo from the given Xml node.
112 * @param {Node} query Xml query node with jingleinfo in the child nodes.
114 * @return {?string} Pretty version of jingleinfo. Null if error.
116 remoting.FormatIq.prototype.prettyJingleinfo = function(query) {
117 var nodes = query.childNodes;
118 var stun_servers = '';
120 for (var i = 0; i < nodes.length; i++) {
123 var name = node.nodeName;
124 if (name == 'stun') {
126 var stun_nodes = node.childNodes;
127 for(var s = 0; s < stun_nodes.length; s++) {
129 var stun_node = stun_nodes[s];
130 var sname = stun_node.nodeName;
131 if (sname == 'server') {
132 var stun_str = this.calcServerString(stun_node);
139 result += '\n stun ' + sserver;
140 } else if (name == 'relay') {
143 var relay_nodes = node.childNodes;
144 for(var r = 0; r < relay_nodes.length; r++) {
146 var relay_node = relay_nodes[r];
147 var rname = relay_node.nodeName;
148 if (rname == 'token') {
149 token = token + relay_node.textContent;
151 if (rname == 'server') {
152 var relay_str = this.calcServerString(relay_node);
156 rserver += relay_str;
159 result += '\n relay ' + rserver + ' token: ' + token;
169 * Pretty print the session-initiate or session-accept info from the given
172 * @param {Node} jingle Xml node with jingle session-initiate or session-accept
173 * info contained in child nodes.
175 * @return {?string} Pretty version of jingle stanza. Null if error.
177 remoting.FormatIq.prototype.prettySessionInitiateAccept = function(jingle) {
178 if (jingle.childNodes.length != 1) {
181 var content = jingle.firstChild;
182 if (content.nodeName != 'content') {
185 var content_children = content.childNodes;
187 for (var c = 0; c < content_children.length; c++) {
189 var content_child = content_children[c];
190 var cname = content_child.nodeName;
191 if (cname == 'description') {
195 var desc_children = content_child.childNodes;
196 for (var d = 0; d < desc_children.length; d++) {
198 var desc = desc_children[d];
199 var dname = desc.nodeName;
200 if (dname == 'control' || dname == 'event' || dname == 'video') {
201 var channel_str = this.calcChannelString(desc);
205 channels += channel_str;
206 } else if (dname == 'initial-resolution') {
207 resolution = desc.getAttribute('width') + 'x' +
208 desc.getAttribute('height');
209 } else if (dname == 'authentication') {
210 var auth_children = desc.childNodes;
211 for (var a = 0; a < auth_children.length; a++) {
213 var auth_info = auth_children[a];
214 if (auth_info.nodeName == 'auth-token') {
215 auth = auth + ' (auth-token) ' + auth_info.textContent;
216 } else if (auth_info.nodeName == 'certificate') {
217 auth = auth + ' (certificate) ' + auth_info.textContent;
218 } else if (auth_info.nodeName == 'master-key') {
219 auth = auth + ' (master-key) ' + auth_info.textContent;
228 result += '\n channels: ' + channels;
229 result += '\n auth: ' + auth;
230 result += '\n initial resolution: ' + resolution;
231 } else if (cname == 'transport') {
232 // The 'transport' node is currently empty.
233 var transport_children = content_child.childNodes;
234 if (transport_children.length != 0) {
245 * Pretty print the session-terminate info from the given Xml node.
247 * @param {Node} jingle Xml node with jingle session-terminate info contained in
250 * @return {?string} Pretty version of jingle session-terminate stanza. Null if
253 remoting.FormatIq.prototype.prettySessionTerminate = function(jingle) {
254 if (jingle.childNodes.length != 1) {
257 var reason = jingle.firstChild;
258 if (reason.nodeName != 'reason' || reason.childNodes.length != 1) {
261 var info = reason.firstChild;
262 if (info.nodeName == 'success' || info.nodeName == 'general-error') {
263 return '\n reason=' + info.nodeName;
269 * Pretty print the transport-info info from the given Xml node.
271 * @param {Node} jingle Xml node with jingle transport info contained in child
274 * @return {?string} Pretty version of jingle transport-info stanza. Null if
277 remoting.FormatIq.prototype.prettyTransportInfo = function(jingle) {
278 if (jingle.childNodes.length != 1) {
281 var content = jingle.firstChild;
282 if (content.nodeName != 'content') {
285 var transport = content.firstChild;
286 if (transport.nodeName != 'transport') {
289 var transport_children = transport.childNodes;
291 for (var t = 0; t < transport_children.length; t++) {
293 var candidate = transport_children[t];
294 if (candidate.nodeName != 'candidate') {
297 if (!this.verifyAttributes(candidate, 'name,address,port,preference,' +
298 'username,protocol,generation,password,type,' +
302 var name = candidate.getAttribute('name');
303 var address = candidate.getAttribute('address');
304 var port = candidate.getAttribute('port');
305 var pref = candidate.getAttribute('preference');
306 var username = candidate.getAttribute('username');
307 var protocol = candidate.getAttribute('protocol');
308 var generation = candidate.getAttribute('generation');
309 var password = candidate.getAttribute('password');
310 var type = candidate.getAttribute('type');
311 var network = candidate.getAttribute('network');
313 var info = name + ': ' + address + ':' + port + ' ' + protocol +
314 ' name:' + username + ' pwd:' + password +
318 info = info + " network:'" + network + "'";
320 result += '\n ' + info;
326 * Pretty print the jingle action contained in the given Xml node.
328 * @param {Node} jingle Xml node with jingle action contained in child nodes.
329 * @param {string} action String containing the jingle action.
331 * @return {?string} Pretty version of jingle action stanze. Null if error.
333 remoting.FormatIq.prototype.prettyJingleAction = function(jingle, action) {
334 if (action == 'session-initiate' || action == 'session-accept') {
335 return this.prettySessionInitiateAccept(jingle);
337 if (action == 'session-terminate') {
338 return this.prettySessionTerminate(jingle);
340 if (action == 'transport-info') {
341 return this.prettyTransportInfo(jingle);
347 * Pretty print the jingle error information contained in the given Xml node.
349 * @param {Node} error Xml node containing error information in child nodes.
351 * @return {?string} Pretty version of error stanze. Null if error.
353 remoting.FormatIq.prototype.prettyError = function(error) {
354 if (!this.verifyAttributes(error, 'xmlns:err,code,type,err:hostname,' +
355 'err:bnsname,err:stacktrace')) {
358 var code = error.getAttribute('code');
359 var type = error.getAttribute('type');
360 var hostname = error.getAttribute('err:hostname');
361 var bnsname = error.getAttribute('err:bnsname');
362 var stacktrace = error.getAttribute('err:stacktrace');
364 var result = '\n error ' + code + ' ' + type + " hostname:'" +
365 hostname + "' bnsname:'" + bnsname + "'";
366 var children = error.childNodes;
367 for (var i = 0; i < children.length; i++) {
369 var child = children[i];
370 result += '\n ' + child.nodeName;
373 var stack = stacktrace.split(' | ');
374 result += '\n stacktrace:';
375 // We use 'length-1' because the stack trace ends with " | " which results
376 // in an empty string at the end after the split.
377 for (var s = 0; s < stack.length - 1; s++) {
378 result += '\n ' + stack[s];
385 * Print out the heading line for an iq node.
387 * @param {string} action String describing action (send/receive).
388 * @param {string} id Packet id.
389 * @param {string} desc Description of iq action for this node.
390 * @param {string|null} sid Session id.
392 * @return {string} Pretty version of stanza heading info.
394 remoting.FormatIq.prototype.prettyIqHeading = function(action, id, desc,
396 var message = 'iq ' + action + ' id=' + id;
398 message = message + ' ' + desc;
401 message = message + ' sid=' + sid;
407 * Print out an iq 'result'-type node.
409 * @param {string} action String describing action (send/receive).
410 * @param {NodeList} iq_list Node list containing the 'result' xml.
412 * @return {?string} Pretty version of Iq result stanza. Null if error.
414 remoting.FormatIq.prototype.prettyIqResult = function(action, iq_list) {
417 var id = iq.getAttribute('id');
418 var iq_children = iq.childNodes;
420 if (iq_children.length == 0) {
421 return this.prettyIqHeading(action, id, 'result (empty)', null);
422 } else if (iq_children.length == 1) {
424 var child = iq_children[0];
425 if (child.nodeName == 'query') {
426 if (!this.verifyAttributes(child, 'xmlns')) {
429 var xmlns = child.getAttribute('xmlns');
430 if (xmlns == 'google:jingleinfo') {
431 var result = this.prettyIqHeading(action, id, 'result ' + xmlns, null);
432 result += this.prettyJingleinfo(child);
436 } else if (child.nodeName == 'rem:log-result') {
437 if (!this.verifyAttributes(child, 'xmlns:rem')) {
440 return this.prettyIqHeading(action, id, 'result (log-result)', null);
447 * Print out an Iq 'get'-type node.
449 * @param {string} action String describing action (send/receive).
450 * @param {NodeList} iq_list Node containing the 'get' xml.
452 * @return {?string} Pretty version of Iq get stanza. Null if error.
454 remoting.FormatIq.prototype.prettyIqGet = function(action, iq_list) {
457 var id = iq.getAttribute('id');
458 var iq_children = iq.childNodes;
460 if (iq_children.length != 1) {
465 var query = iq_children[0];
466 if (query.nodeName != 'query') {
469 if (!this.verifyAttributes(query, 'xmlns')) {
472 var xmlns = query.getAttribute('xmlns');
473 return this.prettyIqHeading(action, id, 'get ' + xmlns, null);
477 * Print out an iq 'set'-type node.
479 * @param {string} action String describing action (send/receive).
480 * @param {NodeList} iq_list Node containing the 'set' xml.
482 * @return {?string} Pretty version of Iq set stanza. Null if error.
484 remoting.FormatIq.prototype.prettyIqSet = function(action, iq_list) {
487 var id = iq.getAttribute('id');
488 var iq_children = iq.childNodes;
490 var children = iq_children.length;
493 var child = iq_children[0];
494 if (child.nodeName == 'gr:log') {
496 if (!this.verifyAttributes(grlog, 'xmlns:gr')) {
500 if (grlog.childNodes.length != 1) {
503 var grentry = grlog.firstChild;
504 if (grentry.nodeName != 'gr:entry') {
507 if (!this.verifyAttributes(grentry, 'role,event-name,session-state,' +
508 'os-name,cpu,browser-version,' +
512 var role = grentry.getAttribute('role');
513 var event_name = grentry.getAttribute('event-name');
514 var session_state = grentry.getAttribute('session-state');
515 var os_name = grentry.getAttribute('os-name');
516 var cpu = grentry.getAttribute('cpu');
517 var browser_version = grentry.getAttribute('browser-version');
518 var webapp_version = grentry.getAttribute('webapp-version');
520 var result = this.prettyIqHeading(action, id, role + ' ' + event_name +
521 ' ' + session_state, null);
522 result += '\n ' + os_name + ' ' + cpu + " browser:" + browser_version +
523 " webapp:" + webapp_version;
526 if (child.nodeName == 'jingle') {
528 if (!this.verifyAttributes(jingle, 'xmlns,action,sid,initiator')) {
532 var jingle_action = jingle.getAttribute('action');
533 var sid = jingle.getAttribute('sid');
535 var result = this.prettyIqHeading(action, id, 'set ' + jingle_action,
537 var action_str = this.prettyJingleAction(jingle, jingle_action);
541 return result + action_str;
548 * Print out an iq 'error'-type node.
550 * @param {string} action String describing action (send/receive).
551 * @param {NodeList} iq_list Node containing the 'error' xml.
553 * @return {?string} Pretty version of iq error stanza. Null if error parsing
556 remoting.FormatIq.prototype.prettyIqError = function(action, iq_list) {
559 var id = iq.getAttribute('id');
560 var iq_children = iq.childNodes;
562 var children = iq_children.length;
568 var jingle = iq_children[0];
569 if (jingle.nodeName != 'jingle') {
572 if (!this.verifyAttributes(jingle, 'xmlns,action,sid,initiator')) {
575 var jingle_action = jingle.getAttribute('action');
576 var sid = jingle.getAttribute('sid');
577 var result = this.prettyIqHeading(action, id, 'error from ' + jingle_action,
579 var action_str = this.prettyJingleAction(jingle, jingle_action);
583 result += action_str;
586 var error = iq_children[1];
587 if (error.nodeName != 'cli:error') {
591 var error_str = this.prettyError(error);
600 * Try to log a pretty-print the given IQ stanza (XML).
601 * Return true if the stanza was successfully printed.
603 * @param {boolean} send True if we're sending this stanza; false for receiving.
604 * @param {string} message The XML stanza to add to the log.
606 * @return {?string} Pretty version of the Iq stanza. Null if error.
608 remoting.FormatIq.prototype.prettyIq = function(send, message) {
609 var parser = new DOMParser();
610 var xml = parser.parseFromString(message, 'text/xml');
612 var iq_list = xml.getElementsByTagName('iq');
614 if (iq_list && iq_list.length > 0) {
617 if (!this.verifyAttributes(iq, 'xmlns,xmlns:cli,id,to,from,type'))
620 // Verify that the to/from fields match the expected sender/receiver.
621 var to = iq.getAttribute('to');
622 var from = iq.getAttribute('from');
624 var bot = remoting.settings.DIRECTORY_BOT_JID;
626 if (to && to != this.hostJid && to != bot) {
627 console.warn('FormatIq: bad to: ' + to);
630 if (from && from != this.clientJid) {
631 console.warn('FormatIq: bad from: ' + from);
637 action = action + " (to bot)";
640 if (to && to != this.clientJid) {
641 console.warn('FormatIq: bad to: ' + to);
644 if (from && from != this.hostJid && from != bot) {
645 console.warn('FormatIq: bad from: ' + from);
651 action = action + " (from bot)";
655 var type = iq.getAttribute('type');
656 if (type == 'result') {
657 return this.prettyIqResult(action, iq_list);
658 } else if (type == 'get') {
659 return this.prettyIqGet(action, iq_list);
660 } else if (type == 'set') {
661 return this.prettyIqSet(action, iq_list);
662 } else if (type == 'error') {
663 return this.prettyIqError(action, iq_list);
671 * Return a pretty-formatted string for the IQ stanza being sent.
672 * If the stanza cannot be made pretty, then a string with a raw dump of the
673 * stanza will be returned.
675 * @param {string} message The XML stanza to make pretty.
677 * @return {string} Pretty version of XML stanza being sent. A raw dump of the
678 * stanza is returned if there was a parsing error.
680 remoting.FormatIq.prototype.prettifySendIq = function(message) {
681 var result = this.prettyIq(true, message);
683 // Fall back to showing the raw stanza.
684 return 'Sending Iq: ' + message;
690 * Return a pretty-formatted string for the IQ stanza that was received.
691 * If the stanza cannot be made pretty, then a string with a raw dump of the
692 * stanza will be returned.
694 * @param {string} message The XML stanza to make pretty.
696 * @return {string} Pretty version of XML stanza that was received. A raw dump
697 * of the stanza is returned if there was a parsing error.
699 remoting.FormatIq.prototype.prettifyReceiveIq = function(message) {
700 var result = this.prettyIq(false, message);
702 // Fall back to showing the raw stanza.
703 return 'Receiving Iq: ' + message;
708 /** @type {remoting.FormatIq} */
709 remoting.formatIq = null;