mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-22 09:57:02 +01:00
feat: refactor background.js to support overrides (#17625)
This commit is contained in:
parent
c36438868d
commit
3814616c7a
@ -2,6 +2,7 @@
|
|||||||
* @file The entry point for the web extension singleton process.
|
* @file The entry point for the web extension singleton process.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import EventEmitter from 'events';
|
||||||
import endOfStream from 'end-of-stream';
|
import endOfStream from 'end-of-stream';
|
||||||
import pump from 'pump';
|
import pump from 'pump';
|
||||||
import debounce from 'debounce-stream';
|
import debounce from 'debounce-stream';
|
||||||
@ -96,6 +97,9 @@ const PHISHING_WARNING_PAGE_TIMEOUT = ONE_SECOND_IN_MILLISECONDS;
|
|||||||
const ACK_KEEP_ALIVE_MESSAGE = 'ACK_KEEP_ALIVE_MESSAGE';
|
const ACK_KEEP_ALIVE_MESSAGE = 'ACK_KEEP_ALIVE_MESSAGE';
|
||||||
const WORKER_KEEP_ALIVE_MESSAGE = 'WORKER_KEEP_ALIVE_MESSAGE';
|
const WORKER_KEEP_ALIVE_MESSAGE = 'WORKER_KEEP_ALIVE_MESSAGE';
|
||||||
|
|
||||||
|
// Event emitter for state persistence
|
||||||
|
export const statePersistenceEvents = new EventEmitter();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This deferred Promise is used to track whether initialization has finished.
|
* This deferred Promise is used to track whether initialization has finished.
|
||||||
*
|
*
|
||||||
@ -177,8 +181,6 @@ browser.runtime.onConnectExternal.addListener(async (...args) => {
|
|||||||
connectExternal(...args);
|
connectExternal(...args);
|
||||||
});
|
});
|
||||||
|
|
||||||
initialize().catch(log.error);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @typedef {import('../../shared/constants/transaction').TransactionMeta} TransactionMeta
|
* @typedef {import('../../shared/constants/transaction').TransactionMeta} TransactionMeta
|
||||||
*/
|
*/
|
||||||
@ -331,7 +333,7 @@ async function loadPhishingWarningPage() {
|
|||||||
*
|
*
|
||||||
* @returns {Promise<MetaMaskState>} Last data emitted from previous instance of MetaMask.
|
* @returns {Promise<MetaMaskState>} Last data emitted from previous instance of MetaMask.
|
||||||
*/
|
*/
|
||||||
async function loadStateFromPersistence() {
|
export async function loadStateFromPersistence() {
|
||||||
// migrations
|
// migrations
|
||||||
const migrator = new Migrator({ migrations });
|
const migrator = new Migrator({ migrations });
|
||||||
migrator.on('error', console.warn);
|
migrator.on('error', console.warn);
|
||||||
@ -384,8 +386,9 @@ async function loadStateFromPersistence() {
|
|||||||
*
|
*
|
||||||
* @param {object} initState - The initial state to start the controller with, matches the state that is emitted from the controller.
|
* @param {object} initState - The initial state to start the controller with, matches the state that is emitted from the controller.
|
||||||
* @param {string} initLangCode - The region code for the language preferred by the current user.
|
* @param {string} initLangCode - The region code for the language preferred by the current user.
|
||||||
|
* @param {object} overrides - object with callbacks that are allowed to override the setup controller logic (usefull for desktop app)
|
||||||
*/
|
*/
|
||||||
function setupController(initState, initLangCode) {
|
export function setupController(initState, initLangCode, overrides) {
|
||||||
//
|
//
|
||||||
// MetaMask Controller
|
// MetaMask Controller
|
||||||
//
|
//
|
||||||
@ -410,6 +413,7 @@ function setupController(initState, initLangCode) {
|
|||||||
return openMetamaskTabsIDs;
|
return openMetamaskTabsIDs;
|
||||||
},
|
},
|
||||||
localStore,
|
localStore,
|
||||||
|
overrides,
|
||||||
});
|
});
|
||||||
|
|
||||||
setupEnsIpfsResolver({
|
setupEnsIpfsResolver({
|
||||||
@ -426,7 +430,10 @@ function setupController(initState, initLangCode) {
|
|||||||
pump(
|
pump(
|
||||||
storeAsStream(controller.store),
|
storeAsStream(controller.store),
|
||||||
debounce(1000),
|
debounce(1000),
|
||||||
createStreamSink((state) => localStore.set(state)),
|
createStreamSink(async (state) => {
|
||||||
|
await localStore.set(state);
|
||||||
|
statePersistenceEvents.emit('state-persisted', state);
|
||||||
|
}),
|
||||||
(error) => {
|
(error) => {
|
||||||
log.error('MetaMask - Persistence pipeline failed', error);
|
log.error('MetaMask - Persistence pipeline failed', error);
|
||||||
},
|
},
|
||||||
@ -495,7 +502,8 @@ function setupController(initState, initLangCode) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isMetaMaskInternalProcess) {
|
if (isMetaMaskInternalProcess) {
|
||||||
const portStream = new PortStream(remotePort);
|
const portStream =
|
||||||
|
overrides?.getPortStream?.(remotePort) || new PortStream(remotePort);
|
||||||
// communication with popup
|
// communication with popup
|
||||||
controller.isClientOpen = true;
|
controller.isClientOpen = true;
|
||||||
controller.setupTrustedCommunication(portStream, remotePort.sender);
|
controller.setupTrustedCommunication(portStream, remotePort.sender);
|
||||||
@ -553,7 +561,8 @@ function setupController(initState, initLangCode) {
|
|||||||
senderUrl.origin === phishingPageUrl.origin &&
|
senderUrl.origin === phishingPageUrl.origin &&
|
||||||
senderUrl.pathname === phishingPageUrl.pathname
|
senderUrl.pathname === phishingPageUrl.pathname
|
||||||
) {
|
) {
|
||||||
const portStream = new PortStream(remotePort);
|
const portStream =
|
||||||
|
overrides?.getPortStream?.(remotePort) || new PortStream(remotePort);
|
||||||
controller.setupPhishingCommunication({
|
controller.setupPhishingCommunication({
|
||||||
connectionStream: portStream,
|
connectionStream: portStream,
|
||||||
});
|
});
|
||||||
@ -575,13 +584,18 @@ function setupController(initState, initLangCode) {
|
|||||||
|
|
||||||
// communication with page or other extension
|
// communication with page or other extension
|
||||||
connectExternal = (remotePort) => {
|
connectExternal = (remotePort) => {
|
||||||
const portStream = new PortStream(remotePort);
|
const portStream =
|
||||||
|
overrides?.getPortStream?.(remotePort) || new PortStream(remotePort);
|
||||||
controller.setupUntrustedCommunication({
|
controller.setupUntrustedCommunication({
|
||||||
connectionStream: portStream,
|
connectionStream: portStream,
|
||||||
sender: remotePort.sender,
|
sender: remotePort.sender,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (overrides?.registerConnectListeners) {
|
||||||
|
overrides.registerConnectListeners(connectRemote, connectExternal);
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// User Interface setup
|
// User Interface setup
|
||||||
//
|
//
|
||||||
@ -839,3 +853,11 @@ function setupSentryGetStateGlobal(store) {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function initBackground() {
|
||||||
|
initialize().catch(log.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!process.env.SKIP_BACKGROUND_INITIALIZATION) {
|
||||||
|
initBackground();
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user