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() {
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,
31 observer
: 'onScrollbarWidthChange_',
44 /** Only set when |isDangerous| is true. */
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
;
85 this.$.progress
.indeterminate
= data
.percent
< 0;
86 this.$.progress
.value
= data
.percent
;
91 if (this.isDangerous_
) {
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
;
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
&&
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
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
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
);
177 * @param {!downloads.Data} data
178 * @return {string} User-visible status update text.
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') : '';
203 onCancelClick_: function() {
204 this.actionService_
.cancel(this.id_
);
211 onDragStart_: function(e
) {
213 this.actionService_
.drag(this.id_
);
220 onFileLinkClick_: function(e
) {
222 this.actionService_
.openFile(this.id_
);
226 onPauseClick_: function() {
227 this.actionService_
.pause(this.id_
);
231 onRemoveClick_: function() {
232 assert(!this.$.remove
.disabled
);
233 this.actionService_
.remove(this.id_
);
237 onSaveDangerous_: function() {
238 this.actionService_
.saveDangerous(this.id_
);
242 onDiscardDangerous_: function() {
243 this.actionService_
.discardDangerous(this.id_
);
247 onResumeClick_: function() {
248 this.actionService_
.resume(this.id_
);
252 onRetryClick_: function() {
253 this.actionService_
.download(this.$['file-link'].href
);
257 onScrollbarWidthChange_: function() {
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';
271 onShowClick_: function() {
272 this.actionService_
.show(this.id_
);