1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 01:47:00 +01:00

MV3: support Service Worker restart for phishing warning pages (#16488)

* dapp: fix phishing Could not establish connection

* dapp: only call phishing keep alive timer on load

Co-authored-by: Jyoti Puri <jyotipuri@gmail.com>

Co-authored-by: Jyoti Puri <jyotipuri@gmail.com>
This commit is contained in:
Ariella Vu 2022-11-17 22:22:52 +07:00 committed by GitHub
parent 0841c1df4f
commit 2966b9f665
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,10 +6,7 @@ import PortStream from 'extension-port-stream';
import { obj as createThoughStream } from 'through2';
import { EXTENSION_MESSAGES, MESSAGE_TYPE } from '../../shared/constants/app';
import {
checkForLastError,
checkForLastErrorAndWarn,
} from '../../shared/modules/browser-runtime.utils';
import { checkForLastError } from '../../shared/modules/browser-runtime.utils';
import { isManifestV3 } from '../../shared/modules/mv3.utils';
import shouldInjectProvider from '../../shared/modules/provider-injection';
@ -141,11 +138,7 @@ function setupPhishingPageStreams() {
});
if (isManifestV3) {
phishingPageStream.on('data', ({ data: { method } }) => {
if (!IGNORE_INIT_METHODS_FOR_KEEP_ALIVE.includes(method)) {
runWorkerKeepAliveInterval();
}
});
runWorkerKeepAliveInterval();
}
// create and connect channel muxers
@ -220,13 +213,25 @@ const destroyPhishingExtStreams = () => {
* so that streams may be re-established later the phishing extension port is reconnected.
*/
const onDisconnectDestroyPhishingStreams = () => {
checkForLastErrorAndWarn();
const err = checkForLastError();
phishingExtPort.onDisconnect.removeListener(
onDisconnectDestroyPhishingStreams,
);
destroyPhishingExtStreams();
/**
* If an error is found, reset the streams. When running two or more dapps, resetting the service
* worker may cause the error, "Error: Could not establish connection. Receiving end does not
* exist.", due to a race-condition. The disconnect event may be called by runtime.connect which
* may cause issues. We suspect that this is a chromium bug as this event should only be called
* once the port and connections are ready. Delay time is arbitrary.
*/
if (err) {
console.warn(`${err} Resetting the phishing streams.`);
setTimeout(setupPhishingExtStreams, 1000);
}
};
/**