Skip to content

Commit

Permalink
Renames ImageRequest to ImageRequestTask
Browse files Browse the repository at this point in the history
During a cleanup https://codereview.chromium.org/1148563004 the image
Request class was renamed to ImageRequest. However, the file name was
kept as request.js. Make the class and its file name the same.

Bug: 903742
Change-Id: I6f2d7bbfb6f3f94001d0cd1e629fc27f83061c45
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2245429
Reviewed-by: Luciano Pacheco <lucmult@chromium.org>
Commit-Queue: Bo Majewski <majewski@chromium.org>
Cr-Commit-Position: refs/heads/master@{#779705}
  • Loading branch information
Bo Majewski authored and Commit Bot committed Jun 18, 2020
1 parent c18c02f commit 95a98a7
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 105 deletions.
4 changes: 4 additions & 0 deletions chrome/browser/chromeos/file_manager/image_loader_jstest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ IN_PROC_BROWSER_TEST_F(ImageLoaderJsTest, CacheTest) {
IN_PROC_BROWSER_TEST_F(ImageLoaderJsTest, ImageLoaderTest) {
RunTestURL("image_loader_unittest_gen.html");
}

IN_PROC_BROWSER_TEST_F(ImageLoaderJsTest, SchedulerTest) {
RunTestURL("scheduler_unittest_gen.html");
}
16 changes: 12 additions & 4 deletions ui/file_manager/image_loader/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ js_type_check("closure_compile_module") {
":image_loader",
":image_loader_client",
":image_loader_util",
":image_request_task",
":load_image_request",
":piex_loader",
":request",
":scheduler",
]
}
Expand All @@ -39,9 +39,9 @@ js_unittest("cache_unittest") {
js_library("image_loader") {
deps = [
":cache",
":image_request_task",
":load_image_request",
":piex_loader",
":request",
":scheduler",
"//ui/file_manager/externs:file_manager_private",
]
Expand Down Expand Up @@ -96,7 +96,7 @@ js_library("piex_loader") {
externs_list = [ "//ui/file_manager/externs/platform.js" ]
}

js_library("request") {
js_library("image_request_task") {
deps = [
":cache",
":image_loader_util",
Expand All @@ -108,7 +108,14 @@ js_library("request") {
}

js_library("scheduler") {
deps = [ ":request" ]
deps = [ ":image_request_task" ]
}

js_unittest("scheduler_unittest") {
deps = [
":scheduler",
"//ui/webui/resources/js:webui_resource_test",
]
}

js_test_gen_html("js_test_gen_html") {
Expand All @@ -117,6 +124,7 @@ js_test_gen_html("js_test_gen_html") {
":cache_unittest",
":image_loader_client_unittest",
":image_loader_unittest",
":scheduler_unittest",
]
}

Expand Down
2 changes: 1 addition & 1 deletion ui/file_manager/image_loader/background_scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// <include src="image_loader.js">
// <include src="image_loader_util.js">
// <include src="piex_loader.js">
// <include src="request.js">
// <include src="image_request_task.js">
// <include src="scheduler.js">
// Entry point.
// <include src="background.js">
2 changes: 1 addition & 1 deletion ui/file_manager/image_loader/image_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ ImageLoader.prototype.onMessage_ = function(senderOrigin, request, callback) {
return false; // No callback calls.
} else {
// Create a request task and add it to the scheduler (queue).
const requestTask = new ImageRequest(
const requestTask = new ImageRequestTask(
requestId, this.cache_, this.piexLoader_, request, callback);
this.scheduler_.add(requestTask);
return true; // Request will call the callback.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @param {function(!LoadImageResponse)} callback Response handler.
* @constructor
*/
function ImageRequest(id, cache, piexLoader, request, callback) {
function ImageRequestTask(id, cache, piexLoader, request, callback) {
/**
* Global ID (concatenated client ID and client request ID).
* @type {string}
Expand Down Expand Up @@ -111,21 +111,21 @@ function ImageRequest(id, cache, piexLoader, request, callback) {
* @const
* @type {number}
*/
ImageRequest.VIDEO_THUMBNAIL_POSITION = 3; // [sec]
ImageRequestTask.VIDEO_THUMBNAIL_POSITION = 3; // [sec]

/**
* The maximum milliseconds to load video. If loading video exceeds the limit,
* we give up generating video thumbnail and free the consumed memory.
* @const
* @type {number}
*/
ImageRequest.MAX_MILLISECONDS_TO_LOAD_VIDEO = 3000;
ImageRequestTask.MAX_MILLISECONDS_TO_LOAD_VIDEO = 3000;

/**
* A map which is used to estimate content type from extension.
* @enum {string}
*/
ImageRequest.ExtensionContentTypeMap = {
ImageRequestTask.ExtensionContentTypeMap = {
gif: 'image/gif',
png: 'image/png',
svg: 'image/svg',
Expand All @@ -138,15 +138,15 @@ ImageRequest.ExtensionContentTypeMap = {
* Returns ID of the request.
* @return {string} Request ID.
*/
ImageRequest.prototype.getId = function() {
ImageRequestTask.prototype.getId = function() {
return this.id_;
};

/**
* Returns the client's task ID for the request.
* @return {number}
*/
ImageRequest.prototype.getClientTaskId = function() {
ImageRequestTask.prototype.getClientTaskId = function() {
// Every incoming request should have been given a taskId.
assert(this.request_.taskId);
return this.request_.taskId;
Expand All @@ -158,7 +158,7 @@ ImageRequest.prototype.getClientTaskId = function() {
*
* @return {number} Priority.
*/
ImageRequest.prototype.getPriority = function() {
ImageRequestTask.prototype.getPriority = function() {
return (this.request_.priority !== undefined) ? this.request_.priority : 2;
};

Expand All @@ -169,7 +169,7 @@ ImageRequest.prototype.getPriority = function() {
* @param {function()} onSuccess Success callback.
* @param {function()} onFailure Failure callback.
*/
ImageRequest.prototype.loadFromCacheAndProcess = function(
ImageRequestTask.prototype.loadFromCacheAndProcess = function(
onSuccess, onFailure) {
this.loadFromCache_(
function(width, height, ifd, data) { // Found in cache.
Expand All @@ -185,14 +185,14 @@ ImageRequest.prototype.loadFromCacheAndProcess = function(
*
* @param {function()} callback Completion callback.
*/
ImageRequest.prototype.downloadAndProcess = function(callback) {
ImageRequestTask.prototype.downloadAndProcess = function(callback) {
if (this.downloadCallback_) {
throw new Error('Downloading already started.');
}

this.downloadCallback_ = callback;
this.downloadOriginal_(this.onImageLoad_.bind(this),
this.onImageError_.bind(this));
this.downloadOriginal_(
this.onImageLoad_.bind(this), this.onImageError_.bind(this));
};

/**
Expand All @@ -203,7 +203,7 @@ ImageRequest.prototype.downloadAndProcess = function(callback) {
* @param {function()} onFailure Failure callback.
* @private
*/
ImageRequest.prototype.loadFromCache_ = function(onSuccess, onFailure) {
ImageRequestTask.prototype.loadFromCache_ = function(onSuccess, onFailure) {
const cacheKey = LoadImageRequest.cacheKey(this.request_);

if (!cacheKey) {
Expand Down Expand Up @@ -238,7 +238,7 @@ ImageRequest.prototype.loadFromCache_ = function(onSuccess, onFailure) {
* @param {string} data Image data.
* @private
*/
ImageRequest.prototype.saveToCache_ = function(width, height, data) {
ImageRequestTask.prototype.saveToCache_ = function(width, height, data) {
const timestamp = this.request_.timestamp;

if (!this.request_.cache || !timestamp) {
Expand All @@ -262,7 +262,7 @@ ImageRequest.prototype.saveToCache_ = function(width, height, data) {
* @param {function()} onFailure Failure callback.
* @private
*/
ImageRequest.prototype.downloadOriginal_ = function(onSuccess, onFailure) {
ImageRequestTask.prototype.downloadOriginal_ = function(onSuccess, onFailure) {
this.image_.onload = function() {
URL.revokeObjectURL(this.image_.src);
onSuccess();
Expand Down Expand Up @@ -324,12 +324,14 @@ ImageRequest.prototype.downloadOriginal_ = function(onSuccess, onFailure) {

// Load video thumbnails by using video tag instead of XHR.
if (fileType.type === 'video') {
this.createVideoThumbnailUrl_(this.request_.url).then(function(url) {
this.image_.src = url;
}.bind(this)).catch(function(error) {
console.error('Video thumbnail error: ', error);
onFailure();
});
this.createVideoThumbnailUrl_(this.request_.url)
.then(function(url) {
this.image_.src = url;
}.bind(this))
.catch(function(error) {
console.error('Video thumbnail error: ', error);
onFailure();
});
return;
}

Expand All @@ -353,21 +355,21 @@ ImageRequest.prototype.downloadOriginal_ = function(onSuccess, onFailure) {
* thumbnail.
* @private
*/
ImageRequest.prototype.createVideoThumbnailUrl_ = function(url) {
ImageRequestTask.prototype.createVideoThumbnailUrl_ = function(url) {
const video =
assertInstanceof(document.createElement('video'), HTMLVideoElement);
return Promise
.race([
new Promise((resolve, reject) => {
video.addEventListener('canplay', resolve);
video.addEventListener('error', reject);
video.currentTime = ImageRequest.VIDEO_THUMBNAIL_POSITION;
video.currentTime = ImageRequestTask.VIDEO_THUMBNAIL_POSITION;
video.preload = 'auto';
video.src = url;
video.load();
}),
new Promise((resolve) => {
setTimeout(resolve, ImageRequest.MAX_MILLISECONDS_TO_LOAD_VIDEO);
setTimeout(resolve, ImageRequestTask.MAX_MILLISECONDS_TO_LOAD_VIDEO);
}).then(() => {
// If we don't receive 'canplay' event after 3 seconds have passed for
// some reason (e.g. unseekable video), we give up generating
Expand Down Expand Up @@ -396,7 +398,7 @@ ImageRequest.prototype.createVideoThumbnailUrl_ = function(url) {
* type and the fetched data.
* @param {function()} onFailure Failure callback.
*/
ImageRequest.prototype.load = function(url, onSuccess, onFailure) {
ImageRequestTask.prototype.load = function(url, onSuccess, onFailure) {
this.aborted_ = false;

// Do not call any callbacks when aborting.
Expand All @@ -405,7 +407,8 @@ ImageRequest.prototype.load = function(url, onSuccess, onFailure) {
// When content type is not available, try to estimate it from url.
if (!contentType) {
contentType =
ImageRequest.ExtensionContentTypeMap[this.extractExtension_(url)];
ImageRequestTask
.ExtensionContentTypeMap[this.extractExtension_(url)];
}

if (!this.aborted_) {
Expand All @@ -422,15 +425,16 @@ ImageRequest.prototype.load = function(url, onSuccess, onFailure) {
// The query parameter is workaround for crbug.com/379678, which forces the
// browser to obtain the latest contents of the image.
const noCacheUrl = url + '?nocache=' + Date.now();
this.xhr_ = ImageRequest.load_(noCacheUrl, onMaybeSuccess, onMaybeFailure);
this.xhr_ =
ImageRequestTask.load_(noCacheUrl, onMaybeSuccess, onMaybeFailure);
};

/**
* Extracts extension from url.
* @param {string} url Url.
* @return {string} Extracted extension, e.g. png.
*/
ImageRequest.prototype.extractExtension_ = function(url) {
ImageRequestTask.prototype.extractExtension_ = function(url) {
const result = (/\.([a-zA-Z]+)$/i).exec(url);
return result ? result[1] : '';
};
Expand All @@ -446,7 +450,7 @@ ImageRequest.prototype.extractExtension_ = function(url) {
* @return {XMLHttpRequest} XHR instance.
* @private
*/
ImageRequest.load_ = function(url, onSuccess, onFailure) {
ImageRequestTask.load_ = function(url, onSuccess, onFailure) {
const xhr = new XMLHttpRequest();
xhr.responseType = 'blob';

Expand Down Expand Up @@ -481,7 +485,7 @@ ImageRequest.load_ = function(url, onSuccess, onFailure) {
* @param {boolean} imageChanged Whether the image has been changed.
* @private
*/
ImageRequest.prototype.sendImage_ = function(imageChanged) {
ImageRequestTask.prototype.sendImage_ = function(imageChanged) {
let width;
let height;
let data;
Expand Down Expand Up @@ -525,7 +529,7 @@ ImageRequest.prototype.sendImage_ = function(imageChanged) {
* @param {string} data Image data.
* @private
*/
ImageRequest.prototype.sendImageData_ = function(width, height, data) {
ImageRequestTask.prototype.sendImageData_ = function(width, height, data) {
const result = {width, height, ifd: this.ifd_, data};
this.sendResponse_(new LoadImageResponse(
LoadImageResponseStatus.SUCCESS, this.getClientTaskId(), result));
Expand All @@ -536,7 +540,7 @@ ImageRequest.prototype.sendImageData_ = function(width, height, data) {
* and finalizes the request process.
* @private
*/
ImageRequest.prototype.onImageLoad_ = function() {
ImageRequestTask.prototype.onImageLoad_ = function() {
// Perform processing if the url is not a data url, or if there are some
// operations requested.
if (!(this.request_.url.match(/^data/) ||
Expand All @@ -559,7 +563,7 @@ ImageRequest.prototype.onImageLoad_ = function() {
* finalizes the request process.
* @private
*/
ImageRequest.prototype.onImageError_ = function() {
ImageRequestTask.prototype.onImageError_ = function() {
this.sendResponse_(new LoadImageResponse(
LoadImageResponseStatus.ERROR, this.getClientTaskId()));
this.cleanup_();
Expand All @@ -569,7 +573,7 @@ ImageRequest.prototype.onImageError_ = function() {
/**
* Cancels the request.
*/
ImageRequest.prototype.cancel = function() {
ImageRequestTask.prototype.cancel = function() {
this.cleanup_();

// If downloading has started, then call the callback.
Expand All @@ -582,7 +586,7 @@ ImageRequest.prototype.cancel = function() {
* Cleans up memory used by this request.
* @private
*/
ImageRequest.prototype.cleanup_ = function() {
ImageRequestTask.prototype.cleanup_ = function() {
this.image_.onerror = function() {};
this.image_.onload = function() {};

Expand Down
Loading

0 comments on commit 95a98a7

Please sign in to comment.