| OLD | NEW |
| (Empty) |
| 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. | |
| 4 */ | |
| 5 | |
| 6 /** | |
| 7 * @fileoverview | |
| 8 * A class that provides an interface to a WCS connection. | |
| 9 */ | |
| 10 | |
| 11 'use strict'; | |
| 12 | |
| 13 /** @suppress {duplicate} */ | |
| 14 var remoting = remoting || {}; | |
| 15 | |
| 16 /** @type {remoting.Wcs} */ | |
| 17 remoting.wcs = null; | |
| 18 | |
| 19 /** | |
| 20 * @constructor | |
| 21 * @param {remoting.WcsIqClient} wcsIqClient The WCS client. | |
| 22 * @param {string} token An OAuth2 access token. | |
| 23 * @param {function(boolean): void} onReady A function called when the WCS | |
| 24 * client has received a full JID. | |
| 25 */ | |
| 26 remoting.Wcs = function(wcsIqClient, token, onReady) { | |
| 27 /** | |
| 28 * The WCS client. | |
| 29 * @type {remoting.WcsIqClient} | |
| 30 * @private | |
| 31 */ | |
| 32 this.wcsIqClient_ = wcsIqClient; | |
| 33 | |
| 34 /** | |
| 35 * The OAuth2 access token. | |
| 36 * @type {string} | |
| 37 * @private | |
| 38 */ | |
| 39 this.token_ = token; | |
| 40 | |
| 41 /** | |
| 42 * The function called when the WCS client has received a full JID. | |
| 43 * @type {function(boolean): void} | |
| 44 * @private | |
| 45 */ | |
| 46 this.onReady_ = onReady; | |
| 47 | |
| 48 /** | |
| 49 * The full JID of the WCS client. | |
| 50 * @type {string} | |
| 51 * @private | |
| 52 */ | |
| 53 this.clientFullJid_ = ''; | |
| 54 | |
| 55 /** @type {remoting.Wcs} */ | |
| 56 var that = this; | |
| 57 /** | |
| 58 * A timer that polls for an updated access token. | |
| 59 * @type {number} | |
| 60 * @private | |
| 61 */ | |
| 62 this.pollForUpdatedToken_ = setInterval( | |
| 63 function() { | |
| 64 /** @param {string} token */ | |
| 65 var updateAccessToken = function(token) { | |
| 66 that.updateAccessToken_(token); | |
| 67 } | |
| 68 remoting.oauth2.callWithToken(updateAccessToken); | |
| 69 }, | |
| 70 60 * 1000); | |
| 71 | |
| 72 /** | |
| 73 * A function called when an IQ stanza is received. | |
| 74 * @param {string} stanza The IQ stanza. | |
| 75 * @private | |
| 76 */ | |
| 77 this.onIq_ = function(stanza) {}; | |
| 78 | |
| 79 // Handle messages from the WcsIqClient. | |
| 80 /** @param {Array.<string>} msg An array of message strings. */ | |
| 81 var onMessage = function(msg) { that.onMessage_(msg); }; | |
| 82 this.wcsIqClient_.setOnMessage(onMessage); | |
| 83 | |
| 84 // Start the WcsIqClient. | |
| 85 this.wcsIqClient_.connectChannel(); | |
| 86 }; | |
| 87 | |
| 88 /** | |
| 89 * Passes an access token to the WcsIqClient, if the token has been updated. | |
| 90 * | |
| 91 * @param {string} tokenNew A (possibly updated) access token. | |
| 92 * @return {void} Nothing. | |
| 93 * @private | |
| 94 */ | |
| 95 remoting.Wcs.prototype.updateAccessToken_ = function(tokenNew) { | |
| 96 if (tokenNew != this.token_) { | |
| 97 this.token_ = tokenNew; | |
| 98 this.wcsIqClient_.updateAccessToken(this.token_); | |
| 99 } | |
| 100 }; | |
| 101 | |
| 102 /** | |
| 103 * Handles a message coming from the WcsIqClient. | |
| 104 * | |
| 105 * @param {Array.<string>} msg The message. | |
| 106 * @return {void} Nothing. | |
| 107 * @private | |
| 108 */ | |
| 109 remoting.Wcs.prototype.onMessage_ = function(msg) { | |
| 110 if (msg[0] == 'is') { | |
| 111 this.onIq_(msg[1]); | |
| 112 } else if (msg[0] == 'cfj') { | |
| 113 this.clientFullJid_ = msg[1]; | |
| 114 remoting.debug.log('Received JID: ' + this.clientFullJid_); | |
| 115 this.onReady_(true); | |
| 116 this.onReady_ = function(success) {}; | |
| 117 } | |
| 118 }; | |
| 119 | |
| 120 /** | |
| 121 * Gets the full JID of the WCS client. | |
| 122 * | |
| 123 * @return {string} The full JID. | |
| 124 */ | |
| 125 remoting.Wcs.prototype.getJid = function() { | |
| 126 return this.clientFullJid_; | |
| 127 }; | |
| 128 | |
| 129 /** | |
| 130 * Sends an IQ stanza. | |
| 131 * | |
| 132 * @param {string} stanza An IQ stanza. | |
| 133 * @return {void} Nothing. | |
| 134 */ | |
| 135 remoting.Wcs.prototype.sendIq = function(stanza) { | |
| 136 this.wcsIqClient_.sendIq(stanza); | |
| 137 }; | |
| 138 | |
| 139 /** | |
| 140 * Sets the function called when an IQ stanza is received. | |
| 141 * | |
| 142 * @param {function(string): void} onIq The function called when an IQ stanza | |
| 143 * is received. | |
| 144 * @return {void} Nothing. | |
| 145 */ | |
| 146 remoting.Wcs.prototype.setOnIq = function(onIq) { | |
| 147 this.onIq_ = onIq; | |
| 148 }; | |
| OLD | NEW |