📄 nsupdatenotifier.js
字号:
info.URL = this.getTarget(rdf, aDS, src, "URL"); info.productName = this.getTarget(rdf, aDS, src, "productName"); } catch (ex) { info = null; debug("Exception getting update info: " + ex); // NOTE: If the (possibly remote) datasource doesn't exist // or fails to load the first |GetTarget()| call will fail // bringing us to this exception handler. In turn, we // will fail silently. Testing has revealed that for a // non-existent datasource (invalid URI) the // |nsIRDFXMLSinkObserver.onEndLoad()| is called instead of // |nsIRDFXMLSinkObserver.onError()| as one may expect. In // addition, if we QI the aSink parameter of |onEndLoad()| // to an |nsIRDFRemoteDataSource| and check the |loaded| // boolean, it reflects true so we can't use that. The // safe way to know we have failed to load the datasource // is by handling the first exception as we are doing now. } return info; }, getTarget: function(aRDF, aDS, aSrc, aProp) { var src = aRDF.GetResource(aSrc); var arc = aRDF.GetResource("http://home.netscape.com/NC-rdf#" + aProp); var target = aDS.GetTarget(src, arc, true); return target.QueryInterface(Components.interfaces.nsIRDFLiteral).Value; }, newerVersionAvailable: function(aUpdateInfo) { // sanity check if (!aUpdateInfo.registryName || !aUpdateInfo.version) { debug("Sanity check failed: aUpdateInfo is invalid!"); return false; } // when we know we are updating the ``Browser'' component // we can rely on Necko to give us the app version if (aUpdateInfo.registryName == "Browser") return this.neckoHaveNewer(aUpdateInfo); return this.xpinstallHaveNewer(aUpdateInfo); }, neckoHaveNewer: function(aUpdateInfo) { try { var httpHandler = Components. classes["@mozilla.org/network/protocol;1?name=http"]. getService(Components.interfaces.nsIHttpProtocolHandler); var synthesized = this.synthesizeVersion(httpHandler.misc, httpHandler.productSub); var local = new nsVersion(synthesized); var server = new nsVersion(aUpdateInfo.version); return (server.isNewerThan(local)); } catch (ex) { // fail silently debug("Exception getting httpHandler: " + ex); return false; } return false; // return value expected from this function }, xpinstallHaveNewer: function(aUpdateInfo) { // XXX Once InstallTrigger is a component we will be able to // get at it without needing to reference it from hiddenDOMWindow. // This will enable us to |compareVersion()|s even when // XPInstall is disabled but update notifications are enabled. // See <http://bugzilla.mozilla.org/show_bug.cgi?id=121506>. var ass = Components.classes["@mozilla.org/appshell/appShellService;1"]. getService(Components.interfaces.nsIAppShellService); var trigger = ass.hiddenDOMWindow.InstallTrigger; var diffLevel = trigger.compareVersion(aUpdateInfo.registryName, aUpdateInfo.version); if (diffLevel < trigger.EQUAL && diffLevel != trigger.NOT_FOUND) return true; return false; // already have newer version or // fail silently if old version not found on disk }, synthesizeVersion: function(aMisc, aProductSub) { // Strip out portion of nsIHttpProtocolHandler.misc that // contains version info and stuff all ``missing'' portions // with a default 0 value. We are interested in the first 3 // numbers delimited by periods. The 4th comes from aProductSub. // e.g., x => x.0.0, x.1 => x.1.0, x.1.2 => x.1.2, x.1.2.3 => x.1.2 var synthesized = "0.0.0."; // match only digits and periods after "rv:" in the misc var onlyVer = /rv:([0-9.]+)/.exec(aMisc); // original string in onlyVer[0], matched substring in onlyVer[1] if (onlyVer && onlyVer.length >= 2) { var parts = onlyVer[1].split('.'); var len = parts.length; if (len > 0) { synthesized = ""; // extract first 3 dot delimited numbers in misc (after "rv:") for (var i = 0; i < 3; ++i) { synthesized += ((len >= i+1) ? parts[i] : "0") + "."; } } } // tack on productSub for nsVersion.mBuild field if available synthesized += aProductSub ? aProductSub : "0"; return synthesized; }, getBundle: function(aURI) { if (!aURI) return null; var bundle = null; try { var strBundleService = Components. classes["@mozilla.org/intl/stringbundle;1"]. getService(Components.interfaces.nsIStringBundleService); bundle = strBundleService.createBundle(aURI); } catch (ex) { bundle = null; debug("Exception getting bundle " + aURI + ": " + ex); } return bundle; }}//////////////////////////////////////////////////////////////////////////// nsVersion//// Constructs a version object given a string representation. This// constructor populates the mMajor, mMinor, mRelease, and mBuild// fields regardless of whether string contains all the fields.// The default for all unspecified fields is 0.//////////////////////////////////////////////////////////////////////////function nsVersion(aStringVersion){ var parts = aStringVersion.split('.'); var len = parts.length; this.mMajor = (len >= 1) ? this.getValidInt(parts[0]) : 0; this.mMinor = (len >= 2) ? this.getValidInt(parts[1]) : 0; this.mRelease = (len >= 3) ? this.getValidInt(parts[2]) : 0; this.mBuild = (len >= 4) ? this.getValidInt(parts[3]) : 0;}nsVersion.prototype ={ isNewerThan: function(aOther) { if (this.mMajor == aOther.mMajor) { if (this.mMinor == aOther.mMinor) { if (this.mRelease == aOther.mRelease) { if (this.mBuild <= aOther.mBuild) return false; else return true; // build is newer } else if (this.mRelease < aOther.mRelease) return false; else return true; // release is newer } else if (this.mMinor < aOther.mMinor) return false; else return true; // minor is newer } else if (this.mMajor < aOther.mMajor) return false; else return true; // major is newer return false; }, getValidInt: function(aString) { var integer = parseInt(aString); if (isNaN(integer)) return 0; return integer; }}//////////////////////////////////////////////////////////////////////////// nsUpdateNotifierModule : nsIModule//////////////////////////////////////////////////////////////////////////var nsUpdateNotifierModule ={ mClassName: "Update Notifier", mContractID: "@mozilla.org/update-notifier;1", mClassID: Components.ID("8b6dcf5e-3b5a-4fff-bff5-65a8fa9d71b2"), getClassObject: function(aCompMgr, aCID, aIID) { if (!aCID.equals(this.mClassID)) throw Components.results.NS_ERROR_NO_INTERFACE; if (!aIID.equals(Components.interfaces.nsIFactory)) throw Components.results.NS_ERROR_NOT_IMPLEMENTED; return this.mFactory; }, registerSelf: function(aCompMgr, aFileSpec, aLocation, aType) { if (kDebug) dump("*** Registering nsUpdateNotifier (a JavaScript Module)\n"); aCompMgr = aCompMgr.QueryInterface( Components.interfaces.nsIComponentRegistrar); aCompMgr.registerFactoryLocation(this.mClassID, this.mClassName, this.mContractID, aFileSpec, aLocation, aType); // receive startup notification from the profile manager // (we get |createInstance()|d at startup-notification time) this.getCategoryManager().addCategoryEntry("profile-startup-category", this.mContractID, "", true, true); }, unregisterSelf: function(aCompMgr, aFileSpec, aLocation) { aCompMgr = aCompMgr.QueryInterface( Components.interfaces.nsIComponentRegistrar); aCompMgr.unregisterFactoryLocation(this.mClassID, aFileSpec); this.getCategoryManager().deleteCategoryEntry("profile-startup-category", this.mContractID, true); }, canUnload: function(aCompMgr) { return true; }, getCategoryManager: function() { return Components.classes["@mozilla.org/categorymanager;1"]. getService(Components.interfaces.nsICategoryManager); }, ////////////////////////////////////////////////////////////////////// // // mFactory : nsIFactory // ////////////////////////////////////////////////////////////////////// mFactory: { createInstance: function(aOuter, aIID) { if (aOuter != null) throw Components.results.NS_ERROR_NO_AGGREGATION; if (!aIID.equals(Components.interfaces.nsIObserver) && !aIID.equals(Components.interfaces.nsIProfileStartupListener) && !aIID.equals(Components.interfaces.nsISupports)) throw Components.results.NS_ERROR_INVALID_ARG; // return the singleton return nsUpdateNotifier.QueryInterface(aIID); }, lockFactory: function(aLock) { // quiten warnings } }};function NSGetModule(aCompMgr, aFileSpec){ return nsUpdateNotifierModule;}//////////////////////////////////////////////////////////////////////////// Debug helper//////////////////////////////////////////////////////////////////////////if (!kDebug) debug = function(m) {};else debug = function(m) {dump("\t *** nsUpdateNotifier: " + m + "\n");};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -