| OLD | NEW |
| 1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
| 4 */ | 4 */ |
| 5 | 5 |
| 6 /** | 6 /** |
| 7 * @fileoverview | 7 * @fileoverview |
| 8 * A class that loads a WCS IQ client and constructs remoting.wcs as a | 8 * A class that loads a WCS IQ client and constructs remoting.wcs as a |
| 9 * wrapper for it. | 9 * wrapper for it. |
| 10 */ | 10 */ |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 /** | 38 /** |
| 39 * Starts loading the WCS IQ client. | 39 * Starts loading the WCS IQ client. |
| 40 * | 40 * |
| 41 * When it's loaded, construct remoting.wcs as a wrapper for it. | 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|, | 42 * When the WCS connection is ready, or on error, call |onReady| or |onError|, |
| 43 * respectively. | 43 * respectively. |
| 44 * | 44 * |
| 45 * @param {string} token An OAuth2 access token. | 45 * @param {string} token An OAuth2 access token. |
| 46 * @param {function(string): void} onReady The callback function, called with | 46 * @param {function(string): void} onReady The callback function, called with |
| 47 * a client JID when WCS has been loaded. | 47 * a client JID when WCS has been loaded. |
| 48 * @param {function(remoting.Error):void} onError Function to invoke with an | 48 * @param {function(!remoting.Error):void} onError Function to invoke with an |
| 49 * error code on failure. | 49 * error code on failure. |
| 50 * @return {void} Nothing. | 50 * @return {void} Nothing. |
| 51 */ | 51 */ |
| 52 remoting.WcsLoader.prototype.start = function(token, onReady, onError) { | 52 remoting.WcsLoader.prototype.start = function(token, onReady, onError) { |
| 53 var node = document.getElementById(this.SCRIPT_NODE_ID_); | 53 var node = document.getElementById(this.SCRIPT_NODE_ID_); |
| 54 if (node) { | 54 if (node) { |
| 55 console.error('Multiple calls to WcsLoader.start are not allowed.'); | 55 console.error('Multiple calls to WcsLoader.start are not allowed.'); |
| 56 onError(remoting.Error.UNEXPECTED); | 56 onError(remoting.Error.UNEXPECTED); |
| 57 return; | 57 return; |
| 58 } | 58 } |
| 59 | 59 |
| 60 // Create a script node to load the WCS driver. | 60 // Create a script node to load the WCS driver. |
| 61 node = document.createElement('script'); | 61 node = document.createElement('script'); |
| 62 node.id = this.SCRIPT_NODE_ID_; | 62 node.id = this.SCRIPT_NODE_ID_; |
| 63 node.src = remoting.settings.TALK_GADGET_URL + 'iq?access_token=' + token; | 63 node.src = remoting.settings.TALK_GADGET_URL + 'iq?access_token=' + token; |
| 64 node.type = 'text/javascript'; | 64 node.type = 'text/javascript'; |
| 65 document.body.insertBefore(node, document.body.firstChild); | 65 document.body.insertBefore(node, document.body.firstChild); |
| 66 | 66 |
| 67 /** @type {remoting.WcsLoader} */ | 67 /** @type {remoting.WcsLoader} */ |
| 68 var that = this; | 68 var that = this; |
| 69 var onLoad = function() { | 69 var onLoad = function() { |
| 70 that.constructWcs_(token, onReady); | 70 that.constructWcs_(token, onReady); |
| 71 }; | 71 }; |
| 72 var onLoadError = function(event) { | 72 var onLoadError = function(event) { |
| 73 // The DOM Event object has no detail on the nature of the error, so try to | 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. | 74 // validate the token to get a better idea. |
| 75 /** @param {remoting.Error} error Error code. */ | 75 /** @param {!remoting.Error} error Error code. */ |
| 76 var onValidateError = function(error) { | 76 var onValidateError = function(error) { |
| 77 var typedNode = /** @type {Element} */ (node); | 77 var typedNode = /** @type {Element} */ (node); |
| 78 typedNode.parentNode.removeChild(node); | 78 typedNode.parentNode.removeChild(node); |
| 79 onError(error); | 79 onError(error); |
| 80 }; | 80 }; |
| 81 var onValidateOk = function() { | 81 var onValidateOk = function() { |
| 82 // We can reach the authentication server and validate the token. Either | 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 | 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 | 84 // cookie problem. Only the cookie problem can be fixed by the user, so |
| 85 // suggest that fix. | 85 // suggest that fix. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 103 remoting.WcsLoader.prototype.constructWcs_ = function(token, onReady) { | 103 remoting.WcsLoader.prototype.constructWcs_ = function(token, onReady) { |
| 104 remoting.wcs = new remoting.Wcs( | 104 remoting.wcs = new remoting.Wcs( |
| 105 remoting.wcsLoader.wcsIqClient, token, onReady); | 105 remoting.wcsLoader.wcsIqClient, token, onReady); |
| 106 }; | 106 }; |
| 107 | 107 |
| 108 /** | 108 /** |
| 109 * Validates an OAuth2 access token. | 109 * Validates an OAuth2 access token. |
| 110 * | 110 * |
| 111 * @param {string} token The access token. | 111 * @param {string} token The access token. |
| 112 * @param {function():void} onOk Callback to invoke if the token is valid. | 112 * @param {function():void} onOk Callback to invoke if the token is valid. |
| 113 * @param {function(remoting.Error):void} onError Function to invoke with an | 113 * @param {function(!remoting.Error):void} onError Function to invoke with an |
| 114 * error code on failure. | 114 * error code on failure. |
| 115 * @return {void} Nothing. | 115 * @return {void} Nothing. |
| 116 */ | 116 */ |
| 117 remoting.WcsLoader.prototype.validateToken = function(token, onOk, onError) { | 117 remoting.WcsLoader.prototype.validateToken = function(token, onOk, onError) { |
| 118 /** @type {XMLHttpRequest} */ | 118 /** @type {XMLHttpRequest} */ |
| 119 var xhr = new XMLHttpRequest(); | 119 var xhr = new XMLHttpRequest(); |
| 120 xhr.onreadystatechange = function() { | 120 xhr.onreadystatechange = function() { |
| 121 if (xhr.readyState != 4) { | 121 if (xhr.readyState != 4) { |
| 122 return; | 122 return; |
| 123 } | 123 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 136 } | 136 } |
| 137 onError(error); | 137 onError(error); |
| 138 } | 138 } |
| 139 }; | 139 }; |
| 140 var parameters = '?access_token=' + encodeURIComponent(token); | 140 var parameters = '?access_token=' + encodeURIComponent(token); |
| 141 xhr.open('GET', | 141 xhr.open('GET', |
| 142 remoting.settings.OAUTH2_API_BASE_URL + '/v1/tokeninfo' + parameters, | 142 remoting.settings.OAUTH2_API_BASE_URL + '/v1/tokeninfo' + parameters, |
| 143 true); | 143 true); |
| 144 xhr.send(null); | 144 xhr.send(null); |
| 145 }; | 145 }; |
| OLD | NEW |