DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;

Cu.import("resource://gre/modules/XPCOMUtils.jsm");

XPCOMUtils.defineLazyModuleGetter(this, "FreeSpaceWatcher",
                                  "resource://gre/modules/FreeSpaceWatcher.jsm");

this.EXPORTED_SYMBOLS = ["AppDownloadManager"];

function debug(aMsg) {
  //dump("-*-*- AppDownloadManager.jsm : " + aMsg + "\n");
}

this.AppDownloadManager = {
  // Minimum disk free space we want to keep, in bytes.
  // Keep synchronized with Webapps.jsm
  MIN_REMAINING_FREESPACE: 5 * 1024 * 1024,

  downloads: {},
  count: 0,
  cancelFunc: null,
  timer: null,

  /**
   * Registers the function called when we need to cancel a download.
   * The function will be called with a single parameter being the
   * manifest URL.
   */
  registerCancelFunction: function app_dlMgr_registerCancel(aFunction) {
    this.cancelFunc = aFunction;
  },

  /**
   * Adds a download to the list of current downloads.
   * @param aManifestURL The manifest URL for the application being downloaded.
   * @param aDownload    An opaque object representing the download.
   */
  add: function app_dlMgr_add(aManifestURL, aDownload) {
    debug("Adding " + aManifestURL);
    if (!(aManifestURL in this.downloads)) {
      this.count++;
      if (this.count == 1) {
        this.timer = FreeSpaceWatcher.create(this.MIN_REMAINING_FREESPACE,
                                             this._spaceWatcher.bind(this));
      }
    }
    this.downloads[aManifestURL] = aDownload;
  },

  /**
   * Retrieves a download from the list of current downloads.
   * @param  aManifestURL The manifest URL for the application being retrieved.
   * @return              The opaque object representing the download.
   */
  get: function app_dlMgr_get(aManifestURL) {
    debug("Getting " + aManifestURL);
    if (!this.downloads[aManifestURL]) {
      return null;
    }
    return this.downloads[aManifestURL];
  },

  /**
   * Removes a download of the list of current downloads.
   * @param aManifestURL The manifest URL for the application being removed.
   */
  remove: function app_dlMgr_remove(aManifestURL) {
    debug("Removing " + aManifestURL);
    if (aManifestURL in this.downloads) {
      this.count--;
      delete this.downloads[aManifestURL];
      if (this.count == 0) {
        FreeSpaceWatcher.stop(this.timer);
      }
    }
  },

  /**
   * Callback for the free space watcher. This will call cancel on downloads
   * if needed.
   */
  _spaceWatcher: function app_dlMgr_watcher(aStatus) {
    debug("Disk space is now " + aStatus);
    if (aStatus == "free") {
      // Nothing to do.
      return;
    }

    // We cancel all downloads, because we don't know which ones we could
    // keep running. We can improve that later if we have better heuristics,
    // or when we'll support pause & resume we should just pause downloads.
    for (let url in this.downloads) {
      this.cancelFunc(url, "INSUFFICIENT_STORAGE");
    }
  }
}