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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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. |
86 onValidateError(remoting.Error.AUTHENTICATION_FAILED); | 86 onValidateError(new remoting.Error( |
87 } | 87 remoting.Error.Tag.AUTHENTICATION_FAILED)); |
| 88 }; |
88 that.validateToken(token, onValidateOk, onValidateError); | 89 that.validateToken(token, onValidateOk, onValidateError); |
89 } | 90 }; |
90 node.addEventListener('load', onLoad, false); | 91 node.addEventListener('load', onLoad, false); |
91 node.addEventListener('error', onLoadError, false); | 92 node.addEventListener('error', onLoadError, false); |
92 }; | 93 }; |
93 | 94 |
94 /** | 95 /** |
95 * Constructs the remoting.wcs object. | 96 * Constructs the remoting.wcs object. |
96 * | 97 * |
97 * @param {string} token An OAuth2 access token. | 98 * @param {string} token An OAuth2 access token. |
98 * @param {function(string): void} onReady The callback function, called with | 99 * @param {function(string): void} onReady The callback function, called with |
99 * an OAuth2 access token when WCS has been loaded. | 100 * an OAuth2 access token when WCS has been loaded. |
(...skipping 17 matching lines...) Expand all Loading... |
117 remoting.WcsLoader.prototype.validateToken = function(token, onOk, onError) { | 118 remoting.WcsLoader.prototype.validateToken = function(token, onOk, onError) { |
118 /** @type {XMLHttpRequest} */ | 119 /** @type {XMLHttpRequest} */ |
119 var xhr = new XMLHttpRequest(); | 120 var xhr = new XMLHttpRequest(); |
120 xhr.onreadystatechange = function() { | 121 xhr.onreadystatechange = function() { |
121 if (xhr.readyState != 4) { | 122 if (xhr.readyState != 4) { |
122 return; | 123 return; |
123 } | 124 } |
124 if (xhr.status == 200) { | 125 if (xhr.status == 200) { |
125 onOk(); | 126 onOk(); |
126 } else { | 127 } else { |
127 var error = remoting.Error.AUTHENTICATION_FAILED; | 128 var error = new remoting.Error(remoting.Error.Tag.AUTHENTICATION_FAILED); |
128 switch (xhr.status) { | 129 switch (xhr.status) { |
129 case 0: | 130 case 0: |
130 error = remoting.Error.NETWORK_FAILURE; | 131 error = new remoting.Error(remoting.Error.Tag.NETWORK_FAILURE); |
131 break; | 132 break; |
132 case 502: // No break | 133 case 502: // No break |
133 case 503: | 134 case 503: |
134 error = remoting.Error.SERVICE_UNAVAILABLE; | 135 error = new remoting.Error(remoting.Error.Tag.SERVICE_UNAVAILABLE); |
135 break; | 136 break; |
136 } | 137 } |
137 onError(error); | 138 onError(error); |
138 } | 139 } |
139 }; | 140 }; |
140 var parameters = '?access_token=' + encodeURIComponent(token); | 141 var parameters = '?access_token=' + encodeURIComponent(token); |
141 xhr.open('GET', | 142 xhr.open('GET', |
142 remoting.settings.OAUTH2_API_BASE_URL + '/v1/tokeninfo' + parameters, | 143 remoting.settings.OAUTH2_API_BASE_URL + '/v1/tokeninfo' + parameters, |
143 true); | 144 true); |
144 xhr.send(null); | 145 xhr.send(null); |
145 }; | 146 }; |
OLD | NEW |