various whitespace
[conkeror.git] / modules / download-manager.js
blobda845f35b929c5be288560122be3538b63c9e8b4
1 /**
2  * (C) Copyright 2008 Jeremy Maitin-Shepard
3  * (C) Copyright 2009 John Foerch
4  *
5  * Use, modification, and distribution are subject to the terms specified in the
6  * COPYING file.
7 **/
9 require("special-buffer.js");
10 require("mime-type-override.js");
11 require("minibuffer-read-mime-type.js");
12 require("compat/Map.js");
14 var download_manager_service = Cc["@mozilla.org/download-manager;1"]
15     .getService(Ci.nsIDownloadManager);
17 var unmanaged_download_info_list = [];
19 var id_to_download_info = new Map();
21 try {
22     Components.utils.import("resource://gre/modules/Downloads.jsm");
23     var use_downloads_jsm = true;
25     function lookup_download(download) {
26         return id_to_download_info.get(download);
27     }
28 } catch (e) {
29     var use_downloads_jsm = false;
31     function lookup_download(download) {
32         return id_to_download_info.get(download.id);
33     }
37 // Import these constants for convenience
38 const DOWNLOAD_NOTSTARTED = Ci.nsIDownloadManager.DOWNLOAD_NOTSTARTED;
39 const DOWNLOAD_DOWNLOADING = Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING;
40 const DOWNLOAD_FINISHED = Ci.nsIDownloadManager.DOWNLOAD_FINISHED;
41 const DOWNLOAD_FAILED = Ci.nsIDownloadManager.DOWNLOAD_FAILED;
42 const DOWNLOAD_CANCELED = Ci.nsIDownloadManager.DOWNLOAD_CANCELED;
43 const DOWNLOAD_PAUSED = Ci.nsIDownloadManager.DOWNLOAD_PAUSED;
44 const DOWNLOAD_QUEUED = Ci.nsIDownloadManager.DOWNLOAD_QUEUED;
45 const DOWNLOAD_BLOCKED = Ci.nsIDownloadManager.DOWNLOAD_BLOCKED;
46 const DOWNLOAD_SCANNING = Ci.nsIDownloadManager.DOWNLOAD_SCANNING;
49 const DOWNLOAD_NOT_TEMPORARY = 0;
50 const DOWNLOAD_TEMPORARY_FOR_ACTION = 1;
51 const DOWNLOAD_TEMPORARY_FOR_COMMAND = 2;
53 function download_info (source_buffer, mozilla_info, target_file) {
54     this.source_buffer = source_buffer;
55     this.target_file = target_file;
56     if (mozilla_info != null)
57         this.attach(mozilla_info);
59 download_info.prototype = {
60     constructor: download_info,
61     target_file: null,
62     shell_command: null,
63     shell_command_cwd: null,
64     temporary_status: DOWNLOAD_NOT_TEMPORARY,
65     action_description: null,
66     set_shell_command: function (str, cwd) {
67         this.shell_command = str;
68         this.shell_command_cwd = cwd;
69         if (this.mozilla_info)
70             download_shell_command_change_hook.run(this);
71     },
73     /**
74      * None of the following members may be used until attach is called
75      */
77     // Reflectors to properties of nsIDownload
78     get source () { return this.mozilla_info.source; },
79     get id () { return this.mozilla_info.id; },
80     get referrer () { return this.mozilla_info.referrer; },
82     target_file_text: function () {
83         let target = this.target_file.path;
84         let display = this.display_name;
85         if (target.indexOf(display, target.length - display.length) == -1)
86             target += " (" + display + ")";
87         return target;
88     },
90     throw_if_removed: function () {
91         if (this.removed)
92             throw interactive_error("Download has already been removed from the download manager.");
93     },
95     throw_state_error: function () {
96         switch (this.state) {
97         case DOWNLOAD_DOWNLOADING:
98             throw interactive_error("Download is already in progress.");
99         case DOWNLOAD_FINISHED:
100             throw interactive_error("Download has already completed.");
101         case DOWNLOAD_FAILED:
102             throw interactive_error("Download has already failed.");
103         case DOWNLOAD_CANCELED:
104             throw interactive_error("Download has already been canceled.");
105         case DOWNLOAD_PAUSED:
106             throw interactive_error("Download has already been paused.");
107         case DOWNLOAD_QUEUED:
108             throw interactive_error("Download is queued.");
109         default:
110             throw new Error("Download has unexpected state: " + this.state);
111         }
112     },
114     // Download manager operations
115     cancel: function () {
116         this.throw_if_removed();
117         switch (this.state) {
118         case DOWNLOAD_DOWNLOADING:
119         case DOWNLOAD_PAUSED:
120         case DOWNLOAD_QUEUED:
121             if (use_downloads_jsm) {
122                 yield this.mozilla_info.finalize(true);
123             } else {
124                 try {
125                     download_manager_service.cancelDownload(this.id);
126                 } catch (e) {
127                     throw interactive_error("Download cannot be canceled.");
128                 }
129             }
130             break;
131         default:
132             this.throw_state_error();
133         }
134     },
136     retry: function () {
137         this.throw_if_removed();
138         switch (this.state) {
139         case DOWNLOAD_CANCELED:
140         case DOWNLOAD_FAILED:
141             if (use_downloads_jsm) {
142                 yield this.mozilla_info.start();
143             } else {
144                 try {
145                     download_manager_service.retryDownload(this.id);
146                 } catch (e) {
147                     throw interactive_error("Download cannot be retried.");
148                 }
149             }
150             break;
151         default:
152             this.throw_state_error();
153         }
154     },
156     resume: function () {
157         this.throw_if_removed();
158         switch (this.state) {
159         case DOWNLOAD_PAUSED:
160             if (use_downloads_jsm) {
161                 yield this.mozilla_info.start();
162             } else {
164                 try {
165                     download_manager_service.resumeDownload(this.id);
166                 } catch (e) {
167                     throw interactive_error("Download cannot be resumed.");
168                 }
169             }
170             break;
171         default:
172             this.throw_state_error();
173         }
174     },
176     pause: function () {
177         this.throw_if_removed();
178         switch (this.state) {
179         case DOWNLOAD_DOWNLOADING:
180         case DOWNLOAD_QUEUED:
181             if (use_downloads_jsm) {
182                 yield this.mozilla_info.cancel();
183             } else {
184                 try {
185                     download_manager_service.pauseDownload(this.id);
186                 } catch (e) {
187                     throw interactive_error("Download cannot be paused.");
188                 }
189             }
190             break;
191         default:
192             this.throw_state_error();
193         }
194     },
196     remove: function () {
197         this.throw_if_removed();
198         switch (this.state) {
199         case DOWNLOAD_FAILED:
200         case DOWNLOAD_CANCELED:
201         case DOWNLOAD_FINISHED:
202             if (use_downloads_jsm) {
203                 let list = yield Downloads.getList(Downloads.ALL);
204                 yield list.remove(this.mozilla_info);
205             } else {
206                 try {
207                     download_manager_service.removeDownload(this.id);
208                 } catch (e) {
209                     throw interactive_error("Download cannot be removed.");
210                 }
211             }
212                 break;
213         default:
214             throw interactive_error("Download is still in progress.");
215         }
216     },
218     delete_target: function () {
219         if (this.state != DOWNLOAD_FINISHED)
220             throw interactive_error("Download has not finished.");
221         try {
222             this.target_file.remove(false);
223         } catch (e) {
224             if ("result" in e) {
225                 switch (e.result) {
226                 case Cr.NS_ERROR_FILE_TARGET_DOES_NOT_EXIST:
227                     throw interactive_error("File has already been deleted.");
228                 case Cr.NS_ERROR_FILE_ACCESS_DENIED:
229                     throw interactive_error("Access denied");
230                 case Cr.NS_ERROR_FILE_DIR_NOT_EMPTY:
231                     throw interactive_error("Failed to delete file.");
232                 }
233             }
234             throw e;
235         }
236     }
239 if (!use_downloads_jsm) {
240     download_info.prototype.__proto__ = {
241         attach: function (mozilla_info, existing) {
242             if (!this.target_file)
243                 this.__defineGetter__("target_file", function () {
244                     return this.mozilla_info.targetFile;
245                 });
246             else if (this.target_file.path != mozilla_info.targetFile.path)
247                 throw interactive_error("Download target file unexpected.");
249             this.mozilla_info = mozilla_info;
251             if (use_downloads_jsm) {
252                 id_to_download_info.set(mozilla_info, this);
253             } else {
254                 id_to_download_info.set(mozilla_info.id, this);
255             }
257             if (existing)
258                 existing_download_added_hook.run(this);
259             else
260                 download_added_hook.run(this);
261         },
262         get source_uri_string () { return this.mozilla_info.source.spec; },
263         get source_uri () { return this.mozilla_info.source; },
264         get display_name () { return this.mozilla_info.displayName; },
265         get amount_transferred () { return this.mozilla_info.amountTransferred; },
266         get percent_complete () { return this.mozilla_info.percentComplete; },
267         get speed () { return this.mozilla_info.speed; },
268         get state () { return this.mozilla_info.state; },
269         get start_time () { return this.mozilla_info.startTime / 1000; },
270         get MIME_info () { return this.mozilla_info.MIMEInfo; },
271         get MIME_type () {
272             if (this.MIME_info)
273                 return this.MIME_info.MIMEType;
274             return null;
275         },
276         get size () {
277             var s = this.mozilla_info.size;
278             /* nsIDownload.size is a PRUint64, and will have value
279              * LL_MAXUINT (2^64 - 1) to indicate an unknown size.  Because
280              * JavaScript only has a double numerical type, this value
281              * cannot be represented exactly, so 2^36 is used instead as the cutoff. */
282             if (s < 68719476736 /* 2^36 */)
283                 return s;
284             return -1;
285         },
286     };
287 } else {
288     download_info.prototype.__proto__ = {
289         attach: function (mozilla_info, existing) {
290             if (!this.target_file)
291                 this.__defineGetter__("target_file", function () {
292                     return make_file(this.mozilla_info.target.path);
293                 });
294             else if (this.target_file.path != mozilla_info.target.path)
295                 throw interactive_error("Download target file unexpected.");
297             this.mozilla_info = mozilla_info;
298             id_to_download_info.set(mozilla_info, this);
300             if (existing)
301                 existing_download_added_hook.run(this);
302             else
303                 download_added_hook.run(this);
304         },
305         get source_uri () { return make_uri(this.mozilla_info.source.url); },
306         get source_uri_string () { return this.mozilla_info.source.url; },
307         get display_name () { return this.mozilla_info.target.path; },
308         get amount_transferred () { return this.mozilla_info.currentBytes; },
309         get percent_complete () { return this.mozilla_info.progress; },
310         get speed () { return 1000 * this.amount_transferred / (Date.now() - this.start_time); },
311         get start_time () { return this.mozilla_info.startTime.getTime(); },
312         get MIME_type () { return this.mozilla_info.contentType; },
313         get state () {
314             if (this.mozilla_info.succeeded)
315                 return DOWNLOAD_FINISHED;
316             if (this.mozilla_info.canceled)
317                 return DOWNLOAD_CANCELED;
318             if (this.mozilla_info.error)
319                 return DOWNLOAD_FAILED;
320             if (!this.mozilla_info.startTime)
321                 return DOWNLOAD_NOTSTARTED;
322             return DOWNLOAD_DOWNLOADING;
323         },
324         get size () {
325             if (this.mozilla_info.hasProgress)
326                 return this.mozilla_info.totalBytes;
327             return -1;
328         },
329     };
332 var define_download_local_hook = simple_local_hook_definer();
334 function register_download (buffer, source_uri, target_file) {
335     var info = new download_info(buffer, null, target_file);
336     info.registered_time_stamp = Date.now();
337     info.registered_source_uri = source_uri;
338     unmanaged_download_info_list.push(info);
339     return info;
342 function match_registered_download (source) {
343     let list = unmanaged_download_info_list;
344     let t = Date.now();
345     for (let i = 0; i < list.length; ++i) {
346         let x = list[i];
347         if (x.registered_source_uri.spec == source) {
348             list.splice(i, 1);
349             return x;
350         }
351         if (t - x.registered_time_stamp > download_info_max_queue_delay) {
352             list.splice(i, 1);
353             --i;
354             continue;
355         }
356     }
357     return null;
360 define_download_local_hook("existing_download_added_hook");
361 define_download_local_hook("download_added_hook");
362 define_download_local_hook("download_removed_hook");
363 define_download_local_hook("download_finished_hook");
364 define_download_local_hook("download_progress_change_hook");
365 define_download_local_hook("download_state_change_hook");
366 define_download_local_hook("download_shell_command_change_hook");
368 define_variable('delete_temporary_files_for_command', true,
369     'If this is set to true, temporary files downloaded to run a command '+
370     'on them will be deleted once the command completes. If not, the file '+
371     'will stay around forever unless deleted outside the browser.');
373 var download_info_max_queue_delay = 100;
375 if (!use_downloads_jsm) {
377     var download_progress_listener = {
378         QueryInterface: generate_QI(Ci.nsIDownloadProgressListener),
380         onDownloadStateChange: function (state, download) {
381             var info = null;
382             /* FIXME: Determine if only new downloads will have this state
383              * as their previous state. */
385             dumpln("download state change: " + download.source.spec + ": " + state + ", " + download.state + ", " + download.id);
387             if (state == DOWNLOAD_NOTSTARTED) {
388                 info = match_registered_download(download.source.spec);
389                 if (info == null) {
390                     info = new download_info(null, download);
391                     dumpln("error: encountered unknown new download");
392                 } else {
393                     info.attach(download);
394                 }
395             } else {
396                 info = id_to_download_info.get(download.id);
397                 if (info == null) {
398                     dumpln("Error: encountered unknown download");
400                 } else {
401                     info.mozilla_info = download;
402                     download_state_change_hook.run(info);
403                     if (info.state == DOWNLOAD_FINISHED) {
404                         download_finished_hook.run(info);
406                         if (info.shell_command != null) {
407                             info.running_shell_command = true;
408                             spawn(function () {
409                                 try {
410                                     yield shell_command_with_argument(info.shell_command,
411                                                                       info.target_file.path,
412                                                                       $cwd = info.shell_command_cwd);
413                                 } catch (e) {
414                                     handle_interactive_error(info.source_buffer.window, e);
415                                 } finally  {
416                                     if (info.temporary_status == DOWNLOAD_TEMPORARY_FOR_COMMAND)
417                                         if(delete_temporary_files_for_command) {
418                                             info.target_file.remove(false /* not recursive */);
419                                         }
420                                     info.running_shell_command = false;
421                                     download_shell_command_change_hook.run(info);
422                                 }
423                             }());
424                             download_shell_command_change_hook.run(info);
425                         }
426                     }
427                 }
428             }
429         },
431         onProgressChange: function (progress, request, cur_self_progress, max_self_progress,
432                                     cur_total_progress, max_total_progress,
433                                     download) {
434             var info = id_to_download_info.get(download.id);
435             if (info == null) {
436                 dumpln("error: encountered unknown download in progress change");
437                 return;
438             }
439             info.mozilla_info = download;
440             download_progress_change_hook.run(info);
441             //dumpln("download progress change: " + download.source.spec + ": " + cur_self_progress + "/" + max_self_progress + " "
442             // + cur_total_progress + "/" + max_total_progress + ", " + download.state + ", " + download.id);
443         },
445         onSecurityChange: function (progress, request, state, download) {
446         },
448         onStateChange: function (progress, request, state_flags, status, download) {
449         }
450     };
452     var download_observer = {
453         observe: function (subject, topic, data) {
454             switch(topic) {
455             case "download-manager-remove-download":
456                 var ids = [];
457                 if (!subject) {
458                     // Remove all downloads
459                     for (let i in id_to_download_info)
460                         ids.push(i);
461                 } else {
462                     let id = subject.QueryInterface(Ci.nsISupportsPRUint32);
463                     /* FIXME: determine if this should really be an error */
464                     if (!(id in id_to_download_info)) {
465                         dumpln("Error: download-manager-remove-download event received for unknown download: " + id);
466                     } else
467                         ids.push(id);
468                 }
469                 for each (let i in ids) {
470                     dumpln("deleting download: " + i);
471                     let d = id_to_download_info[i];
472                     d.removed = true;
473                     download_removed_hook.run(d);
474                     id_to_download_info.delete(i);
475                 }
476                 break;
477             }
478         }
479     };
481     observer_service.addObserver(download_observer, "download-manager-remove-download", false);
483     try {
484         download_manager_service.addListener(download_progress_listener);
485     } catch (e) {
486         dumpln("Failed to register download progress listener.");
487         dump_error(e);
488     }
489 } else {
491     spawn(function() {
492         let list = yield Downloads.getList(Downloads.ALL);
493         let view = {
494             onDownloadAdded: function (download) {
495                 let info = match_registered_download(download.source.url);
497                 if (info == null) {
498                     info = new download_info(null, download);
499                     dumpln("Encountered unknown new download");
500                 } else {
501                     info.attach(download);
502                 }
503             },
504             onDownloadChanged: function (download) {
505                 let info = lookup_download(download);
506                 if (!info)
507                     dumpln("error: onDownloadChanged: encountered unknown download");
508                 else {
509                     download_progress_change_hook.run(info);
510                     download_state_change_hook.run(info);
512                     if (info.state == DOWNLOAD_FINISHED) {
513                         download_finished_hook.run(info);
515                         if (info.shell_command != null) {
516                             info.running_shell_command = true;
517                             spawn(function () {
518                                 try {
519                                     yield shell_command_with_argument(info.shell_command,
520                                                                       info.target_file.path,
521                                                                       $cwd = info.shell_command_cwd);
522                                 } catch (e) {
523                                     handle_interactive_error(info.source_buffer.window, e);
524                                 } finally  {
525                                     if (info.temporary_status == DOWNLOAD_TEMPORARY_FOR_COMMAND)
526                                         if(delete_temporary_files_for_command) {
527                                             info.target_file.remove(false /* not recursive */);
528                                         }
529                                     info.running_shell_command = false;
530                                     download_shell_command_change_hook.run(info);
531                                 }
532                             }());
533                             download_shell_command_change_hook.run(info);
534                         }
535                     }
536                 }
537             },
538             onDownloadRemoved: function (download) {
539                 let info = lookup_download(download);
540                 if (!info)
541                     dumpln("error: onDownloadRemoved: encountered unknown download");
542                 else {
543                     dumpln("Removing download: " + info.source_uri_string);
544                     info.removed = true;
545                     download_removed_hook.run(info);
546                     id_to_download_info.delete(download);
547                 }
548             }
549         };
550         list.addView(view);
551     }());
554 define_variable("download_buffer_min_update_interval", 2000,
555     "Minimum interval (in milliseconds) between updates in download progress buffers.\n" +
556     "Lowering this interval will increase the promptness of the progress display at " +
557     "the cost of using additional processor time.");
559 function download_buffer_modality (buffer, element) {
560     buffer.keymaps.push(download_buffer_keymap);
563 define_keywords("$info");
564 function download_buffer (window) {
565     this.constructor_begin();
566     keywords(arguments);
567     special_buffer.call(this, window, forward_keywords(arguments));
568     this.info = arguments.$info;
569     this.local.cwd = this.info.target_file.parent;
570     this.description = this.info.source_uri_string;
571     this.update_title();
573     this.progress_change_handler_fn = method_caller(this, this.handle_progress_change);
575     // With Downloads.jsm integration, download_progress_change_hook is redundant with download_state_change_hook
576     if (!use_downloads_jsm)
577         add_hook.call(this.info, "download_progress_change_hook", this.progress_change_handler_fn);
579     add_hook.call(this.info, "download_state_change_hook", this.progress_change_handler_fn);
580     this.command_change_handler_fn = method_caller(this, this.update_command_field);
581     add_hook.call(this.info, "download_shell_command_change_hook", this.command_change_handler_fn);
582     this.modalities.push(download_buffer_modality);
583     this.constructor_end();
585 download_buffer.prototype = {
586     constructor: download_buffer,
587     __proto__: special_buffer.prototype,
588     toString: function () "#<download_buffer>",
590     destroy: function () {
591         if (!use_downloads_jsm)
592             remove_hook.call(this.info, "download_progress_change_hook", this.progress_change_handler_fn);
594         remove_hook.call(this.info, "download_state_change_hook", this.progress_change_handler_fn);
595         remove_hook.call(this.info, "download_shell_command_change_hook", this.command_change_handler_fn);
597         // Remove all node references
598         delete this.status_textnode;
599         delete this.target_file_node;
600         delete this.transferred_div_node;
601         delete this.transferred_textnode;
602         delete this.progress_container_node;
603         delete this.progress_bar_node;
604         delete this.percent_textnode;
605         delete this.time_textnode;
606         delete this.command_div_node;
607         delete this.command_label_textnode;
608         delete this.command_textnode;
610         special_buffer.prototype.destroy.call(this);
611     },
613     update_title: function () {
614         try {
615             // FIXME: do this properly
616             var new_title;
617             var info = this.info;
618             var append_transfer_info = false;
619             var append_speed_info = true;
620             var label = null;
621             switch(info.state) {
622             case DOWNLOAD_DOWNLOADING:
623                 label = "Downloading";
624                 append_transfer_info = true;
625                 break;
626             case DOWNLOAD_FINISHED:
627                 label = "Download complete";
628                 break;
629             case DOWNLOAD_FAILED:
630                 label = "Download failed";
631                 append_transfer_info = true;
632                 append_speed_info = false;
633                 break;
634             case DOWNLOAD_CANCELED:
635                 label = "Download canceled";
636                 append_transfer_info = true;
637                 append_speed_info = false;
638                 break;
639             case DOWNLOAD_PAUSED:
640                 label = "Download paused";
641                 append_transfer_info = true;
642                 append_speed_info = false;
643                 break;
644             case DOWNLOAD_QUEUED:
645             default:
646                 label = "Download queued";
647                 break;
648             }
650             if (append_transfer_info) {
651                 if (append_speed_info)
652                     new_title = label + " at " + pretty_print_file_size(info.speed).join(" ") + "/s: ";
653                 else
654                     new_title = label + ": ";
655                 var trans = pretty_print_file_size(info.amount_transferred);
656                 if (info.size >= 0) {
657                     var total = pretty_print_file_size(info.size);
658                     if (trans[1] == total[1])
659                         new_title += trans[0] + "/" + total[0] + " " + total[1];
660                     else
661                         new_title += trans.join(" ") + "/" + total.join(" ");
662                 } else
663                     new_title += trans.join(" ");
664                 if (info.percent_complete >= 0)
665                     new_title += " (" + info.percent_complete + "%)";
666             } else
667                 new_title = label;
668             if (new_title != this.title) {
669                 this.title = new_title;
670                 return true;
671             }
672             return false;
673         } catch (e) {
674             dump_error(e);
675             throw e;
676         }
677     },
679     handle_progress_change: function () {
680         var cur_time = Date.now();
681         if (this.last_update == null ||
682             (cur_time - this.last_update) > download_buffer_min_update_interval ||
683             this.info.state != this.previous_state) {
685             if (this.update_title())
686                 buffer_title_change_hook.run(this);
688             if (this.generated) {
689                 this.update_fields();
690             }
691             this.previous_status = this.info.status;
692             this.last_update = cur_time;
693         }
694     },
696     generate: function () {
697         var d = this.document;
698         var g = new dom_generator(d, XHTML_NS);
700         /* Warning: If any additional node references are saved in
701          * this function, appropriate code to delete the saved
702          * properties must be added to destroy method. */
704         var info = this.info;
706         d.body.setAttribute("class", "download-buffer");
708         g.add_stylesheet("chrome://conkeror-gui/content/downloads.css");
710         var row, cell;
711         var table = g.element("table", d.body);
713         row = g.element("tr", table, "class", "download-info", "id", "download-source");
714         cell = g.element("td", row, "class", "download-label");
715         this.status_textnode = g.text("", cell);
716         cell = g.element("td", row, "class", "download-value");
717         g.text(info.source_uri_string, cell);
719         row = g.element("tr", table, "class", "download-info", "id", "download-target");
720         cell = g.element("td", row, "class", "download-label");
721         var target_label;
722         if (info.temporary_status != DOWNLOAD_NOT_TEMPORARY)
723             target_label = "Temp. file:";
724         else
725             target_label = "Target:";
726         g.text(target_label, cell);
727         cell = g.element("td", row, "class", "download-value");
728         this.target_file_node = g.text("", cell);
730         row = g.element("tr", table, "class", "download-info", "id", "download-mime-type");
731         cell = g.element("td", row, "class", "download-label");
732         g.text("MIME type:", cell);
733         cell = g.element("td", row, "class", "download-value");
734         g.text(info.MIME_type || "unknown", cell);
736         this.transferred_div_node = row =
737             g.element("tr", table, "class", "download-info", "id", "download-transferred");
738         cell = g.element("td", row, "class", "download-label");
739         g.text("Transferred:", cell);
740         cell = g.element("td", row, "class", "download-value");
741         var sub_item = g.element("div", cell);
742         this.transferred_textnode = g.text("", sub_item);
743         sub_item = g.element("div", cell, "id", "download-percent");
744         this.percent_textnode = g.text("", sub_item);
745         this.progress_container_node = sub_item = g.element("div", cell, "id", "download-progress-container");
746         this.progress_bar_node = g.element("div", sub_item, "id", "download-progress-bar");
748         row = g.element("tr", table, "class", "download-info", "id", "download-time");
749         cell = g.element("td", row, "class", "download-label");
750         g.text("Time:", cell);
751         cell = g.element("td", row, "class", "download-value");
752         this.time_textnode = g.text("", cell);
754         if (info.action_description != null) {
755             row = g.element("tr", table, "class", "download-info", "id", "download-action");
756             cell = g.element("div", row, "class", "download-label");
757             g.text("Action:", cell);
758             cell = g.element("div", row, "class", "download-value");
759             g.text(info.action_description, cell);
760         }
762         this.command_div_node = row = g.element("tr", table, "class", "download-info", "id", "download-command");
763         cell = g.element("td", row, "class", "download-label");
764         this.command_label_textnode = g.text("Run command:", cell);
765         cell = g.element("td", row, "class", "download-value");
766         this.command_textnode = g.text("", cell);
768         this.update_fields();
769         this.update_command_field();
770     },
772     update_fields: function () {
773         if (!this.generated)
774             return;
775         var info = this.info;
776         var label = null;
777         switch (info.state) {
778         case DOWNLOAD_DOWNLOADING:
779             label = "Downloading";
780             break;
781         case DOWNLOAD_FINISHED:
782             label = "Completed";
783             break;
784         case DOWNLOAD_FAILED:
785             label = "Failed";
786             break;
787         case DOWNLOAD_CANCELED:
788             label = "Canceled";
789             break;
790         case DOWNLOAD_PAUSED:
791             label = "Paused";
792             break;
793         case DOWNLOAD_QUEUED:
794         default:
795             label = "Queued";
796             break;
797         }
798         this.status_textnode.nodeValue = label + ":";
799         this.target_file_node.nodeValue = info.target_file_text();
800         this.update_time_field();
802         var tran_text = "";
803         if (info.state == DOWNLOAD_FINISHED)
804             tran_text = pretty_print_file_size(info.size).join(" ");
805         else {
806             var trans = pretty_print_file_size(info.amount_transferred);
807             if (info.size >= 0) {
808                 var total = pretty_print_file_size(info.size);
809                 if (trans[1] == total[1])
810                     tran_text += trans[0] + "/" + total[0] + " " + total[1];
811                 else
812                     tran_text += trans.join(" ") + "/" + total.join(" ");
813             } else
814                 tran_text += trans.join(" ");
815         }
816         this.transferred_textnode.nodeValue = tran_text;
817         if (info.percent_complete >= 0) {
818             this.progress_container_node.style.display = "";
819             this.percent_textnode.nodeValue = info.percent_complete + "%";
820             this.progress_bar_node.style.width = info.percent_complete + "%";
821         } else {
822             this.percent_textnode.nodeValue = "";
823             this.progress_container_node.style.display = "none";
824         }
826         this.update_command_field();
827     },
829     update_time_field: function () {
830         var info = this.info;
831         var elapsed_text = pretty_print_time((Date.now() - info.start_time) / 1000) + " elapsed";
832         var text = "";
833         if (info.state == DOWNLOAD_DOWNLOADING)
834             text = pretty_print_file_size(info.speed).join(" ") + "/s, ";
835         if (info.state == DOWNLOAD_DOWNLOADING &&
836             info.size >= 0 &&
837             info.speed > 0)
838         {
839             let remaining = (info.size - info.amount_transferred) / info.speed;
840             text += pretty_print_time(remaining) + " left (" + elapsed_text + ")";
841         } else
842             text = elapsed_text;
843         this.time_textnode.nodeValue = text;
844     },
846     update_command_field: function () {
847         if (!this.generated)
848             return;
849         if (this.info.shell_command != null) {
850             this.command_div_node.style.display = "";
851             var label;
852             if (this.info.running_shell_command)
853                 label = "Running:";
854             else if (this.info.state == DOWNLOAD_FINISHED)
855                 label = "Ran command:";
856             else
857                 label = "Run command:";
858             this.command_label_textnode.nodeValue = label;
859             this.command_textnode.nodeValue = this.info.shell_command;
860         } else
861             this.command_div_node.style.display = "none";
862     }
865 function download_cancel (buffer) {
866     check_buffer(buffer, download_buffer);
867     var info = buffer.info;
868     yield info.cancel();
869     buffer.window.minibuffer.message("Download canceled");
871 interactive("download-cancel",
872     "Cancel the current download.\n" +
873     "The download can later be retried using the `download-retry' "+
874     "command, but any data already transferred will be lost.",
875     function (I) {
876         let result = yield I.window.minibuffer.read_single_character_option(
877             $prompt = "Cancel this download? (y/n)",
878             $options = ["y", "n"]);
879         if (result == "y")
880             yield download_cancel(I.buffer);
881     });
883 function download_retry (buffer) {
884     check_buffer(buffer, download_buffer);
885     var info = buffer.info;
886     yield info.retry();
887     buffer.window.minibuffer.message("Download retried");
889 interactive("download-retry",
890     "Retry a failed or canceled download.\n" +
891     "This command can be used to retry a download that failed or "+
892     "was canceled using the `download-cancel' command.  The download "+
893     "will begin from the start again.",
894     function (I) { yield download_retry(I.buffer); });
896 function download_pause (buffer) {
897     check_buffer(buffer, download_buffer);
898     yield buffer.info.pause();
899     buffer.window.minibuffer.message("Download paused");
901 interactive("download-pause",
902     "Pause the current download.\n" +
903     "The download can later be resumed using the `download-resume' command. "+
904     "The data already transferred will not be lost.",
905     function (I) { yield download_pause(I.buffer); });
907 function download_resume (buffer) {
908     check_buffer(buffer, download_buffer);
909     yield buffer.info.resume();
910     buffer.window.minibuffer.message("Download resumed");
912 interactive("download-resume",
913     "Resume the current download.\n" +
914     "This command can be used to resume a download paused using the "+
915     "`download-pause' command.",
916     function (I) { yield download_resume(I.buffer); });
918 function download_remove (buffer) {
919     check_buffer(buffer, download_buffer);
920     yield buffer.info.remove();
921     buffer.window.minibuffer.message("Download removed");
923 interactive("download-remove",
924     "Remove the current download from the download manager.\n" +
925     "This command can only be used on inactive (paused, canceled, "+
926     "completed, or failed) downloads.",
927     function (I) { yield download_remove(I.buffer); });
929 function download_retry_or_resume (buffer) {
930     check_buffer(buffer, download_buffer);
931     var info = buffer.info;
932     if (info.state == DOWNLOAD_PAUSED)
933         yield download_resume(buffer);
934     else
935         yield download_retry(buffer);
937 interactive("download-retry-or-resume",
938     "Retry or resume the current download.\n" +
939     "This command can be used to resume a download paused using the " +
940     "`download-pause' command or canceled using the `download-cancel' "+
941     "command.",
942     function (I) { yield download_retry_or_resume(I.buffer); });
944 function download_pause_or_resume (buffer) {
945     check_buffer(buffer, download_buffer);
946     var info = buffer.info;
947     if (info.state == DOWNLOAD_PAUSED)
948         yield download_resume(buffer);
949     else
950         yield download_pause(buffer);
952 interactive("download-pause-or-resume",
953     "Pause or resume the current download.\n" +
954     "This command toggles the paused state of the current download.",
955     function (I) { yield download_pause_or_resume(I.buffer); });
957 function download_delete_target (buffer) {
958     check_buffer(buffer, download_buffer);
959     var info = buffer.info;
960     info.delete_target();
961     buffer.window.minibuffer.message("Deleted file: " + info.target_file.path);
963 interactive("download-delete-target",
964     "Delete the target file of the current download.\n" +
965     "This command can only be used if the download has finished successfully.",
966     function (I) { download_delete_target(I.buffer); });
968 function download_shell_command (buffer, cwd, cmd) {
969     check_buffer(buffer, download_buffer);
970     var info = buffer.info;
971     if (info.state == DOWNLOAD_FINISHED) {
972         shell_command_with_argument_blind(cmd, info.target_file.path, $cwd = cwd);
973         return;
974     }
975     if (info.state != DOWNLOAD_DOWNLOADING &&
976         info.state != DOWNLOAD_PAUSED &&
977         info.state != DOWNLOAD_QUEUED)
978     {
979         info.throw_state_error();
980     }
981     if (cmd == null || cmd.length == 0)
982         info.set_shell_command(null, cwd);
983     else
984         info.set_shell_command(cmd, cwd);
985     buffer.window.minibuffer.message("Queued shell command: " + cmd);
987 interactive("download-shell-command",
988     "Run a shell command on the target file of the current download.\n"+
989     "If the download is still in progress, the shell command will be queued "+
990     "to run when the download finishes.",
991     function (I) {
992         var buffer = check_buffer(I.buffer, download_buffer);
993         var cwd = buffer.info.shell_command_cwd || I.local.cwd;
994         var cmd = yield I.minibuffer.read_shell_command(
995             $cwd = cwd,
996             $initial_value = buffer.info.shell_command ||
997                 external_content_handlers.get(buffer.info.MIME_type));
998         download_shell_command(buffer, cwd, cmd);
999     });
1001 function download_manager_ui () {}
1002 download_manager_ui.prototype = {
1003     constructor: download_manager_ui,
1004     QueryInterface: XPCOMUtils.generateQI([Ci.nsIDownloadManagerUI]),
1006     getAttention: function () {},
1007     show: function () {},
1008     visible: false
1012 interactive("download-manager-show-builtin-ui",
1013     "Show the built-in (Firefox-style) download manager window.",
1014     function (I) {
1015         Components.classesByID["{7dfdf0d1-aff6-4a34-bad1-d0fe74601642}"]
1016             .getService(Ci.nsIDownloadManagerUI)
1017             .show(I.window);
1018     });
1022  * Download-show
1023  */
1025 define_variable("download_temporary_file_open_buffer_delay", 500,
1026     "Delay (in milliseconds) before a download buffer is opened for "+
1027     "temporary downloads.  If the download completes before this amount "+
1028     "of time, no download buffer will be opened.  This variable takes "+
1029     "effect only if `open_download_buffer_automatically' is in "+
1030     "`download_added_hook', which is the case by default.");
1032 define_variable("download_buffer_automatic_open_target", OPEN_NEW_WINDOW,
1033     "Target(s) for download buffers created by "+
1034     "`open_download_buffer_automatically'.");
1036 minibuffer_auto_complete_preferences.download = true;
1038 function download_completer (completions) {
1039     keywords(arguments);
1040     if (! use_downloads_jsm) {
1041         completions = function (visitor) {
1042             var dls = download_manager_service.activeDownloads;
1043             while (dls.hasMoreElements()) {
1044                 let dl = dls.getNext();
1045                 visitor(dl);
1046             }
1047         };
1048     }
1049     all_word_completer.call(this, forward_keywords(arguments),
1050                             $completions = completions);
1052 download_completer.prototype = {
1053     constructor: download_completer,
1054     __proto__: all_word_completer.prototype,
1055     toString: function () "#<download_completer>",
1056     get_string: function (x) {
1057         if (use_downloads_jsm)
1058             return x.target.path;
1059         else
1060             return x.displayName;
1061     },
1062     get_description: function (x) {
1063         if (use_downloads_jsm)
1064             return x.source.url;
1065         else
1066             return x.source.spec
1067     }
1070 minibuffer.prototype.read_download = function () {
1071     keywords(arguments,
1072              $prompt = "Download",
1073              $auto_complete = "download",
1074              $auto_complete_initial = true,
1075              $require_match = true);
1076     if (use_downloads_jsm) {
1077         var list = yield Downloads.getList(Downloads.ALL);
1078         var all_downloads = yield list.getAll();
1079         var completer = new download_completer(all_downloads);
1080     } else {
1081         completer = new download_completer();
1082     }
1083     var result = yield this.read(forward_keywords(arguments),
1084                                  $completer = completer);
1085     yield co_return(result);
1089 function download_show (window, target, mozilla_info) {
1090     if (! window)
1091         target = OPEN_NEW_WINDOW;
1092     var info = lookup_download(mozilla_info);
1093     if (!info) {
1094         info = new download_info(null, null);
1095         info.attach(mozilla_info, true /* existing */);
1096     }
1097     create_buffer(window, buffer_creator(download_buffer, $info = info), target);
1100 function download_show_new_window (I) {
1101     var mozilla_info = yield I.minibuffer.read_download($prompt = "Show download:");
1102     download_show(I.window, OPEN_NEW_WINDOW, mozilla_info);
1105 function download_show_new_buffer (I) {
1106     var mozilla_info = yield I.minibuffer.read_download($prompt = "Show download:");
1107     download_show(I.window, OPEN_NEW_BUFFER, mozilla_info);
1110 function download_show_new_buffer_background (I) {
1111     var mozilla_info = yield I.minibuffer.read_download($prompt = "Show download:");
1112     download_show(I.window, OPEN_NEW_BUFFER_BACKGROUND, mozilla_info);
1115 function open_download_buffer_automatically (info) {
1116     var buf = info.source_buffer;
1117     var target = download_buffer_automatic_open_target;
1118     if (info.temporary_status == DOWNLOAD_NOT_TEMPORARY ||
1119         download_temporary_file_open_buffer_delay == 0)
1120     {
1121         download_show(buf ? buf.window : null, target, info.mozilla_info);
1122     } else {
1123         var timer = null;
1124         function finish () {
1125             timer.cancel();
1126         }
1127         add_hook.call(info, "download_finished_hook", finish);
1128         timer = call_after_timeout(function () {
1129             remove_hook.call(info, "download_finished_hook", finish);
1130             download_show(buf ? buf.window : null, target, info.mozilla_info);
1131         }, download_temporary_file_open_buffer_delay);
1132     }
1134 add_hook("download_added_hook", open_download_buffer_automatically);
1136 interactive("download-show",
1137     "Prompt for an ongoing download and open a download buffer showing "+
1138     "its progress.",
1139     alternates(download_show_new_buffer,
1140                download_show_new_window));
1142 provide("download-manager");