michael@0: #filter substitution michael@0: michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: * @class nsURLFormatterService michael@0: * michael@0: * nsURLFormatterService exposes methods to substitute variables in URL formats. michael@0: * michael@0: * Mozilla Applications linking to Mozilla websites are strongly encouraged to use michael@0: * URLs of the following format: michael@0: * michael@0: * http[s]://%SERVICE%.mozilla.[com|org]/%LOCALE%/ michael@0: */ michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: const PREF_APP_DISTRIBUTION = "distribution.id"; michael@0: const PREF_APP_DISTRIBUTION_VERSION = "distribution.version"; michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "UpdateChannel", michael@0: "resource://gre/modules/UpdateChannel.jsm"); michael@0: michael@0: function nsURLFormatterService() { michael@0: XPCOMUtils.defineLazyGetter(this, "appInfo", function UFS_appInfo() { michael@0: return Cc["@mozilla.org/xre/app-info;1"]. michael@0: getService(Ci.nsIXULAppInfo). michael@0: QueryInterface(Ci.nsIXULRuntime); michael@0: }); michael@0: michael@0: XPCOMUtils.defineLazyGetter(this, "ABI", function UFS_ABI() { michael@0: let ABI = "default"; michael@0: try { michael@0: ABI = this.appInfo.XPCOMABI; michael@0: michael@0: if ("@mozilla.org/xpcom/mac-utils;1" in Cc) { michael@0: // Mac universal build should report a different ABI than either macppc michael@0: // or mactel. michael@0: let macutils = Cc["@mozilla.org/xpcom/mac-utils;1"] michael@0: .getService(Ci.nsIMacUtils); michael@0: if (macutils && macutils.isUniversalBinary) { michael@0: ABI = "Universal-gcc3"; michael@0: } michael@0: } michael@0: } catch (e) {} michael@0: michael@0: return ABI; michael@0: }); michael@0: michael@0: XPCOMUtils.defineLazyGetter(this, "OSVersion", function UFS_OSVersion() { michael@0: let OSVersion = "default"; michael@0: let sysInfo = Cc["@mozilla.org/system-info;1"]. michael@0: getService(Ci.nsIPropertyBag2); michael@0: try { michael@0: OSVersion = sysInfo.getProperty("name") + " " + michael@0: sysInfo.getProperty("version"); michael@0: OSVersion += " (" + sysInfo.getProperty("secondaryLibrary") + ")"; michael@0: } catch (e) {} michael@0: michael@0: return encodeURIComponent(OSVersion); michael@0: }); michael@0: michael@0: XPCOMUtils.defineLazyGetter(this, "distribution", function UFS_distribution() { michael@0: let distribution = { id: "default", version: "default" }; michael@0: michael@0: let defaults = Services.prefs.getDefaultBranch(null); michael@0: try { michael@0: distribution.id = defaults.getCharPref(PREF_APP_DISTRIBUTION); michael@0: } catch (e) {} michael@0: try { michael@0: distribution.version = defaults.getCharPref(PREF_APP_DISTRIBUTION_VERSION); michael@0: } catch (e) {} michael@0: michael@0: return distribution; michael@0: }); michael@0: } michael@0: michael@0: nsURLFormatterService.prototype = { michael@0: classID: Components.ID("{e6156350-2be8-11db-a98b-0800200c9a66}"), michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIURLFormatter]), michael@0: michael@0: _defaults: { michael@0: LOCALE: function() Cc["@mozilla.org/chrome/chrome-registry;1"]. michael@0: getService(Ci.nsIXULChromeRegistry). michael@0: getSelectedLocale('global'), michael@0: VENDOR: function() this.appInfo.vendor, michael@0: NAME: function() this.appInfo.name, michael@0: ID: function() this.appInfo.ID, michael@0: VERSION: function() this.appInfo.version, michael@0: APPBUILDID: function() this.appInfo.appBuildID, michael@0: PLATFORMVERSION: function() this.appInfo.platformVersion, michael@0: PLATFORMBUILDID: function() this.appInfo.platformBuildID, michael@0: APP: function() this.appInfo.name.toLowerCase().replace(/ /, ""), michael@0: OS: function() this.appInfo.OS, michael@0: XPCOMABI: function() this.ABI, michael@0: BUILD_TARGET: function() this.appInfo.OS + "_" + this.ABI, michael@0: OS_VERSION: function() this.OSVersion, michael@0: CHANNEL: function() UpdateChannel.get(), michael@0: MOZILLA_API_KEY: function() "@MOZ_MOZILLA_API_KEY@", michael@0: GOOGLE_API_KEY: function() "@MOZ_GOOGLE_API_KEY@", michael@0: DISTRIBUTION: function() this.distribution.id, michael@0: DISTRIBUTION_VERSION: function() this.distribution.version michael@0: }, michael@0: michael@0: formatURL: function uf_formatURL(aFormat) { michael@0: var _this = this; michael@0: var replacementCallback = function(aMatch, aKey) { michael@0: if (aKey in _this._defaults) { michael@0: return _this._defaults[aKey].call(_this); michael@0: } michael@0: Cu.reportError("formatURL: Couldn't find value for key: " + aKey); michael@0: return aMatch; michael@0: } michael@0: return aFormat.replace(/%([A-Z_]+)%/g, replacementCallback); michael@0: }, michael@0: michael@0: formatURLPref: function uf_formatURLPref(aPref) { michael@0: var format = null; michael@0: var PS = Cc['@mozilla.org/preferences-service;1']. michael@0: getService(Ci.nsIPrefBranch); michael@0: michael@0: try { michael@0: format = PS.getComplexValue(aPref, Ci.nsISupportsString).data; michael@0: } catch(ex) { michael@0: Cu.reportError("formatURLPref: Couldn't get pref: " + aPref); michael@0: return "about:blank"; michael@0: } michael@0: michael@0: if (!PS.prefHasUserValue(aPref) && michael@0: /^(data:text\/plain,.+=.+|chrome:\/\/.+\/locale\/.+\.properties)$/.test(format)) { michael@0: // This looks as if it might be a localised preference michael@0: try { michael@0: format = PS.getComplexValue(aPref, Ci.nsIPrefLocalizedString).data; michael@0: } catch(ex) {} michael@0: } michael@0: michael@0: return this.formatURL(format); michael@0: } michael@0: }; michael@0: michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory([nsURLFormatterService]);