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 * Load WCS if necessary, then invoke the callback with an access token.
35 * @param {function(string): void} onReady The callback function, called with
36 * an OAuth2 access token when WCS has been loaded.
37 * @param {function(remoting.Error):void} onError Function to invoke with an
38 * error code on failure.
39 * @return {void} Nothing.
41 remoting
.WcsLoader
.load = function(onReady
, onError
) {
42 if (!remoting
.wcsLoader
) {
43 remoting
.wcsLoader
= new remoting
.WcsLoader();
45 /** @param {string} token The OAuth2 access token. */
46 var start = function(token
) {
47 remoting
.wcsLoader
.start_(token
, onReady
, onError
);
49 remoting
.oauth2
.callWithToken(start
, onError
);
53 * The URL of the GTalk gadget.
57 remoting
.WcsLoader
.prototype.TALK_GADGET_URL_
=
58 'https://chromoting-client.talkgadget.google.com/talkgadget/';
61 * The id of the script node.
65 remoting
.WcsLoader
.prototype.SCRIPT_NODE_ID_
= 'wcs-script-node';
68 * The attribute name indicating that the WCS has finished loading.
72 remoting
.WcsLoader
.prototype.SCRIPT_NODE_LOADED_FLAG_
= 'wcs-script-loaded';
75 * Starts loading the WCS IQ client.
77 * When it's loaded, construct remoting.wcs as a wrapper for it.
78 * When the WCS connection is ready, or on error, call |onReady| or |onError|,
81 * @param {string} token An OAuth2 access token.
82 * @param {function(string): void} onReady The callback function, called with
83 * an OAuth2 access token when WCS has been loaded.
84 * @param {function(remoting.Error):void} onError Function to invoke with an
85 * error code on failure.
86 * @return {void} Nothing.
89 remoting
.WcsLoader
.prototype.start_ = function(token
, onReady
, onError
) {
90 var node
= document
.getElementById(this.SCRIPT_NODE_ID_
);
92 // The first time, there will be no script node, so create one.
93 node
= document
.createElement('script');
94 node
.id
= this.SCRIPT_NODE_ID_
;
95 node
.src
= this.TALK_GADGET_URL_
+ 'iq?access_token=' + token
;
96 node
.type
= 'text/javascript';
97 document
.body
.insertBefore(node
, document
.body
.firstChild
);
98 } else if (node
.hasAttribute(this.SCRIPT_NODE_LOADED_FLAG_
)) {
99 // Subsequently, explicitly invoke onReady if onload has already fired.
100 // TODO(jamiewalch): It's possible that the WCS client has not finished
101 // initializing. Add support for multiple callbacks to the remoting.Wcs
102 // class to address this.
106 /** @type {remoting.WcsLoader} */
108 var onLoad = function() {
109 var typedNode
= /** @type {Element} */ (node
);
110 typedNode
.setAttribute(that
.SCRIPT_NODE_LOADED_FLAG_
, true);
111 that
.constructWcs_(token
, onReady
);
113 var onLoadError = function(event
) {
114 // The DOM Event object has no detail on the nature of the error, so try to
115 // validate the token to get a better idea.
116 /** @param {remoting.Error} error Error code. */
117 var onValidateError = function(error
) {
118 var typedNode
= /** @type {Element} */ (node
);
119 typedNode
.parentNode
.removeChild(node
);
122 var onValidateOk = function() {
123 // We can reach the authentication server and validate the token. Either
124 // there's something wrong with the talkgadget service, or there is a
125 // cookie problem. Only the cookie problem can be fixed by the user, so
127 onValidateError(remoting
.Error
.AUTHENTICATION_FAILED
);
129 remoting
.oauth2
.validateToken(token
, onValidateOk
, onValidateError
);
131 node
.addEventListener('load', onLoad
, false);
132 node
.addEventListener('error', onLoadError
, false);
136 * Constructs the remoting.wcs object.
138 * @param {string} token An OAuth2 access token.
139 * @param {function(string): void} onReady The callback function, called with
140 * an OAuth2 access token when WCS has been loaded.
141 * @return {void} Nothing.
144 remoting
.WcsLoader
.prototype.constructWcs_ = function(token
, onReady
) {
145 remoting
.wcs
= new remoting
.Wcs(
146 remoting
.wcsLoader
.wcsIqClient
,
148 function() { onReady(token
); });