Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | const Cc = Components.classes; |
michael@0 | 8 | const Ci = Components.interfaces; |
michael@0 | 9 | const Cu = Components.utils; |
michael@0 | 10 | |
michael@0 | 11 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 12 | Cu.import("resource://gre/modules/FileUtils.jsm"); |
michael@0 | 13 | |
michael@0 | 14 | this.EXPORTED_SYMBOLS = ["FreeSpaceWatcher"]; |
michael@0 | 15 | |
michael@0 | 16 | function debug(aMsg) { |
michael@0 | 17 | //dump("-*-*- FreeSpaceWatcher.jsm : " + aMsg + "\n"); |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | // Polling delay for free space, in ms. |
michael@0 | 21 | const DEFAULT_WATCHER_DELAY = 1000; |
michael@0 | 22 | |
michael@0 | 23 | this.FreeSpaceWatcher = { |
michael@0 | 24 | timers: {}, |
michael@0 | 25 | id: 0, |
michael@0 | 26 | |
michael@0 | 27 | /** |
michael@0 | 28 | * This function will call aOnStatusChange |
michael@0 | 29 | * each time the free space for apps crosses aThreshold, checking |
michael@0 | 30 | * every aDelay milliseconds, or every second by default. |
michael@0 | 31 | * aOnStatusChange is called with either "free" or "full" and will |
michael@0 | 32 | * always be called at least one to get the initial status. |
michael@0 | 33 | * @param aThreshold The amount of space in bytes to watch for. |
michael@0 | 34 | * @param aOnStatusChange The function called when the state changes. |
michael@0 | 35 | * @param aDelay How often (in ms) we check free space. Defaults |
michael@0 | 36 | * to DEFAULT_WATCHER_DELAY. |
michael@0 | 37 | * @return An opaque value to use with stop(). |
michael@0 | 38 | */ |
michael@0 | 39 | create: function spaceWatcher_create(aThreshold, aOnStatusChange, aDelay) { |
michael@0 | 40 | let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); |
michael@0 | 41 | debug("Creating new FreeSpaceWatcher"); |
michael@0 | 42 | let callback = { |
michael@0 | 43 | currentStatus: null, |
michael@0 | 44 | notify: function(aTimer) { |
michael@0 | 45 | try { |
michael@0 | 46 | let checkFreeSpace = function (freeBytes) { |
michael@0 | 47 | debug("Free bytes: " + freeBytes); |
michael@0 | 48 | let newStatus = freeBytes > aThreshold; |
michael@0 | 49 | if (newStatus != callback.currentStatus) { |
michael@0 | 50 | debug("New status: " + (newStatus ? "free" : "full")); |
michael@0 | 51 | aOnStatusChange(newStatus ? "free" : "full"); |
michael@0 | 52 | callback.currentStatus = newStatus; |
michael@0 | 53 | } |
michael@0 | 54 | }; |
michael@0 | 55 | |
michael@0 | 56 | let navigator = Services.wm.getMostRecentWindow("navigator:browser") |
michael@0 | 57 | .navigator; |
michael@0 | 58 | let deviceStorage = null; |
michael@0 | 59 | |
michael@0 | 60 | if (navigator.getDeviceStorage) { |
michael@0 | 61 | deviceStorage = navigator.getDeviceStorage("apps"); |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | if (deviceStorage) { |
michael@0 | 65 | let req = deviceStorage.freeSpace(); |
michael@0 | 66 | req.onsuccess = req.onerror = function statResult(e) { |
michael@0 | 67 | if (!e.target.result) { |
michael@0 | 68 | return; |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | let freeBytes = e.target.result; |
michael@0 | 72 | checkFreeSpace(freeBytes); |
michael@0 | 73 | } |
michael@0 | 74 | } else { |
michael@0 | 75 | // deviceStorage isn't available, so use the webappsDir instead. |
michael@0 | 76 | // This needs to be moved from a hardcoded string to DIRECTORY_NAME |
michael@0 | 77 | // in AppsUtils. See bug 852685. |
michael@0 | 78 | let dir = FileUtils.getDir("webappsDir", ["webapps"], true, true); |
michael@0 | 79 | let freeBytes; |
michael@0 | 80 | try { |
michael@0 | 81 | freeBytes = dir.diskSpaceAvailable; |
michael@0 | 82 | } catch(e) { |
michael@0 | 83 | // If disk space information isn't available, we should assume |
michael@0 | 84 | // that there is enough free space, and that we'll fail when |
michael@0 | 85 | // we actually run out of disk space. |
michael@0 | 86 | callback.currentStatus = true; |
michael@0 | 87 | } |
michael@0 | 88 | if (freeBytes) { |
michael@0 | 89 | // We have disk space information. Call this here so that |
michael@0 | 90 | // any exceptions are caught in the outer catch block. |
michael@0 | 91 | checkFreeSpace(freeBytes); |
michael@0 | 92 | } |
michael@0 | 93 | } |
michael@0 | 94 | } catch(e) { |
michael@0 | 95 | // If the aOnStatusChange callback has errored we'll end up here. |
michael@0 | 96 | debug(e); |
michael@0 | 97 | } |
michael@0 | 98 | } |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | timer.initWithCallback(callback, aDelay || DEFAULT_WATCHER_DELAY, |
michael@0 | 102 | Ci.nsITimer.TYPE_REPEATING_SLACK); |
michael@0 | 103 | let id = "timer-" + this.id++; |
michael@0 | 104 | this.timers[id] = timer; |
michael@0 | 105 | return id; |
michael@0 | 106 | }, |
michael@0 | 107 | |
michael@0 | 108 | /** |
michael@0 | 109 | * This function stops a running watcher. |
michael@0 | 110 | * @param aId The opaque timer id returned by create(). |
michael@0 | 111 | */ |
michael@0 | 112 | stop: function spaceWatcher_stop(aId) { |
michael@0 | 113 | if (this.timers[aId]) { |
michael@0 | 114 | this.timers[aId].cancel(); |
michael@0 | 115 | delete this.timers[aId]; |
michael@0 | 116 | } |
michael@0 | 117 | } |
michael@0 | 118 | } |