| OLD | NEW |
| (Empty) |
| 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 loads a WCS IQ client and constructs remoting.wcs as a | |
| 9 * wrapper for it. | |
| 10 */ | |
| 11 | |
| 12 'use strict'; | |
| 13 | |
| 14 /** @suppress {duplicate} */ | |
| 15 var remoting = remoting || {}; | |
| 16 | |
| 17 /** @type {remoting.WcsLoader} */ | |
| 18 remoting.wcsLoader = null; | |
| 19 | |
| 20 /** | |
| 21 * @constructor | |
| 22 * @private | |
| 23 */ | |
| 24 remoting.WcsLoader = function() { | |
| 25 /** | |
| 26 * The WCS client that will be downloaded. This variable is initialized (via | |
| 27 * remoting.wcsLoader) by the downloaded Javascript. | |
| 28 * @type {remoting.WcsIqClient} | |
| 29 */ | |
| 30 this.wcsIqClient = null; | |
| 31 }; | |
| 32 | |
| 33 /** | |
| 34 * Load WCS if necessary, then invoke the callback with an access token. | |
| 35 * | |
| 36 * @param {function(string?): void} onReady The callback function, called with | |
| 37 * an OAuth2 access token when WCS has been loaded, or with null on error. | |
| 38 * @return {void} Nothing. | |
| 39 */ | |
| 40 remoting.WcsLoader.load = function(onReady) { | |
| 41 if (!remoting.wcsLoader) { | |
| 42 remoting.wcsLoader = new remoting.WcsLoader(); | |
| 43 } | |
| 44 /** @param {string} token The OAuth2 access token. */ | |
| 45 var start = function(token) { | |
| 46 remoting.wcsLoader.start_(token, onReady); | |
| 47 }; | |
| 48 remoting.oauth2.callWithToken(start); | |
| 49 }; | |
| 50 | |
| 51 /** | |
| 52 * The URL of the GTalk gadget. | |
| 53 * @type {string} | |
| 54 * @private | |
| 55 */ | |
| 56 remoting.WcsLoader.prototype.TALK_GADGET_URL_ = | |
| 57 'https://talkgadget.google.com/talkgadget/'; | |
| 58 | |
| 59 /** | |
| 60 * The id of the script node. | |
| 61 * @type {string} | |
| 62 * @private | |
| 63 */ | |
| 64 remoting.WcsLoader.prototype.SCRIPT_NODE_ID_ = 'wcs-script-node'; | |
| 65 | |
| 66 /** | |
| 67 * The attribute name indicating that the WCS has finished loading. | |
| 68 * @type {string} | |
| 69 * @private | |
| 70 */ | |
| 71 remoting.WcsLoader.prototype.SCRIPT_NODE_LOADED_FLAG_ = 'wcs-script-loaded'; | |
| 72 | |
| 73 /** | |
| 74 * Starts loading the WCS IQ client. | |
| 75 * | |
| 76 * When it's loaded, construct remoting.wcs as a wrapper for it. | |
| 77 * When the WCS connection is ready, or on error, call |onReady|. | |
| 78 * | |
| 79 * @param {string} token An OAuth2 access token. | |
| 80 * @param {function(string?): void} onReady The callback function, called with | |
| 81 * an OAuth2 access token when WCS has been loaded, or with null on error. | |
| 82 * @return {void} Nothing. | |
| 83 * @private | |
| 84 */ | |
| 85 remoting.WcsLoader.prototype.start_ = function(token, onReady) { | |
| 86 var node = document.getElementById(this.SCRIPT_NODE_ID_); | |
| 87 if (!node) { | |
| 88 // The first time, there will be no script node, so create one. | |
| 89 node = document.createElement('script'); | |
| 90 node.id = this.SCRIPT_NODE_ID_; | |
| 91 node.src = this.TALK_GADGET_URL_ + 'iq?access_token=' + token; | |
| 92 node.type = 'text/javascript'; | |
| 93 document.body.insertBefore(node, document.body.firstChild); | |
| 94 } else if (node.hasAttribute(this.SCRIPT_NODE_LOADED_FLAG_)) { | |
| 95 // Subsequently, explicitly invoke onReady if onload has already fired. | |
| 96 // TODO(jamiewalch): It's possible that the WCS client has not finished | |
| 97 // initializing. Add support for multiple callbacks to the remoting.Wcs | |
| 98 // class to address this. | |
| 99 onReady(token); | |
| 100 return; | |
| 101 } | |
| 102 /** @type {remoting.WcsLoader} */ | |
| 103 var that = this; | |
| 104 var onLoad = function() { | |
| 105 var typedNode = /** @type {Element} */ (node); | |
| 106 typedNode.setAttribute(that.SCRIPT_NODE_LOADED_FLAG_, true); | |
| 107 that.constructWcs_(token, onReady); | |
| 108 }; | |
| 109 var onError = function() { | |
| 110 var typedNode = /** @type {Element} */ (node); | |
| 111 typedNode.parentNode.removeChild(node); | |
| 112 onReady(null); | |
| 113 }; | |
| 114 node.addEventListener('load', onLoad, false); | |
| 115 node.addEventListener('error', onError, false); | |
| 116 }; | |
| 117 | |
| 118 /** | |
| 119 * Constructs the remoting.wcs object. | |
| 120 * | |
| 121 * @param {string} token An OAuth2 access token. | |
| 122 * @param {function(string?): void} onReady The callback function, called with | |
| 123 * an OAuth2 access token when WCS has been loaded, or with null on error. | |
| 124 * @return {void} Nothing. | |
| 125 * @private | |
| 126 */ | |
| 127 remoting.WcsLoader.prototype.constructWcs_ = function(token, onReady) { | |
| 128 remoting.wcs = new remoting.Wcs( | |
| 129 remoting.wcsLoader.wcsIqClient, | |
| 130 token, | |
| 131 function() { onReady(token); }); | |
| 132 }; | |
| OLD | NEW |