summaryrefslogtreecommitdiffstats
path: root/toolkit/components/urlformatter
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /toolkit/components/urlformatter
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'toolkit/components/urlformatter')
-rw-r--r--toolkit/components/urlformatter/api_keys.in4
-rw-r--r--toolkit/components/urlformatter/moz.build27
-rw-r--r--toolkit/components/urlformatter/nsIURLFormatter.idl50
-rw-r--r--toolkit/components/urlformatter/nsURLFormatter.js168
-rw-r--r--toolkit/components/urlformatter/nsURLFormatter.manifest2
-rw-r--r--toolkit/components/urlformatter/tests/unit/.eslintrc.js7
-rw-r--r--toolkit/components/urlformatter/tests/unit/head_urlformatter.js16
-rw-r--r--toolkit/components/urlformatter/tests/unit/test_urlformatter.js65
-rw-r--r--toolkit/components/urlformatter/tests/unit/xpcshell.ini6
9 files changed, 345 insertions, 0 deletions
diff --git a/toolkit/components/urlformatter/api_keys.in b/toolkit/components/urlformatter/api_keys.in
new file mode 100644
index 000000000..3fa48dd9a
--- /dev/null
+++ b/toolkit/components/urlformatter/api_keys.in
@@ -0,0 +1,4 @@
+#define MOZ_MOZILLA_API_KEY @MOZ_MOZILLA_API_KEY@
+#define MOZ_GOOGLE_API_KEY @MOZ_GOOGLE_API_KEY@
+#define MOZ_BING_API_KEY @MOZ_BING_API_KEY@
+#define MOZ_BING_API_CLIENTID @MOZ_BING_API_CLIENTID@
diff --git a/toolkit/components/urlformatter/moz.build b/toolkit/components/urlformatter/moz.build
new file mode 100644
index 000000000..1543fddda
--- /dev/null
+++ b/toolkit/components/urlformatter/moz.build
@@ -0,0 +1,27 @@
+# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
+# vim: set filetype=python:
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+XPCSHELL_TESTS_MANIFESTS += ['tests/unit/xpcshell.ini']
+
+XPIDL_SOURCES += [
+ 'nsIURLFormatter.idl',
+]
+
+XPIDL_MODULE = 'urlformatter'
+
+EXTRA_COMPONENTS += [
+ 'nsURLFormatter.manifest',
+]
+
+EXTRA_PP_COMPONENTS += [
+ 'nsURLFormatter.js',
+]
+
+CONFIGURE_SUBST_FILES += [
+ 'api_keys',
+]
+
+DEFINES['OBJDIR'] = OBJDIR
diff --git a/toolkit/components/urlformatter/nsIURLFormatter.idl b/toolkit/components/urlformatter/nsIURLFormatter.idl
new file mode 100644
index 000000000..a4a549d57
--- /dev/null
+++ b/toolkit/components/urlformatter/nsIURLFormatter.idl
@@ -0,0 +1,50 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/**
+ * nsIURLFormatter
+ *
+ * nsIURLFormatter exposes methods to substitute variables in URL formats.
+ * Variable names can contain 'A-Z' letters and '_' characters.
+ *
+ * Mozilla Applications linking to Mozilla websites are strongly encouraged to use
+ * URLs of the following format:
+ *
+ * http[s]://%SERVICE%.mozilla.[com|org]/%LOCALE%/
+ */
+
+#include "nsISupports.idl"
+
+[scriptable, uuid(4ab31d30-372d-11db-a98b-0800200c9a66)]
+interface nsIURLFormatter: nsISupports
+{
+ /**
+ * formatURL - Formats a string URL
+ *
+ * The set of known variables is predefined.
+ * If a variable is unknown, it is left unchanged and a non-fatal error is reported.
+ *
+ * @param aFormat string Unformatted URL.
+ *
+ * @return The formatted URL.
+ */
+ AString formatURL(in AString aFormat);
+
+ /**
+ * formatURLPref - Formats a string URL stored in a preference
+ *
+ * If the preference value cannot be retrieved, a fatal error is reported
+ * and the "about:blank" URL is returned.
+ *
+ * @param aPref string Preference name.
+ *
+ * @return The formatted URL returned by formatURL(), or "about:blank".
+ */
+ AString formatURLPref(in AString aPref);
+
+ /**
+ * Remove all of the sensitive query parameter strings from URLs in |aMsg|.
+ */
+ AString trimSensitiveURLs(in AString aMsg);
+};
diff --git a/toolkit/components/urlformatter/nsURLFormatter.js b/toolkit/components/urlformatter/nsURLFormatter.js
new file mode 100644
index 000000000..970de0537
--- /dev/null
+++ b/toolkit/components/urlformatter/nsURLFormatter.js
@@ -0,0 +1,168 @@
+#filter substitution
+#include @OBJDIR@/api_keys
+
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+ /**
+ * @class nsURLFormatterService
+ *
+ * nsURLFormatterService exposes methods to substitute variables in URL formats.
+ *
+ * Mozilla Applications linking to Mozilla websites are strongly encouraged to use
+ * URLs of the following format:
+ *
+ * http[s]://%SERVICE%.mozilla.[com|org]/%LOCALE%/
+ */
+
+const Cc = Components.classes;
+const Ci = Components.interfaces;
+const Cu = Components.utils;
+
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+Cu.import("resource://gre/modules/Services.jsm");
+
+const PREF_APP_DISTRIBUTION = "distribution.id";
+const PREF_APP_DISTRIBUTION_VERSION = "distribution.version";
+
+XPCOMUtils.defineLazyModuleGetter(this, "UpdateUtils",
+ "resource://gre/modules/UpdateUtils.jsm");
+
+function nsURLFormatterService() {
+ XPCOMUtils.defineLazyGetter(this, "appInfo", function UFS_appInfo() {
+ return Cc["@mozilla.org/xre/app-info;1"].
+ getService(Ci.nsIXULAppInfo).
+ QueryInterface(Ci.nsIXULRuntime);
+ });
+
+ XPCOMUtils.defineLazyGetter(this, "ABI", function UFS_ABI() {
+ let ABI = "default";
+ try {
+ ABI = this.appInfo.XPCOMABI;
+
+ if ("@mozilla.org/xpcom/mac-utils;1" in Cc) {
+ // Mac universal build should report a different ABI than either macppc
+ // or mactel.
+ let macutils = Cc["@mozilla.org/xpcom/mac-utils;1"]
+ .getService(Ci.nsIMacUtils);
+ if (macutils && macutils.isUniversalBinary) {
+ ABI = "Universal-gcc3";
+ }
+ }
+ } catch (e) {}
+
+ return ABI;
+ });
+
+ XPCOMUtils.defineLazyGetter(this, "OSVersion", function UFS_OSVersion() {
+ let OSVersion = "default";
+ let sysInfo = Cc["@mozilla.org/system-info;1"].
+ getService(Ci.nsIPropertyBag2);
+ try {
+ OSVersion = sysInfo.getProperty("name") + " " +
+ sysInfo.getProperty("version");
+ OSVersion += " (" + sysInfo.getProperty("secondaryLibrary") + ")";
+ } catch (e) {}
+
+ return encodeURIComponent(OSVersion);
+ });
+
+ XPCOMUtils.defineLazyGetter(this, "distribution", function UFS_distribution() {
+ let distribution = { id: "default", version: "default" };
+
+ let defaults = Services.prefs.getDefaultBranch(null);
+ try {
+ distribution.id = defaults.getCharPref(PREF_APP_DISTRIBUTION);
+ } catch (e) {}
+ try {
+ distribution.version = defaults.getCharPref(PREF_APP_DISTRIBUTION_VERSION);
+ } catch (e) {}
+
+ return distribution;
+ });
+}
+
+nsURLFormatterService.prototype = {
+ classID: Components.ID("{e6156350-2be8-11db-a98b-0800200c9a66}"),
+ QueryInterface: XPCOMUtils.generateQI([Ci.nsIURLFormatter]),
+
+ _defaults: {
+ LOCALE: () => Cc["@mozilla.org/chrome/chrome-registry;1"].
+ getService(Ci.nsIXULChromeRegistry).
+ getSelectedLocale('global'),
+ REGION: function() {
+ try {
+ // When the geoip lookup failed to identify the region, we fallback to
+ // the 'ZZ' region code to mean 'unknown'.
+ return Services.prefs.getCharPref("browser.search.region") || "ZZ";
+ } catch(e) {
+ return "ZZ";
+ }
+ },
+ VENDOR: function() { return this.appInfo.vendor; },
+ NAME: function() { return this.appInfo.name; },
+ ID: function() { return this.appInfo.ID; },
+ VERSION: function() { return this.appInfo.version; },
+ MAJOR_VERSION: function() { return this.appInfo.version.replace(/^([^\.]+\.[0-9]+[a-z]*).*/gi, "$1"); },
+ APPBUILDID: function() { return this.appInfo.appBuildID; },
+ PLATFORMVERSION: function() { return this.appInfo.platformVersion; },
+ PLATFORMBUILDID: function() { return this.appInfo.platformBuildID; },
+ APP: function() { return this.appInfo.name.toLowerCase().replace(/ /, ""); },
+ OS: function() { return this.appInfo.OS; },
+ XPCOMABI: function() { return this.ABI; },
+ BUILD_TARGET: function() { return this.appInfo.OS + "_" + this.ABI; },
+ OS_VERSION: function() { return this.OSVersion; },
+ CHANNEL: () => UpdateUtils.UpdateChannel,
+ MOZILLA_API_KEY: () => "@MOZ_MOZILLA_API_KEY@",
+ GOOGLE_API_KEY: () => "@MOZ_GOOGLE_API_KEY@",
+ BING_API_CLIENTID:() => "@MOZ_BING_API_CLIENTID@",
+ BING_API_KEY: () => "@MOZ_BING_API_KEY@",
+ DISTRIBUTION: function() { return this.distribution.id; },
+ DISTRIBUTION_VERSION: function() { return this.distribution.version; }
+ },
+
+ formatURL: function uf_formatURL(aFormat) {
+ var _this = this;
+ var replacementCallback = function(aMatch, aKey) {
+ if (aKey in _this._defaults) {
+ return _this._defaults[aKey].call(_this);
+ }
+ Cu.reportError("formatURL: Couldn't find value for key: " + aKey);
+ return aMatch;
+ }
+ return aFormat.replace(/%([A-Z_]+)%/g, replacementCallback);
+ },
+
+ formatURLPref: function uf_formatURLPref(aPref) {
+ var format = null;
+ var PS = Cc['@mozilla.org/preferences-service;1'].
+ getService(Ci.nsIPrefBranch);
+
+ try {
+ format = PS.getComplexValue(aPref, Ci.nsISupportsString).data;
+ } catch(ex) {
+ Cu.reportError("formatURLPref: Couldn't get pref: " + aPref);
+ return "about:blank";
+ }
+
+ if (!PS.prefHasUserValue(aPref) &&
+ /^(data:text\/plain,.+=.+|chrome:\/\/.+\/locale\/.+\.properties)$/.test(format)) {
+ // This looks as if it might be a localised preference
+ try {
+ format = PS.getComplexValue(aPref, Ci.nsIPrefLocalizedString).data;
+ } catch(ex) {}
+ }
+
+ return this.formatURL(format);
+ },
+
+ trimSensitiveURLs: function uf_trimSensitiveURLs(aMsg) {
+ // Only the google API key is sensitive for now.
+ return "@MOZ_GOOGLE_API_KEY@" ? aMsg.replace(/@MOZ_GOOGLE_API_KEY@/g,
+ "[trimmed-google-api-key]")
+ : aMsg;
+ }
+};
+
+this.NSGetFactory = XPCOMUtils.generateNSGetFactory([nsURLFormatterService]);
diff --git a/toolkit/components/urlformatter/nsURLFormatter.manifest b/toolkit/components/urlformatter/nsURLFormatter.manifest
new file mode 100644
index 000000000..3f9123d73
--- /dev/null
+++ b/toolkit/components/urlformatter/nsURLFormatter.manifest
@@ -0,0 +1,2 @@
+component {e6156350-2be8-11db-a98b-0800200c9a66} nsURLFormatter.js
+contract @mozilla.org/toolkit/URLFormatterService;1 {e6156350-2be8-11db-a98b-0800200c9a66}
diff --git a/toolkit/components/urlformatter/tests/unit/.eslintrc.js b/toolkit/components/urlformatter/tests/unit/.eslintrc.js
new file mode 100644
index 000000000..d35787cd2
--- /dev/null
+++ b/toolkit/components/urlformatter/tests/unit/.eslintrc.js
@@ -0,0 +1,7 @@
+"use strict";
+
+module.exports = {
+ "extends": [
+ "../../../../../testing/xpcshell/xpcshell.eslintrc.js"
+ ]
+};
diff --git a/toolkit/components/urlformatter/tests/unit/head_urlformatter.js b/toolkit/components/urlformatter/tests/unit/head_urlformatter.js
new file mode 100644
index 000000000..8af2aaac4
--- /dev/null
+++ b/toolkit/components/urlformatter/tests/unit/head_urlformatter.js
@@ -0,0 +1,16 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+var Cc = Components.classes;
+var Ci = Components.interfaces;
+var Cr = Components.results;
+var Cu = Components.utils;
+
+Cu.import("resource://testing-common/AppInfo.jsm", this);
+updateAppInfo({
+ name: "Url Formatter Test",
+ ID: "urlformattertest@test.mozilla.org",
+ version: "1",
+ platformVersion: "2.0",
+});
+var gAppInfo = getAppInfo();
diff --git a/toolkit/components/urlformatter/tests/unit/test_urlformatter.js b/toolkit/components/urlformatter/tests/unit/test_urlformatter.js
new file mode 100644
index 000000000..393b0dc33
--- /dev/null
+++ b/toolkit/components/urlformatter/tests/unit/test_urlformatter.js
@@ -0,0 +1,65 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+function run_test() {
+ var formatter = Cc["@mozilla.org/toolkit/URLFormatterService;1"].
+ getService(Ci.nsIURLFormatter);
+ var locale = Cc["@mozilla.org/chrome/chrome-registry;1"].
+ getService(Ci.nsIXULChromeRegistry).
+ getSelectedLocale('global');
+ var prefs = Cc['@mozilla.org/preferences-service;1'].
+ getService(Ci.nsIPrefBranch);
+ var sysInfo = Cc["@mozilla.org/system-info;1"].
+ getService(Ci.nsIPropertyBag2);
+ var OSVersion = sysInfo.getProperty("name") + " " +
+ sysInfo.getProperty("version");
+ try {
+ OSVersion += " (" + sysInfo.getProperty("secondaryLibrary") + ")";
+ } catch (e) {}
+ OSVersion = encodeURIComponent(OSVersion);
+ var macutils = null;
+ try {
+ macutils = Cc["@mozilla.org/xpcom/mac-utils;1"].
+ getService(Ci.nsIMacUtils);
+ } catch (e) {}
+ var appInfo = Cc["@mozilla.org/xre/app-info;1"].
+ getService(Ci.nsIXULAppInfo).
+ QueryInterface(Ci.nsIXULRuntime);
+ var abi = macutils && macutils.isUniversalBinary ? "Universal-gcc3" : appInfo.XPCOMABI;
+
+ let channel = "default";
+ let defaults = prefs.QueryInterface(Ci.nsIPrefService).getDefaultBranch(null);
+ try {
+ channel = defaults.getCharPref("app.update.channel");
+ } catch (e) {}
+ // Set distribution values.
+ defaults.setCharPref("distribution.id", "bacon");
+ defaults.setCharPref("distribution.version", "1.0");
+
+ var upperUrlRaw = "http://%LOCALE%.%VENDOR%.foo/?name=%NAME%&id=%ID%&version=%VERSION%&platversion=%PLATFORMVERSION%&abid=%APPBUILDID%&pbid=%PLATFORMBUILDID%&app=%APP%&os=%OS%&abi=%XPCOMABI%";
+ var lowerUrlRaw = "http://%locale%.%vendor%.foo/?name=%name%&id=%id%&version=%version%&platversion=%platformversion%&abid=%appbuildid%&pbid=%platformbuildid%&app=%app%&os=%os%&abi=%xpcomabi%";
+ // XXX %APP%'s RegExp is not global, so it only replaces the first space
+ var ulUrlRef = "http://" + locale + ".Mozilla.foo/?name=Url Formatter Test&id=urlformattertest@test.mozilla.org&version=1&platversion=2.0&abid=" + gAppInfo.appBuildID + "&pbid=" + gAppInfo.platformBuildID + "&app=urlformatter test&os=XPCShell&abi=" + abi;
+ var multiUrl = "http://%VENDOR%.%VENDOR%.%NAME%.%VENDOR%.%NAME%";
+ var multiUrlRef = "http://Mozilla.Mozilla.Url Formatter Test.Mozilla.Url Formatter Test";
+ var encodedUrl = "https://%LOCALE%.%VENDOR%.foo/?q=%E3%82%BF%E3%83%96&app=%NAME%&ver=%PLATFORMVERSION%";
+ var encodedUrlRef = "https://" + locale + ".Mozilla.foo/?q=%E3%82%BF%E3%83%96&app=Url Formatter Test&ver=2.0";
+ var advancedUrl = "http://test.mozilla.com/%NAME%/%VERSION%/%APPBUILDID%/%BUILD_TARGET%/%LOCALE%/%CHANNEL%/%OS_VERSION%/%DISTRIBUTION%/%DISTRIBUTION_VERSION%/";
+ var advancedUrlRef = "http://test.mozilla.com/Url Formatter Test/1/" + gAppInfo.appBuildID + "/XPCShell_" + abi + "/" + locale + "/" + channel + "/" + OSVersion + "/bacon/1.0/";
+
+ var pref = "xpcshell.urlformatter.test";
+ var str = Cc["@mozilla.org/supports-string;1"].
+ createInstance(Ci.nsISupportsString);
+ str.data = upperUrlRaw;
+ prefs.setComplexValue(pref, Ci.nsISupportsString, str);
+
+ do_check_eq(formatter.formatURL(upperUrlRaw), ulUrlRef);
+ do_check_eq(formatter.formatURLPref(pref), ulUrlRef);
+ // Keys must be uppercase
+ do_check_neq(formatter.formatURL(lowerUrlRaw), ulUrlRef);
+ do_check_eq(formatter.formatURL(multiUrl), multiUrlRef);
+ // Encoded strings must be kept as is (Bug 427304)
+ do_check_eq(formatter.formatURL(encodedUrl), encodedUrlRef);
+
+ do_check_eq(formatter.formatURL(advancedUrl), advancedUrlRef);
+}
diff --git a/toolkit/components/urlformatter/tests/unit/xpcshell.ini b/toolkit/components/urlformatter/tests/unit/xpcshell.ini
new file mode 100644
index 000000000..2f82beaa3
--- /dev/null
+++ b/toolkit/components/urlformatter/tests/unit/xpcshell.ini
@@ -0,0 +1,6 @@
+[DEFAULT]
+head = head_urlformatter.js
+tail =
+skip-if = toolkit == 'android'
+
+[test_urlformatter.js]