Chromium Code Reviews| 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 * @typedef {{ | |
| 9 * cache: (boolean|undefined), | |
| 10 * priority: (number|undefined), | |
| 11 * taskId: number, | |
| 12 * timestamp: (number|undefined), | |
| 13 * url: string | |
| 14 * }} | |
| 15 */ | |
| 16 var LoadImageRequest; | |
|
fukino
2014/10/07 09:52:45
This type defines how the request object given thr
| |
| 17 | |
| 18 /** | |
| 8 * Creates and starts downloading and then resizing of the image. Finally, | 19 * Creates and starts downloading and then resizing of the image. Finally, |
| 9 * returns the image using the callback. | 20 * returns the image using the callback. |
| 10 * | 21 * |
| 11 * @param {string} id Request ID. | 22 * @param {string} id Request ID. |
| 12 * @param {Cache} cache Cache object. | 23 * @param {Cache} cache Cache object. |
| 13 * @param {Object} request Request message as a hash array. | 24 * @param {LoadImageRequest} request Request message as a hash array. |
| 14 * @param {function(Object)} callback Callback used to send the response. | 25 * @param {function(Object)} callback Callback used to send the response. |
| 15 * @constructor | 26 * @constructor |
| 16 */ | 27 */ |
| 17 function Request(id, cache, request, callback) { | 28 function Request(id, cache, request, callback) { |
| 18 /** | 29 /** |
| 19 * @type {string} | 30 * @type {string} |
| 20 * @private | 31 * @private |
| 21 */ | 32 */ |
| 22 this.id_ = id; | 33 this.id_ = id; |
| 23 | 34 |
| 24 /** | 35 /** |
| 25 * @type {Cache} | 36 * @type {Cache} |
| 26 * @private | 37 * @private |
| 27 */ | 38 */ |
| 28 this.cache_ = cache; | 39 this.cache_ = cache; |
| 29 | 40 |
| 30 /** | 41 /** |
| 31 * @type {Object} | 42 * @type {LoadImageRequest} |
| 32 * @private | 43 * @private |
| 33 */ | 44 */ |
| 34 this.request_ = request; | 45 this.request_ = request; |
| 35 | 46 |
| 36 /** | 47 /** |
| 37 * @type {function(Object)} | 48 * @type {function(Object)} |
| 38 * @private | 49 * @private |
| 39 */ | 50 */ |
| 40 this.sendResponse_ = callback; | 51 this.sendResponse_ = callback; |
| 41 | 52 |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 236 * | 247 * |
| 237 * @param {string} url URL to the resource to be fetched. | 248 * @param {string} url URL to the resource to be fetched. |
| 238 * @param {function(string, Blob)} onSuccess Success callback with the content | 249 * @param {function(string, Blob)} onSuccess Success callback with the content |
| 239 * type and the fetched data. | 250 * type and the fetched data. |
| 240 * @param {function()} onFailure Failure callback. | 251 * @param {function()} onFailure Failure callback. |
| 241 */ | 252 */ |
| 242 AuthorizedXHR.prototype.load = function(url, onSuccess, onFailure) { | 253 AuthorizedXHR.prototype.load = function(url, onSuccess, onFailure) { |
| 243 this.aborted_ = false; | 254 this.aborted_ = false; |
| 244 | 255 |
| 245 // Do not call any callbacks when aborting. | 256 // Do not call any callbacks when aborting. |
| 246 var onMaybeSuccess = function(contentType, response) { | 257 var onMaybeSuccess = /** @type {function(string, Blob)} */ ( |
| 247 if (!this.aborted_) | 258 function(contentType, response) { |
| 248 onSuccess(contentType, response); | 259 if (!this.aborted_) |
| 249 }.bind(this); | 260 onSuccess(contentType, response); |
| 261 }.bind(this)); | |
| 250 | 262 |
| 251 var onMaybeFailure = function(opt_code) { | 263 var onMaybeFailure = /** @type {function(number=)} */ ( |
|
mtomasz
2014/10/07 09:56:15
Sometimes we use number|underfined, but sometimes
fukino
2014/10/07 10:49:44
number= can be used only for optional parameter. (
mtomasz
2014/10/07 10:54:29
Got it. Thanks for the explanation!
| |
| 252 if (!this.aborted_) | 264 function(opt_code) { |
| 253 onFailure(); | 265 if (!this.aborted_) |
| 254 }.bind(this); | 266 onFailure(); |
| 267 }.bind(this)); | |
| 255 | 268 |
| 256 // Fetches the access token and makes an authorized call. If refresh is true, | 269 // Fetches the access token and makes an authorized call. If refresh is true, |
| 257 // then forces refreshing the access token. | 270 // then forces refreshing the access token. |
| 258 var requestTokenAndCall = function(refresh, onInnerSuccess, onInnerFailure) { | 271 var requestTokenAndCall = function(refresh, onInnerSuccess, onInnerFailure) { |
| 259 chrome.fileManagerPrivate.requestAccessToken(refresh, function(token) { | 272 chrome.fileManagerPrivate.requestAccessToken(refresh, function(token) { |
| 260 if (this.aborted_) | 273 if (this.aborted_) |
| 261 return; | 274 return; |
| 262 if (!token) { | 275 if (!token) { |
| 263 onInnerFailure(); | 276 onInnerFailure(); |
| 264 return; | 277 return; |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 277 | 290 |
| 278 // Do not request a token for local resources, since it is not necessary. | 291 // Do not request a token for local resources, since it is not necessary. |
| 279 if (/^filesystem:/.test(url)) { | 292 if (/^filesystem:/.test(url)) { |
| 280 // The query parameter is workaround for | 293 // The query parameter is workaround for |
| 281 // crbug.com/379678, which force to obtain the latest contents of the image. | 294 // crbug.com/379678, which force to obtain the latest contents of the image. |
| 282 var noCacheUrl = url + '?nocache=' + Date.now(); | 295 var noCacheUrl = url + '?nocache=' + Date.now(); |
| 283 this.xhr_ = AuthorizedXHR.load_( | 296 this.xhr_ = AuthorizedXHR.load_( |
| 284 null, | 297 null, |
| 285 noCacheUrl, | 298 noCacheUrl, |
| 286 onMaybeSuccess, | 299 onMaybeSuccess, |
| 287 onMaybeSuccess); | 300 onMaybeFailure); |
|
fukino
2014/10/07 09:52:45
This was changed onMaybeSuccess unintentionally, s
| |
| 288 return; | 301 return; |
| 289 } | 302 } |
| 290 | 303 |
| 291 // Make the request with reusing the current token. If it fails, then retry. | 304 // Make the request with reusing the current token. If it fails, then retry. |
| 292 requestTokenAndCall(false, onMaybeSuccess, maybeRetryCall); | 305 requestTokenAndCall(false, onMaybeSuccess, maybeRetryCall); |
| 293 }; | 306 }; |
| 294 | 307 |
| 295 /** | 308 /** |
| 296 * Fetches data using authorized XmlHttpRequest with the provided OAuth2 token. | 309 * Fetches data using authorized XmlHttpRequest with the provided OAuth2 token. |
| 297 * If the token is invalid, the request will fail. | 310 * If the token is invalid, the request will fail. |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 434 this.image_.src = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAA' + | 447 this.image_.src = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAA' + |
| 435 'ABAAEAAAICTAEAOw=='; | 448 'ABAAEAAAICTAEAOw=='; |
| 436 | 449 |
| 437 this.xhr_.onload = function() {}; | 450 this.xhr_.onload = function() {}; |
| 438 this.xhr_.abort(); | 451 this.xhr_.abort(); |
| 439 | 452 |
| 440 // Dispose memory allocated by Canvas. | 453 // Dispose memory allocated by Canvas. |
| 441 this.canvas_.width = 0; | 454 this.canvas_.width = 0; |
| 442 this.canvas_.height = 0; | 455 this.canvas_.height = 0; |
| 443 }; | 456 }; |
| OLD | NEW |