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.
37 remoting
.WcsLoader
.prototype.SCRIPT_NODE_ID_
= 'wcs-script-node';
40 * Starts loading the WCS IQ client.
42 * When it's loaded, construct remoting.wcs as a wrapper for it.
43 * When the WCS connection is ready, or on error, call |onReady| or |onError|,
46 * @param {string} token An OAuth2 access token.
47 * @param {function(string): void} onReady The callback function, called with
48 * a client JID when WCS has been loaded.
49 * @param {function(remoting.Error):void} onError Function to invoke with an
50 * error code on failure.
51 * @return {void} Nothing.
53 remoting
.WcsLoader
.prototype.start = function(token
, onReady
, onError
) {
54 var node
= document
.getElementById(this.SCRIPT_NODE_ID_
);
56 console
.error('Multiple calls to WcsLoader.start are not allowed.');
57 onError(remoting
.Error
.UNEXPECTED
);
61 // Create a script node to load the WCS driver.
62 node
= document
.createElement('script');
63 node
.id
= this.SCRIPT_NODE_ID_
;
64 node
.src
= remoting
.settings
.TALK_GADGET_URL
+ 'iq?access_token=' + token
;
65 node
.type
= 'text/javascript';
66 document
.body
.insertBefore(node
, document
.body
.firstChild
);
68 /** @type {remoting.WcsLoader} */
70 var onLoad = function() {
71 that
.constructWcs_(token
, onReady
);
73 var onLoadError = function(event
) {
74 // The DOM Event object has no detail on the nature of the error, so try to
75 // validate the token to get a better idea.
76 /** @param {remoting.Error} error Error code. */
77 var onValidateError = function(error
) {
78 var typedNode
= /** @type {Element} */ (node
);
79 typedNode
.parentNode
.removeChild(node
);
82 var onValidateOk = function() {
83 // We can reach the authentication server and validate the token. Either
84 // there's something wrong with the talkgadget service, or there is a
85 // cookie problem. Only the cookie problem can be fixed by the user, so
87 onValidateError(remoting
.Error
.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
= remoting
.Error
.AUTHENTICATION_FAILED
;
129 switch (xhr
.status
) {
131 error
= remoting
.Error
.NETWORK_FAILURE
;
133 case 502: // No break
135 error
= remoting
.Error
.SERVICE_UNAVAILABLE
;
141 var parameters
= '?access_token=' + encodeURIComponent(token
);
143 remoting
.settings
.OAUTH2_API_BASE_URL
+ '/v1/tokeninfo' + parameters
,