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 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 // Fetches the access token and makes an authorized call. If refresh is true, | 253 // Fetches the access token and makes an authorized call. If refresh is true, |
254 // then forces refreshing the access token. | 254 // then forces refreshing the access token. |
255 var requestTokenAndCall = function(refresh, onInnerSuccess, onInnerFailure) { | 255 var requestTokenAndCall = function(refresh, onInnerSuccess, onInnerFailure) { |
256 chrome.fileBrowserPrivate.requestAccessToken(refresh, function(token) { | 256 chrome.fileBrowserPrivate.requestAccessToken(refresh, function(token) { |
257 if (this.aborted_) | 257 if (this.aborted_) |
258 return; | 258 return; |
259 if (!token) { | 259 if (!token) { |
260 onInnerFailure(); | 260 onInnerFailure(); |
261 return; | 261 return; |
262 } | 262 } |
263 this.xhr_ = AuthorizedXHR.loadWithToken_( | 263 this.xhr_ = AuthorizedXHR.load_( |
264 token, url, onInnerSuccess, onInnerFailure); | 264 token, url, onInnerSuccess, onInnerFailure); |
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.load_(null, url, onMaybeSuccess, onMaybeFailure); |
| 278 return; |
| 279 } |
| 280 |
275 // Make the request with reusing the current token. If it fails, then retry. | 281 // Make the request with reusing the current token. If it fails, then retry. |
276 requestTokenAndCall(false, onMaybeSuccess, maybeRetryCall); | 282 requestTokenAndCall(false, onMaybeSuccess, maybeRetryCall); |
277 }; | 283 }; |
278 | 284 |
279 /** | 285 /** |
280 * Fetches data using authorized XmlHttpRequest with the provided OAuth2 token. | 286 * Fetches data using authorized XmlHttpRequest with the provided OAuth2 token. |
281 * If the token is invalid, the request will fail. | 287 * If the token is invalid, the request will fail. |
282 * | 288 * |
283 * @param {string} token OAuth2 token to be injected to the request. | 289 * @param {?string} token OAuth2 token to be injected to the request. Null for |
| 290 * no token. |
284 * @param {string} url URL to the resource to be fetched. | 291 * @param {string} url URL to the resource to be fetched. |
285 * @param {function(string, Blob}) onSuccess Success callback with the content | 292 * @param {function(string, Blob}) onSuccess Success callback with the content |
286 * type and the fetched data. | 293 * type and the fetched data. |
287 * @param {function(number=)} onFailure Failure callback with the error code | 294 * @param {function(number=)} onFailure Failure callback with the error code |
288 * if available. | 295 * if available. |
| 296 * @return {AuthorizedXHR} XHR instance. |
289 * @private | 297 * @private |
290 */ | 298 */ |
291 AuthorizedXHR.loadWithToken_ = function(token, url, onSuccess, onFailure) { | 299 AuthorizedXHR.load_ = function(token, url, onSuccess, onFailure) { |
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 } |
| 323 |
| 324 return xhr; |
314 }; | 325 }; |
315 | 326 |
316 /** | 327 /** |
317 * Sends the resized image via the callback. If the image has been changed, | 328 * 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. | 329 * then packs the canvas contents, otherwise sends the raw image data. |
319 * | 330 * |
320 * @param {boolean} imageChanged Whether the image has been changed. | 331 * @param {boolean} imageChanged Whether the image has been changed. |
321 * @private | 332 * @private |
322 */ | 333 */ |
323 Request.prototype.sendImage_ = function(imageChanged) { | 334 Request.prototype.sendImage_ = function(imageChanged) { |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 this.image_.src = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAA' + | 429 this.image_.src = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAA' + |
419 'ABAAEAAAICTAEAOw=='; | 430 'ABAAEAAAICTAEAOw=='; |
420 | 431 |
421 this.xhr_.onload = function() {}; | 432 this.xhr_.onload = function() {}; |
422 this.xhr_.abort(); | 433 this.xhr_.abort(); |
423 | 434 |
424 // Dispose memory allocated by Canvas. | 435 // Dispose memory allocated by Canvas. |
425 this.canvas_.width = 0; | 436 this.canvas_.width = 0; |
426 this.canvas_.height = 0; | 437 this.canvas_.height = 0; |
427 }; | 438 }; |
OLD | NEW |