dom/apps/src/FreeSpaceWatcher.jsm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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/. */
     5 "use strict";
     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");
    18 }
    20 // Polling delay for free space, in ms.
    21 const DEFAULT_WATCHER_DELAY = 1000;
    23 this.FreeSpaceWatcher = {
    24   timers: {},
    25   id: 0,
    27   /**
    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().
    38    */
    39   create: function spaceWatcher_create(aThreshold, aOnStatusChange, aDelay) {
    40     let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
    41     debug("Creating new FreeSpaceWatcher");
    42     let callback = {
    43       currentStatus: null,
    44       notify: function(aTimer) {
    45         try {
    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;
    53             }
    54           };
    56           let navigator = Services.wm.getMostRecentWindow("navigator:browser")
    57                                   .navigator;
    58           let deviceStorage = null;
    60           if (navigator.getDeviceStorage) {
    61             deviceStorage = navigator.getDeviceStorage("apps");
    62           }
    64           if (deviceStorage) {
    65             let req = deviceStorage.freeSpace();
    66             req.onsuccess = req.onerror = function statResult(e) {
    67               if (!e.target.result) {
    68                 return;
    69               }
    71               let freeBytes = e.target.result;
    72               checkFreeSpace(freeBytes);
    73             }
    74           } else {
    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);
    79             let freeBytes;
    80             try {
    81               freeBytes = dir.diskSpaceAvailable;
    82             } catch(e) {
    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;
    87             }
    88             if (freeBytes) {
    89               // We have disk space information. Call this here so that
    90               // any exceptions are caught in the outer catch block.
    91               checkFreeSpace(freeBytes);
    92             }
    93           }
    94         } catch(e) {
    95           // If the aOnStatusChange callback has errored we'll end up here.
    96           debug(e);
    97         }
    98       }
    99     }
   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;
   105     return id;
   106   },
   108   /**
   109    * This function stops a running watcher.
   110    * @param aId The opaque timer id returned by create().
   111    */
   112   stop: function spaceWatcher_stop(aId) {
   113     if (this.timers[aId]) {
   114       this.timers[aId].cancel();
   115       delete this.timers[aId];
   116     }
   117   }
   118 }

mercurial