1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 cr.define('downloads', function() {
6 var ItemView = Polymer({
7 is: 'downloads-item-view',
10 * @param {!downloads.ThrottledIconLoader} iconLoader
11 * @param {!downloads.ActionService} actionService
13 factoryImpl: function(iconLoader, actionService) {
14 /** @private {!downloads.ThrottledIconLoader} */
15 this.iconLoader_ = iconLoader;
17 /** @private {!downloads.ActionService} */
18 this.actionService_ = actionService;
23 reflectToAttribute: true,
28 isDangerous_: {type: Boolean, value: false},
30 /** Only set when |isDangerous| is true. */
34 /** @param {!downloads.Data} data */
35 update: function(data) {
36 assert(!this.id_ || data.id == this.id_);
37 this.id_ = data.id; // This is the only thing saved from |data|.
39 this.ensureTextIs_(this.$.since, data.since_string);
40 this.ensureTextIs_(this.$.date, data.date_string);
42 var dangerText = this.getDangerText_(data);
43 this.isDangerous = !!dangerText;
46 this.ensureTextIs_(this.$.description, dangerText);
48 var dangerType = data.danger_type;
49 var dangerousFile = dangerType == downloads.DangerType.DANGEROUS_FILE;
50 this.$.description.classList.toggle('malware', !dangerousFile);
52 var idr = dangerousFile ? 'IDR_WARNING' : 'IDR_SAFEBROWSING_WARNING';
53 var iconUrl = 'chrome://theme/' + idr;
54 this.iconLoader_.loadScaledIcon(this.$['dangerous-icon'], iconUrl);
57 dangerType == downloads.DangerType.DANGEROUS_CONTENT ||
58 dangerType == downloads.DangerType.DANGEROUS_HOST ||
59 dangerType == downloads.DangerType.DANGEROUS_URL ||
60 dangerType == downloads.DangerType.POTENTIALLY_UNWANTED;
62 var iconUrl = 'chrome://fileicon/' + encodeURIComponent(data.file_path);
63 this.iconLoader_.loadScaledIcon(this.$['safe-icon'], iconUrl);
65 /** @const */ var noFile =
66 data.state == downloads.States.CANCELLED ||
67 data.state == downloads.States.INTERRUPTED ||
68 data.file_externally_removed;
69 this.$.safe.classList.toggle('no-file', noFile);
71 /** @const */ var completelyOnDisk =
72 data.state == downloads.States.COMPLETE &&
73 !data.file_externally_removed;
75 this.$['file-link'].href = data.url;
76 this.ensureTextIs_(this.$['file-link'], data.file_name);
77 this.$['file-link'].hidden = !completelyOnDisk;
79 this.ensureTextIs_(this.$.name, data.file_name);
80 this.$.name.hidden = completelyOnDisk;
82 this.$.show.hidden = !completelyOnDisk;
84 this.$.retry.hidden = !data.retry;
86 /** @const */ var isInProgress =
87 data.state == downloads.States.IN_PROGRESS;
88 this.$.pause.hidden = !isInProgress;
90 this.$.resume.hidden = !data.resume;
92 /** @const */ var isPaused = data.state == downloads.States.PAUSED;
93 /** @const */ var showCancel = isPaused || isInProgress;
94 this.$.cancel.hidden = !showCancel;
96 this.$['safe-remove'].hidden = showCancel ||
97 !loadTimeData.getBoolean('allowDeletingHistory');
99 /** @const */ var controlledByExtension = data.by_ext_id &&
101 this.$['controlled-by'].hidden = !controlledByExtension;
102 if (controlledByExtension) {
103 var link = this.$['controlled-by'].querySelector('a');
104 link.href = 'chrome://extensions#' + data.by_ext_id;
105 link.setAttribute('column-type', 'controlled-by');
106 link.textContent = data.by_ext_name;
109 this.ensureTextIs_(this.$['src-url'], data.url);
110 this.$['src-url'].href = data.url;
112 // TODO(dbeam): "Cancelled" should show status next to the file name.
113 this.ensureTextIs_(this.$.status, this.getStatusText_(data));
115 /** @const */ var hasPercent = isFinite(data.percent);
116 this.$.progress.hidden = !hasPercent;
119 this.$.progress.indeterminate = data.percent < 0;
120 this.$.progress.value = data.percent;
126 * Overwrite |el|'s textContent if it differs from |text|.
127 * @param {!Element} el
128 * @param {string} text
131 ensureTextIs_: function(el, text) {
132 if (el.textContent != text)
133 el.textContent = text;
137 * @param {!downloads.Data} data
138 * @return {string} Text describing the danger of a download. Empty if not
141 getDangerText_: function(data) {
142 switch (data.danger_type) {
143 case downloads.DangerType.DANGEROUS_FILE:
144 return loadTimeData.getStringF('dangerFileDesc', data.file_name);
145 case downloads.DangerType.DANGEROUS_URL:
146 return loadTimeData.getString('dangerUrlDesc');
147 case downloads.DangerType.DANGEROUS_CONTENT: // Fall through.
148 case downloads.DangerType.DANGEROUS_HOST:
149 return loadTimeData.getStringF('dangerContentDesc', data.file_name);
150 case downloads.DangerType.UNCOMMON_CONTENT:
151 return loadTimeData.getStringF('dangerUncommonDesc', data.file_name);
152 case downloads.DangerType.POTENTIALLY_UNWANTED:
153 return loadTimeData.getStringF('dangerSettingsDesc', data.file_name);
160 * @param {!downloads.Data} data
161 * @return {string} User-visible status update text.
164 getStatusText_: function(data) {
165 switch (data.state) {
166 case downloads.States.IN_PROGRESS:
167 case downloads.States.PAUSED: // Fallthrough.
168 assert(typeof data.progress_status_text == 'string');
169 return data.progress_status_text;
170 case downloads.States.CANCELLED:
171 return loadTimeData.getString('statusCancelled');
172 case downloads.States.DANGEROUS:
173 break; // Intentionally hit assertNotReached(); at bottom.
174 case downloads.States.INTERRUPTED:
175 assert(typeof data.last_reason_text == 'string');
176 return data.last_reason_text;
177 case downloads.States.COMPLETE:
178 return data.file_externally_removed ?
179 loadTimeData.getString('statusRemoved') : '';
186 onCancelClick_: function() {
187 this.actionService_.cancel(this.id_);
191 onDangerousRemoveOrDiscardClick_: function() {
192 this.actionService_.discardDangerous(this.id_);
199 onDragStart_: function(e) {
201 this.actionService_.drag(this.id_);
208 onFileLinkClick_: function(e) {
210 this.actionService_.openFile(this.id_);
214 onPauseClick_: function() {
215 this.actionService_.pause(this.id_);
219 onRemoveClick_: function() {
220 this.actionService_.remove(this.id_);
224 onRestoreOrSaveClick_: function() {
225 this.actionService_.saveDangerous(this.id_);
229 onResumeClick_: function() {
230 this.actionService_.resume(this.id_);
233 onRetryClick_: function() {
234 this.actionService_.download(this.$['file-link'].href);
238 onShowClick_: function() {
239 this.actionService_.show(this.id_);
243 return {ItemView: ItemView};