summaryrefslogtreecommitdiffstats
path: root/remoting/webapp/wcs.js
blob: d5c42573f9edf3d1909bedcc67315011f8d8d141 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* Copyright (c) 2012 The Chromium Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/**
 * @fileoverview
 * A class that provides an interface to a WCS connection.
 */

'use strict';

/** @suppress {duplicate} */
var remoting = remoting || {};

/** @type {remoting.Wcs} */
remoting.wcs = null;

/**
 * @constructor
 * @param {remoting.WcsIqClient} wcsIqClient The WCS client.
 * @param {string} token An OAuth2 access token.
 * @param {function(boolean): void} onReady A function called when the WCS
 *     client has received a full JID.
 */
remoting.Wcs = function(wcsIqClient, token, onReady) {
  /**
   * The WCS client.
   * @type {remoting.WcsIqClient}
   * @private
   */
  this.wcsIqClient_ = wcsIqClient;

  /**
   * The OAuth2 access token.
   * @type {string}
   * @private
   */
  this.token_ = token;

  /**
   * The function called when the WCS client has received a full JID.
   * @type {function(boolean): void}
   * @private
   */
  this.onReady_ = onReady;

  /**
   * The full JID of the WCS client.
   * @type {string}
   * @private
   */
  this.clientFullJid_ = '';

  var updateAccessToken = this.updateAccessToken_.bind(this);
  /** @param {remoting.Error} error */
  var onError = function(error) {
    console.error('updateAccessToken: Authentication failed: ' + error);
  };

  /**
   * A timer that polls for an updated access token.
   * @type {number}
   * @private
   */
  this.pollForUpdatedToken_ = setInterval(
      function() {
        remoting.oauth2.callWithToken(updateAccessToken, onError);
      },
      60 * 1000);

  /**
   * A function called when an IQ stanza is received.
   * @param {string} stanza The IQ stanza.
   * @private
   */
  this.onIq_ = function(stanza) {};

  // Handle messages from the WcsIqClient.
  this.wcsIqClient_.setOnMessage(this.onMessage_.bind(this));

  // Start the WcsIqClient.
  this.wcsIqClient_.connectChannel();
};

/**
 * Passes an access token to the WcsIqClient, if the token has been updated.
 *
 * @param {string} tokenNew A (possibly updated) access token.
 * @return {void} Nothing.
 * @private
 */
remoting.Wcs.prototype.updateAccessToken_ = function(tokenNew) {
  if (tokenNew != this.token_) {
    this.token_ = tokenNew;
    this.wcsIqClient_.updateAccessToken(this.token_);
  }
};

/**
 * Handles a message coming from the WcsIqClient.
 *
 * @param {Array.<string>} msg The message.
 * @return {void} Nothing.
 * @private
 */
remoting.Wcs.prototype.onMessage_ = function(msg) {
  if (msg[0] == 'is') {
    this.onIq_(msg[1]);
  } else if (msg[0] == 'cfj') {
    this.clientFullJid_ = msg[1];
    console.log('Received JID: ' + this.clientFullJid_);
    this.onReady_(true);
    this.onReady_ = function(success) {};
  }
};

/**
 * Gets the full JID of the WCS client.
 *
 * @return {string} The full JID.
 */
remoting.Wcs.prototype.getJid = function() {
  return this.clientFullJid_;
};

/**
 * Sends an IQ stanza.
 *
 * @param {string} stanza An IQ stanza.
 * @return {void} Nothing.
 */
remoting.Wcs.prototype.sendIq = function(stanza) {
  this.wcsIqClient_.sendIq(stanza);
};

/**
 * Sets the function called when an IQ stanza is received.
 *
 * @param {function(string): void} onIq The function called when an IQ stanza
 *     is received.
 * @return {void} Nothing.
 */
remoting.Wcs.prototype.setOnIq = function(onIq) {
  this.onIq_ = onIq;
};