Bug 1843001 - Allow fetch() to localhost in Workers while network is offline. r=asuth,necko-reviewers,valentin

Differential Revision: https://phabricator.services.mozilla.com/D198374
This commit is contained in:
Eden Chuang 2024-09-23 09:07:52 +00:00
Родитель ac610a638e
Коммит a009c49f93
8 изменённых файлов: 124 добавлений и 11 удалений

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

@ -297,6 +297,18 @@ RefPtr<FetchServicePromises> FetchService::FetchInstance::Fetch() {
return mPromises;
}
bool FetchService::FetchInstance::IsLocalHostFetch() const {
if (!mPrincipal) {
return false;
}
bool res;
nsresult rv = mPrincipal->GetIsLoopbackHost(&res);
if (NS_WARN_IF(NS_FAILED(rv))) {
return false;
}
return res;
}
void FetchService::FetchInstance::Cancel() {
MOZ_ASSERT(XRE_IsParentProcess());
MOZ_ASSERT(NS_IsMainThread());
@ -778,12 +790,15 @@ NS_IMETHODIMP FetchService::Observe(nsISupports* aSubject, const char* aTopic,
mOffline = false;
} else {
mOffline = true;
// Network is offline, cancel running fetchs.
for (auto it = mFetchInstanceTable.begin(), end = mFetchInstanceTable.end();
it != end; ++it) {
it->GetData()->Cancel();
}
mFetchInstanceTable.Clear();
// Network is offline, cancel the running fetch that is not to local server.
mFetchInstanceTable.RemoveIf([](auto& entry) {
bool res = entry.Data()->IsLocalHostFetch();
if (res) {
return false;
}
entry.Data()->Cancel();
return true;
});
}
return NS_OK;
}
@ -795,11 +810,6 @@ RefPtr<FetchServicePromises> FetchService::Fetch(FetchArgs&& aArgs) {
FETCH_LOG(("FetchService::Fetch (%s)", aArgs.is<NavigationPreloadArgs>()
? "NavigationPreload"
: "WorkerFetch"));
if (mOffline) {
FETCH_LOG(("FetchService::Fetch network offline"));
return NetworkErrorResponse(NS_ERROR_OFFLINE, aArgs);
}
// Create FetchInstance
RefPtr<FetchInstance> fetch = MakeRefPtr<FetchInstance>();
@ -810,6 +820,11 @@ RefPtr<FetchServicePromises> FetchService::Fetch(FetchArgs&& aArgs) {
return NetworkErrorResponse(rv, fetch->Args());
}
if (mOffline && !fetch->IsLocalHostFetch()) {
FETCH_LOG(("FetchService::Fetch network offline"));
return NetworkErrorResponse(NS_ERROR_OFFLINE, fetch->Args());
}
// Call FetchInstance::Fetch() to start an asynchronous fetching.
RefPtr<FetchServicePromises> promises = fetch->Fetch();
MOZ_ASSERT(promises);

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

@ -171,6 +171,8 @@ class FetchService final : public nsIObserver {
void Cancel();
bool IsLocalHostFetch() const;
/* FetchDriverObserver interface */
void OnResponseEnd(FetchDriverObserver::EndReason aReason,
JS::Handle<JS::Value> aReasonDetails) override;

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

@ -208,6 +208,10 @@ support-files = [
"onmessageerror_worker.js",
"pref/fetch_nonexistent_file.html",
"pref/intercept_nonexistent_file_sw.js",
"offline_fetch/register.html",
"offline_fetch/index.html",
"offline_fetch/unregister.html",
"offline_fetch/sw.js",
]
["test_abrupt_completion.html"]
@ -338,6 +342,9 @@ skip-if = ["os == 'linux' && bits == 64 && debug"] # Bug 1749068
["test_not_intercept_plugin.html"]
skip-if = ["serviceworker_e10s"] # leaks InterceptedHttpChannel and others things
["test_offline_localhost_fetch.html"]
skip-if = ["os == 'android'"]
["test_onmessageerror.html"]
skip-if = ["xorigin"] # Bug 1792790

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

@ -0,0 +1,4 @@
<!DOCTYPE html>
<script>
window.parent.postMessage({status: "done"}, "*");
</script>

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

@ -0,0 +1,16 @@
<!DOCTYPE html>
<script>
function ok(v, msg) {
window.parent.postMessage({status: "ok", result: !!v, message: msg}, "*");
}
function done(reg) {
ok(reg.active, "The active worker should be available.");
window.parent.postMessage({status: "registrationdone"}, "*");
}
navigator.serviceWorker.ready.then(reg => {
done(reg);
});
navigator.serviceWorker.register("sw.js", {scope: "."});
</script>

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

@ -0,0 +1,3 @@
self.addEventListener("fetch", function (event) {
event.respondWith(fetch(event.request));
});

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

@ -0,0 +1,12 @@
<!DOCTYPE html>
<script>
navigator.serviceWorker.getRegistration(".").then(function(registration) {
registration.unregister().then(function(success) {
if (success) {
window.parent.postMessage({status: "unregistrationdone"}, "*");
}
}, function(e) {
dump("Unregistering the SW failed with " + e + "\n");
});
});
</script>

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

@ -0,0 +1,54 @@
<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html>
<head>
<title>Bug 1843001</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<iframe></iframe>
<script>
const basePath = window.location.pathname.replace(/\/[^\/]*$/, '/');
const localhostBaseURL = "http://127.0.0.1:8888" + basePath;
var iframe;
function runTest() {
iframe = document.querySelector("iframe");
iframe.src = localhostBaseURL + "offline_fetch/register.html";
var ios;
window.onmessage = function(e) {
if (e.data.status == "ok") {
ok(e.data.result, e.data.message);
} else if (e.data.status == "registrationdone") {
ios = SpecialPowers.Cc["@mozilla.org/network/io-service;1"]
.getService(SpecialPowers.Ci.nsIIOService);
ios.offline = true;
iframe.src = localhostBaseURL + "offline_fetch/index.html";
} else if (e.data.status == "done") {
ios.offline = false;
iframe.src = localhostBaseURL + "offline_fetch/unregister.html";
} else if (e.data.status == "unregistrationdone") {
window.onmessage = null;
ok(true, "Test finished successfully");
SimpleTest.finish();
}
};
}
SimpleTest.waitForExplicitFinish();
onload = function() {
SpecialPowers.pushPrefEnv({"set": [
["dom.serviceWorkers.exemptFromPerDomainMax", true],
["dom.serviceWorkers.enabled", true],
["dom.serviceWorkers.testing.enabled", true],
]}, runTest);
};
</script>
</pre>
</body>
</html>