michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: this.EXPORTED_SYMBOLS = ["WebappsUpdater"]; michael@0: michael@0: const Cc = Components.classes; michael@0: const Cu = Components.utils; michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyServiceGetter(this, "settings", michael@0: "@mozilla.org/settingsService;1", michael@0: "nsISettingsService"); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "SystemAppProxy", michael@0: "resource://gre/modules/SystemAppProxy.jsm"); michael@0: michael@0: function debug(aStr) { michael@0: //dump("--*-- WebappsUpdater: " + aStr); michael@0: } michael@0: michael@0: this.WebappsUpdater = { michael@0: _checkingApps: false, michael@0: michael@0: handleContentStart: function() { michael@0: }, michael@0: michael@0: sendChromeEvent: function(aType, aDetail) { michael@0: let detail = aDetail || {}; michael@0: detail.type = aType; michael@0: michael@0: let sent = SystemAppProxy.dispatchEvent(detail); michael@0: if (!sent) { michael@0: debug("Warning: Couldn't send update event " + aType + michael@0: ": no content browser. Will send again when content becomes available."); michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: }, michael@0: michael@0: _appsUpdated: function(aApps) { michael@0: debug("appsUpdated: " + aApps.length + " apps to update"); michael@0: let lock = settings.createLock(); michael@0: lock.set("apps.updateStatus", "check-complete", null); michael@0: this.sendChromeEvent("apps-update-check", { apps: aApps }); michael@0: this._checkingApps = false; michael@0: }, michael@0: michael@0: // Trigger apps update check and wait for all to be done before michael@0: // notifying gaia. michael@0: updateApps: function() { michael@0: debug("updateApps (" + this._checkingApps + ")"); michael@0: // Don't start twice. michael@0: if (this._checkingApps) { michael@0: return; michael@0: } michael@0: michael@0: this._checkingApps = true; michael@0: michael@0: let self = this; michael@0: michael@0: let window = Services.wm.getMostRecentWindow("navigator:browser"); michael@0: let all = window.navigator.mozApps.mgmt.getAll(); michael@0: michael@0: all.onsuccess = function() { michael@0: let appsCount = this.result.length; michael@0: let appsChecked = 0; michael@0: let appsToUpdate = []; michael@0: this.result.forEach(function updateApp(aApp) { michael@0: let update = aApp.checkForUpdate(); michael@0: update.onsuccess = function() { michael@0: if (aApp.downloadAvailable) { michael@0: appsToUpdate.push(aApp.manifestURL); michael@0: } michael@0: michael@0: appsChecked += 1; michael@0: if (appsChecked == appsCount) { michael@0: self._appsUpdated(appsToUpdate); michael@0: } michael@0: } michael@0: update.onerror = function() { michael@0: appsChecked += 1; michael@0: if (appsChecked == appsCount) { michael@0: self._appsUpdated(appsToUpdate); michael@0: } michael@0: } michael@0: }); michael@0: } michael@0: michael@0: all.onerror = function() { michael@0: // Could not get the app list, just notify to update nothing. michael@0: self._appsUpdated([]); michael@0: } michael@0: } michael@0: };