Convert cacheinvalidation_unittests to run exclusively on Swarming
[chromium-blink-merge.git] / remoting / webapp / crd / js / wcs.js
blob2e72af08460029cf7e10df9c133e41202eec91d0
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 */
6 /**
7 * @fileoverview
8 * A class that provides an interface to a WCS connection.
9 */
11 'use strict';
13 /** @suppress {duplicate} */
14 var remoting = remoting || {};
16 /** @type {remoting.Wcs} */
17 remoting.wcs = null;
19 /**
20 * @constructor
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) {
26 /**
27 * The WCS client.
28 * @private {remoting.WcsIqClient}
30 this.wcsIqClient_ = wcsIqClient;
32 /**
33 * The OAuth2 access token.
34 * @private {string}
36 this.token_ = token;
38 /**
39 * The function called when the WCS client has received a full JID.
40 * @private {?function(string): void}
42 this.onReady_ = onReady;
44 /**
45 * The full JID of the WCS client.
46 * @private {string}
48 this.clientFullJid_ = '';
50 /**
51 * A function called when an IQ stanza is received.
52 * @param {string} stanza The IQ stanza.
53 * @private
55 this.onIq_ = function(stanza) {};
57 // Handle messages from the WcsIqClient.
58 this.wcsIqClient_.setOnMessage(this.onMessage_.bind(this));
60 // Start the WcsIqClient.
61 this.wcsIqClient_.connectChannel();
64 /**
65 * Passes an access token to the WcsIqClient, if the token has been updated.
67 * @param {string} tokenNew A (possibly updated) access token.
68 * @return {void} Nothing.
70 remoting.Wcs.prototype.updateAccessToken = function(tokenNew) {
71 if (tokenNew != this.token_) {
72 this.token_ = tokenNew;
73 this.wcsIqClient_.updateAccessToken(this.token_);
77 /**
78 * Handles a message coming from the WcsIqClient.
80 * @param {Array<string>} msg The message.
81 * @return {void} Nothing.
82 * @private
84 remoting.Wcs.prototype.onMessage_ = function(msg) {
85 if (msg[0] == 'is') {
86 this.onIq_(msg[1]);
87 } else if (msg[0] == 'cfj') {
88 this.clientFullJid_ = msg[1];
89 console.log('Received JID: ' + this.clientFullJid_);
90 if (this.onReady_) {
91 this.onReady_(this.clientFullJid_);
92 this.onReady_ = null;
97 /**
98 * Gets the full JID of the WCS client.
100 * @return {string} The full JID.
102 remoting.Wcs.prototype.getJid = function() {
103 return this.clientFullJid_;
107 * Sends an IQ stanza.
109 * @param {string} stanza An IQ stanza.
110 * @return {void} Nothing.
112 remoting.Wcs.prototype.sendIq = function(stanza) {
113 this.wcsIqClient_.sendIq(stanza);
117 * Sets the function called when an IQ stanza is received.
119 * @param {function(string): void} onIq The function called when an IQ stanza
120 * is received.
121 * @return {void} Nothing.
123 remoting.Wcs.prototype.setOnIq = function(onIq) {
124 this.onIq_ = onIq;