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

Side by Side Diff: chrome/browser/resources/downloads/downloads.js

Issue 442133002: Typecheck chrome://downloads using CompilerPass.java (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@true_master
Patch Set: Created 6 years, 4 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
« no previous file with comments | « no previous file | chrome/browser/ui/webui/downloads_dom_handler.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 // TODO(jhawkins): Use hidden instead of showInline* and display:none. 5 // TODO(jhawkins): Use hidden instead of showInline* and display:none.
6 6
7 /** 7 /**
8 * The type of the download object. The definition is based on
9 * chrome/browser/ui/webui/downloads_dom_handler.cc:CreateDownloadItemValue()
10 * @typedef {{by_ext_id: (string|undefined),
11 * by_ext_name: (string|undefined),
12 * danger_type: (string|undefined),
13 * date_string: string,
14 * file_externally_removed: boolean,
15 * file_name: string,
16 * file_path: string,
17 * file_url: string,
18 * id: string,
19 * last_reason_text: (string|undefined),
20 * otr: boolean,
21 * percent: (number|undefined),
22 * progress_status_text: (string|undefined),
23 * received: (number|undefined),
24 * resume: boolean,
25 * retry: boolean,
26 * since_string: string,
27 * started: number,
28 * state: string,
29 * total: number,
30 * url: string}}
31 */
32 var BackendDownloadObject;
33
34 /**
8 * Sets the display style of a node. 35 * Sets the display style of a node.
9 * @param {!Element} node The target element to show or hide. 36 * @param {!Element} node The target element to show or hide.
10 * @param {boolean} isShow Should the target element be visible. 37 * @param {boolean} isShow Should the target element be visible.
11 */ 38 */
12 function showInline(node, isShow) { 39 function showInline(node, isShow) {
13 node.style.display = isShow ? 'inline' : 'none'; 40 node.style.display = isShow ? 'inline' : 'none';
14 } 41 }
15 42
16 /** 43 /**
17 * Sets the display style of a node. 44 * Sets the display style of a node.
18 * @param {!Element} node The target element to show or hide. 45 * @param {!Element} node The target element to show or hide.
19 * @param {boolean} isShow Should the target element be visible. 46 * @param {boolean} isShow Should the target element be visible.
20 */ 47 */
21 function showInlineBlock(node, isShow) { 48 function showInlineBlock(node, isShow) {
22 node.style.display = isShow ? 'inline-block' : 'none'; 49 node.style.display = isShow ? 'inline-block' : 'none';
23 } 50 }
24 51
25 /** 52 /**
26 * Creates a link with a specified onclick handler and content. 53 * Creates a link with a specified onclick handler and content.
27 * @param {function()} onclick The onclick handler. 54 * @param {function()} onclick The onclick handler.
28 * @param {string} value The link text. 55 * @param {string} value The link text.
29 * @return {Element} The created link element. 56 * @return {!Element} The created link element.
30 */ 57 */
31 function createLink(onclick, value) { 58 function createLink(onclick, value) {
32 var link = document.createElement('a'); 59 var link = document.createElement('a');
33 link.onclick = onclick; 60 link.onclick = onclick;
34 link.href = '#'; 61 link.href = '#';
35 link.textContent = value; 62 link.textContent = value;
36 link.oncontextmenu = function() { return false; }; 63 link.oncontextmenu = function() { return false; };
37 return link; 64 return link;
38 } 65 }
39 66
(...skipping 11 matching lines...) Expand all
51 return button; 78 return button;
52 } 79 }
53 80
54 /////////////////////////////////////////////////////////////////////////////// 81 ///////////////////////////////////////////////////////////////////////////////
55 // Downloads 82 // Downloads
56 /** 83 /**
57 * Class to hold all the information about the visible downloads. 84 * Class to hold all the information about the visible downloads.
58 * @constructor 85 * @constructor
59 */ 86 */
60 function Downloads() { 87 function Downloads() {
88 /**
89 * @type {!Object.<string, Download>}
90 * @private
91 */
61 this.downloads_ = {}; 92 this.downloads_ = {};
62 this.node_ = $('downloads-display'); 93 this.node_ = $('downloads-display');
63 this.summary_ = $('downloads-summary-text'); 94 this.summary_ = $('downloads-summary-text');
64 this.searchText_ = ''; 95 this.searchText_ = '';
65 96
66 // Keep track of the dates of the newest and oldest downloads so that we 97 // Keep track of the dates of the newest and oldest downloads so that we
67 // know where to insert them. 98 // know where to insert them.
68 this.newestTime_ = -1; 99 this.newestTime_ = -1;
69 100
70 // Icon load request queue. 101 // Icon load request queue.
71 this.iconLoadQueue_ = []; 102 this.iconLoadQueue_ = [];
72 this.isIconLoading_ = false; 103 this.isIconLoading_ = false;
73 } 104 }
74 105
75 /** 106 /**
76 * Called when a download has been updated or added. 107 * Called when a download has been updated or added.
77 * @param {Object} download A backend download object (see downloads_ui.cc) 108 * @param {BackendDownloadObject} download A backend download object
78 */ 109 */
79 Downloads.prototype.updated = function(download) { 110 Downloads.prototype.updated = function(download) {
80 var id = download.id; 111 var id = download.id;
81 if (!!this.downloads_[id]) { 112 if (!!this.downloads_[id]) {
82 this.downloads_[id].update(download); 113 this.downloads_[id].update(download);
83 } else { 114 } else {
84 this.downloads_[id] = new Download(download); 115 this.downloads_[id] = new Download(download);
85 // We get downloads in display order, so we don't have to worry about 116 // We get downloads in display order, so we don't have to worry about
86 // maintaining correct order - we can assume that any downloads not in 117 // maintaining correct order - we can assume that any downloads not in
87 // display order are new ones and so we can add them to the top of the 118 // display order are new ones and so we can add them to the top of the
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 153
123 var hasDownloads = false; 154 var hasDownloads = false;
124 for (var i in this.downloads_) { 155 for (var i in this.downloads_) {
125 hasDownloads = true; 156 hasDownloads = true;
126 break; 157 break;
127 } 158 }
128 }; 159 };
129 160
130 /** 161 /**
131 * Returns the number of downloads in the model. Used by tests. 162 * Returns the number of downloads in the model. Used by tests.
132 * @return {integer} Returns the number of downloads shown on the page. 163 * @return {number} Returns the number of downloads shown on the page.
133 */ 164 */
134 Downloads.prototype.size = function() { 165 Downloads.prototype.size = function() {
135 return Object.keys(this.downloads_).length; 166 return Object.keys(this.downloads_).length;
136 }; 167 };
137 168
138 /** 169 /**
139 * Update the date visibility in our nodes so that no date is 170 * Update the date visibility in our nodes so that no date is
140 * repeated. 171 * repeated.
141 * @private 172 * @private
142 */ 173 */
143 Downloads.prototype.updateDateDisplay_ = function() { 174 Downloads.prototype.updateDateDisplay_ = function() {
144 var dateContainers = document.getElementsByClassName('date-container'); 175 var dateContainers = document.getElementsByClassName('date-container');
145 var displayed = {}; 176 var displayed = {};
146 for (var i = 0, container; container = dateContainers[i]; i++) { 177 for (var i = 0, container; container = dateContainers[i]; i++) {
147 var dateString = container.getElementsByClassName('date')[0].innerHTML; 178 var dateString = container.getElementsByClassName('date')[0].innerHTML;
148 if (!!displayed[dateString]) { 179 if (!!displayed[dateString]) {
149 container.style.display = 'none'; 180 container.style.display = 'none';
150 } else { 181 } else {
151 displayed[dateString] = true; 182 displayed[dateString] = true;
152 container.style.display = 'block'; 183 container.style.display = 'block';
153 } 184 }
154 } 185 }
155 }; 186 };
156 187
157 /** 188 /**
158 * Remove a download. 189 * Remove a download.
159 * @param {number} id The id of the download to remove. 190 * @param {string} id The id of the download to remove.
160 */ 191 */
161 Downloads.prototype.remove = function(id) { 192 Downloads.prototype.remove = function(id) {
162 this.node_.removeChild(this.downloads_[id].node); 193 this.node_.removeChild(this.downloads_[id].node);
163 delete this.downloads_[id]; 194 delete this.downloads_[id];
164 this.updateDateDisplay_(); 195 this.updateDateDisplay_();
165 }; 196 };
166 197
167 /** 198 /**
168 * Clear all downloads and reset us back to a null state. 199 * Clear all downloads and reset us back to a null state.
169 */ 200 */
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 if (!this.downloads_[downloads[i].id]) 257 if (!this.downloads_[downloads[i].id])
227 return true; 258 return true;
228 } 259 }
229 return false; 260 return false;
230 }; 261 };
231 262
232 /////////////////////////////////////////////////////////////////////////////// 263 ///////////////////////////////////////////////////////////////////////////////
233 // Download 264 // Download
234 /** 265 /**
235 * A download and the DOM representation for that download. 266 * A download and the DOM representation for that download.
236 * @param {Object} download A backend download object (see downloads_ui.cc) 267 * @param {BackendDownloadObject} download A backend download object
237 * @constructor 268 * @constructor
238 */ 269 */
239 function Download(download) { 270 function Download(download) {
240 // Create DOM 271 // Create DOM
241 this.node = createElementWithClassName( 272 this.node = createElementWithClassName(
242 'div', 'download' + (download.otr ? ' otr' : '')); 273 'div', 'download' + (download.otr ? ' otr' : ''));
243 274
244 // Dates 275 // Dates
245 this.dateContainer_ = createElementWithClassName('div', 'date-container'); 276 this.dateContainer_ = createElementWithClassName('div', 'date-container');
246 this.node.appendChild(this.dateContainer_); 277 this.node.appendChild(this.dateContainer_);
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 loadTimeData.getString('danger_discard')); 411 loadTimeData.getString('danger_discard'));
381 this.danger_.appendChild(this.dangerDiscard_); 412 this.danger_.appendChild(this.dangerDiscard_);
382 413
383 // Update member vars. 414 // Update member vars.
384 this.update(download); 415 this.update(download);
385 } 416 }
386 417
387 /** 418 /**
388 * The states a download can be in. These correspond to states defined in 419 * The states a download can be in. These correspond to states defined in
389 * DownloadsDOMHandler::CreateDownloadItemValue 420 * DownloadsDOMHandler::CreateDownloadItemValue
421 * @enum {string}
390 */ 422 */
391 Download.States = { 423 Download.States = {
392 IN_PROGRESS: 'IN_PROGRESS', 424 IN_PROGRESS: 'IN_PROGRESS',
393 CANCELLED: 'CANCELLED', 425 CANCELLED: 'CANCELLED',
394 COMPLETE: 'COMPLETE', 426 COMPLETE: 'COMPLETE',
395 PAUSED: 'PAUSED', 427 PAUSED: 'PAUSED',
396 DANGEROUS: 'DANGEROUS', 428 DANGEROUS: 'DANGEROUS',
397 INTERRUPTED: 'INTERRUPTED', 429 INTERRUPTED: 'INTERRUPTED',
398 }; 430 };
399 431
400 /** 432 /**
401 * Explains why a download is in DANGEROUS state. 433 * Explains why a download is in DANGEROUS state.
434 * @enum {string}
402 */ 435 */
403 Download.DangerType = { 436 Download.DangerType = {
404 NOT_DANGEROUS: 'NOT_DANGEROUS', 437 NOT_DANGEROUS: 'NOT_DANGEROUS',
405 DANGEROUS_FILE: 'DANGEROUS_FILE', 438 DANGEROUS_FILE: 'DANGEROUS_FILE',
406 DANGEROUS_URL: 'DANGEROUS_URL', 439 DANGEROUS_URL: 'DANGEROUS_URL',
407 DANGEROUS_CONTENT: 'DANGEROUS_CONTENT', 440 DANGEROUS_CONTENT: 'DANGEROUS_CONTENT',
408 UNCOMMON_CONTENT: 'UNCOMMON_CONTENT', 441 UNCOMMON_CONTENT: 'UNCOMMON_CONTENT',
409 DANGEROUS_HOST: 'DANGEROUS_HOST', 442 DANGEROUS_HOST: 'DANGEROUS_HOST',
410 POTENTIALLY_UNWANTED: 'POTENTIALLY_UNWANTED', 443 POTENTIALLY_UNWANTED: 'POTENTIALLY_UNWANTED',
411 }; 444 };
412 445
413 /** 446 /**
414 * @param {number} a Some float. 447 * @param {number} a Some float.
415 * @param {number} b Some float. 448 * @param {number} b Some float.
416 * @param {number} opt_pct Percent of min(a,b). 449 * @param {number=} opt_pct Percent of min(a,b).
417 * @return {boolean} true if a is within opt_pct percent of b. 450 * @return {boolean} true if a is within opt_pct percent of b.
418 */ 451 */
419 function floatEq(a, b, opt_pct) { 452 function floatEq(a, b, opt_pct) {
420 return Math.abs(a - b) < (Math.min(a, b) * (opt_pct || 1.0) / 100.0); 453 return Math.abs(a - b) < (Math.min(a, b) * (opt_pct || 1.0) / 100.0);
421 } 454 }
422 455
423 /** 456 /**
424 * Constants and "constants" for the progress meter. 457 * Constants and "constants" for the progress meter.
425 */ 458 */
426 Download.Progress = { 459 Download.Progress = {
(...skipping 15 matching lines...) Expand all
442 Download.Progress.height = Download.Progress.SIDE * Download.Progress.scale; 475 Download.Progress.height = Download.Progress.SIDE * Download.Progress.scale;
443 Download.Progress.radius = Download.Progress.HALF * Download.Progress.scale; 476 Download.Progress.radius = Download.Progress.HALF * Download.Progress.scale;
444 Download.Progress.centerX = Download.Progress.HALF * Download.Progress.scale; 477 Download.Progress.centerX = Download.Progress.HALF * Download.Progress.scale;
445 Download.Progress.centerY = Download.Progress.HALF * Download.Progress.scale; 478 Download.Progress.centerY = Download.Progress.HALF * Download.Progress.scale;
446 } 479 }
447 computeDownloadProgress(); 480 computeDownloadProgress();
448 481
449 // Listens for when device-pixel-ratio changes between any zoom level. 482 // Listens for when device-pixel-ratio changes between any zoom level.
450 [0.3, 0.4, 0.6, 0.7, 0.8, 0.95, 1.05, 1.2, 1.4, 1.6, 1.9, 2.2, 2.7, 3.5, 4.5 483 [0.3, 0.4, 0.6, 0.7, 0.8, 0.95, 1.05, 1.2, 1.4, 1.6, 1.9, 2.2, 2.7, 3.5, 4.5
451 ].forEach(function(scale) { 484 ].forEach(function(scale) {
452 matchMedia('(-webkit-min-device-pixel-ratio:' + scale + ')').addListener( 485 window.matchMedia('(-webkit-min-device-pixel-ratio:' + scale + ')')
486 .addListener(
Dan Beam 2014/08/05 23:14:33 var media = '(-webkit-min-device-pixel-ratio:' + s
Vitaly Pavlenko 2014/08/06 00:28:53 Done.
453 function() { 487 function() {
454 computeDownloadProgress(); 488 computeDownloadProgress();
455 }); 489 });
456 }); 490 });
457 491
458 var ImageCache = {}; 492 var ImageCache = {};
459 function getCachedImage(src) { 493 function getCachedImage(src) {
460 if (!ImageCache[src]) { 494 if (!ImageCache[src]) {
461 ImageCache[src] = new Image(); 495 ImageCache[src] = new Image();
462 ImageCache[src].src = src; 496 ImageCache[src].src = src;
463 } 497 }
464 return ImageCache[src]; 498 return ImageCache[src];
465 } 499 }
466 500
467 /** 501 /**
468 * Updates the download to reflect new data. 502 * Updates the download to reflect new data.
469 * @param {Object} download A backend download object (see downloads_ui.cc) 503 * @param {BackendDownloadObject} download A backend download object
470 */ 504 */
471 Download.prototype.update = function(download) { 505 Download.prototype.update = function(download) {
472 this.id_ = download.id; 506 this.id_ = download.id;
473 this.filePath_ = download.file_path; 507 this.filePath_ = download.file_path;
474 this.fileUrl_ = download.file_url; 508 this.fileUrl_ = download.file_url;
475 this.fileName_ = download.file_name; 509 this.fileName_ = download.file_name;
476 this.url_ = download.url; 510 this.url_ = download.url;
477 this.state_ = download.state; 511 this.state_ = download.state;
478 this.fileExternallyRemoved_ = download.file_externally_removed; 512 this.fileExternallyRemoved_ = download.file_externally_removed;
479 this.dangerType_ = download.danger_type; 513 this.dangerType_ = download.danger_type;
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
707 case Download.States.DANGEROUS: 741 case Download.States.DANGEROUS:
708 // danger_url_desc is also used by DANGEROUS_CONTENT. 742 // danger_url_desc is also used by DANGEROUS_CONTENT.
709 var desc = this.dangerType_ == Download.DangerType.DANGEROUS_FILE ? 743 var desc = this.dangerType_ == Download.DangerType.DANGEROUS_FILE ?
710 'danger_file_desc' : 'danger_url_desc'; 744 'danger_file_desc' : 'danger_url_desc';
711 return loadTimeData.getString(desc); 745 return loadTimeData.getString(desc);
712 case Download.States.INTERRUPTED: 746 case Download.States.INTERRUPTED:
713 return this.lastReasonDescription_; 747 return this.lastReasonDescription_;
714 case Download.States.COMPLETE: 748 case Download.States.COMPLETE:
715 return this.fileExternallyRemoved_ ? 749 return this.fileExternallyRemoved_ ?
716 loadTimeData.getString('status_removed') : ''; 750 loadTimeData.getString('status_removed') : '';
751 default:
752 assertNotReached();
753 return '';
717 } 754 }
Dan Beam 2014/08/05 23:14:33 nit: assertNotReached(); return ''; after th
Vitaly Pavlenko 2014/08/06 00:29:25 Done.
718 }; 755 };
719 756
720 /** 757 /**
721 * Tells the backend to initiate a drag, allowing users to drag 758 * Tells the backend to initiate a drag, allowing users to drag
722 * files from the download page and have them appear as native file 759 * files from the download page and have them appear as native file
723 * drags. 760 * drags.
724 * @return {boolean} Returns false to prevent the default action. 761 * @return {boolean} Returns false to prevent the default action.
725 * @private 762 * @private
726 */ 763 */
727 Download.prototype.drag_ = function() { 764 Download.prototype.drag_ = function() {
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
859 openDownloadsFolderLink.oncontextmenu = function() { return false; }; 896 openDownloadsFolderLink.oncontextmenu = function() { return false; };
860 897
861 $('search-link').onclick = function(e) { 898 $('search-link').onclick = function(e) {
862 setSearch(''); 899 setSearch('');
863 e.preventDefault(); 900 e.preventDefault();
864 $('term').value = ''; 901 $('term').value = '';
865 return false; 902 return false;
866 }; 903 };
867 904
868 $('term').onsearch = function(e) { 905 $('term').onsearch = function(e) {
869 setSearch(this.value); 906 setSearch($('term').value);
870 }; 907 };
871 } 908 }
872 909
873 function setSearch(searchText) { 910 function setSearch(searchText) {
874 fifoResults.length = 0; 911 fifoResults.length = 0;
875 downloads.setSearchText(searchText); 912 downloads.setSearchText(searchText);
876 searchText = searchText.toString().match(/(?:[^\s"]+|"[^"]*")+/g); 913 searchText = searchText.toString().match(/(?:[^\s"]+|"[^"]*")+/g);
877 if (searchText) { 914 if (searchText) {
878 searchText = searchText.map(function(term) { 915 searchText = searchText.map(function(term) {
879 // strip quotes 916 // strip quotes
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
942 if (Date.now() - start > 50) { 979 if (Date.now() - start > 50) {
943 clearTimeout(resultsTimeout); 980 clearTimeout(resultsTimeout);
944 resultsTimeout = setTimeout(tryDownloadUpdatedPeriodically, 5); 981 resultsTimeout = setTimeout(tryDownloadUpdatedPeriodically, 5);
945 break; 982 break;
946 } 983 }
947 } 984 }
948 } 985 }
949 986
950 // Add handlers to HTML elements. 987 // Add handlers to HTML elements.
951 window.addEventListener('DOMContentLoaded', load); 988 window.addEventListener('DOMContentLoaded', load);
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/ui/webui/downloads_dom_handler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698