Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | #filter substitution |
michael@0 | 2 | |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | /** |
michael@0 | 8 | * @class nsURLFormatterService |
michael@0 | 9 | * |
michael@0 | 10 | * nsURLFormatterService exposes methods to substitute variables in URL formats. |
michael@0 | 11 | * |
michael@0 | 12 | * Mozilla Applications linking to Mozilla websites are strongly encouraged to use |
michael@0 | 13 | * URLs of the following format: |
michael@0 | 14 | * |
michael@0 | 15 | * http[s]://%SERVICE%.mozilla.[com|org]/%LOCALE%/ |
michael@0 | 16 | */ |
michael@0 | 17 | |
michael@0 | 18 | const Cc = Components.classes; |
michael@0 | 19 | const Ci = Components.interfaces; |
michael@0 | 20 | const Cu = Components.utils; |
michael@0 | 21 | |
michael@0 | 22 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 23 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 24 | |
michael@0 | 25 | const PREF_APP_DISTRIBUTION = "distribution.id"; |
michael@0 | 26 | const PREF_APP_DISTRIBUTION_VERSION = "distribution.version"; |
michael@0 | 27 | |
michael@0 | 28 | XPCOMUtils.defineLazyModuleGetter(this, "UpdateChannel", |
michael@0 | 29 | "resource://gre/modules/UpdateChannel.jsm"); |
michael@0 | 30 | |
michael@0 | 31 | function nsURLFormatterService() { |
michael@0 | 32 | XPCOMUtils.defineLazyGetter(this, "appInfo", function UFS_appInfo() { |
michael@0 | 33 | return Cc["@mozilla.org/xre/app-info;1"]. |
michael@0 | 34 | getService(Ci.nsIXULAppInfo). |
michael@0 | 35 | QueryInterface(Ci.nsIXULRuntime); |
michael@0 | 36 | }); |
michael@0 | 37 | |
michael@0 | 38 | XPCOMUtils.defineLazyGetter(this, "ABI", function UFS_ABI() { |
michael@0 | 39 | let ABI = "default"; |
michael@0 | 40 | try { |
michael@0 | 41 | ABI = this.appInfo.XPCOMABI; |
michael@0 | 42 | |
michael@0 | 43 | if ("@mozilla.org/xpcom/mac-utils;1" in Cc) { |
michael@0 | 44 | // Mac universal build should report a different ABI than either macppc |
michael@0 | 45 | // or mactel. |
michael@0 | 46 | let macutils = Cc["@mozilla.org/xpcom/mac-utils;1"] |
michael@0 | 47 | .getService(Ci.nsIMacUtils); |
michael@0 | 48 | if (macutils && macutils.isUniversalBinary) { |
michael@0 | 49 | ABI = "Universal-gcc3"; |
michael@0 | 50 | } |
michael@0 | 51 | } |
michael@0 | 52 | } catch (e) {} |
michael@0 | 53 | |
michael@0 | 54 | return ABI; |
michael@0 | 55 | }); |
michael@0 | 56 | |
michael@0 | 57 | XPCOMUtils.defineLazyGetter(this, "OSVersion", function UFS_OSVersion() { |
michael@0 | 58 | let OSVersion = "default"; |
michael@0 | 59 | let sysInfo = Cc["@mozilla.org/system-info;1"]. |
michael@0 | 60 | getService(Ci.nsIPropertyBag2); |
michael@0 | 61 | try { |
michael@0 | 62 | OSVersion = sysInfo.getProperty("name") + " " + |
michael@0 | 63 | sysInfo.getProperty("version"); |
michael@0 | 64 | OSVersion += " (" + sysInfo.getProperty("secondaryLibrary") + ")"; |
michael@0 | 65 | } catch (e) {} |
michael@0 | 66 | |
michael@0 | 67 | return encodeURIComponent(OSVersion); |
michael@0 | 68 | }); |
michael@0 | 69 | |
michael@0 | 70 | XPCOMUtils.defineLazyGetter(this, "distribution", function UFS_distribution() { |
michael@0 | 71 | let distribution = { id: "default", version: "default" }; |
michael@0 | 72 | |
michael@0 | 73 | let defaults = Services.prefs.getDefaultBranch(null); |
michael@0 | 74 | try { |
michael@0 | 75 | distribution.id = defaults.getCharPref(PREF_APP_DISTRIBUTION); |
michael@0 | 76 | } catch (e) {} |
michael@0 | 77 | try { |
michael@0 | 78 | distribution.version = defaults.getCharPref(PREF_APP_DISTRIBUTION_VERSION); |
michael@0 | 79 | } catch (e) {} |
michael@0 | 80 | |
michael@0 | 81 | return distribution; |
michael@0 | 82 | }); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | nsURLFormatterService.prototype = { |
michael@0 | 86 | classID: Components.ID("{e6156350-2be8-11db-a98b-0800200c9a66}"), |
michael@0 | 87 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIURLFormatter]), |
michael@0 | 88 | |
michael@0 | 89 | _defaults: { |
michael@0 | 90 | LOCALE: function() Cc["@mozilla.org/chrome/chrome-registry;1"]. |
michael@0 | 91 | getService(Ci.nsIXULChromeRegistry). |
michael@0 | 92 | getSelectedLocale('global'), |
michael@0 | 93 | VENDOR: function() this.appInfo.vendor, |
michael@0 | 94 | NAME: function() this.appInfo.name, |
michael@0 | 95 | ID: function() this.appInfo.ID, |
michael@0 | 96 | VERSION: function() this.appInfo.version, |
michael@0 | 97 | APPBUILDID: function() this.appInfo.appBuildID, |
michael@0 | 98 | PLATFORMVERSION: function() this.appInfo.platformVersion, |
michael@0 | 99 | PLATFORMBUILDID: function() this.appInfo.platformBuildID, |
michael@0 | 100 | APP: function() this.appInfo.name.toLowerCase().replace(/ /, ""), |
michael@0 | 101 | OS: function() this.appInfo.OS, |
michael@0 | 102 | XPCOMABI: function() this.ABI, |
michael@0 | 103 | BUILD_TARGET: function() this.appInfo.OS + "_" + this.ABI, |
michael@0 | 104 | OS_VERSION: function() this.OSVersion, |
michael@0 | 105 | CHANNEL: function() UpdateChannel.get(), |
michael@0 | 106 | MOZILLA_API_KEY: function() "@MOZ_MOZILLA_API_KEY@", |
michael@0 | 107 | GOOGLE_API_KEY: function() "@MOZ_GOOGLE_API_KEY@", |
michael@0 | 108 | DISTRIBUTION: function() this.distribution.id, |
michael@0 | 109 | DISTRIBUTION_VERSION: function() this.distribution.version |
michael@0 | 110 | }, |
michael@0 | 111 | |
michael@0 | 112 | formatURL: function uf_formatURL(aFormat) { |
michael@0 | 113 | var _this = this; |
michael@0 | 114 | var replacementCallback = function(aMatch, aKey) { |
michael@0 | 115 | if (aKey in _this._defaults) { |
michael@0 | 116 | return _this._defaults[aKey].call(_this); |
michael@0 | 117 | } |
michael@0 | 118 | Cu.reportError("formatURL: Couldn't find value for key: " + aKey); |
michael@0 | 119 | return aMatch; |
michael@0 | 120 | } |
michael@0 | 121 | return aFormat.replace(/%([A-Z_]+)%/g, replacementCallback); |
michael@0 | 122 | }, |
michael@0 | 123 | |
michael@0 | 124 | formatURLPref: function uf_formatURLPref(aPref) { |
michael@0 | 125 | var format = null; |
michael@0 | 126 | var PS = Cc['@mozilla.org/preferences-service;1']. |
michael@0 | 127 | getService(Ci.nsIPrefBranch); |
michael@0 | 128 | |
michael@0 | 129 | try { |
michael@0 | 130 | format = PS.getComplexValue(aPref, Ci.nsISupportsString).data; |
michael@0 | 131 | } catch(ex) { |
michael@0 | 132 | Cu.reportError("formatURLPref: Couldn't get pref: " + aPref); |
michael@0 | 133 | return "about:blank"; |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | if (!PS.prefHasUserValue(aPref) && |
michael@0 | 137 | /^(data:text\/plain,.+=.+|chrome:\/\/.+\/locale\/.+\.properties)$/.test(format)) { |
michael@0 | 138 | // This looks as if it might be a localised preference |
michael@0 | 139 | try { |
michael@0 | 140 | format = PS.getComplexValue(aPref, Ci.nsIPrefLocalizedString).data; |
michael@0 | 141 | } catch(ex) {} |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | return this.formatURL(format); |
michael@0 | 145 | } |
michael@0 | 146 | }; |
michael@0 | 147 | |
michael@0 | 148 | this.NSGetFactory = XPCOMUtils.generateNSGetFactory([nsURLFormatterService]); |