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/Services.jsm");
12 Cu.import("resource://gre/modules/FileUtils.jsm");
14 this.EXPORTED_SYMBOLS = ["FreeSpaceWatcher"];
16 function debug(aMsg) {
17 //dump("-*-*- FreeSpaceWatcher.jsm : " + aMsg + "\n");
20 // Polling delay for free space, in ms.
21 const DEFAULT_WATCHER_DELAY = 1000;
23 this.FreeSpaceWatcher = {
28 * This function will call aOnStatusChange
29 * each time the free space for apps crosses aThreshold, checking
30 * every aDelay milliseconds, or every second by default.
31 * aOnStatusChange is called with either "free" or "full" and will
32 * always be called at least one to get the initial status.
33 * @param aThreshold The amount of space in bytes to watch for.
34 * @param aOnStatusChange The function called when the state changes.
35 * @param aDelay How often (in ms) we check free space. Defaults
36 * to DEFAULT_WATCHER_DELAY.
37 * @return An opaque value to use with stop().
39 create: function spaceWatcher_create(aThreshold, aOnStatusChange, aDelay) {
40 let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
41 debug("Creating new FreeSpaceWatcher");
44 notify: function(aTimer) {
46 let checkFreeSpace = function (freeBytes) {
47 debug("Free bytes: " + freeBytes);
48 let newStatus = freeBytes > aThreshold;
49 if (newStatus != callback.currentStatus) {
50 debug("New status: " + (newStatus ? "free" : "full"));
51 aOnStatusChange(newStatus ? "free" : "full");
52 callback.currentStatus = newStatus;
56 let navigator = Services.wm.getMostRecentWindow("navigator:browser")
58 let deviceStorage = null;
60 if (navigator.getDeviceStorage) {
61 deviceStorage = navigator.getDeviceStorage("apps");
65 let req = deviceStorage.freeSpace();
66 req.onsuccess = req.onerror = function statResult(e) {
67 if (!e.target.result) {
71 let freeBytes = e.target.result;
72 checkFreeSpace(freeBytes);
75 // deviceStorage isn't available, so use the webappsDir instead.
76 // This needs to be moved from a hardcoded string to DIRECTORY_NAME
77 // in AppsUtils. See bug 852685.
78 let dir = FileUtils.getDir("webappsDir", ["webapps"], true, true);
81 freeBytes = dir.diskSpaceAvailable;
83 // If disk space information isn't available, we should assume
84 // that there is enough free space, and that we'll fail when
85 // we actually run out of disk space.
86 callback.currentStatus = true;
89 // We have disk space information. Call this here so that
90 // any exceptions are caught in the outer catch block.
91 checkFreeSpace(freeBytes);
95 // If the aOnStatusChange callback has errored we'll end up here.
101 timer.initWithCallback(callback, aDelay || DEFAULT_WATCHER_DELAY,
102 Ci.nsITimer.TYPE_REPEATING_SLACK);
103 let id = "timer-" + this.id++;
104 this.timers[id] = timer;
109 * This function stops a running watcher.
110 * @param aId The opaque timer id returned by create().
112 stop: function spaceWatcher_stop(aId) {
113 if (this.timers[aId]) {
114 this.timers[aId].cancel();
115 delete this.timers[aId];