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;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
|
|
|
const VERBOSE = 1;
|
|
|
|
let log =
|
|
|
|
VERBOSE ?
|
|
|
|
function log_dump(msg) { dump("UpdatePrompt: "+ msg +"\n"); } :
|
|
|
|
function log_noop(msg) { };
|
|
|
|
|
2012-09-12 16:53:03 +00:00
|
|
|
const APPLY_PROMPT_TIMEOUT =
|
|
|
|
Services.prefs.getIntPref("b2g.update.apply-prompt-timeout");
|
|
|
|
const APPLY_WAIT_TIMEOUT =
|
|
|
|
Services.prefs.getIntPref("b2g.update.apply-wait-timeout");
|
|
|
|
const SELF_DESTRUCT_TIMEOUT =
|
|
|
|
Services.prefs.getIntPref("b2g.update.self-destruct-timeout");
|
|
|
|
|
2012-09-12 16:53:03 +00:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(Services, "aus",
|
|
|
|
"@mozilla.org/updates/update-service;1",
|
|
|
|
"nsIApplicationUpdateService");
|
|
|
|
|
2012-03-21 22:50:53 +00:00
|
|
|
function UpdatePrompt() { }
|
|
|
|
|
|
|
|
UpdatePrompt.prototype = {
|
|
|
|
classID: Components.ID("{88b3eb21-d072-4e3b-886d-f89d8c49fe59}"),
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIUpdatePrompt]),
|
|
|
|
|
2012-09-12 16:53:03 +00:00
|
|
|
_update: null,
|
|
|
|
_applyPromptTimer: null,
|
|
|
|
_applyWaitTimer: null,
|
2012-03-26 07:46:21 +00:00
|
|
|
_selfDestructTimer: null,
|
|
|
|
|
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) {
|
|
|
|
if (!this.sendUpdateEvent("update-available", aUpdate,
|
|
|
|
this.handleAvailableResult)) {
|
|
|
|
|
|
|
|
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-09-12 16:53:03 +00:00
|
|
|
if (!this.sendUpdateEvent("update-downloaded", aUpdate,
|
|
|
|
this.handleDownloadedResult)) {
|
|
|
|
log("Unable to prompt, forcing restart");
|
|
|
|
this.restartProcess();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Schedule a fallback timeout in case the UI is unable to respond or show
|
|
|
|
// a prompt for some reason
|
|
|
|
this._applyPromptTimer = this.createTimer(APPLY_PROMPT_TIMEOUT);
|
|
|
|
},
|
|
|
|
|
|
|
|
sendUpdateEvent: function UP_sendUpdateEvent(aType, aUpdate, aCallback,
|
|
|
|
aDetail) {
|
|
|
|
let browser = Services.wm.getMostRecentWindow("navigator:browser");
|
|
|
|
if (!browser) {
|
|
|
|
log("Warning: Couldn't send update event " + aType +
|
|
|
|
": no content browser");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let content = browser.getContentWindow();
|
|
|
|
if (!content) {
|
|
|
|
log("Warning: Couldn't send update event " + aType +
|
|
|
|
": no content window");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let detail = aDetail || {};
|
|
|
|
detail.type = aType;
|
|
|
|
detail.displayVersion = aUpdate.displayVersion;
|
|
|
|
detail.detailsURL = aUpdate.detailsURL;
|
|
|
|
|
|
|
|
let patch = aUpdate.selectedPatch;
|
|
|
|
if (!patch) {
|
|
|
|
// For now we just check the first patch to get size information if a
|
|
|
|
// patch hasn't been selected yet.
|
|
|
|
if (aUpdate.patchCount == 0) {
|
|
|
|
log("Warning: no patches available in update");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
patch = aUpdate.getPatchAt(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
detail.size = patch.size;
|
|
|
|
detail.updateType = patch.type;
|
|
|
|
|
|
|
|
if (!aCallback) {
|
|
|
|
browser.shell.sendChromeEvent(detail);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._update = aUpdate;
|
|
|
|
let resultType = aType + "-result";
|
|
|
|
|
|
|
|
let handleContentEvent = (function(e) {
|
|
|
|
if (!e.detail) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let detail = e.detail;
|
|
|
|
if (detail.type == resultType) {
|
|
|
|
aCallback.call(this, detail);
|
|
|
|
content.removeEventListener("mozContentEvent", handleContentEvent);
|
|
|
|
this._update = null;
|
|
|
|
}
|
|
|
|
}).bind(this);
|
|
|
|
|
|
|
|
content.addEventListener("mozContentEvent", handleContentEvent);
|
|
|
|
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-09-12 16:53:03 +00:00
|
|
|
handleDownloadedResult: function UP_handleDownloadedResult(aDetail) {
|
|
|
|
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":
|
|
|
|
// Wait for a fixed period of time, allowing the user to temporarily
|
|
|
|
// postpone applying an update
|
|
|
|
this._applyWaitTimer = this.createTimer(APPLY_WAIT_TIMEOUT);
|
|
|
|
break;
|
|
|
|
case "restart":
|
|
|
|
this.restartProcess();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-09-12 16:53:03 +00:00
|
|
|
downloadUpdate: function UP_downloadUpdate(aUpdate) {
|
|
|
|
Services.aus.downloadUpdate(aUpdate, true);
|
|
|
|
},
|
|
|
|
|
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-03-26 07:46:21 +00:00
|
|
|
// If not cleanly shut down within 5 seconds, this process will
|
|
|
|
// explode.
|
2012-09-12 16:53:03 +00:00
|
|
|
this._selfDestructTimer = this.createTimer(SELF_DESTRUCT_TIMEOUT);
|
2012-03-26 07:46:21 +00:00
|
|
|
|
2012-09-12 16:53:03 +00:00
|
|
|
let appStartup = Cc["@mozilla.org/toolkit/app-startup;1"]
|
|
|
|
.getService(Ci.nsIAppStartup);
|
2012-03-21 22:50:53 +00:00
|
|
|
// NB: on Gonk, we rely on the system process manager to restart
|
|
|
|
// us. Trying to restart here would conflict with the process
|
|
|
|
// manager. We should be using a runtime check to detect Gonk
|
|
|
|
// instead of this gross ifdef, but the ifdef works for now.
|
|
|
|
appStartup.quit(appStartup.eForceQuit
|
|
|
|
#ifndef ANDROID
|
|
|
|
| appStartup.eRestart
|
|
|
|
#endif
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2012-09-12 16:53:03 +00:00
|
|
|
notify: function UP_notify(aTimer) {
|
|
|
|
if (aTimer == this._selfDestructTimer) {
|
|
|
|
this._selfDestructTimer = null;
|
|
|
|
this.selfDestruct();
|
|
|
|
} else if (aTimer == this._applyPromptTimer) {
|
|
|
|
log("Timed out waiting for result, restarting");
|
|
|
|
this._applyPromptTimer = null;
|
|
|
|
this.restartProcess();
|
|
|
|
} else if (aTimer == this._applyWaitTimer) {
|
|
|
|
this._applyWaitTimer = null;
|
|
|
|
this.showUpdatePrompt();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
selfDestruct: function UP_selfDestruct() {
|
2012-03-26 07:46:21 +00:00
|
|
|
#ifdef ANDROID
|
|
|
|
Cu.import("resource://gre/modules/ctypes.jsm");
|
|
|
|
let libc = ctypes.open("libc.so");
|
2012-09-12 16:53:03 +00:00
|
|
|
let _exit = libc.declare("_exit", ctypes.default_abi,
|
2012-03-26 07:46:21 +00:00
|
|
|
ctypes.void_t, // [return]
|
|
|
|
ctypes.int); // status
|
|
|
|
|
2012-09-12 16:53:03 +00:00
|
|
|
log("Self-destruct timer fired; didn't cleanly shut down. BOOM");
|
|
|
|
_exit(0);
|
2012-03-26 07:46:21 +00:00
|
|
|
#endif
|
|
|
|
},
|
|
|
|
|
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-03-21 22:50:53 +00:00
|
|
|
showUpdateInstalled: function UP_showUpdateInstalled() { },
|
|
|
|
|
|
|
|
showUpdateError: function UP_showUpdateError(aUpdate) {
|
|
|
|
if (aUpdate.state == "failed") {
|
2012-08-27 15:27:14 +00:00
|
|
|
log("Failed to download update, errorCode: " + aUpdate.errorCode);
|
2012-03-21 22:50:53 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
showUpdateHistory: function UP_showUpdateHistory(aParent) { },
|
|
|
|
};
|
|
|
|
|
|
|
|
const NSGetFactory = XPCOMUtils.generateNSGetFactory([UpdatePrompt]);
|