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