OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /** | 7 /** |
8 * Creates and starts downloading and then resizing of the image. Finally, | 8 * Creates and starts downloading and then resizing of the image. Finally, |
9 * returns the image using the callback. | 9 * returns the image using the callback. |
10 * | 10 * |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
265 }.bind(this)); | 265 }.bind(this)); |
266 }.bind(this); | 266 }.bind(this); |
267 | 267 |
268 // Refreshes the access token and retries the request. | 268 // Refreshes the access token and retries the request. |
269 var maybeRetryCall = function(code) { | 269 var maybeRetryCall = function(code) { |
270 if (this.aborted_) | 270 if (this.aborted_) |
271 return; | 271 return; |
272 requestTokenAndCall(true, onMaybeSuccess, onMaybeFailure); | 272 requestTokenAndCall(true, onMaybeSuccess, onMaybeFailure); |
273 }.bind(this); | 273 }.bind(this); |
274 | 274 |
275 // Do not request a token for local resources, since it is not necessary. | |
276 if (url.indexOf('filesystem:') === 0) { | |
277 this.xhr_ = AuthorizedXHR.loadWithToken_( | |
278 null, url, onMaybeSuccess, onMaybeFailure); | |
279 return; | |
280 } | |
281 | |
275 // Make the request with reusing the current token. If it fails, then retry. | 282 // Make the request with reusing the current token. If it fails, then retry. |
276 requestTokenAndCall(false, onMaybeSuccess, maybeRetryCall); | 283 requestTokenAndCall(false, onMaybeSuccess, maybeRetryCall); |
277 }; | 284 }; |
278 | 285 |
279 /** | 286 /** |
280 * Fetches data using authorized XmlHttpRequest with the provided OAuth2 token. | 287 * Fetches data using authorized XmlHttpRequest with the provided OAuth2 token. |
281 * If the token is invalid, the request will fail. | 288 * If the token is invalid, the request will fail. |
282 * | 289 * |
283 * @param {string} token OAuth2 token to be injected to the request. | 290 * @param {?string} token OAuth2 token to be injected to the request. Null for |
291 * no token. | |
284 * @param {string} url URL to the resource to be fetched. | 292 * @param {string} url URL to the resource to be fetched. |
285 * @param {function(string, Blob}) onSuccess Success callback with the content | 293 * @param {function(string, Blob}) onSuccess Success callback with the content |
286 * type and the fetched data. | 294 * type and the fetched data. |
287 * @param {function(number=)} onFailure Failure callback with the error code | 295 * @param {function(number=)} onFailure Failure callback with the error code |
288 * if available. | 296 * if available. |
289 * @private | 297 * @private |
290 */ | 298 */ |
291 AuthorizedXHR.loadWithToken_ = function(token, url, onSuccess, onFailure) { | 299 AuthorizedXHR.loadWithToken_ = function(token, url, onSuccess, onFailure) { |
yoshiki
2013/11/01 09:33:55
nit: how about renaming it just 'load'? We no long
yoshiki
2013/11/01 09:34:17
s/load/load_/
mtomasz
2013/11/05 00:50:22
Done.
| |
292 var xhr = new XMLHttpRequest(); | 300 var xhr = new XMLHttpRequest(); |
293 xhr.responseType = 'blob'; | 301 xhr.responseType = 'blob'; |
294 | 302 |
295 xhr.onreadystatechange = function() { | 303 xhr.onreadystatechange = function() { |
296 if (xhr.readyState != 4) | 304 if (xhr.readyState != 4) |
297 return; | 305 return; |
298 if (xhr.status != 200) { | 306 if (xhr.status != 200) { |
299 onFailure(xhr.status); | 307 onFailure(xhr.status); |
300 return; | 308 return; |
301 } | 309 } |
302 var contentType = xhr.getResponseHeader('Content-Type'); | 310 var contentType = xhr.getResponseHeader('Content-Type'); |
303 onSuccess(contentType, xhr.response); | 311 onSuccess(contentType, xhr.response); |
304 }.bind(this); | 312 }.bind(this); |
305 | 313 |
306 // Perform a xhr request. | 314 // Perform a xhr request. |
307 try { | 315 try { |
308 xhr.open('GET', url, true); | 316 xhr.open('GET', url, true); |
309 xhr.setRequestHeader('Authorization', 'Bearer ' + token); | 317 if (token) |
318 xhr.setRequestHeader('Authorization', 'Bearer ' + token); | |
310 xhr.send(); | 319 xhr.send(); |
311 } catch (e) { | 320 } catch (e) { |
312 onFailure(); | 321 onFailure(); |
313 } | 322 } |
314 }; | 323 }; |
315 | 324 |
316 /** | 325 /** |
317 * Sends the resized image via the callback. If the image has been changed, | 326 * Sends the resized image via the callback. If the image has been changed, |
318 * then packs the canvas contents, otherwise sends the raw image data. | 327 * then packs the canvas contents, otherwise sends the raw image data. |
319 * | 328 * |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
418 this.image_.src = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAA' + | 427 this.image_.src = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAA' + |
419 'ABAAEAAAICTAEAOw=='; | 428 'ABAAEAAAICTAEAOw=='; |
420 | 429 |
421 this.xhr_.onload = function() {}; | 430 this.xhr_.onload = function() {}; |
422 this.xhr_.abort(); | 431 this.xhr_.abort(); |
423 | 432 |
424 // Dispose memory allocated by Canvas. | 433 // Dispose memory allocated by Canvas. |
425 this.canvas_.width = 0; | 434 this.canvas_.width = 0; |
426 this.canvas_.height = 0; | 435 this.canvas_.height = 0; |
427 }; | 436 }; |
OLD | NEW |