b2g/components/WebappsUpdater.jsm

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     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 this.EXPORTED_SYMBOLS = ["WebappsUpdater"];
     9 const Cc = Components.classes;
    10 const Cu = Components.utils;
    12 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    13 Cu.import("resource://gre/modules/Services.jsm");
    15 XPCOMUtils.defineLazyServiceGetter(this, "settings",
    16                                    "@mozilla.org/settingsService;1",
    17                                    "nsISettingsService");
    19 XPCOMUtils.defineLazyModuleGetter(this, "SystemAppProxy",
    20                                   "resource://gre/modules/SystemAppProxy.jsm");
    22 function debug(aStr) {
    23   //dump("--*-- WebappsUpdater: " + aStr);
    24 }
    26 this.WebappsUpdater = {
    27   _checkingApps: false,
    29   handleContentStart: function() {
    30   },
    32   sendChromeEvent: function(aType, aDetail) {
    33     let detail = aDetail || {};
    34     detail.type = aType;
    36     let sent = SystemAppProxy.dispatchEvent(detail);
    37     if (!sent) {
    38       debug("Warning: Couldn't send update event " + aType +
    39           ": no content browser. Will send again when content becomes available.");
    40       return false;
    41     }
    43     return true;
    44   },
    46   _appsUpdated: function(aApps) {
    47     debug("appsUpdated: " + aApps.length + " apps to update");
    48     let lock = settings.createLock();
    49     lock.set("apps.updateStatus", "check-complete", null);
    50     this.sendChromeEvent("apps-update-check", { apps: aApps });
    51     this._checkingApps = false;
    52   },
    54   // Trigger apps update check and wait for all to be done before
    55   // notifying gaia.
    56   updateApps: function() {
    57     debug("updateApps (" + this._checkingApps + ")");
    58     // Don't start twice.
    59     if (this._checkingApps) {
    60       return;
    61     }
    63     this._checkingApps = true;
    65     let self = this;
    67     let window = Services.wm.getMostRecentWindow("navigator:browser");
    68     let all = window.navigator.mozApps.mgmt.getAll();
    70     all.onsuccess = function() {
    71       let appsCount = this.result.length;
    72       let appsChecked = 0;
    73       let appsToUpdate = [];
    74       this.result.forEach(function updateApp(aApp) {
    75         let update = aApp.checkForUpdate();
    76         update.onsuccess = function() {
    77           if (aApp.downloadAvailable) {
    78             appsToUpdate.push(aApp.manifestURL);
    79           }
    81           appsChecked += 1;
    82           if (appsChecked == appsCount) {
    83             self._appsUpdated(appsToUpdate);
    84           }
    85         }
    86         update.onerror = function() {
    87           appsChecked += 1;
    88           if (appsChecked == appsCount) {
    89             self._appsUpdated(appsToUpdate);
    90           }
    91         }
    92       });
    93     }
    95     all.onerror = function() {
    96       // Could not get the app list, just notify to update nothing.
    97       self._appsUpdated([]);
    98     }
    99   }
   100 };

mercurial