|
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/. */ |
|
4 |
|
5 "use strict"; |
|
6 |
|
7 const Cc = Components.classes; |
|
8 const Ci = Components.interfaces; |
|
9 const Cu = Components.utils; |
|
10 |
|
11 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
12 |
|
13 XPCOMUtils.defineLazyModuleGetter(this, "FreeSpaceWatcher", |
|
14 "resource://gre/modules/FreeSpaceWatcher.jsm"); |
|
15 |
|
16 this.EXPORTED_SYMBOLS = ["AppDownloadManager"]; |
|
17 |
|
18 function debug(aMsg) { |
|
19 //dump("-*-*- AppDownloadManager.jsm : " + aMsg + "\n"); |
|
20 } |
|
21 |
|
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, |
|
26 |
|
27 downloads: {}, |
|
28 count: 0, |
|
29 cancelFunc: null, |
|
30 timer: null, |
|
31 |
|
32 /** |
|
33 * Registers the function called when we need to cancel a download. |
|
34 * The function will be called with a single parameter being the |
|
35 * manifest URL. |
|
36 */ |
|
37 registerCancelFunction: function app_dlMgr_registerCancel(aFunction) { |
|
38 this.cancelFunc = aFunction; |
|
39 }, |
|
40 |
|
41 /** |
|
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. |
|
45 */ |
|
46 add: function app_dlMgr_add(aManifestURL, aDownload) { |
|
47 debug("Adding " + aManifestURL); |
|
48 if (!(aManifestURL in this.downloads)) { |
|
49 this.count++; |
|
50 if (this.count == 1) { |
|
51 this.timer = FreeSpaceWatcher.create(this.MIN_REMAINING_FREESPACE, |
|
52 this._spaceWatcher.bind(this)); |
|
53 } |
|
54 } |
|
55 this.downloads[aManifestURL] = aDownload; |
|
56 }, |
|
57 |
|
58 /** |
|
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. |
|
62 */ |
|
63 get: function app_dlMgr_get(aManifestURL) { |
|
64 debug("Getting " + aManifestURL); |
|
65 if (!this.downloads[aManifestURL]) { |
|
66 return null; |
|
67 } |
|
68 return this.downloads[aManifestURL]; |
|
69 }, |
|
70 |
|
71 /** |
|
72 * Removes a download of the list of current downloads. |
|
73 * @param aManifestURL The manifest URL for the application being removed. |
|
74 */ |
|
75 remove: function app_dlMgr_remove(aManifestURL) { |
|
76 debug("Removing " + aManifestURL); |
|
77 if (aManifestURL in this.downloads) { |
|
78 this.count--; |
|
79 delete this.downloads[aManifestURL]; |
|
80 if (this.count == 0) { |
|
81 FreeSpaceWatcher.stop(this.timer); |
|
82 } |
|
83 } |
|
84 }, |
|
85 |
|
86 /** |
|
87 * Callback for the free space watcher. This will call cancel on downloads |
|
88 * if needed. |
|
89 */ |
|
90 _spaceWatcher: function app_dlMgr_watcher(aStatus) { |
|
91 debug("Disk space is now " + aStatus); |
|
92 if (aStatus == "free") { |
|
93 // Nothing to do. |
|
94 return; |
|
95 } |
|
96 |
|
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"); |
|
102 } |
|
103 } |
|
104 } |