bug 563738 (part1) - Add missing information to the URLFormatter. r=dietrich a=blocking

This commit is contained in:
Marco Bonardo 2010-09-09 12:20:07 +02:00
Родитель 897cda976f
Коммит e4bbaca9c0
2 изменённых файлов: 104 добавлений и 15 удалений

Просмотреть файл

@ -20,6 +20,7 @@
#
# Contributor(s):
# Dietrich Ayala <dietrich@mozilla.com>
# Marco Bonardo <mak77@bonardo.net>
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
@ -50,23 +51,96 @@ 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_UPDATE_CHANNEL = "app.update.channel";
const PREF_PARTNER_BRANCH = "app.partner.";
const PREF_APP_DISTRIBUTION = "distribution.id";
const PREF_APP_DISTRIBUTION_VERSION = "distribution.version";
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;
// 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, "updateChannel", function UFS_updateChannel() {
// Read the update channel from defaults only. We do this to ensure that
// the channel is tightly coupled with the application and does not apply
// to other instances of the application that may use the same profile.
let channel = "default";
let defaults = Services.prefs.getDefaultBranch(null);
try {
channel = defaults.getCharPref(PREF_APP_UPDATE_CHANNEL);
} catch (e) {}
try {
let partners = Services.prefs.getChildList(PREF_PARTNER_BRANCH).sort();
if (partners.length) {
channel += "-cck";
partners.forEach(function (prefName) {
channel += "-" + Services.prefs.getCharPref(prefName);
});
}
} catch (e) {}
return channel;
});
XPCOMUtils.defineLazyGetter(this, "distribution", function UFS_distribution() {
let distribution = { id: "default", version: "default" };
let defaults = Services.prefs.getDefaultBranch(null);
try {
distibution.id = defaults.getCharPref(PREF_APP_DISTRIBUTION);
} catch (e) {}
try {
distibution.version = defaults.getCharPref(PREF_APP_DISTRIBUTION_VERSION);
} catch (e) {}
return distribution;
});
}
function nsURLFormatterService() {}
nsURLFormatterService.prototype = {
classID: Components.ID("{e6156350-2be8-11db-a98b-0800200c9a66}"),
QueryInterface: XPCOMUtils.generateQI([Ci.nsIURLFormatter]),
_defaults: {
get appInfo() {
if (!this._appInfo)
this._appInfo = Cc["@mozilla.org/xre/app-info;1"].
getService(Ci.nsIXULAppInfo).
QueryInterface(Ci.nsIXULRuntime);
return this._appInfo;
},
LOCALE: function() Cc["@mozilla.org/chrome/chrome-registry;1"].
getService(Ci.nsIXULChromeRegistry).getSelectedLocale('global'),
LOCALE: function() Cc["@mozilla.org/chrome/chrome-registry;1"].
getService(Ci.nsIXULChromeRegistry).
getSelectedLocale('global'),
VENDOR: function() this.appInfo.vendor,
NAME: function() this.appInfo.name,
ID: function() this.appInfo.ID,
@ -76,18 +150,24 @@ nsURLFormatterService.prototype = {
PLATFORMBUILDID: function() this.appInfo.platformBuildID,
APP: function() this.appInfo.name.toLowerCase().replace(/ /, ""),
OS: function() this.appInfo.OS,
XPCOMABI: function() this.appInfo.XPCOMABI
XPCOMABI: function() this.ABI,
BUILD_TARGET: function() this.appInfo.OS + "_" + this.ABI,
OS_VERSION: function() this.OSVersion,
CHANNEL: function() this.updateChannel,
DISTRIBUTION: function() this.distribution.id,
DISTRIBUTION_VERSION: function() this.distribution.version
},
formatURL: function uf_formatURL(aFormat) {
var _this = this;
var replacementCallback = function(aMatch, aKey) {
if (aKey in _this._defaults) // supported defaults
return _this._defaults[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);
return aFormat.replace(/%([A-Z_]+)%/g, replacementCallback);
},
formatURLPref: function uf_formatURLPref(aPref) {

Просмотреть файл

@ -42,6 +42,11 @@ function run_test() {
var prefs = Cc['@mozilla.org/preferences-service;1'].
getService(Ci.nsIPrefBranch);
var sysInfo = Cc["@mozilla.org/system-info;1"].
getService(Ci.nsIPropertyBag2);
var OSVersion = encodeURIComponent(sysInfo.getProperty("name") + " " +
sysInfo.getProperty("version"));
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
@ -50,6 +55,8 @@ function run_test() {
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/2007122405/XPCShell_noarch-spidermonkey/" + locale + "/default/" + OSVersion + "/default/default/";
var pref = "xpcshell.urlformatter.test";
var str = Cc["@mozilla.org/supports-string;1"].
@ -64,4 +71,6 @@ function run_test() {
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);
}