dom/apps/src/AppDownloadManager.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/apps/src/AppDownloadManager.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,104 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +const Cc = Components.classes;
    1.11 +const Ci = Components.interfaces;
    1.12 +const Cu = Components.utils;
    1.13 +
    1.14 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.15 +
    1.16 +XPCOMUtils.defineLazyModuleGetter(this, "FreeSpaceWatcher",
    1.17 +                                  "resource://gre/modules/FreeSpaceWatcher.jsm");
    1.18 +
    1.19 +this.EXPORTED_SYMBOLS = ["AppDownloadManager"];
    1.20 +
    1.21 +function debug(aMsg) {
    1.22 +  //dump("-*-*- AppDownloadManager.jsm : " + aMsg + "\n");
    1.23 +}
    1.24 +
    1.25 +this.AppDownloadManager = {
    1.26 +  // Minimum disk free space we want to keep, in bytes.
    1.27 +  // Keep synchronized with Webapps.jsm
    1.28 +  MIN_REMAINING_FREESPACE: 5 * 1024 * 1024,
    1.29 +
    1.30 +  downloads: {},
    1.31 +  count: 0,
    1.32 +  cancelFunc: null,
    1.33 +  timer: null,
    1.34 +
    1.35 +  /**
    1.36 +   * Registers the function called when we need to cancel a download.
    1.37 +   * The function will be called with a single parameter being the
    1.38 +   * manifest URL.
    1.39 +   */
    1.40 +  registerCancelFunction: function app_dlMgr_registerCancel(aFunction) {
    1.41 +    this.cancelFunc = aFunction;
    1.42 +  },
    1.43 +
    1.44 +  /**
    1.45 +   * Adds a download to the list of current downloads.
    1.46 +   * @param aManifestURL The manifest URL for the application being downloaded.
    1.47 +   * @param aDownload    An opaque object representing the download.
    1.48 +   */
    1.49 +  add: function app_dlMgr_add(aManifestURL, aDownload) {
    1.50 +    debug("Adding " + aManifestURL);
    1.51 +    if (!(aManifestURL in this.downloads)) {
    1.52 +      this.count++;
    1.53 +      if (this.count == 1) {
    1.54 +        this.timer = FreeSpaceWatcher.create(this.MIN_REMAINING_FREESPACE,
    1.55 +                                             this._spaceWatcher.bind(this));
    1.56 +      }
    1.57 +    }
    1.58 +    this.downloads[aManifestURL] = aDownload;
    1.59 +  },
    1.60 +
    1.61 +  /**
    1.62 +   * Retrieves a download from the list of current downloads.
    1.63 +   * @param  aManifestURL The manifest URL for the application being retrieved.
    1.64 +   * @return              The opaque object representing the download.
    1.65 +   */
    1.66 +  get: function app_dlMgr_get(aManifestURL) {
    1.67 +    debug("Getting " + aManifestURL);
    1.68 +    if (!this.downloads[aManifestURL]) {
    1.69 +      return null;
    1.70 +    }
    1.71 +    return this.downloads[aManifestURL];
    1.72 +  },
    1.73 +
    1.74 +  /**
    1.75 +   * Removes a download of the list of current downloads.
    1.76 +   * @param aManifestURL The manifest URL for the application being removed.
    1.77 +   */
    1.78 +  remove: function app_dlMgr_remove(aManifestURL) {
    1.79 +    debug("Removing " + aManifestURL);
    1.80 +    if (aManifestURL in this.downloads) {
    1.81 +      this.count--;
    1.82 +      delete this.downloads[aManifestURL];
    1.83 +      if (this.count == 0) {
    1.84 +        FreeSpaceWatcher.stop(this.timer);
    1.85 +      }
    1.86 +    }
    1.87 +  },
    1.88 +
    1.89 +  /**
    1.90 +   * Callback for the free space watcher. This will call cancel on downloads
    1.91 +   * if needed.
    1.92 +   */
    1.93 +  _spaceWatcher: function app_dlMgr_watcher(aStatus) {
    1.94 +    debug("Disk space is now " + aStatus);
    1.95 +    if (aStatus == "free") {
    1.96 +      // Nothing to do.
    1.97 +      return;
    1.98 +    }
    1.99 +
   1.100 +    // We cancel all downloads, because we don't know which ones we could
   1.101 +    // keep running. We can improve that later if we have better heuristics,
   1.102 +    // or when we'll support pause & resume we should just pause downloads.
   1.103 +    for (let url in this.downloads) {
   1.104 +      this.cancelFunc(url, "INSUFFICIENT_STORAGE");
   1.105 +    }
   1.106 +  }
   1.107 +}

mercurial