1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 const Cc = Components.classes;
8 const Ci = Components.interfaces;
9 const Cu = Components.utils;
11 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
13 XPCOMUtils.defineLazyModuleGetter(this, "FreeSpaceWatcher",
14 "resource://gre/modules/FreeSpaceWatcher.jsm");
16 this.EXPORTED_SYMBOLS = ["AppDownloadManager"];
18 function debug(aMsg) {
19 //dump("-*-*- AppDownloadManager.jsm : " + aMsg + "\n");
22 this.AppDownloadManager = {
23 // Minimum disk free space we want to keep, in bytes.
24 // Keep synchronized with Webapps.jsm
25 MIN_REMAINING_FREESPACE: 5 * 1024 * 1024,
33 * Registers the function called when we need to cancel a download.
34 * The function will be called with a single parameter being the
37 registerCancelFunction: function app_dlMgr_registerCancel(aFunction) {
38 this.cancelFunc = aFunction;
42 * Adds a download to the list of current downloads.
43 * @param aManifestURL The manifest URL for the application being downloaded.
44 * @param aDownload An opaque object representing the download.
46 add: function app_dlMgr_add(aManifestURL, aDownload) {
47 debug("Adding " + aManifestURL);
48 if (!(aManifestURL in this.downloads)) {
50 if (this.count == 1) {
51 this.timer = FreeSpaceWatcher.create(this.MIN_REMAINING_FREESPACE,
52 this._spaceWatcher.bind(this));
55 this.downloads[aManifestURL] = aDownload;
59 * Retrieves a download from the list of current downloads.
60 * @param aManifestURL The manifest URL for the application being retrieved.
61 * @return The opaque object representing the download.
63 get: function app_dlMgr_get(aManifestURL) {
64 debug("Getting " + aManifestURL);
65 if (!this.downloads[aManifestURL]) {
68 return this.downloads[aManifestURL];
72 * Removes a download of the list of current downloads.
73 * @param aManifestURL The manifest URL for the application being removed.
75 remove: function app_dlMgr_remove(aManifestURL) {
76 debug("Removing " + aManifestURL);
77 if (aManifestURL in this.downloads) {
79 delete this.downloads[aManifestURL];
80 if (this.count == 0) {
81 FreeSpaceWatcher.stop(this.timer);
87 * Callback for the free space watcher. This will call cancel on downloads
90 _spaceWatcher: function app_dlMgr_watcher(aStatus) {
91 debug("Disk space is now " + aStatus);
92 if (aStatus == "free") {
97 // We cancel all downloads, because we don't know which ones we could
98 // keep running. We can improve that later if we have better heuristics,
99 // or when we'll support pause & resume we should just pause downloads.
100 for (let url in this.downloads) {
101 this.cancelFunc(url, "INSUFFICIENT_STORAGE");