|
90 | 90 | // extra hops to ensure that scripts don't run when CSP has not been set through HTTP headers |
91 | 91 | this.syncFetchPolicy(); |
92 | 92 | } else { |
93 | | - let msg = {id: "fetchPolicy", url, contextUrl: url}; |
94 | | - debug(`Synchronously fetching policy for ${url}.`); |
95 | | - let policy = browser.runtime.sendSyncMessage(msg); |
96 | | - if (!policy) { |
97 | | - debug(`Couldn't retrieve policy synchronously, trying async.`); |
98 | | - (async () => this.setup(await browser.runtime.sendMessage(msg)))(); |
99 | | - } else { |
| 93 | + let msg = {id: "fetchChildPolicy", url, contextUrl: url}; |
| 94 | + |
| 95 | + let asyncFetch = (async () => { |
| 96 | + let policy = null; |
| 97 | + for (let attempts = 10; !(policy || this.policy) && attempts-- > 0;) { |
| 98 | + try { |
| 99 | + debug(`Retrieving policy asynchronously (${attempts} attempts left).`); |
| 100 | + policy = await Messages.send(msg.id, msg) || this.domPolicy; |
| 101 | + debug("Asynchronous policy", policy); |
| 102 | + } catch (e) { |
| 103 | + error(e, "(Asynchronous policy fetch)"); |
| 104 | + } |
| 105 | + } |
100 | 106 | this.setup(policy); |
101 | | - } |
| 107 | + }); |
| 108 | + debug(`Synchronously fetching policy for ${url}.`); |
| 109 | + let policy = null; |
| 110 | + let attempts = 100; |
| 111 | + let refetch = () => { |
| 112 | + policy = browser.runtime.sendSyncMessage(msg) || this.domPolicy; |
| 113 | + if (policy) { |
| 114 | + this.setup(policy); |
| 115 | + } else if (attempts-- > 0) { |
| 116 | + debug(`Couldn't retrieve policy synchronously (${attempts} attempts left).`); |
| 117 | + if (asyncFetch) { |
| 118 | + asyncFetch(); |
| 119 | + asyncFetch = null; |
| 120 | + } |
| 121 | + queueMicrotask(refetch); |
| 122 | + } |
| 123 | + }; |
| 124 | + refetch(); |
102 | 125 | } |
103 | 126 | }, |
104 | 127 |
|
105 | 128 | setup(policy) { |
106 | 129 | if (this.policy) return false; |
107 | | - debug("%s, %s, fetched %o", document.URL, document.readyState, policy); |
| 130 | + debug("%s, %s, fetched %o", document.URL, document.readyState, policy, new Error().stack); // DEV_ONLY |
108 | 131 | if (!policy) { |
109 | 132 | policy = {permissions: {capabilities: []}, localFallback: true}; |
110 | 133 | } |
|
0 commit comments