2012-03-21 22:50:53 +00:00
|
|
|
/* -*- Mode: Java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: sw=2 ts=8 et :
|
|
|
|
*/
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
2013-01-31 01:23:51 +00:00
|
|
|
const Cr = Components.results;
|
2012-03-21 22:50:53 +00:00
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2013-09-11 12:00:48 +00:00
|
|
|
Cu.import("resource://gre/modules/WebappsUpdater.jsm");
|
2012-03-21 22:50:53 +00:00
|
|
|
|
|
|
|
const VERBOSE = 1;
|
|
|
|
let log =
|
|
|
|
VERBOSE ?
|
|
|
|
function log_dump(msg) { dump("UpdatePrompt: "+ msg +"\n"); } :
|
|
|
|
function log_noop(msg) { };
|
|
|
|
|
2013-01-14 23:10:19 +00:00
|
|
|
const PREF_APPLY_PROMPT_TIMEOUT = "b2g.update.apply-prompt-timeout";
|
|
|
|
const PREF_APPLY_IDLE_TIMEOUT = "b2g.update.apply-idle-timeout";
|
|
|
|
const PREF_DOWNLOAD_WATCHDOG_TIMEOUT = "b2g.update.download-watchdog-timeout";
|
|
|
|
const PREF_DOWNLOAD_WATCHDOG_MAX_RETRIES = "b2g.update.download-watchdog-max-retries";
|
2012-12-26 17:28:42 +00:00
|
|
|
|
2012-10-16 19:15:37 +00:00
|
|
|
const NETWORK_ERROR_OFFLINE = 111;
|
2013-01-10 17:13:39 +00:00
|
|
|
const HTTP_ERROR_OFFSET = 1000;
|
2012-12-15 00:05:39 +00:00
|
|
|
|
|
|
|
const STATE_DOWNLOADING = 'downloading';
|
2012-10-02 18:33:49 +00:00
|
|
|
|
2012-09-12 16:53:03 +00:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(Services, "aus",
|
|
|
|
"@mozilla.org/updates/update-service;1",
|
|
|
|
"nsIApplicationUpdateService");
|
|
|
|
|
2012-10-16 19:15:37 +00:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(Services, "um",
|
|
|
|
"@mozilla.org/updates/update-manager;1",
|
|
|
|
"nsIUpdateManager");
|
|
|
|
|
2012-10-02 18:33:49 +00:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(Services, "idle",
|
|
|
|
"@mozilla.org/widget/idleservice;1",
|
|
|
|
"nsIIdleService");
|
2012-10-13 18:11:09 +00:00
|
|
|
|
2012-10-16 19:15:37 +00:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(Services, "settings",
|
|
|
|
"@mozilla.org/settingsService;1",
|
|
|
|
"nsISettingsService");
|
|
|
|
|
2012-10-23 18:51:16 +00:00
|
|
|
function UpdateCheckListener(updatePrompt) {
|
|
|
|
this._updatePrompt = updatePrompt;
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateCheckListener.prototype = {
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIUpdateCheckListener]),
|
|
|
|
|
|
|
|
_updatePrompt: null,
|
|
|
|
|
|
|
|
onCheckComplete: function UCL_onCheckComplete(request, updates, updateCount) {
|
2013-01-25 18:32:56 +00:00
|
|
|
if (Services.um.activeUpdate) {
|
2013-01-25 18:27:49 +00:00
|
|
|
// We're actively downloading an update, that's the update the user should
|
|
|
|
// see, even if a newer update is available.
|
|
|
|
this._updatePrompt.setUpdateStatus("active-update");
|
|
|
|
this._updatePrompt.showUpdateAvailable(Services.um.activeUpdate);
|
2012-10-23 18:51:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updateCount == 0) {
|
|
|
|
this._updatePrompt.setUpdateStatus("no-updates");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let update = Services.aus.selectUpdate(updates, updateCount);
|
|
|
|
if (!update) {
|
|
|
|
this._updatePrompt.setUpdateStatus("already-latest-version");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._updatePrompt.setUpdateStatus("check-complete");
|
|
|
|
this._updatePrompt.showUpdateAvailable(update);
|
|
|
|
},
|
|
|
|
|
|
|
|
onError: function UCL_onError(request, update) {
|
2013-01-10 17:13:39 +00:00
|
|
|
// nsIUpdate uses a signed integer for errorCode while any platform errors
|
|
|
|
// require all 32 bits.
|
|
|
|
let errorCode = update.errorCode >>> 0;
|
|
|
|
let isNSError = (errorCode >>> 31) == 1;
|
|
|
|
|
|
|
|
if (errorCode == NETWORK_ERROR_OFFLINE) {
|
2012-10-23 18:51:16 +00:00
|
|
|
this._updatePrompt.setUpdateStatus("retry-when-online");
|
2013-01-10 17:13:39 +00:00
|
|
|
} else if (isNSError) {
|
|
|
|
this._updatePrompt.setUpdateStatus("check-error-" + errorCode);
|
|
|
|
} else if (errorCode > HTTP_ERROR_OFFSET) {
|
|
|
|
let httpErrorCode = errorCode - HTTP_ERROR_OFFSET;
|
|
|
|
this._updatePrompt.setUpdateStatus("check-error-http-" + httpErrorCode);
|
2012-10-23 18:51:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Services.aus.QueryInterface(Ci.nsIUpdateCheckListener);
|
|
|
|
Services.aus.onError(request, update);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-13 18:11:09 +00:00
|
|
|
function UpdatePrompt() {
|
|
|
|
this.wrappedJSObject = this;
|
2012-10-23 18:51:16 +00:00
|
|
|
this._updateCheckListener = new UpdateCheckListener(this);
|
2012-11-15 20:52:19 +00:00
|
|
|
Services.obs.addObserver(this, "update-check-start", false);
|
2012-10-13 18:11:09 +00:00
|
|
|
}
|
2012-03-21 22:50:53 +00:00
|
|
|
|
|
|
|
UpdatePrompt.prototype = {
|
|
|
|
classID: Components.ID("{88b3eb21-d072-4e3b-886d-f89d8c49fe59}"),
|
2012-09-12 16:53:03 +00:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIUpdatePrompt,
|
2012-10-16 19:15:37 +00:00
|
|
|
Ci.nsIUpdateCheckListener,
|
2012-09-12 16:53:03 +00:00
|
|
|
Ci.nsIRequestObserver,
|
2012-10-02 18:33:49 +00:00
|
|
|
Ci.nsIProgressEventSink,
|
|
|
|
Ci.nsIObserver]),
|
2012-10-13 18:11:09 +00:00
|
|
|
_xpcom_factory: XPCOMUtils.generateSingletonFactory(UpdatePrompt),
|
2012-03-21 22:50:53 +00:00
|
|
|
|
2012-09-12 16:53:03 +00:00
|
|
|
_update: null,
|
|
|
|
_applyPromptTimer: null,
|
2012-10-02 18:33:49 +00:00
|
|
|
_waitingForIdle: false,
|
2012-10-23 18:51:16 +00:00
|
|
|
_updateCheckListner: null,
|
2013-01-15 07:40:51 +00:00
|
|
|
_pendingEvents: [],
|
2012-03-26 07:46:21 +00:00
|
|
|
|
2012-12-26 17:28:42 +00:00
|
|
|
get applyPromptTimeout() {
|
|
|
|
return Services.prefs.getIntPref(PREF_APPLY_PROMPT_TIMEOUT);
|
|
|
|
},
|
|
|
|
|
|
|
|
get applyIdleTimeout() {
|
|
|
|
return Services.prefs.getIntPref(PREF_APPLY_IDLE_TIMEOUT);
|
|
|
|
},
|
|
|
|
|
2013-01-15 07:40:51 +00:00
|
|
|
handleContentStart: function UP_handleContentStart(shell) {
|
|
|
|
let content = shell.contentBrowser.contentWindow;
|
|
|
|
content.addEventListener("mozContentEvent", this);
|
|
|
|
|
|
|
|
for (let i = 0; i < this._pendingEvents.length; i++) {
|
|
|
|
shell.sendChromeEvent(this._pendingEvents[i]);
|
|
|
|
}
|
|
|
|
this._pendingEvents.length = 0;
|
|
|
|
},
|
|
|
|
|
2012-03-21 22:50:53 +00:00
|
|
|
// nsIUpdatePrompt
|
|
|
|
|
|
|
|
// FIXME/bug 737601: we should have users opt-in to downloading
|
|
|
|
// updates when on a billed pipe. Initially, opt-in for 3g, but
|
|
|
|
// that doesn't cover all cases.
|
|
|
|
checkForUpdates: function UP_checkForUpdates() { },
|
2012-09-12 16:53:03 +00:00
|
|
|
|
|
|
|
showUpdateAvailable: function UP_showUpdateAvailable(aUpdate) {
|
2012-10-13 18:11:09 +00:00
|
|
|
if (!this.sendUpdateEvent("update-available", aUpdate)) {
|
2012-09-12 16:53:03 +00:00
|
|
|
|
|
|
|
log("Unable to prompt for available update, forcing download");
|
|
|
|
this.downloadUpdate(aUpdate);
|
|
|
|
}
|
|
|
|
},
|
2012-03-21 22:50:53 +00:00
|
|
|
|
|
|
|
showUpdateDownloaded: function UP_showUpdateDownloaded(aUpdate, aBackground) {
|
2012-10-02 18:33:49 +00:00
|
|
|
// The update has been downloaded and staged. We send the update-downloaded
|
|
|
|
// event right away. After the user has been idle for a while, we send the
|
|
|
|
// update-prompt-restart event, increasing the chances that we can apply the
|
|
|
|
// update quietly without user intervention.
|
|
|
|
this.sendUpdateEvent("update-downloaded", aUpdate);
|
|
|
|
|
2012-12-26 17:28:42 +00:00
|
|
|
if (Services.idle.idleTime >= this.applyIdleTimeout) {
|
2012-10-02 18:33:49 +00:00
|
|
|
this.showApplyPrompt(aUpdate);
|
2012-09-12 16:53:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-26 17:28:42 +00:00
|
|
|
let applyIdleTimeoutSeconds = this.applyIdleTimeout / 1000;
|
2012-10-02 18:33:49 +00:00
|
|
|
// We haven't been idle long enough, so register an observer
|
|
|
|
log("Update is ready to apply, registering idle timeout of " +
|
2012-12-26 17:28:42 +00:00
|
|
|
applyIdleTimeoutSeconds + " seconds before prompting.");
|
2012-10-02 18:33:49 +00:00
|
|
|
|
|
|
|
this._update = aUpdate;
|
|
|
|
this.waitForIdle();
|
2012-09-12 16:53:03 +00:00
|
|
|
},
|
|
|
|
|
2012-09-12 16:53:03 +00:00
|
|
|
showUpdateError: function UP_showUpdateError(aUpdate) {
|
2012-10-16 19:15:37 +00:00
|
|
|
log("Update error, state: " + aUpdate.state + ", errorCode: " +
|
|
|
|
aUpdate.errorCode);
|
|
|
|
this.sendUpdateEvent("update-error", aUpdate);
|
|
|
|
this.setUpdateStatus(aUpdate.statusText);
|
2012-09-12 16:53:03 +00:00
|
|
|
},
|
2012-09-12 16:53:03 +00:00
|
|
|
|
2012-09-12 16:53:03 +00:00
|
|
|
showUpdateHistory: function UP_showUpdateHistory(aParent) { },
|
2012-11-27 17:53:17 +00:00
|
|
|
showUpdateInstalled: function UP_showUpdateInstalled() {
|
|
|
|
let lock = Services.settings.createLock();
|
|
|
|
lock.set("deviceinfo.last_updated", Date.now(), null, null);
|
|
|
|
},
|
2012-09-12 16:53:03 +00:00
|
|
|
|
2012-10-02 18:33:49 +00:00
|
|
|
// Custom functions
|
|
|
|
|
|
|
|
waitForIdle: function UP_waitForIdle() {
|
|
|
|
if (this._waitingForIdle) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._waitingForIdle = true;
|
2012-12-26 17:28:42 +00:00
|
|
|
Services.idle.addIdleObserver(this, this.applyIdleTimeout / 1000);
|
2012-10-02 18:33:49 +00:00
|
|
|
Services.obs.addObserver(this, "quit-application", false);
|
|
|
|
},
|
|
|
|
|
2012-10-16 19:15:37 +00:00
|
|
|
setUpdateStatus: function UP_setUpdateStatus(aStatus) {
|
|
|
|
log("Setting gecko.updateStatus: " + aStatus);
|
|
|
|
|
|
|
|
let lock = Services.settings.createLock();
|
|
|
|
lock.set("gecko.updateStatus", aStatus, null);
|
|
|
|
},
|
|
|
|
|
2012-10-02 18:33:49 +00:00
|
|
|
showApplyPrompt: function UP_showApplyPrompt(aUpdate) {
|
2012-10-13 18:11:09 +00:00
|
|
|
if (!this.sendUpdateEvent("update-prompt-apply", aUpdate)) {
|
2012-10-02 18:33:49 +00:00
|
|
|
log("Unable to prompt, forcing restart");
|
|
|
|
this.restartProcess();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-10 15:12:34 +00:00
|
|
|
#ifdef MOZ_B2G_RIL
|
|
|
|
let window = Services.wm.getMostRecentWindow("navigator:browser");
|
|
|
|
let pinReq = window.navigator.mozIccManager.getCardLock("pin");
|
|
|
|
pinReq.onsuccess = function(e) {
|
|
|
|
if (e.target.result.enabled) {
|
|
|
|
// The SIM is pin locked. Don't use a fallback timer. This means that
|
|
|
|
// the user has to press Install to apply the update. If we use the
|
|
|
|
// timer, and the timer reboots the phone, then the phone will be
|
|
|
|
// unusable until the SIM is unlocked.
|
|
|
|
log("SIM is pin locked. Not starting fallback timer.");
|
|
|
|
} else {
|
|
|
|
// This means that no pin lock is enabled, so we go ahead and start
|
|
|
|
// the fallback timer.
|
|
|
|
this._applyPromptTimer = this.createTimer(this.applyPromptTimeout);
|
|
|
|
}
|
|
|
|
}.bind(this);
|
|
|
|
pinReq.onerror = function(e) {
|
|
|
|
this._applyPromptTimer = this.createTimer(this.applyPromptTimeout);
|
|
|
|
}.bind(this);
|
|
|
|
#else
|
2012-10-02 18:33:49 +00:00
|
|
|
// Schedule a fallback timeout in case the UI is unable to respond or show
|
|
|
|
// a prompt for some reason.
|
2012-12-26 17:28:42 +00:00
|
|
|
this._applyPromptTimer = this.createTimer(this.applyPromptTimeout);
|
2013-09-10 15:12:34 +00:00
|
|
|
#endif
|
2012-10-02 18:33:49 +00:00
|
|
|
},
|
|
|
|
|
2013-01-02 18:41:40 +00:00
|
|
|
_copyProperties: ["appVersion", "buildID", "detailsURL", "displayVersion",
|
|
|
|
"errorCode", "isOSUpdate", "platformVersion",
|
|
|
|
"previousAppVersion", "state", "statusText"],
|
|
|
|
|
2012-10-13 18:11:09 +00:00
|
|
|
sendUpdateEvent: function UP_sendUpdateEvent(aType, aUpdate) {
|
2013-01-02 18:41:40 +00:00
|
|
|
let detail = {};
|
|
|
|
for each (let property in this._copyProperties) {
|
|
|
|
detail[property] = aUpdate[property];
|
|
|
|
}
|
2012-09-12 16:53:03 +00:00
|
|
|
|
|
|
|
let patch = aUpdate.selectedPatch;
|
2012-10-16 19:15:37 +00:00
|
|
|
if (!patch && aUpdate.patchCount > 0) {
|
2012-09-12 16:53:03 +00:00
|
|
|
// For now we just check the first patch to get size information if a
|
|
|
|
// patch hasn't been selected yet.
|
|
|
|
patch = aUpdate.getPatchAt(0);
|
|
|
|
}
|
|
|
|
|
2012-10-16 19:15:37 +00:00
|
|
|
if (patch) {
|
|
|
|
detail.size = patch.size;
|
|
|
|
detail.updateType = patch.type;
|
|
|
|
} else {
|
|
|
|
log("Warning: no patches available in update");
|
|
|
|
}
|
2012-09-12 16:53:03 +00:00
|
|
|
|
2012-09-12 16:53:03 +00:00
|
|
|
this._update = aUpdate;
|
2012-10-13 18:11:09 +00:00
|
|
|
return this.sendChromeEvent(aType, detail);
|
2012-09-12 16:53:03 +00:00
|
|
|
},
|
|
|
|
|
2012-10-13 18:11:09 +00:00
|
|
|
sendChromeEvent: function UP_sendChromeEvent(aType, aDetail) {
|
2013-01-15 07:40:51 +00:00
|
|
|
let detail = aDetail || {};
|
|
|
|
detail.type = aType;
|
|
|
|
|
2012-09-12 16:53:03 +00:00
|
|
|
let browser = Services.wm.getMostRecentWindow("navigator:browser");
|
|
|
|
if (!browser) {
|
2013-01-15 07:40:51 +00:00
|
|
|
this._pendingEvents.push(detail);
|
2012-09-12 16:53:03 +00:00
|
|
|
log("Warning: Couldn't send update event " + aType +
|
2013-01-15 07:40:51 +00:00
|
|
|
": no content browser. Will send again when content becomes available.");
|
2012-09-12 16:53:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-12 16:53:03 +00:00
|
|
|
browser.shell.sendChromeEvent(detail);
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2012-09-12 16:53:03 +00:00
|
|
|
handleAvailableResult: function UP_handleAvailableResult(aDetail) {
|
|
|
|
// If the user doesn't choose "download", the updater will implicitly call
|
|
|
|
// showUpdateAvailable again after a certain period of time
|
|
|
|
switch (aDetail.result) {
|
|
|
|
case "download":
|
|
|
|
this.downloadUpdate(this._update);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-10-02 18:33:49 +00:00
|
|
|
handleApplyPromptResult: function UP_handleApplyPromptResult(aDetail) {
|
2012-09-12 16:53:03 +00:00
|
|
|
if (this._applyPromptTimer) {
|
|
|
|
this._applyPromptTimer.cancel();
|
|
|
|
this._applyPromptTimer = null;
|
|
|
|
}
|
2012-03-21 22:50:53 +00:00
|
|
|
|
2012-09-12 16:53:03 +00:00
|
|
|
switch (aDetail.result) {
|
|
|
|
case "wait":
|
2012-10-02 18:33:49 +00:00
|
|
|
// Wait until the user is idle before prompting to apply the update
|
|
|
|
this.waitForIdle();
|
2012-09-12 16:53:03 +00:00
|
|
|
break;
|
|
|
|
case "restart":
|
2012-10-01 02:55:51 +00:00
|
|
|
this.finishUpdate();
|
2012-10-13 18:11:09 +00:00
|
|
|
this._update = null;
|
2012-09-12 16:53:03 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-09-12 16:53:03 +00:00
|
|
|
downloadUpdate: function UP_downloadUpdate(aUpdate) {
|
2012-11-30 19:35:54 +00:00
|
|
|
if (!aUpdate) {
|
|
|
|
aUpdate = Services.um.activeUpdate;
|
|
|
|
if (!aUpdate) {
|
|
|
|
log("No active update found to download");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-15 00:05:39 +00:00
|
|
|
let status = Services.aus.downloadUpdate(aUpdate, true);
|
|
|
|
if (status == STATE_DOWNLOADING) {
|
|
|
|
Services.aus.addDownloadListener(this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-22 22:57:10 +00:00
|
|
|
// If the update has already been downloaded and applied, then
|
|
|
|
// Services.aus.downloadUpdate will return immediately and not
|
|
|
|
// call showUpdateDownloaded, so we detect this.
|
|
|
|
if (aUpdate.state == "applied" && aUpdate.errorCode == 0) {
|
|
|
|
this.showUpdateDownloaded(aUpdate, true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-15 00:05:39 +00:00
|
|
|
log("Error downloading update " + aUpdate.name + ": " + aUpdate.errorCode);
|
2013-02-26 03:11:14 +00:00
|
|
|
let errorCode = aUpdate.errorCode >>> 0;
|
|
|
|
if (errorCode == Cr.NS_ERROR_FILE_TOO_BIG) {
|
2012-12-15 00:05:39 +00:00
|
|
|
aUpdate.statusText = "file-too-big";
|
|
|
|
}
|
|
|
|
this.showUpdateError(aUpdate);
|
2012-09-12 16:53:03 +00:00
|
|
|
},
|
|
|
|
|
2012-11-30 19:35:54 +00:00
|
|
|
handleDownloadCancel: function UP_handleDownloadCancel() {
|
|
|
|
log("Pausing download");
|
|
|
|
Services.aus.pauseDownload();
|
|
|
|
},
|
|
|
|
|
2012-10-01 02:55:51 +00:00
|
|
|
finishUpdate: function UP_finishUpdate() {
|
|
|
|
if (!this._update.isOSUpdate) {
|
|
|
|
// Standard gecko+gaia updates will just need to restart the process
|
|
|
|
this.restartProcess();
|
|
|
|
return;
|
|
|
|
}
|
2013-10-31 12:05:08 +00:00
|
|
|
|
2012-10-01 02:55:51 +00:00
|
|
|
try {
|
2013-10-31 12:05:08 +00:00
|
|
|
Services.aus.applyOsUpdate(this._update);
|
2012-10-01 02:55:51 +00:00
|
|
|
}
|
2013-10-31 12:05:08 +00:00
|
|
|
catch (e) {
|
|
|
|
this._update.errorCode = Cr.NS_ERROR_FAILURE;
|
|
|
|
this.showUpdateError(this._update);
|
2012-10-01 02:55:51 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-09-12 16:53:03 +00:00
|
|
|
restartProcess: function UP_restartProcess() {
|
2012-03-21 22:50:53 +00:00
|
|
|
log("Update downloaded, restarting to apply it");
|
|
|
|
|
2012-10-02 07:26:32 +00:00
|
|
|
#ifndef MOZ_WIDGET_GONK
|
2012-09-12 16:53:03 +00:00
|
|
|
let appStartup = Cc["@mozilla.org/toolkit/app-startup;1"]
|
2012-10-02 07:26:32 +00:00
|
|
|
.getService(Ci.nsIAppStartup);
|
|
|
|
appStartup.quit(appStartup.eForceQuit | appStartup.eRestart);
|
|
|
|
#else
|
|
|
|
// NB: on Gonk, we rely on the system process manager to restart us.
|
|
|
|
let pmService = Cc["@mozilla.org/power/powermanagerservice;1"]
|
|
|
|
.getService(Ci.nsIPowerManagerService);
|
|
|
|
pmService.restart();
|
2012-03-21 22:50:53 +00:00
|
|
|
#endif
|
|
|
|
},
|
|
|
|
|
2012-10-13 18:11:09 +00:00
|
|
|
forceUpdateCheck: function UP_forceUpdateCheck() {
|
|
|
|
log("Forcing update check");
|
|
|
|
|
|
|
|
let checker = Cc["@mozilla.org/updates/update-checker;1"]
|
|
|
|
.createInstance(Ci.nsIUpdateChecker);
|
2012-10-23 18:51:16 +00:00
|
|
|
checker.checkForUpdates(this._updateCheckListener, true);
|
2012-10-13 18:11:09 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
handleEvent: function UP_handleEvent(evt) {
|
|
|
|
if (evt.type !== "mozContentEvent") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let detail = evt.detail;
|
|
|
|
if (!detail) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (detail.type) {
|
|
|
|
case "force-update-check":
|
|
|
|
this.forceUpdateCheck();
|
|
|
|
break;
|
|
|
|
case "update-available-result":
|
|
|
|
this.handleAvailableResult(detail);
|
2013-02-22 22:57:10 +00:00
|
|
|
// If we started the apply prompt timer, this means that we're waiting
|
|
|
|
// for the user to press Later or Install Now. In this situation we
|
|
|
|
// don't want to clear this._update, becuase handleApplyPromptResult
|
|
|
|
// needs it.
|
2013-04-17 13:29:50 +00:00
|
|
|
if (this._applyPromptTimer == null && !this._waitingForIdle) {
|
2013-02-22 22:57:10 +00:00
|
|
|
this._update = null;
|
|
|
|
}
|
2012-10-13 18:11:09 +00:00
|
|
|
break;
|
2012-11-30 19:35:54 +00:00
|
|
|
case "update-download-cancel":
|
|
|
|
this.handleDownloadCancel();
|
|
|
|
break;
|
2012-10-13 18:11:09 +00:00
|
|
|
case "update-prompt-apply-result":
|
|
|
|
this.handleApplyPromptResult(detail);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-10-02 18:33:49 +00:00
|
|
|
// nsIObserver
|
|
|
|
|
|
|
|
observe: function UP_observe(aSubject, aTopic, aData) {
|
|
|
|
switch (aTopic) {
|
|
|
|
case "idle":
|
|
|
|
this._waitingForIdle = false;
|
|
|
|
this.showApplyPrompt(this._update);
|
|
|
|
// Fall through
|
|
|
|
case "quit-application":
|
2012-12-26 17:28:42 +00:00
|
|
|
Services.idle.removeIdleObserver(this, this.applyIdleTimeout / 1000);
|
2012-10-02 18:33:49 +00:00
|
|
|
Services.obs.removeObserver(this, "quit-application");
|
|
|
|
break;
|
2012-11-15 20:52:19 +00:00
|
|
|
case "update-check-start":
|
2013-09-11 12:00:48 +00:00
|
|
|
WebappsUpdater.updateApps();
|
2012-11-15 20:52:19 +00:00
|
|
|
break;
|
2012-10-02 18:33:49 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-10-13 18:11:09 +00:00
|
|
|
// nsITimerCallback
|
|
|
|
|
2012-09-12 16:53:03 +00:00
|
|
|
notify: function UP_notify(aTimer) {
|
2012-09-19 23:13:31 +00:00
|
|
|
if (aTimer == this._applyPromptTimer) {
|
2012-09-12 16:53:03 +00:00
|
|
|
log("Timed out waiting for result, restarting");
|
|
|
|
this._applyPromptTimer = null;
|
2012-10-01 02:55:51 +00:00
|
|
|
this.finishUpdate();
|
2013-02-22 22:57:10 +00:00
|
|
|
this._update = null;
|
2013-08-20 17:41:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (aTimer == this._watchdogTimer) {
|
|
|
|
log("Download watchdog fired");
|
|
|
|
this._watchdogTimer = null;
|
|
|
|
this._autoRestartDownload = true;
|
|
|
|
Services.aus.pauseDownload();
|
|
|
|
return;
|
2012-09-12 16:53:03 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
createTimer: function UP_createTimer(aTimeoutMs) {
|
|
|
|
let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
timer.initWithCallback(this, aTimeoutMs, timer.TYPE_ONE_SHOT);
|
|
|
|
return timer;
|
|
|
|
},
|
|
|
|
|
2012-09-12 16:53:03 +00:00
|
|
|
// nsIRequestObserver
|
2012-03-21 22:50:53 +00:00
|
|
|
|
2013-01-08 16:13:32 +00:00
|
|
|
_startedSent: false,
|
|
|
|
|
2013-01-14 23:10:19 +00:00
|
|
|
_watchdogTimer: null,
|
|
|
|
|
|
|
|
_autoRestartDownload: false,
|
|
|
|
_autoRestartCount: 0,
|
|
|
|
|
|
|
|
startWatchdogTimer: function UP_startWatchdogTimer() {
|
2013-08-20 17:41:23 +00:00
|
|
|
let watchdogTimeout = 120000; // 120 seconds
|
|
|
|
try {
|
|
|
|
watchdogTimeout = Services.prefs.getIntPref(PREF_DOWNLOAD_WATCHDOG_TIMEOUT);
|
|
|
|
} catch (e) {
|
|
|
|
// This means that the preference doesn't exist. watchdogTimeout will
|
|
|
|
// retain its default assigned above.
|
|
|
|
}
|
|
|
|
if (watchdogTimeout <= 0) {
|
|
|
|
// 0 implies don't bother using the watchdog timer at all.
|
|
|
|
this._watchdogTimer = null;
|
|
|
|
return;
|
|
|
|
}
|
2013-01-14 23:10:19 +00:00
|
|
|
if (this._watchdogTimer) {
|
|
|
|
this._watchdogTimer.cancel();
|
|
|
|
} else {
|
|
|
|
this._watchdogTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
}
|
2013-08-20 17:41:23 +00:00
|
|
|
this._watchdogTimer.initWithCallback(this, watchdogTimeout,
|
2013-01-14 23:10:19 +00:00
|
|
|
Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
},
|
|
|
|
|
|
|
|
stopWatchdogTimer: function UP_stopWatchdogTimer() {
|
|
|
|
if (this._watchdogTimer) {
|
|
|
|
this._watchdogTimer.cancel();
|
|
|
|
this._watchdogTimer = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
touchWatchdogTimer: function UP_touchWatchdogTimer() {
|
|
|
|
this.startWatchdogTimer();
|
|
|
|
},
|
|
|
|
|
2012-09-12 16:53:03 +00:00
|
|
|
onStartRequest: function UP_onStartRequest(aRequest, aContext) {
|
2013-01-08 16:13:32 +00:00
|
|
|
// Wait until onProgress to send the update-download-started event, in case
|
|
|
|
// this request turns out to fail for some reason
|
|
|
|
this._startedSent = false;
|
2013-01-14 23:10:19 +00:00
|
|
|
this.startWatchdogTimer();
|
2012-03-21 22:50:53 +00:00
|
|
|
},
|
|
|
|
|
2012-09-12 16:53:03 +00:00
|
|
|
onStopRequest: function UP_onStopRequest(aRequest, aContext, aStatusCode) {
|
2013-01-14 23:10:19 +00:00
|
|
|
this.stopWatchdogTimer();
|
|
|
|
Services.aus.removeDownloadListener(this);
|
2013-01-08 16:13:32 +00:00
|
|
|
let paused = !Components.isSuccessCode(aStatusCode);
|
2013-01-14 23:10:19 +00:00
|
|
|
if (!paused) {
|
|
|
|
// The download was successful, no need to restart
|
|
|
|
this._autoRestartDownload = false;
|
|
|
|
}
|
|
|
|
if (this._autoRestartDownload) {
|
|
|
|
this._autoRestartDownload = false;
|
|
|
|
let watchdogMaxRetries = Services.prefs.getIntPref(PREF_DOWNLOAD_WATCHDOG_MAX_RETRIES);
|
|
|
|
this._autoRestartCount++;
|
|
|
|
if (this._autoRestartCount > watchdogMaxRetries) {
|
|
|
|
log("Download - retry count exceeded - error");
|
|
|
|
// We exceeded the max retries. Treat the download like an error,
|
|
|
|
// which will give the user a chance to restart manually later.
|
|
|
|
this._autoRestartCount = 0;
|
|
|
|
if (Services.um.activeUpdate) {
|
|
|
|
this.showUpdateError(Services.um.activeUpdate);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
log("Download - restarting download - attempt " + this._autoRestartCount);
|
|
|
|
this.downloadUpdate(null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._autoRestartCount = 0;
|
2013-01-08 16:13:32 +00:00
|
|
|
this.sendChromeEvent("update-download-stopped", {
|
2013-01-14 23:10:19 +00:00
|
|
|
paused: paused
|
2013-01-08 16:13:32 +00:00
|
|
|
});
|
2012-09-12 16:53:03 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// nsIProgressEventSink
|
|
|
|
|
|
|
|
onProgress: function UP_onProgress(aRequest, aContext, aProgress,
|
|
|
|
aProgressMax) {
|
2013-01-14 23:10:19 +00:00
|
|
|
if (aProgress == aProgressMax) {
|
|
|
|
// The update.mar validation done by onStopRequest may take
|
|
|
|
// a while before the onStopRequest callback is made, so stop
|
|
|
|
// the timer now.
|
|
|
|
this.stopWatchdogTimer();
|
|
|
|
} else {
|
|
|
|
this.touchWatchdogTimer();
|
|
|
|
}
|
2013-01-08 16:13:32 +00:00
|
|
|
if (!this._startedSent) {
|
|
|
|
this.sendChromeEvent("update-download-started", {
|
|
|
|
total: aProgressMax
|
|
|
|
});
|
|
|
|
this._startedSent = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.sendChromeEvent("update-download-progress", {
|
2012-09-12 16:53:03 +00:00
|
|
|
progress: aProgress,
|
|
|
|
total: aProgressMax
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
onStatus: function UP_onStatus(aRequest, aUpdate, aStatus, aStatusArg) { }
|
2012-03-21 22:50:53 +00:00
|
|
|
};
|
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([UpdatePrompt]);
|