| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 /** | 5 /** |
| 6 * @fileoverview | 6 * @fileoverview |
| 7 * Module to format IQ messages so they can be displayed in the debug log. | 7 * Module to format IQ messages so they can be displayed in the debug log. |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 'use strict'; | 10 'use strict'; |
| 11 | 11 |
| 12 /** @suppress {duplicate} */ | 12 /** @suppress {duplicate} */ |
| 13 var remoting = remoting || {}; | 13 var remoting = remoting || {}; |
| 14 | 14 |
| 15 /** | 15 /** |
| 16 * @constructor | 16 * @constructor |
| 17 * @param {string} clientJid |
| 18 * @param {string} hostJid |
| 17 */ | 19 */ |
| 18 remoting.FormatIq = function() { | 20 remoting.FormatIq = function(clientJid, hostJid) { |
| 19 this.clientJid = ''; | 21 /** @private */ |
| 20 this.hostJid = ''; | 22 this.clientJid_ = clientJid; |
| 23 /** @private */ |
| 24 this.hostJid_ = hostJid; |
| 21 }; | 25 }; |
| 22 | 26 |
| 23 /** | 27 /** |
| 24 * Verify that the only attributes on the given |node| are those specified | 28 * Verify that the only attributes on the given |node| are those specified |
| 25 * in the |attrs| string. | 29 * in the |attrs| string. |
| 26 * | 30 * |
| 27 * @param {Node} node The node to verify. | 31 * @param {Node} node The node to verify. |
| 28 * @param {string} validAttrs Comma-separated list of valid attributes. | 32 * @param {string} validAttrs Comma-separated list of valid attributes. |
| 29 * | 33 * |
| 30 * @return {boolean} True if the node contains only valid attributes. | 34 * @return {boolean} True if the node contains only valid attributes. |
| 31 */ | 35 */ |
| 32 remoting.FormatIq.prototype.verifyAttributes = function(node, validAttrs) { | 36 remoting.FormatIq.prototype.verifyAttributes = function(node, validAttrs) { |
| 33 var attrs = ',' + validAttrs + ','; | 37 var attrs = ',' + validAttrs + ','; |
| 34 var len = node.attributes.length; | 38 var len = node.attributes.length; |
| 35 for (var i = 0; i < len; i++) { | 39 for (var i = 0; i < len; i++) { |
| 36 /** @type {Node} */ | 40 /** @type {Node} */ |
| 37 var attrNode = node.attributes[i]; | 41 var attrNode = node.attributes[i]; |
| 38 var attr = attrNode.nodeName; | 42 var attr = attrNode.nodeName; |
| 39 if (attrs.indexOf(',' + attr + ',') == -1) { | 43 if (attrs.indexOf(',' + attr + ',') == -1) { |
| 40 return false; | 44 return false; |
| 41 } | 45 } |
| 42 } | 46 } |
| 43 return true; | 47 return true; |
| 44 }; | 48 }; |
| 45 | 49 |
| 46 /** | 50 /** |
| 47 * Record the client and host JIDs so that we can check them against the | |
| 48 * params in the IQ packets. | |
| 49 * | |
| 50 * @param {string} clientJid The client JID string. | |
| 51 * @param {string} hostJid The host JID string. | |
| 52 */ | |
| 53 remoting.FormatIq.prototype.setJids = function(clientJid, hostJid) { | |
| 54 this.clientJid = clientJid; | |
| 55 this.hostJid = hostJid; | |
| 56 }; | |
| 57 | |
| 58 /** | |
| 59 * Calculate the 'pretty' version of data from the |server| node. | 51 * Calculate the 'pretty' version of data from the |server| node. |
| 60 * | 52 * |
| 61 * @param {Node} server Xml node with server info. | 53 * @param {Node} server Xml node with server info. |
| 62 * | 54 * |
| 63 * @return {?string} Formatted server string. Null if error. | 55 * @return {?string} Formatted server string. Null if error. |
| 64 */ | 56 */ |
| 65 remoting.FormatIq.prototype.calcServerString = function(server) { | 57 remoting.FormatIq.prototype.calcServerString = function(server) { |
| 66 if (!this.verifyAttributes(server, 'host,udp,tcp,tcpssl')) { | 58 if (!this.verifyAttributes(server, 'host,udp,tcp,tcpssl')) { |
| 67 return null; | 59 return null; |
| 68 } | 60 } |
| (...skipping 547 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 616 var iq = iq_list[0]; | 608 var iq = iq_list[0]; |
| 617 if (!this.verifyAttributes(iq, 'xmlns,xmlns:cli,id,to,from,type')) | 609 if (!this.verifyAttributes(iq, 'xmlns,xmlns:cli,id,to,from,type')) |
| 618 return null; | 610 return null; |
| 619 | 611 |
| 620 // Verify that the to/from fields match the expected sender/receiver. | 612 // Verify that the to/from fields match the expected sender/receiver. |
| 621 var to = iq.getAttribute('to'); | 613 var to = iq.getAttribute('to'); |
| 622 var from = iq.getAttribute('from'); | 614 var from = iq.getAttribute('from'); |
| 623 var action = ''; | 615 var action = ''; |
| 624 var bot = remoting.settings.DIRECTORY_BOT_JID; | 616 var bot = remoting.settings.DIRECTORY_BOT_JID; |
| 625 if (send) { | 617 if (send) { |
| 626 if (to && to != this.hostJid && to != bot) { | 618 if (to && to != this.hostJid_ && to != bot) { |
| 627 console.warn('FormatIq: bad to: ' + to); | 619 console.warn('FormatIq: bad to: ' + to); |
| 628 return null; | 620 return null; |
| 629 } | 621 } |
| 630 if (from && from != this.clientJid) { | 622 if (from && from != this.clientJid_) { |
| 631 console.warn('FormatIq: bad from: ' + from); | 623 console.warn('FormatIq: bad from: ' + from); |
| 632 return null; | 624 return null; |
| 633 } | 625 } |
| 634 | 626 |
| 635 action = "send"; | 627 action = "send"; |
| 636 if (to == bot) { | 628 if (to == bot) { |
| 637 action = action + " (to bot)"; | 629 action = action + " (to bot)"; |
| 638 } | 630 } |
| 639 } else { | 631 } else { |
| 640 if (to && to != this.clientJid) { | 632 if (to && to != this.clientJid_) { |
| 641 console.warn('FormatIq: bad to: ' + to); | 633 console.warn('FormatIq: bad to: ' + to); |
| 642 return null; | 634 return null; |
| 643 } | 635 } |
| 644 if (from && from != this.hostJid && from != bot) { | 636 if (from && from != this.hostJid_ && from != bot) { |
| 645 console.warn('FormatIq: bad from: ' + from); | 637 console.warn('FormatIq: bad from: ' + from); |
| 646 return null; | 638 return null; |
| 647 } | 639 } |
| 648 | 640 |
| 649 action = "receive"; | 641 action = "receive"; |
| 650 if (from == bot) { | 642 if (from == bot) { |
| 651 action = action + " (from bot)"; | 643 action = action + " (from bot)"; |
| 652 } | 644 } |
| 653 } | 645 } |
| 654 | 646 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 697 * of the stanza is returned if there was a parsing error. | 689 * of the stanza is returned if there was a parsing error. |
| 698 */ | 690 */ |
| 699 remoting.FormatIq.prototype.prettifyReceiveIq = function(message) { | 691 remoting.FormatIq.prototype.prettifyReceiveIq = function(message) { |
| 700 var result = this.prettyIq(false, message); | 692 var result = this.prettyIq(false, message); |
| 701 if (!result) { | 693 if (!result) { |
| 702 // Fall back to showing the raw stanza. | 694 // Fall back to showing the raw stanza. |
| 703 return 'Receiving Iq: ' + message; | 695 return 'Receiving Iq: ' + message; |
| 704 } | 696 } |
| 705 return result; | 697 return result; |
| 706 }; | 698 }; |
| 707 | |
| 708 /** @type {remoting.FormatIq} */ | |
| 709 remoting.formatIq = null; | |
| OLD | NEW |