Bug 930924 - Open workers from xpcshell. r=bent

This commit is contained in:
David Rajchenbach-Teller 2013-11-06 09:05:17 -05:00
parent 2fa356509c
commit 6bd6992253
6 changed files with 90 additions and 3 deletions

View File

@ -3584,11 +3584,37 @@ WorkerPrivate::GetLoadInfo(JSContext* aCx, nsPIDOMWindow* aWindow,
// that is creating us in order for us to use relative URIs later on.
JS::RootedScript script(aCx);
if (JS_DescribeScriptedCaller(aCx, &script, nullptr)) {
rv = NS_NewURI(getter_AddRefs(loadInfo.mBaseURI),
JS_GetScriptFilename(aCx, script));
NS_ENSURE_SUCCESS(rv, rv);
const char* fileName = JS_GetScriptFilename(aCx, script);
// In most cases, fileName is URI. In a few other cases
// (e.g. xpcshell), fileName is a file path. Ideally, we would
// prefer testing whether fileName parses as an URI and fallback
// to file path in case of error, but Windows file paths have
// the interesting property that they can be parsed as bogus
// URIs (e.g. C:/Windows/Tmp is interpreted as scheme "C",
// hostname "Windows", path "Tmp"), which defeats this algorithm.
// Therefore, we adopt the opposite convention.
nsCOMPtr<nsIFile> scriptFile =
do_CreateInstance("@mozilla.org/file/local;1", &rv);
if (NS_FAILED(rv)) {
return rv;
}
rv = scriptFile->InitWithPath(NS_ConvertUTF8toUTF16(fileName));
if (NS_SUCCEEDED(rv)) {
rv = NS_NewFileURI(getter_AddRefs(loadInfo.mBaseURI),
scriptFile);
}
if (NS_FAILED(rv)) {
// As expected, fileName is not a path, so proceed with
// a uri.
rv = NS_NewURI(getter_AddRefs(loadInfo.mBaseURI),
fileName);
}
if (NS_FAILED(rv)) {
return rv;
}
}
loadInfo.mXHRParamsAllowed = true;
}

View File

@ -13,3 +13,4 @@ MOCHITEST_MANIFESTS += ['mochitest.ini']
MOCHITEST_CHROME_MANIFESTS += ['chrome.ini']
XPCSHELL_TESTS_MANIFESTS += ['xpcshell/xpcshell.ini']

View File

@ -0,0 +1 @@
content workers ./

View File

@ -0,0 +1,7 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
self.onmessage = function(msg) {
self.postMessage("OK");
};

View File

@ -0,0 +1,44 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
Components.utils.import("resource://gre/modules/Promise.jsm");
// Worker must be loaded from a chrome:// uri, not a file://
// uri, so we first need to load it.
let WORKER_SOURCE_URI = "chrome://workers/content/worker.js";
do_load_manifest("data/chrome.manifest");
function run_test() {
run_next_test();
}
function talk_with_worker(worker) {
let deferred = Promise.defer();
worker.onmessage = function(event) {
let success = true;
if (event.data == "OK") {
deferred.resolve();
} else {
success = false;
deferred.reject(event);
}
do_check_true(success);
worker.terminate();
};
worker.onerror = function(event) {
let error = new Error(event.message, event.filename, event.lineno);
worker.terminate();
deferred.reject(error);
};
worker.postMessage("START");
return deferred.promise;
}
add_task(function test_chrome_worker() {
return talk_with_worker(new ChromeWorker(WORKER_SOURCE_URI));
});
add_task(function test_worker() {
return talk_with_worker(new Worker(WORKER_SOURCE_URI));
});

View File

@ -0,0 +1,8 @@
[DEFAULT]
head =
tail =
support-files =
data/worker.js
data/chrome.manifest
[test_workers.js]