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 loads a WCS IQ client and constructs remoting.wcs as a
14 /** @suppress {duplicate} */
15 var remoting = remoting || {};
17 /** @type {remoting.WcsLoader} */
18 remoting.wcsLoader = null;
23 remoting.WcsLoader = function() {
25 * The WCS client that will be downloaded. This variable is initialized (via
26 * remoting.wcsLoader) by the downloaded Javascript.
27 * @type {remoting.WcsIqClient}
29 this.wcsIqClient = null;
33 * The id of the script node.
36 remoting.WcsLoader.prototype.SCRIPT_NODE_ID_ = 'wcs-script-node';
39 * Starts loading the WCS IQ client.
41 * When it's loaded, construct remoting.wcs as a wrapper for it.
42 * When the WCS connection is ready, or on error, call |onReady| or |onError|,
45 * @param {string} token An OAuth2 access token.
46 * @param {function(string): void} onReady The callback function, called with
47 * a client JID when WCS has been loaded.
48 * @param {function(!remoting.Error):void} onError Function to invoke with an
49 * error code on failure.
50 * @return {void} Nothing.
52 remoting.WcsLoader.prototype.start = function(token, onReady, onError) {
53 var node = document.getElementById(this.SCRIPT_NODE_ID_);
55 console.error('Multiple calls to WcsLoader.start are not allowed.');
56 onError(remoting.Error.unexpected());
60 // Create a script node to load the WCS driver.
61 node = document.createElement('script');
62 node.id = this.SCRIPT_NODE_ID_;
63 node.src = remoting.settings.TALK_GADGET_URL + '/iq?access_token=' + token;
64 node.type = 'text/javascript';
65 document.body.insertBefore(node, document.body.firstChild);
67 /** @type {remoting.WcsLoader} */
69 var onLoad = function() {
70 that.constructWcs_(token, onReady);
72 var onLoadError = function(event) {
73 // The DOM Event object has no detail on the nature of the error, so try to
74 // validate the token to get a better idea.
75 /** @param {!remoting.Error} error Error code. */
76 var onValidateError = function(error) {
77 var typedNode = /** @type {Element} */ (node);
78 typedNode.parentNode.removeChild(node);
81 var onValidateOk = function() {
82 // We can reach the authentication server and validate the token. Either
83 // there's something wrong with the talkgadget service, or there is a
84 // cookie problem. Only the cookie problem can be fixed by the user, so
86 onValidateError(new remoting.Error(
87 remoting.Error.Tag.AUTHENTICATION_FAILED));
89 that.validateToken(token, onValidateOk, onValidateError);
91 node.addEventListener('load', onLoad, false);
92 node.addEventListener('error', onLoadError, false);
96 * Constructs the remoting.wcs object.
98 * @param {string} token An OAuth2 access token.
99 * @param {function(string): void} onReady The callback function, called with
100 * an OAuth2 access token when WCS has been loaded.
101 * @return {void} Nothing.
104 remoting.WcsLoader.prototype.constructWcs_ = function(token, onReady) {
105 remoting.wcs = new remoting.Wcs(
106 remoting.wcsLoader.wcsIqClient, token, onReady);
110 * Validates an OAuth2 access token.
112 * @param {string} token The access token.
113 * @param {function():void} onOk Callback to invoke if the token is valid.
114 * @param {function(!remoting.Error):void} onError Function to invoke with an
115 * error code on failure.
116 * @return {void} Nothing.
118 remoting.WcsLoader.prototype.validateToken = function(token, onOk, onError) {
119 /** @type {XMLHttpRequest} */
120 var xhr = new XMLHttpRequest();
121 xhr.onreadystatechange = function() {
122 if (xhr.readyState != 4) {
125 if (xhr.status == 200) {
128 var error = new remoting.Error(remoting.Error.Tag.AUTHENTICATION_FAILED);
129 switch (xhr.status) {
131 error = new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE);
133 case 502: // No break
135 error = new remoting.Error(remoting.Error.Tag.SERVICE_UNAVAILABLE);
141 var parameters = '?access_token=' + encodeURIComponent(token);
143 remoting.settings.OAUTH2_API_BASE_URL + '/v1/tokeninfo' + parameters,