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