Consolidate best effort policy fetching.

This commit is contained in:
hackademix 2022-02-19 19:57:54 +01:00
parent 4aae87cdc8
commit 44f33fc585
2 changed files with 46 additions and 55 deletions

View File

@ -95,42 +95,49 @@
}
if (!this.syncFetchPolicy) {
let msg = {id: "fetchChildPolicy", url};
let asyncFetch = (async () => {
let policy = null;
for (let attempts = 10; !(policy || this.policy) && attempts-- > 0;) {
try {
debug(`Retrieving policy asynchronously (${attempts} attempts left).`);
policy = await Messages.send(msg.id, msg) || this.domPolicy;
debug("Asynchronous policy", policy);
} catch (e) {
error(e, "(Asynchronous policy fetch)");
}
}
this.setup(policy);
});
debug(`Synchronously fetching policy for ${url}.`);
let policy = null;
let attempts = 100;
let refetch = () => {
policy = browser.runtime.sendSyncMessage(msg) || this.domPolicy;
if (policy) {
this.setup(policy);
} else if (attempts-- > 0) {
debug(`Couldn't retrieve policy synchronously (${attempts} attempts left).`);
if (asyncFetch) {
asyncFetch();
asyncFetch = null;
}
queueMicrotask(refetch);
}
};
refetch();
this.fetchLikeNoTomorrow(url);
}
},
fetchLikeNoTomorrow(url, setup = this.setup.bind(this)) {
let msg = {id: "fetchChildPolicy", url};
let asyncFetch = (async () => {
let policy = null;
for (let attempts = 10; !(policy || this.policy) && attempts-- > 0;) {
try {
debug(`Retrieving policy asynchronously (${attempts} attempts left).`);
policy = await Messages.send(msg.id, msg) || this.domPolicy;
debug("Asynchronous policy", policy);
} catch (e) {
error(e, "(Asynchronous policy fetch)");
}
}
setup(policy);
});
debug(`Synchronously fetching policy for ${url}.`);
let policy = null;
let attempts = 100;
let refetch = () => {
try {
policy = browser.runtime.sendSyncMessage(msg) || this.domPolicy;
} catch (e) {
error(e);
}
if (policy) {
setup(policy);
} else if (attempts-- > 0) {
debug(`Couldn't retrieve policy synchronously (${attempts} attempts left).`);
if (asyncFetch) {
asyncFetch();
asyncFetch = null;
}
queueMicrotask(refetch);
}
};
refetch();
},
setup(policy) {
if (this.policy) return false;
debug("%s, %s, fetched %o", document.URL, document.readyState, policy, new Error().stack); // DEV_ONLY

View File

@ -31,12 +31,12 @@
// injected in the DOM as soon as possible.
debug("No CSP yet for non-HTTP document load: fetching policy synchronously...", ns);
ns.syncSetup = ns.setup.bind(ns);
let syncSetup = ns.setup.bind(ns);
if (window.wrappedJSObject) {
if (top === window) {
let persistentPolicy = null;
ns.syncSetup = policy => {
syncSetup = policy => {
if (persistentPolicy) return;
ns.setup(policy);
persistentPolicy = JSON.stringify(policy);
@ -58,18 +58,14 @@
}
}
} catch (e) {
// cross-origin accesss violation, ignore
// cross-origin access violation, ignore
}
}
if (ns.domPolicy) {
ns.syncSetup(ns.domPolicy);
syncSetup(ns.domPolicy);
return;
}
let syncFetch = callback => {
browser.runtime.sendSyncMessage(
{id: "fetchPolicy", url},
callback);
};
debug("Initial readyState and body", document.readyState, document.body);
let mustFreeze = UA.isMozilla
@ -225,19 +221,7 @@
}
}
for (let attempts = 3; attempts-- > 0;) {
try {
if (ns.policy) break;
syncFetch(ns.syncSetup);
break;
} catch (e) {
if (!Messages.isMissingEndpoint(e) || document.readyState === "complete") {
error(e);
break;
}
error("Background page not ready yet, retrying to fetch policy...")
}
}
ns.fetchLikeNoTomorrow(url, syncSetup);
};
if (ns.pendingSyncFetchPolicy) {