Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(274)

Side by Side Diff: chrome/browser/resources/file_manager/js/media/media_util.js

Issue 14623021: Introduce a priority queue to the image loader. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cleaned up. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 'use strict'; 5 'use strict';
6 6
7 /** 7 /**
8 * Loads a thumbnail using provided url. In CANVAS mode, loaded images 8 * Loads a thumbnail using provided url. In CANVAS mode, loaded images
9 * are attached as <canvas> element, while in IMAGE mode as <img>. 9 * are attached as <canvas> element, while in IMAGE mode as <img>.
10 * <canvas> renders faster than <img>, however has bigger memory overhead. 10 * <canvas> renders faster than <img>, however has bigger memory overhead.
11 * 11 *
12 * @param {string} url File URL. 12 * @param {string} url File URL.
13 * @param {ThumbnailLoader.LoaderType=} opt_loaderType Canvas or Image loader, 13 * @param {ThumbnailLoader.LoaderType=} opt_loaderType Canvas or Image loader,
14 * default: IMAGE. 14 * default: IMAGE.
15 * @param {Object=} opt_metadata Metadata object. 15 * @param {Object=} opt_metadata Metadata object.
16 * @param {string=} opt_mediaType Media type. 16 * @param {string=} opt_mediaType Media type.
17 * @param {ThumbnailLoader.UseEmbedded=} opt_useEmbedded If to use embedded 17 * @param {ThumbnailLoader.UseEmbedded=} opt_useEmbedded If to use embedded
18 * jpeg thumbnail if available. Default: USE_EMBEDDED. 18 * jpeg thumbnail if available. Default: USE_EMBEDDED.
19 * @param {number=} opt_priority Priority, the highest is 0. default: 2.
19 * @constructor 20 * @constructor
20 */ 21 */
21 function ThumbnailLoader( 22 function ThumbnailLoader(url, opt_loaderType, opt_metadata, opt_mediaType,
22 url, opt_loaderType, opt_metadata, opt_mediaType, opt_useEmbedded) { 23 opt_useEmbedded, opt_priority) {
23 opt_useEmbedded = opt_useEmbedded || ThumbnailLoader.UseEmbedded.USE_EMBEDDED; 24 opt_useEmbedded = opt_useEmbedded || ThumbnailLoader.UseEmbedded.USE_EMBEDDED;
24 25
25 this.mediaType_ = opt_mediaType || FileType.getMediaType(url); 26 this.mediaType_ = opt_mediaType || FileType.getMediaType(url);
26 this.loaderType_ = opt_loaderType || ThumbnailLoader.LoaderType.IMAGE; 27 this.loaderType_ = opt_loaderType || ThumbnailLoader.LoaderType.IMAGE;
27 this.metadata_ = opt_metadata; 28 this.metadata_ = opt_metadata;
29 this.priority_ = (opt_priority !== undefined) ? opt_priority : 2;
28 30
29 if (!opt_metadata) { 31 if (!opt_metadata) {
30 this.thumbnailUrl_ = url; // Use the URL directly. 32 this.thumbnailUrl_ = url; // Use the URL directly.
31 return; 33 return;
32 } 34 }
33 35
34 this.fallbackUrl_ = null; 36 this.fallbackUrl_ = null;
35 this.thumbnailUrl_ = null; 37 this.thumbnailUrl_ = null;
36 if (opt_metadata.drive) { 38 if (opt_metadata.drive) {
37 var apps = opt_metadata.drive.driveApps; 39 var apps = opt_metadata.drive.driveApps;
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 this.attachImage(box, fillMode); 150 this.attachImage(box, fillMode);
149 if (opt_onSuccess) 151 if (opt_onSuccess)
150 opt_onSuccess(this.image_, this.transform_); 152 opt_onSuccess(this.image_, this.transform_);
151 }.bind(this); 153 }.bind(this);
152 this.image_.onerror = function() { 154 this.image_.onerror = function() {
153 if (opt_onError) 155 if (opt_onError)
154 opt_onError(); 156 opt_onError();
155 if (this.fallbackUrl_) { 157 if (this.fallbackUrl_) {
156 new ThumbnailLoader(this.fallbackUrl_, 158 new ThumbnailLoader(this.fallbackUrl_,
157 this.loaderType_, 159 this.loaderType_,
158 null, 160 null, // No metadata.
159 this.mediaType_). 161 this.mediaType_,
162 undefined, // Default value for use-embedded.
163 this.priority_).
160 load(box, fillMode, opt_onSuccess); 164 load(box, fillMode, opt_onSuccess);
161 } else { 165 } else {
162 box.setAttribute('generic-thumbnail', this.mediaType_); 166 box.setAttribute('generic-thumbnail', this.mediaType_);
163 } 167 }
164 }.bind(this); 168 }.bind(this);
165 169
166 if (this.image_.src) { 170 if (this.image_.src) {
167 console.warn('Thumbnail already loaded: ' + this.thumbnailUrl_); 171 console.warn('Thumbnail already loaded: ' + this.thumbnailUrl_);
168 return; 172 return;
169 } 173 }
170 174
171 // TODO(mtomasz): Smarter calculation of the requested size. 175 // TODO(mtomasz): Smarter calculation of the requested size.
172 var wasAttached = box.ownerDocument.contains(box); 176 var wasAttached = box.ownerDocument.contains(box);
173 var modificationTime = this.metadata_ && 177 var modificationTime = this.metadata_ &&
174 this.metadata_.filesystem && 178 this.metadata_.filesystem &&
175 this.metadata_.filesystem.modificationTime && 179 this.metadata_.filesystem.modificationTime &&
176 this.metadata_.filesystem.modificationTime.getTime(); 180 this.metadata_.filesystem.modificationTime.getTime();
177 this.taskId_ = util.loadImage( 181 this.taskId_ = util.loadImage(
178 this.image_, 182 this.image_,
179 this.thumbnailUrl_, 183 this.thumbnailUrl_,
180 { maxWidth: ThumbnailLoader.THUMBNAIL_MAX_WIDTH, 184 { maxWidth: ThumbnailLoader.THUMBNAIL_MAX_WIDTH,
181 maxHeight: ThumbnailLoader.THUMBNAIL_MAX_HEIGHT, 185 maxHeight: ThumbnailLoader.THUMBNAIL_MAX_HEIGHT,
182 cache: true, 186 cache: true,
187 priority: this.priority_,
183 timestamp: modificationTime }, 188 timestamp: modificationTime },
184 function() { 189 function() {
185 if (opt_optimizationMode == 190 if (opt_optimizationMode ==
186 ThumbnailLoader.OptimizationMode.DISCARD_DETACHED && 191 ThumbnailLoader.OptimizationMode.DISCARD_DETACHED &&
187 !box.ownerDocument.contains(box)) { 192 !box.ownerDocument.contains(box)) {
188 // If the container is not attached, then invalidate the download. 193 // If the container is not attached, then invalidate the download.
189 return false; 194 return false;
190 } 195 }
191 return true; 196 return true;
192 }); 197 });
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 var modificationTime = this.metadata_ && 259 var modificationTime = this.metadata_ &&
255 this.metadata_.filesystem && 260 this.metadata_.filesystem &&
256 this.metadata_.filesystem.modificationTime && 261 this.metadata_.filesystem.modificationTime &&
257 this.metadata_.filesystem.modificationTime.getTime(); 262 this.metadata_.filesystem.modificationTime.getTime();
258 this.taskId_ = util.loadImage( 263 this.taskId_ = util.loadImage(
259 this.image_, 264 this.image_,
260 this.thumbnailUrl_, 265 this.thumbnailUrl_,
261 { maxWidth: ThumbnailLoader.THUMBNAIL_MAX_WIDTH, 266 { maxWidth: ThumbnailLoader.THUMBNAIL_MAX_WIDTH,
262 maxHeight: ThumbnailLoader.THUMBNAIL_MAX_HEIGHT, 267 maxHeight: ThumbnailLoader.THUMBNAIL_MAX_HEIGHT,
263 cache: true, 268 cache: true,
269 priority: this.priority_,
264 timestamp: modificationTime }); 270 timestamp: modificationTime });
265 }; 271 };
266 272
267 /** 273 /**
268 * Attach the image to a given element. 274 * Attach the image to a given element.
269 * @param {Element} container Parent element. 275 * @param {Element} container Parent element.
270 * @param {ThumbnailLoader.FillMode} fillMode Fill mode. 276 * @param {ThumbnailLoader.FillMode} fillMode Fill mode.
271 */ 277 */
272 ThumbnailLoader.prototype.attachImage = function(container, fillMode) { 278 ThumbnailLoader.prototype.attachImage = function(container, fillMode) {
273 if (!this.hasValidImage()) { 279 if (!this.hasValidImage()) {
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 385
380 function percent(fraction) { 386 function percent(fraction) {
381 return (fraction * 100).toFixed(2) + '%'; 387 return (fraction * 100).toFixed(2) + '%';
382 } 388 }
383 389
384 img.style.width = percent(fractionX); 390 img.style.width = percent(fractionX);
385 img.style.height = percent(fractionY); 391 img.style.height = percent(fractionY);
386 img.style.left = percent((1 - fractionX) / 2); 392 img.style.left = percent((1 - fractionX) / 2);
387 img.style.top = percent((1 - fractionY) / 2); 393 img.style.top = percent((1 - fractionY) / 2);
388 }; 394 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698