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.
8 * A class that provides an interface to a WCS connection.
13 /** @suppress {duplicate} */
14 var remoting = remoting || {};
16 /** @type {remoting.Wcs} */
21 * @param {remoting.WcsIqClient} wcsIqClient The WCS client.
22 * @param {string} token An OAuth2 access token.
23 * @param {function(string): void} onReady Called with the WCS client's JID.
25 remoting.Wcs = function(wcsIqClient, token, onReady) {
28 * @type {remoting.WcsIqClient}
31 this.wcsIqClient_ = wcsIqClient;
34 * The OAuth2 access token.
41 * The function called when the WCS client has received a full JID.
42 * @type {?function(string): void}
45 this.onReady_ = onReady;
48 * The full JID of the WCS client.
52 this.clientFullJid_ = '';
55 * A function called when an IQ stanza is received.
56 * @param {string} stanza The IQ stanza.
59 this.onIq_ = function(stanza) {};
61 // Handle messages from the WcsIqClient.
62 this.wcsIqClient_.setOnMessage(this.onMessage_.bind(this));
64 // Start the WcsIqClient.
65 this.wcsIqClient_.connectChannel();
69 * Passes an access token to the WcsIqClient, if the token has been updated.
71 * @param {string} tokenNew A (possibly updated) access token.
72 * @return {void} Nothing.
74 remoting.Wcs.prototype.updateAccessToken = function(tokenNew) {
75 if (tokenNew != this.token_) {
76 this.token_ = tokenNew;
77 this.wcsIqClient_.updateAccessToken(this.token_);
82 * Handles a message coming from the WcsIqClient.
84 * @param {Array.<string>} msg The message.
85 * @return {void} Nothing.
88 remoting.Wcs.prototype.onMessage_ = function(msg) {
91 } else if (msg[0] == 'cfj') {
92 this.clientFullJid_ = msg[1];
93 console.log('Received JID: ' + this.clientFullJid_);
95 this.onReady_(this.clientFullJid_);
102 * Gets the full JID of the WCS client.
104 * @return {string} The full JID.
106 remoting.Wcs.prototype.getJid = function() {
107 return this.clientFullJid_;
111 * Sends an IQ stanza.
113 * @param {string} stanza An IQ stanza.
114 * @return {void} Nothing.
116 remoting.Wcs.prototype.sendIq = function(stanza) {
117 this.wcsIqClient_.sendIq(stanza);
121 * Sets the function called when an IQ stanza is received.
123 * @param {function(string): void} onIq The function called when an IQ stanza
125 * @return {void} Nothing.
127 remoting.Wcs.prototype.setOnIq = function(onIq) {