tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

DownloadsMacFinderProgress.sys.mjs (2542B)


      1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
      2 /* vim: set ts=2 et sw=2 tw=80 filetype=javascript: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 /**
      8 * Handles the download progress indicator of the macOS Finder.
      9 */
     10 
     11 const lazy = {};
     12 
     13 ChromeUtils.defineESModuleGetters(lazy, {
     14  Downloads: "resource://gre/modules/Downloads.sys.mjs",
     15 });
     16 
     17 export var DownloadsMacFinderProgress = {
     18  /**
     19   * Maps the path of the download, to the according progress indicator instance.
     20   */
     21  _finderProgresses: null,
     22 
     23  /**
     24   * This method is called after a new browser window on macOS is opened, it
     25   * registers for receiving download events for the progressbar of the Finder.
     26   */
     27  register() {
     28    // Ensure to register only once per process and not for every window.
     29    if (!this._finderProgresses) {
     30      this._finderProgresses = new Map();
     31      lazy.Downloads.getList(lazy.Downloads.ALL).then(list =>
     32        list.addView(this)
     33      );
     34    }
     35  },
     36 
     37  onDownloadAdded(download) {
     38    if (download.stopped) {
     39      return;
     40    }
     41 
     42    let finderProgress = Cc[
     43      "@mozilla.org/widget/macfinderprogress;1"
     44    ].createInstance(Ci.nsIMacFinderProgress);
     45 
     46    let path = download.target.path;
     47 
     48    finderProgress.init(path, () => {
     49      download.cancel().catch(console.error);
     50      download.removePartialData().catch(console.error);
     51    });
     52 
     53    if (download.hasProgress) {
     54      finderProgress.updateProgress(download.currentBytes, download.totalBytes);
     55    } else {
     56      finderProgress.updateProgress(0, 0);
     57    }
     58    this._finderProgresses.set(path, finderProgress);
     59  },
     60 
     61  onDownloadChanged(download) {
     62    let path = download.target.path;
     63    let finderProgress = this._finderProgresses.get(path);
     64    if (!finderProgress) {
     65      // The download is not tracked, it may have been restarted,
     66      // thus forward the call to onDownloadAdded to check if it should be tracked.
     67      this.onDownloadAdded(download);
     68    } else if (download.stopped) {
     69      finderProgress.end();
     70      this._finderProgresses.delete(path);
     71    } else {
     72      finderProgress.updateProgress(download.currentBytes, download.totalBytes);
     73    }
     74  },
     75 
     76  onDownloadRemoved(download) {
     77    let path = download.target.path;
     78    let finderProgress = this._finderProgresses.get(path);
     79    if (finderProgress) {
     80      finderProgress.end();
     81      this._finderProgresses.delete(path);
     82    }
     83  },
     84 };