📄 nsupdateservice.js
字号:
* @constructor */function Update(update) { this._properties = {}; this._patches = []; this.installDate = 0; this.isCompleteUpdate = false; this.channel = "default"; // Null <update>, assume this is a message container and do no // further initialization if (!update) return; for (var i = 0; i < update.childNodes.length; ++i) { var patchElement = update.childNodes.item(i); if (patchElement.nodeType != Node.ELEMENT_NODE || patchElement.localName != "patch") continue; patchElement.QueryInterface(Components.interfaces.nsIDOMElement); try { var patch = new UpdatePatch(patchElement); } catch (e) { continue; } this._patches.push(patch); } if (0 == this._patches.length) throw Components.results.NS_ERROR_ILLEGAL_VALUE; for (var i = 0; i < update.attributes.length; ++i) { var attr = update.attributes.item(i); attr.QueryInterface(Components.interfaces.nsIDOMAttr); if (attr.name == "installDate" && attr.value) this.installDate = parseInt(attr.value); else if (attr.name == "isCompleteUpdate") this.isCompleteUpdate = attr.value == "true"; else if (attr.name == "isSecurityUpdate") this.isSecurityUpdate = attr.value == "true"; else if (attr.name == "detailsURL") this._detailsURL = attr.value; else if (attr.name == "channel") this.channel = attr.value; else this[attr.name] = attr.value; } // The Update Name is either the string provided by the <update> element, or // the string: "<App Name> <Update App Version>" var name = ""; if (update.hasAttribute("name")) name = update.getAttribute("name"); else { var sbs = Components.classes["@mozilla.org/intl/stringbundle;1"] .getService(Components.interfaces.nsIStringBundleService); var brandBundle = sbs.createBundle(URI_BRAND_PROPERTIES); var updateBundle = sbs.createBundle(URI_UPDATES_PROPERTIES); var appName = brandBundle.GetStringFromName("brandShortName"); name = updateBundle.formatStringFromName("updateName", [appName, this.version], 2); } this.name = name;}Update.prototype = { /** * See nsIUpdateService.idl */ get patchCount() { return this._patches.length; }, /** * See nsIUpdateService.idl */ getPatchAt: function(index) { return this._patches[index]; }, /** * See nsIUpdateService.idl * * We use a copy of the state cached on this object in |_state| only when * there is no selected patch, i.e. in the case when we could not load * |.activeUpdate| from the update manager for some reason but still have * the update.status file to work with. */ _state: "", set state(state) { if (this.selectedPatch) this.selectedPatch.state = state; this._state = state; return state; }, get state() { if (this.selectedPatch) return this.selectedPatch.state; return this._state; }, /** * See nsIUpdateService.idl */ errorCode: 0, /** * See nsIUpdateService.idl */ get selectedPatch() { for (var i = 0; i < this.patchCount; ++i) { if (this._patches[i].selected) return this._patches[i]; } return null; }, /** * See nsIUpdateService.idl */ get detailsURL() { if (!this._detailsURL) { try { // Try using a default details URL supplied by the distribution // if the update XML does not supply one. var formatter = Components.classes["@mozilla.org/toolkit/URLFormatterService;1"] .getService(Components.interfaces.nsIURLFormatter); return formatter.formatURLPref(PREF_APP_UPDATE_URL_DETAILS); } catch (e) { } } return this._detailsURL || ""; }, /** * See nsIUpdateService.idl */ serialize: function(updates) { var update = updates.createElementNS(URI_UPDATE_NS, "update"); update.setAttribute("type", this.type); update.setAttribute("name", this.name); update.setAttribute("version", this.version); update.setAttribute("extensionVersion", this.extensionVersion); update.setAttribute("detailsURL", this.detailsURL); update.setAttribute("licenseURL", this.licenseURL); update.setAttribute("serviceURL", this.serviceURL); update.setAttribute("installDate", this.installDate); update.setAttribute("statusText", this.statusText); update.setAttribute("buildID", this.buildID); update.setAttribute("isCompleteUpdate", this.isCompleteUpdate); update.setAttribute("channel", this.channel); updates.documentElement.appendChild(update); for (var p in this._properties) { if (this._properties[p].present) update.setAttribute(p, this._properties[p].data); } for (var i = 0; i < this.patchCount; ++i) update.appendChild(this.getPatchAt(i).serialize(updates)); return update; }, /** * A hash of custom properties */ _properties: null, /** * See nsIWritablePropertyBag.idl */ setProperty: function(name, value) { this._properties[name] = { data: value, present: true }; }, /** * See nsIWritablePropertyBag.idl */ deleteProperty: function(name) { if (name in this._properties) this._properties[name].present = false; else throw Components.results.NS_ERROR_FAILURE; }, /** * See nsIPropertyBag.idl */ get enumerator() { var properties = []; for (var p in this._properties) properties.push(this._properties[p].data); return new ArrayEnumerator(properties); }, /** * See nsIPropertyBag.idl */ getProperty: function(name) { if (name in this._properties && this._properties[name].present) return this._properties[name].data; throw Components.results.NS_ERROR_FAILURE; }, /** * See nsISupports.idl */ QueryInterface: function(iid) { if (!iid.equals(Components.interfaces.nsIUpdate_MOZILLA_1_8_BRANCH) && !iid.equals(Components.interfaces.nsIUpdate) && !iid.equals(Components.interfaces.nsIPropertyBag) && !iid.equals(Components.interfaces.nsIWritablePropertyBag) && !iid.equals(Components.interfaces.nsISupports)) throw Components.results.NS_ERROR_NO_INTERFACE; return this; }}; /** * UpdateService * A Service for managing the discovery and installation of software updates. * @constructor */function UpdateService() { gApp = Components.classes["@mozilla.org/xre/app-info;1"] .getService(Components.interfaces.nsIXULAppInfo) .QueryInterface(Components.interfaces.nsIXULRuntime); gPref = Components.classes["@mozilla.org/preferences-service;1"] .getService(Components.interfaces.nsIPrefBranch2); gConsole = Components.classes["@mozilla.org/consoleservice;1"] .getService(Components.interfaces.nsIConsoleService); // Not all builds have a known ABI try { gABI = gApp.XPCOMABI; } catch (e) { LOG("UpdateService", "XPCOM ABI unknown: updates are not possible."); } try { var sysInfo = Components.classes["@mozilla.org/system-info;1"] .getService(Components.interfaces.nsIPropertyBag2); gOSVersion = encodeURIComponent(sysInfo.getProperty("name") + " " + sysInfo.getProperty("version")); } catch (e) { LOG("UpdateService", "OS Version unknown: updates are not possible."); }//@line 1042 "/c/builds/1813/mozilla/toolkit/mozapps/update/src/nsUpdateService.js.in" // Start the update timer only after a profile has been selected so that the // appropriate values for the update check are read from the user's profile. var os = getObserverService(); os.addObserver(this, "profile-after-change", false); // Observe xpcom-shutdown to unhook pref branch observers above to avoid // shutdown leaks. os.addObserver(this, "xpcom-shutdown", false);}UpdateService.prototype = { /** * The downloader we are using to download updates. There is only ever one of * these. */ _downloader: null, /** * Handle Observer Service notifications * @param subject * The subject of the notification * @param topic * The notification name * @param data * Additional data */ observe: function(subject, topic, data) { var os = getObserverService(); switch (topic) { case "profile-after-change": os.removeObserver(this, "profile-after-change"); this._start(); break; case "xpcom-shutdown": os.removeObserver(this, "xpcom-shutdown"); // Release Services gApp = null; gPref = null; gConsole = null; break; } }, /** * Start the Update Service */ _start: function() { // Start logging this._initLoggingPrefs(); // Clean up any extant updates this._postUpdateProcessing(); // Register a background update check timer var tm = Components.classes["@mozilla.org/updates/timer-manager;1"] .getService(Components.interfaces.nsIUpdateTimerManager); var interval = getPref("getIntPref", PREF_APP_UPDATE_INTERVAL, 86400); tm.registerTimer("background-update-timer", this, interval); // Resume fetching... var um = Components.classes["@mozilla.org/updates/update-manager;1"] .getService(Components.interfaces.nsIUpdateManager); var activeUpdate = um.activeUpdate; if (activeUpdate) { var status = this.downloadUpdate(activeUpdate, true); if (status == STATE_NONE) cleanupActiveUpdate(); } }, /** * Perform post-processing on updates lingering in the updates directory * from a previous browser session - either report install failures (and * optionally attempt to fetch a different version if appropriate) or * notify the user of install success. */ _postUpdateProcessing: function() { // Detect installation failures and notify // Bail out if we don't have appropriate permissions if (!this.canUpdate) return; var status = readStatusFile(getUpdatesDir()); // Make sure to cleanup after an update that failed for an unknown reason if (status == "null") status = null; var updRootKey = null;//@line 1138 "/c/builds/1813/mozilla/toolkit/mozapps/update/src/nsUpdateService.js.in" function findPreviousUpdate(key) { var updateDir = getUpdatesDir(key); if (updateDir.exists()) { status = readStatusFile(updateDir); // Previous download should succeed. Otherwise, we will not be here! if (status == STATE_SUCCEEDED) updRootKey = key; else status = null; } } // required when updating from Fx 2.0.0.1 to 2.0.0.3 (or later) // on Windows Vista. if (status == null) findPreviousUpdate(KEY_UAPPDATA); // required to migrate from older versions. if (status == null) findPreviousUpdate(KEY_APPDIR);//@line 1159 "/c/builds/1813/mozilla/toolkit/mozapps/update/src/nsUpdateService.js.in" if (status == STATE_DOWNLOADING) { LOG("UpdateService", "_postUpdateProcessing: Downloading patch, resuming..."); } else if (status != null) { // null status means the update.status file is not present, because either: // 1) no update was performed, and so there's no UI to show // 2) an update was attempted but failed during checking, transfer or // verification, and was cleaned up at that point, and UI notifying of // that error was shown at that stage. var um = Components.classes["@mozilla.org/updates/update-manager;1"]. getService(Components.interfaces.nsIUpdateManager); var prompter = Components.classes["@mozilla.org/updates/update-prompt;1"]. createInstance(Components.interfaces.nsIUpdatePrompt); var shouldCleanup = true; var update = um.activeUpdate; if (!update) { update = new Update(null); } update.state = status; var sbs = Components.classes["@mozilla.org/intl/stringbundle;1"]. getService(Components.interfaces.nsIStringBundleService); var bundle = sbs.createBundle(URI_UPDATES_PROPERTIES); if (status == STATE_SUCCEEDED) { update.statusText = bundle.GetStringFromName("installSuccess"); // Dig through the update history to find the patch that was just // installed and update its metadata. for (var i = 0; i < um.updateCount; ++i) { var umUpdate = um.getUpdateAt(i); if (umUpdate && umUpdate.version == update.version &&
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -