dom/apps/src/FreeSpaceWatcher.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/apps/src/FreeSpaceWatcher.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,118 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +const Cc = Components.classes;
    1.11 +const Ci = Components.interfaces;
    1.12 +const Cu = Components.utils;
    1.13 +
    1.14 +Cu.import("resource://gre/modules/Services.jsm");
    1.15 +Cu.import("resource://gre/modules/FileUtils.jsm");
    1.16 +
    1.17 +this.EXPORTED_SYMBOLS = ["FreeSpaceWatcher"];
    1.18 +
    1.19 +function debug(aMsg) {
    1.20 +  //dump("-*-*- FreeSpaceWatcher.jsm : " + aMsg + "\n");
    1.21 +}
    1.22 +
    1.23 +// Polling delay for free space, in ms.
    1.24 +const DEFAULT_WATCHER_DELAY = 1000;
    1.25 +
    1.26 +this.FreeSpaceWatcher = {
    1.27 +  timers: {},
    1.28 +  id: 0,
    1.29 +
    1.30 +  /**
    1.31 +   * This function  will call aOnStatusChange
    1.32 +   * each time the free space for apps crosses aThreshold, checking
    1.33 +   * every aDelay milliseconds, or every second by default.
    1.34 +   * aOnStatusChange is called with either "free" or "full" and will
    1.35 +   * always be called at least one to get the initial status.
    1.36 +   * @param aThreshold      The amount of space in bytes to watch for.
    1.37 +   * @param aOnStatusChange The function called when the state changes.
    1.38 +   * @param aDelay          How often (in ms) we check free space. Defaults
    1.39 +   *                        to DEFAULT_WATCHER_DELAY.
    1.40 +   * @return                An opaque value to use with stop().
    1.41 +   */
    1.42 +  create: function spaceWatcher_create(aThreshold, aOnStatusChange, aDelay) {
    1.43 +    let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
    1.44 +    debug("Creating new FreeSpaceWatcher");
    1.45 +    let callback = {
    1.46 +      currentStatus: null,
    1.47 +      notify: function(aTimer) {
    1.48 +        try {
    1.49 +          let checkFreeSpace = function (freeBytes) {
    1.50 +            debug("Free bytes: " + freeBytes);
    1.51 +            let newStatus = freeBytes > aThreshold;
    1.52 +            if (newStatus != callback.currentStatus) {
    1.53 +              debug("New status: " + (newStatus ? "free" : "full"));
    1.54 +              aOnStatusChange(newStatus ? "free" : "full");
    1.55 +              callback.currentStatus = newStatus;
    1.56 +            }
    1.57 +          };
    1.58 +
    1.59 +          let navigator = Services.wm.getMostRecentWindow("navigator:browser")
    1.60 +                                  .navigator;
    1.61 +          let deviceStorage = null;
    1.62 +
    1.63 +          if (navigator.getDeviceStorage) {
    1.64 +            deviceStorage = navigator.getDeviceStorage("apps");
    1.65 +          }
    1.66 +
    1.67 +          if (deviceStorage) {
    1.68 +            let req = deviceStorage.freeSpace();
    1.69 +            req.onsuccess = req.onerror = function statResult(e) {
    1.70 +              if (!e.target.result) {
    1.71 +                return;
    1.72 +              }
    1.73 +
    1.74 +              let freeBytes = e.target.result;
    1.75 +              checkFreeSpace(freeBytes);
    1.76 +            }
    1.77 +          } else {
    1.78 +            // deviceStorage isn't available, so use the webappsDir instead.
    1.79 +            // This needs to be moved from a hardcoded string to DIRECTORY_NAME
    1.80 +            // in AppsUtils. See bug 852685.
    1.81 +            let dir = FileUtils.getDir("webappsDir", ["webapps"], true, true);
    1.82 +            let freeBytes;
    1.83 +            try {
    1.84 +              freeBytes = dir.diskSpaceAvailable;
    1.85 +            } catch(e) {
    1.86 +              // If disk space information isn't available, we should assume
    1.87 +              // that there is enough free space, and that we'll fail when
    1.88 +              // we actually run out of disk space.
    1.89 +              callback.currentStatus = true;
    1.90 +            }
    1.91 +            if (freeBytes) {
    1.92 +              // We have disk space information. Call this here so that
    1.93 +              // any exceptions are caught in the outer catch block.
    1.94 +              checkFreeSpace(freeBytes);
    1.95 +            }
    1.96 +          }
    1.97 +        } catch(e) {
    1.98 +          // If the aOnStatusChange callback has errored we'll end up here.
    1.99 +          debug(e);
   1.100 +        }
   1.101 +      }
   1.102 +    }
   1.103 +
   1.104 +    timer.initWithCallback(callback, aDelay || DEFAULT_WATCHER_DELAY,
   1.105 +                           Ci.nsITimer.TYPE_REPEATING_SLACK);
   1.106 +    let id = "timer-" + this.id++;
   1.107 +    this.timers[id] = timer;
   1.108 +    return id;
   1.109 +  },
   1.110 +
   1.111 +  /**
   1.112 +   * This function stops a running watcher.
   1.113 +   * @param aId The opaque timer id returned by create().
   1.114 +   */
   1.115 +  stop: function spaceWatcher_stop(aId) {
   1.116 +    if (this.timers[aId]) {
   1.117 +      this.timers[aId].cancel();
   1.118 +      delete this.timers[aId];
   1.119 +    }
   1.120 +  }
   1.121 +}

mercurial