b2g/components/WebappsUpdateTimer.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/b2g/components/WebappsUpdateTimer.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,67 @@
     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 +/**
     1.9 + * This component triggers an app update check even when system updates are
    1.10 + * disabled to make sure we always check for app updates.
    1.11 + */
    1.12 +
    1.13 +"use strict";
    1.14 +
    1.15 +const Cc = Components.classes;
    1.16 +const Ci = Components.interfaces;
    1.17 +const Cu = Components.utils;
    1.18 +
    1.19 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.20 +Cu.import("resource://gre/modules/Services.jsm");
    1.21 +Cu.import("resource://gre/modules/WebappsUpdater.jsm");
    1.22 +
    1.23 +function debug(aStr) {
    1.24 +  //dump("--*-- WebappsUpdateTimer: " + aStr);
    1.25 +}
    1.26 +
    1.27 +function WebappsUpdateTimer() {
    1.28 +}
    1.29 +
    1.30 +WebappsUpdateTimer.prototype = {
    1.31 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsITimerCallback]),
    1.32 +  classID: Components.ID("{637b0f77-2429-49a0-915f-abf5d0db8b9a}"),
    1.33 +
    1.34 +  notify: function(aTimer) {
    1.35 +    try {
    1.36 +      // We want to check app updates if system updates are disabled or
    1.37 +      // if they update frecency is not daily.
    1.38 +      if (Services.prefs.getBoolPref("app.update.enabled") === true &&
    1.39 +          Services.prefs.getIntPref("app.update.interval") === 86400) {
    1.40 +        return;
    1.41 +      }
    1.42 +    } catch(e) {
    1.43 +      // That should never happen..
    1.44 +    }
    1.45 +
    1.46 +    // If we are offline, wait to be online to start the update check.
    1.47 +    if (Services.io.offline) {
    1.48 +      debug("Network is offline. Setting up an offline status observer.");
    1.49 +      Services.obs.addObserver(this, "network:offline-status-changed", false);
    1.50 +      return;
    1.51 +    }
    1.52 +
    1.53 +    // This will trigger app updates in b2g/components/WebappsUpdater.jsm
    1.54 +    // that also takes care of notifying gaia.
    1.55 +    WebappsUpdater.updateApps();
    1.56 +  },
    1.57 +
    1.58 +  observe: function(aSubject, aTopic, aData) {
    1.59 +    if (aTopic !== "network:offline-status-changed" ||
    1.60 +        aData !== "online") {
    1.61 +      return;
    1.62 +    }
    1.63 +
    1.64 +    debug("Network is online. Checking updates.");
    1.65 +    Services.obs.removeObserver(this, "network:offline-status-changed");
    1.66 +    WebappsUpdater.updateApps();
    1.67 +  }
    1.68 +};
    1.69 +
    1.70 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([WebappsUpdateTimer]);

mercurial