Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / remoting / webapp / crd / js / wcs.js
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(string): void} onReady Called with the WCS client's JID.
24  */
25 remoting.Wcs = function(wcsIqClient, token, onReady) {
26   /**
27    * The WCS client.
28    * @type {remoting.WcsIqClient}
29    * @private
30    */
31   this.wcsIqClient_ = wcsIqClient;
32
33   /**
34    * The OAuth2 access token.
35    * @type {string}
36    * @private
37    */
38   this.token_ = token;
39
40   /**
41    * The function called when the WCS client has received a full JID.
42    * @type {?function(string): void}
43    * @private
44    */
45   this.onReady_ = onReady;
46
47   /**
48    * The full JID of the WCS client.
49    * @type {string}
50    * @private
51    */
52   this.clientFullJid_ = '';
53
54   /**
55    * A function called when an IQ stanza is received.
56    * @param {string} stanza The IQ stanza.
57    * @private
58    */
59   this.onIq_ = function(stanza) {};
60
61   // Handle messages from the WcsIqClient.
62   this.wcsIqClient_.setOnMessage(this.onMessage_.bind(this));
63
64   // Start the WcsIqClient.
65   this.wcsIqClient_.connectChannel();
66 };
67
68 /**
69  * Passes an access token to the WcsIqClient, if the token has been updated.
70  *
71  * @param {string} tokenNew A (possibly updated) access token.
72  * @return {void} Nothing.
73  */
74 remoting.Wcs.prototype.updateAccessToken = function(tokenNew) {
75   if (tokenNew != this.token_) {
76     this.token_ = tokenNew;
77     this.wcsIqClient_.updateAccessToken(this.token_);
78   }
79 };
80
81 /**
82  * Handles a message coming from the WcsIqClient.
83  *
84  * @param {Array.<string>} msg The message.
85  * @return {void} Nothing.
86  * @private
87  */
88 remoting.Wcs.prototype.onMessage_ = function(msg) {
89   if (msg[0] == 'is') {
90     this.onIq_(msg[1]);
91   } else if (msg[0] == 'cfj') {
92     this.clientFullJid_ = msg[1];
93     console.log('Received JID: ' + this.clientFullJid_);
94     if (this.onReady_) {
95       this.onReady_(this.clientFullJid_);
96       this.onReady_ = null;
97     }
98   }
99 };
100
101 /**
102  * Gets the full JID of the WCS client.
103  *
104  * @return {string} The full JID.
105  */
106 remoting.Wcs.prototype.getJid = function() {
107   return this.clientFullJid_;
108 };
109
110 /**
111  * Sends an IQ stanza.
112  *
113  * @param {string} stanza An IQ stanza.
114  * @return {void} Nothing.
115  */
116 remoting.Wcs.prototype.sendIq = function(stanza) {
117   this.wcsIqClient_.sendIq(stanza);
118 };
119
120 /**
121  * Sets the function called when an IQ stanza is received.
122  *
123  * @param {function(string): void} onIq The function called when an IQ stanza
124  *     is received.
125  * @return {void} Nothing.
126  */
127 remoting.Wcs.prototype.setOnIq = function(onIq) {
128   this.onIq_ = onIq;
129 };