Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / resources / md_downloads / item.js
blob83d412d01188cdb274d566c3b4de212fcfbce3cc
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
12      */
13     factoryImpl: function(iconLoader, actionService) {
14       /** @private {!downloads.ThrottledIconLoader} */
15       this.iconLoader_ = iconLoader;
17       /** @private {!downloads.ActionService} */
18       this.actionService_ = actionService;
19     },
21     properties: {
22       hideDate: {
23         type: Boolean,
24         value: true,
25       },
27       readyPromise: {
28         type: Object,
29         value: function() {
30           return new Promise(function(resolve, reject) {
31             this.resolveReadyPromise_ = resolve;
32           }.bind(this));
33         },
34       },
36       completelyOnDisk_: {
37         computed: 'computeCompletelyOnDisk_(' +
38             'data_.state, data_.file_externally_removed)',
39         type: Boolean,
40         value: true,
41       },
43       i18n_: {
44         readOnly: true,
45         type: Object,
46         value: function() {
47           return {
48             cancel: loadTimeData.getString('controlCancel'),
49             discard: loadTimeData.getString('dangerDiscard'),
50             pause: loadTimeData.getString('controlPause'),
51             remove: loadTimeData.getString('controlRemoveFromList'),
52             resume: loadTimeData.getString('controlResume'),
53             restore: loadTimeData.getString('dangerRestore'),
54             retry: loadTimeData.getString('controlRetry'),
55             save: loadTimeData.getString('dangerSave'),
56             show: loadTimeData.getString('controlShowInFolder'),
57           };
58         },
59       },
61       isActive_: {
62         computed: 'computeIsActive_(' +
63             'data_.state, data_.file_externally_removed)',
64         type: Boolean,
65         value: true,
66       },
68       isDangerous_: {
69         computed: 'computeIsDangerous_(data_.state)',
70         type: Boolean,
71         value: false,
72       },
74       isInProgress_: {
75         computed: 'computeIsInProgress_(data_.state)',
76         type: Boolean,
77         value: false,
78       },
80       showCancel_: {
81         computed: 'computeShowCancel_(data_.state)',
82         type: Boolean,
83         value: false,
84       },
86       showProgress_: {
87         computed: 'computeShowProgress_(showCancel_, data_.percent)',
88         type: Boolean,
89         value: false,
90       },
92       isMalware_: {
93         computed: 'computeIsMalware_(isDangerous_, data_.danger_type)',
94         type: Boolean,
95         value: false,
96       },
98       data_: {
99         type: Object,
100       },
101     },
103     observers: [
104       'observeControlledBy_(data_.by_ext_id, data_.by_ext_name)',
105     ],
107     ready: function() {
108       this.content = this.$.content;
109       this.resolveReadyPromise_();
110     },
112     /** @param {!downloads.Data} data */
113     update: function(data) {
114       this.data_ = data;
116       if (!this.isDangerous_) {
117         var icon = 'chrome://fileicon/' + encodeURIComponent(data.file_path);
118         this.iconLoader_.loadScaledIcon(this.$['file-icon'], icon);
119       }
120     },
122     /** @private */
123     computeClass_: function() {
124       var classes = [];
126       if (this.isActive_)
127         classes.push('is-active');
129       if (this.isDangerous_)
130         classes.push('dangerous');
132       if (this.showProgress_)
133         classes.push('show-progress');
135       return classes.join(' ');
136     },
138     /** @private */
139     computeCompletelyOnDisk_: function() {
140       return this.data_.state == downloads.States.COMPLETE &&
141              !this.data_.file_externally_removed;
142     },
144     /** @private */
145     computeDate_: function() {
146       if (this.hideDate)
147         return '';
148       return assert(this.data_.since_string || this.data_.date_string);
149     },
151     /** @private */
152     computeDescription_: function() {
153       var data = this.data_;
155       switch (data.state) {
156         case downloads.States.DANGEROUS:
157           var fileName = data.file_name;
158           switch (data.danger_type) {
159             case downloads.DangerType.DANGEROUS_FILE:
160               return loadTimeData.getStringF('dangerFileDesc', fileName);
161             case downloads.DangerType.DANGEROUS_URL:
162               return loadTimeData.getString('dangerUrlDesc');
163             case downloads.DangerType.DANGEROUS_CONTENT:  // Fall through.
164             case downloads.DangerType.DANGEROUS_HOST:
165               return loadTimeData.getStringF('dangerContentDesc', fileName);
166             case downloads.DangerType.UNCOMMON_CONTENT:
167               return loadTimeData.getStringF('dangerUncommonDesc', fileName);
168             case downloads.DangerType.POTENTIALLY_UNWANTED:
169               return loadTimeData.getStringF('dangerSettingsDesc', fileName);
170           }
171           break;
173         case downloads.States.IN_PROGRESS:
174         case downloads.States.PAUSED:  // Fallthrough.
175           return data.progress_status_text;
176       }
178       return '';
179     },
181     /** @private */
182     computeElevation_: function() {
183       return this.isActive_ ? 1 : 0;
184     },
186     /** @private */
187     computeIsActive_: function() {
188       return this.data_.state != downloads.States.CANCELLED &&
189              this.data_.state != downloads.States.INTERRUPTED &&
190              !this.data_.file_externally_removed;
191     },
193     /** @private */
194     computeIsDangerous_: function() {
195       return this.data_.state == downloads.States.DANGEROUS;
196     },
198     /** @private */
199     computeIsInProgress_: function() {
200       return this.data_.state == downloads.States.IN_PROGRESS;
201     },
203     /** @private */
204     computeIsMalware_: function() {
205       return this.isDangerous_ &&
206           (this.data_.danger_type == downloads.DangerType.DANGEROUS_CONTENT ||
207            this.data_.danger_type == downloads.DangerType.DANGEROUS_HOST ||
208            this.data_.danger_type == downloads.DangerType.DANGEROUS_URL ||
209            this.data_.danger_type == downloads.DangerType.POTENTIALLY_UNWANTED);
210     },
212     /** @private */
213     computeRemoveStyle_: function() {
214       var canDelete = loadTimeData.getBoolean('allowDeletingHistory');
215       var hideRemove = this.isDangerous_ || this.showCancel_ || !canDelete;
216       return hideRemove ? 'visibility: hidden' : '';
217     },
219     /** @private */
220     computeShowCancel_: function() {
221       return this.data_.state == downloads.States.IN_PROGRESS ||
222              this.data_.state == downloads.States.PAUSED;
223     },
225     /** @private */
226     computeShowProgress_: function() {
227       return this.showCancel_ && this.data_.percent >= -1;
228     },
230     /** @private */
231     computeTag_: function() {
232       switch (this.data_.state) {
233         case downloads.States.CANCELLED:
234           return loadTimeData.getString('statusCancelled');
236         case downloads.States.INTERRUPTED:
237           return this.data_.last_reason_text;
239         case downloads.States.COMPLETE:
240           return this.data_.file_externally_removed ?
241               loadTimeData.getString('statusRemoved') : '';
242       }
244       return '';
245     },
247     /** @private */
248     isIndeterminate_: function() {
249       return this.data_.percent == -1;
250     },
252     /** @private */
253     observeControlledBy_: function() {
254       var html = '';
255       if (this.data_.by_ext_id && this.data_.by_ext_name) {
256         var url = 'chrome://extensions#' + this.data_.by_ext_id;
257         var name = this.data_.by_ext_name;
258         html = loadTimeData.getStringF('controlledByUrl', url, name);
259       }
260       this.$['controlled-by'].innerHTML = html;
261     },
263     /** @private */
264     onCancelClick_: function() {
265       this.actionService_.cancel(this.data_.id);
266     },
268     /** @private */
269     onDiscardDangerous_: function() {
270       this.actionService_.discardDangerous(this.data_.id);
271     },
273     /**
274      * @private
275      * @param {Event} e
276      */
277     onDragStart_: function(e) {
278       e.preventDefault();
279       this.actionService_.drag(this.data_.id);
280     },
282     /**
283      * @param {Event} e
284      * @private
285      */
286     onFileLinkClick_: function(e) {
287       e.preventDefault();
288       this.actionService_.openFile(this.data_.id);
289     },
291     /** @private */
292     onPauseClick_: function() {
293       this.actionService_.pause(this.data_.id);
294     },
296     /** @private */
297     onRemoveClick_: function() {
298       this.actionService_.remove(this.data_.id);
299     },
301     /** @private */
302     onResumeClick_: function() {
303       this.actionService_.resume(this.data_.id);
304     },
306     /** @private */
307     onRetryClick_: function() {
308       this.actionService_.download(this.$['file-link'].href);
309     },
311     /** @private */
312     onSaveDangerous_: function() {
313       this.actionService_.saveDangerous(this.data_.id);
314     },
316     /** @private */
317     onShowClick_: function() {
318       this.actionService_.show(this.data_.id);
319     },
320   });
322   return {Item: Item};