mobile/android/components/WebappsUpdateTimer.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/components/WebappsUpdateTimer.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,65 @@
     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 a periodic webapp update check.
    1.10 + */
    1.11 +
    1.12 +"use strict";
    1.13 +
    1.14 +const Cc = Components.classes;
    1.15 +const Ci = Components.interfaces;
    1.16 +const Cu = Components.utils;
    1.17 +
    1.18 +Cu.import("resource://gre/modules/Services.jsm");
    1.19 +Cu.import("resource://gre/modules/WebappManager.jsm");
    1.20 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.21 +
    1.22 +function log(message) {
    1.23 +  // We use *dump* instead of Services.console.logStringMessage so the messages
    1.24 +  // have the INFO level of severity instead of the ERROR level.  And we don't
    1.25 +  // append a newline character to the end of the message because *dump* spills
    1.26 +  // into the Android native logging system, which strips newlines from messages
    1.27 +  // and breaks messages into lines automatically at display time (i.e. logcat).
    1.28 +  dump(message);
    1.29 +}
    1.30 +
    1.31 +function WebappsUpdateTimer() {}
    1.32 +
    1.33 +WebappsUpdateTimer.prototype = {
    1.34 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsITimerCallback,
    1.35 +                                         Ci.nsISupportsWeakReference]),
    1.36 +  classID: Components.ID("{8f7002cb-e959-4f0a-a2e8-563232564385}"),
    1.37 +
    1.38 +  notify: function(aTimer) {
    1.39 +    if (Services.prefs.getIntPref("browser.webapps.checkForUpdates") == 0) {
    1.40 +      // Do nothing, because updates are disabled.
    1.41 +      log("Webapps update timer invoked in webapp process; ignoring.");
    1.42 +      return;
    1.43 +    }
    1.44 +
    1.45 +    // If we are offline, wait to be online to start the update check.
    1.46 +    if (Services.io.offline) {
    1.47 +      log("network offline for webapp update check; waiting");
    1.48 +      Services.obs.addObserver(this, "network:offline-status-changed", true);
    1.49 +      return;
    1.50 +    }
    1.51 +
    1.52 +    log("periodic check for webapp updates");
    1.53 +    WebappManager.checkForUpdates();
    1.54 +  },
    1.55 +
    1.56 +  observe: function(aSubject, aTopic, aData) {
    1.57 +    if (aTopic !== "network:offline-status-changed" || aData !== "online") {
    1.58 +      return;
    1.59 +    }
    1.60 +
    1.61 +    log("network back online for webapp update check; commencing");
    1.62 +    // TODO: observe pref to do this only on wifi.
    1.63 +    Services.obs.removeObserver(this, "network:offline-status-changed");
    1.64 +    WebappManager.checkForUpdates();
    1.65 +  }
    1.66 +};
    1.67 +
    1.68 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([WebappsUpdateTimer]);

mercurial