Commit 1c67b00d authored by Dave Townsend's avatar Dave Townsend
Browse files

Bug 1586216: Fallback to a synchronous channel load if the url preloader...

Bug 1586216: Fallback to a synchronous channel load if the url preloader cannot load the ftl file. r=kmag

Differential Revision: https://phabricator.services.mozilla.com/D48726

--HG--
extra : moz-landing-system : lando
parent f7d6d27d
......@@ -5,6 +5,11 @@ const appinfo = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime);
const { FluentBundle, FluentResource } = ChromeUtils.import("resource://gre/modules/Fluent.jsm");
const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyGlobalGetters(this, ["fetch"]);
ChromeUtils.defineModuleGetter(
this,
"NetUtil",
"resource://gre/modules/NetUtil.jsm"
);
const isParentProcess = appinfo.processType === appinfo.PROCESS_TYPE_DEFAULT;
/**
......@@ -754,8 +759,30 @@ this.L10nRegistry.loadSync = function(uri) {
let data = Cu.readUTF8URI(url);
return data;
} catch (e) {
return false;
if (
e.result == Cr.NS_ERROR_INVALID_ARG ||
e.result == Cr.NS_ERROR_NOT_INITIALIZED
) {
try {
// The preloader doesn't support this url or isn't initialized
// (xpcshell test). Try a synchronous channel load.
let stream = NetUtil.newChannel({
uri,
loadUsingSystemPrincipal: true,
}).open();
return NetUtil.readInputStreamToString(stream, stream.available(), {
charset: "UTF-8",
});
} catch (e) {
Cu.reportError(e);
}
} else {
Cu.reportError(e);
}
}
return false;
};
this.FileSource = FileSource;
......
......@@ -678,6 +678,7 @@ interface nsIXPCComponents_Utils : nsISupports
/*
* Reads the given local file URL and returns its contents. This has the
* same semantics of readUTF8File.
* Only supports file URLs or URLs that point into one of the omnijars.
*/
AUTF8String readUTF8URI(in nsIURI url);
......
......@@ -109,7 +109,7 @@ add_task(async function() {
});
/**
* This test verifies that registries are able to load and return
* This test verifies that registries are able to load and synchronously return
* correct strings available in the language pack.
*/
add_task(async function() {
......@@ -120,15 +120,20 @@ add_task(async function() {
{
// Toolkit string
let bundles = L10nRegistry.generateBundles(["und"], ["toolkit_test.ftl"]);
let bundle0 = (await bundles.next()).value;
let bundles = L10nRegistry.generateBundlesSync(
["und"],
["toolkit_test.ftl"]
);
let bundle0 = bundles.next().value;
ok(bundle0);
equal(bundle0.hasMessage("message-id1"), true);
}
{
// Browser string
let bundles = L10nRegistry.generateBundles(["und"], ["browser.ftl"]);
let bundle0 = (await bundles.next()).value;
let bundles = L10nRegistry.generateBundlesSync(["und"], ["browser.ftl"]);
let bundle0 = bundles.next().value;
ok(bundle0);
equal(bundle0.hasMessage("message-browser"), true);
}
......@@ -146,6 +151,33 @@ add_task(async function() {
Services.locale.requestedLocales = reqLocs;
}
await addon.uninstall();
});
/**
* This test verifies that registries are able to load and asynchronously return
* correct strings available in the language pack.
*/
add_task(async function() {
let [, { addon }] = await Promise.all([
promiseLangpackStartup(),
AddonTestUtils.promiseInstallXPI(ADDONS.langpack_1),
]);
{
// Toolkit string
let bundles = L10nRegistry.generateBundles(["und"], ["toolkit_test.ftl"]);
let bundle0 = (await bundles.next()).value;
equal(bundle0.hasMessage("message-id1"), true);
}
{
// Browser string
let bundles = L10nRegistry.generateBundles(["und"], ["browser.ftl"]);
let bundle0 = (await bundles.next()).value;
equal(bundle0.hasMessage("message-browser"), true);
}
await addon.uninstall();
await promiseShutdownManager();
});
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment