Bug 935377 - Firefox should fix common scheme typos, r=bz

This commit is contained in:
Christian Legnitto 2013-11-17 12:16:45 -08:00
parent e7dfb1c550
commit 3948d15ac9
5 changed files with 154 additions and 1 deletions

View File

@ -29,6 +29,8 @@ using namespace mozilla;
/* Implementation file */
NS_IMPL_ISUPPORTS1(nsDefaultURIFixup, nsIURIFixup)
static bool sFixTypos = true;
nsDefaultURIFixup::nsDefaultURIFixup()
{
/* member initializers and constructor code */
@ -214,6 +216,50 @@ nsDefaultURIFixup::CreateFixupURI(const nsACString& aStringURI, uint32_t aFixupF
scheme.LowerCaseEqualsLiteral("ftp") ||
scheme.LowerCaseEqualsLiteral("file"));
// Check if we want to fix up common scheme typos.
rv = Preferences::AddBoolVarCache(&sFixTypos,
"browser.fixup.typo.scheme",
sFixTypos);
MOZ_ASSERT(NS_SUCCEEDED(rv),
"Failed to observe \"browser.fixup.typo.scheme\"");
// Fix up common scheme typos.
if (sFixTypos && (aFixupFlags & FIXUP_FLAG_FIX_SCHEME_TYPOS)) {
// Fast-path for common cases.
if (scheme.IsEmpty() ||
scheme.LowerCaseEqualsLiteral("http") ||
scheme.LowerCaseEqualsLiteral("https") ||
scheme.LowerCaseEqualsLiteral("ftp") ||
scheme.LowerCaseEqualsLiteral("file")) {
// Do nothing.
} else if (scheme.LowerCaseEqualsLiteral("ttp")) {
// ttp -> http.
uriString.Replace(0, 3, "http");
scheme.AssignLiteral("http");
} else if (scheme.LowerCaseEqualsLiteral("ttps")) {
// ttps -> https.
uriString.Replace(0, 4, "https");
scheme.AssignLiteral("https");
} else if (scheme.LowerCaseEqualsLiteral("tps")) {
// tps -> https.
uriString.Replace(0, 3, "https");
scheme.AssignLiteral("https");
} else if (scheme.LowerCaseEqualsLiteral("ps")) {
// ps -> https.
uriString.Replace(0, 2, "https");
scheme.AssignLiteral("https");
} else if (scheme.LowerCaseEqualsLiteral("ile")) {
// ile -> file.
uriString.Replace(0, 3, "file");
scheme.AssignLiteral("file");
} else if (scheme.LowerCaseEqualsLiteral("le")) {
// le -> file.
uriString.Replace(0, 2, "file");
scheme.AssignLiteral("file");
}
}
// Now we need to check whether "scheme" is something we don't
// really know about.
nsCOMPtr<nsIProtocolHandler> ourHandler, extHandler;

View File

@ -12,7 +12,7 @@ interface nsIInputStream;
/**
* Interface implemented by objects capable of fixing up strings into URIs
*/
[scriptable, uuid(552a23bb-c1b2-426e-a801-d346c6a98f1d)]
[scriptable, uuid(731877f8-973b-414c-b772-9ca1f3fffb7e)]
interface nsIURIFixup : nsISupports
{
/** No fixup flags. */
@ -36,6 +36,11 @@ interface nsIURIFixup : nsISupports
*/
const unsigned long FIXUP_FLAG_USE_UTF8 = 4;
/**
* Fix common scheme typos.
*/
const unsigned long FIXUP_FLAG_FIX_SCHEME_TYPOS = 8;
/**
* Converts an internal URI (e.g. a wyciwyg URI) into one which we can
* expose to the user, for example on the URL bar.

View File

@ -0,0 +1,93 @@
let urifixup = Cc["@mozilla.org/docshell/urifixup;1"].
getService(Ci.nsIURIFixup);
let prefs = Cc["@mozilla.org/preferences-service;1"].
getService(Ci.nsIPrefBranch);
let pref = "browser.fixup.typo.scheme";
let data = [
{
// ttp -> http.
wrong: 'ttp://www.example.com/',
fixed: 'http://www.example.com/',
},
{
// ttps -> https.
wrong: 'ttps://www.example.com/',
fixed: 'https://www.example.com/',
},
{
// tps -> https.
wrong: 'tps://www.example.com/',
fixed: 'https://www.example.com/',
},
{
// ps -> https.
wrong: 'ps://www.example.com/',
fixed: 'https://www.example.com/',
},
{
// ile -> file.
wrong: 'ile:///this/is/a/test.html',
fixed: 'file:///this/is/a/test.html',
},
{
// le -> file.
wrong: 'le:///this/is/a/test.html',
fixed: 'file:///this/is/a/test.html',
},
{
// Valid should not be changed.
wrong: 'https://example.com/this/is/a/test.html',
fixed: 'https://example.com/this/is/a/test.html',
},
{
// Unmatched should not be changed.
wrong: 'whatever://this/is/a/test.html',
fixed: 'whatever://this/is/a/test.html',
},
];
let len = data.length;
function run_test() {
run_next_test();
}
// Make sure we fix what needs fixing when there is no pref set.
add_task(function test_unset_pref_fixes_typos() {
prefs.clearUserPref(pref);
for (let i = 0; i < len; ++i) {
let item = data[i];
let result =
urifixup.createFixupURI(item.wrong,
urifixup.FIXUP_FLAG_FIX_SCHEME_TYPOS).spec;
do_check_eq(result, item.fixed);
}
});
// Make sure we don't do anything when the pref is explicitly
// set to false.
add_task(function test_false_pref_keeps_typos() {
prefs.setBoolPref(pref, false);
for (let i = 0; i < len; ++i) {
let item = data[i];
let result =
urifixup.createFixupURI(item.wrong,
urifixup.FIXUP_FLAG_FIX_SCHEME_TYPOS).spec;
do_check_eq(result, item.wrong);
}
});
// Finally, make sure we still fix what needs fixing if the pref is
// explicitly set to true.
add_task(function test_true_pref_fixes_typos() {
prefs.setBoolPref(pref, true);
for (let i = 0; i < len; ++i) {
let item = data[i];
let result =
urifixup.createFixupURI(item.wrong,
urifixup.FIXUP_FLAG_FIX_SCHEME_TYPOS).spec;
do_check_eq(result, item.fixed);
}
});

View File

@ -4,6 +4,7 @@ tail =
[test_bug414201_jfif.js]
[test_bug442584.js]
[test_nsDefaultURIFixup.js]
[test_nsIDownloadHistory.js]
[test_pb_notification.js]
# Bug 751575: unrelated JS changes cause timeouts on random platforms

View File

@ -969,6 +969,14 @@ pref("network.protocol-handler.external.disks", false);
pref("network.protocol-handler.external.afp", false);
pref("network.protocol-handler.external.moz-icon", false);
// Don't allow external protocol handlers for common typos
pref("network.protocol-handler.external.ttp", false); // http
pref("network.protocol-handler.external.ttps", false); // https
pref("network.protocol-handler.external.tps", false); // https
pref("network.protocol-handler.external.ps", false); // https
pref("network.protocol-handler.external.ile", false); // file
pref("network.protocol-handler.external.le", false); // file
// An exposed protocol handler is one that can be used in all contexts. A
// non-exposed protocol handler is one that can only be used internally by the
// application. For example, a non-exposed protocol would not be loaded by the