[sql] Remove _HAS_EXCEPTIONS=0 from build info.
[chromium-blink-merge.git] / chrome / browser / resources / md_downloads / item.js
blob07e5ce2afc1651e9fdce52ff2560e4eae2d9ee85
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 Item = Polymer({
7 is: 'downloads-item',
9 /**
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;
21 properties: {
22 hideDate: {
23 reflectToAttribute: true,
24 type: Boolean,
25 value: false,
28 scrollbarWidth: {
29 type: Number,
30 value: 0,
31 observer: 'onScrollbarWidthChange_',
34 isDangerous_: {
35 type: Boolean,
36 value: false,
39 isIncognito_: {
40 type: Boolean,
41 value: false,
44 /** Only set when |isDangerous| is true. */
45 isMalware_: Boolean,
48 /** @param {!downloads.Data} data */
49 update: function(data) {
50 assert(!this.id_ || data.id == this.id_);
51 this.id_ = data.id; // This is the only thing saved from |data|.
53 this.isIncognito_ = data.otr;
55 // Danger-independent UI and controls.
56 this.ensureTextIs_(this.$.since, data.since_string);
57 this.ensureTextIs_(this.$.date, data.date_string);
59 /** @const */ var noFile =
60 data.state == downloads.States.CANCELLED ||
61 data.state == downloads.States.INTERRUPTED ||
62 data.file_externally_removed;
63 this.$.content.classList.toggle('no-file', noFile);
65 this.ensureTextIs_(this.$.name, data.file_name);
66 this.ensureTextIs_(this.$.url, data.url);
67 this.$.url.href = data.url;
69 // Danger-dependent UI and controls.
70 var dangerText = this.getDangerText_(data);
71 this.isDangerous_ = !!dangerText;
72 this.$.content.classList.toggle('dangerous', this.isDangerous_);
74 var description = dangerText || this.getStatusText_(data);
76 // Status goes in the "tag" (next to the file name) if there's no file.
77 this.ensureTextIs_(this.$.description, noFile ? '' : description);
78 this.ensureTextIs_(this.$.tag, noFile ? description : '');
80 /** @const */ var showProgress =
81 isFinite(data.percent) && !this.isDangerous_;
82 this.$.progress.hidden = !showProgress;
84 if (showProgress) {
85 this.$.progress.indeterminate = data.percent < 0;
86 this.$.progress.value = data.percent;
89 var disableRemove;
91 if (this.isDangerous_) {
92 this.isMalware_ =
93 data.danger_type == downloads.DangerType.DANGEROUS_CONTENT ||
94 data.danger_type == downloads.DangerType.DANGEROUS_HOST ||
95 data.danger_type == downloads.DangerType.DANGEROUS_URL ||
96 data.danger_type == downloads.DangerType.POTENTIALLY_UNWANTED;
97 disableRemove = true;
98 } else {
99 /** @const */ var completelyOnDisk =
100 data.state == downloads.States.COMPLETE &&
101 !data.file_externally_removed;
103 this.$['file-link'].href = data.url;
104 this.ensureTextIs_(this.$['file-link'], data.file_name);
106 this.$['file-link'].hidden = !completelyOnDisk;
107 this.$.name.hidden = completelyOnDisk;
108 this.$.show.hidden = !completelyOnDisk;
110 this.$.retry.hidden = !data.retry;
112 /** @const */ var isInProgress =
113 data.state == downloads.States.IN_PROGRESS;
114 this.$.pause.hidden = !isInProgress;
116 this.$.resume.hidden = !data.resume;
118 /** @const */ var isPaused = data.state == downloads.States.PAUSED;
119 /** @const */ var showCancel = isPaused || isInProgress;
120 this.$.cancel.hidden = !showCancel;
122 disableRemove = showCancel ||
123 !loadTimeData.getBoolean('allowDeletingHistory');
125 /** @const */ var controlledByExtension = data.by_ext_id &&
126 data.by_ext_name;
127 this.$['controlled-by'].hidden = !controlledByExtension;
128 if (controlledByExtension) {
129 var link = this.$['controlled-by'].querySelector('a');
130 link.href = 'chrome://extensions#' + data.by_ext_id;
131 link.setAttribute('column-type', 'controlled-by');
132 link.textContent = data.by_ext_name;
135 var icon = 'chrome://fileicon/' + encodeURIComponent(data.file_path);
136 this.iconLoader_.loadScaledIcon(this.$['file-icon'], icon);
139 this.$.remove.disabled = disableRemove;
143 * Overwrite |el|'s textContent if it differs from |text|.
144 * @param {!Element} el
145 * @param {string} text
146 * @private
148 ensureTextIs_: function(el, text) {
149 if (el.textContent != text)
150 el.textContent = text;
154 * @param {!downloads.Data} data
155 * @return {string} Text describing the danger of a download. Empty if not
156 * dangerous.
158 getDangerText_: function(data) {
159 switch (data.danger_type) {
160 case downloads.DangerType.DANGEROUS_FILE:
161 return loadTimeData.getStringF('dangerFileDesc', data.file_name);
162 case downloads.DangerType.DANGEROUS_URL:
163 return loadTimeData.getString('dangerUrlDesc');
164 case downloads.DangerType.DANGEROUS_CONTENT: // Fall through.
165 case downloads.DangerType.DANGEROUS_HOST:
166 return loadTimeData.getStringF('dangerContentDesc', data.file_name);
167 case downloads.DangerType.UNCOMMON_CONTENT:
168 return loadTimeData.getStringF('dangerUncommonDesc', data.file_name);
169 case downloads.DangerType.POTENTIALLY_UNWANTED:
170 return loadTimeData.getStringF('dangerSettingsDesc', data.file_name);
171 default:
172 return '';
177 * @param {!downloads.Data} data
178 * @return {string} User-visible status update text.
179 * @private
181 getStatusText_: function(data) {
182 switch (data.state) {
183 case downloads.States.IN_PROGRESS:
184 case downloads.States.PAUSED: // Fallthrough.
185 assert(typeof data.progress_status_text == 'string');
186 return data.progress_status_text;
187 case downloads.States.CANCELLED:
188 return loadTimeData.getString('statusCancelled');
189 case downloads.States.DANGEROUS:
190 break; // Intentionally hit assertNotReached(); at bottom.
191 case downloads.States.INTERRUPTED:
192 assert(typeof data.last_reason_text == 'string');
193 return data.last_reason_text;
194 case downloads.States.COMPLETE:
195 return data.file_externally_removed ?
196 loadTimeData.getString('statusRemoved') : '';
198 assertNotReached();
199 return '';
202 /** @private */
203 onCancelClick_: function() {
204 this.actionService_.cancel(this.id_);
208 * @private
209 * @param {Event} e
211 onDragStart_: function(e) {
212 e.preventDefault();
213 this.actionService_.drag(this.id_);
217 * @param {Event} e
218 * @private
220 onFileLinkClick_: function(e) {
221 e.preventDefault();
222 this.actionService_.openFile(this.id_);
225 /** @private */
226 onPauseClick_: function() {
227 this.actionService_.pause(this.id_);
230 /** @private */
231 onRemoveClick_: function() {
232 assert(!this.$.remove.disabled);
233 this.actionService_.remove(this.id_);
236 /** @private */
237 onSaveDangerous_: function() {
238 this.actionService_.saveDangerous(this.id_);
241 /** @private */
242 onDiscardDangerous_: function() {
243 this.actionService_.discardDangerous(this.id_);
246 /** @private */
247 onResumeClick_: function() {
248 this.actionService_.resume(this.id_);
251 /** @private */
252 onRetryClick_: function() {
253 this.actionService_.download(this.$['file-link'].href);
256 /** @private */
257 onScrollbarWidthChange_: function() {
258 if (!this.$)
259 return;
261 var endCap = this.$['end-cap'];
262 endCap.style.flexBasis = '';
264 if (this.scrollbarWidth) {
265 var basis = parseInt(getComputedStyle(endCap).flexBasis, 10);
266 endCap.style.flexBasis = basis - this.scrollbarWidth + 'px';
270 /** @private */
271 onShowClick_: function() {
272 this.actionService_.show(this.id_);
276 return {Item: Item};