1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-12 20:57:12 +01:00
metamask-extension/app/scripts/app-init.js

163 lines
4.7 KiB
JavaScript
Raw Normal View History

/* global chrome */
2022-07-20 09:40:31 +02:00
// This file is used only for manifest version 3
// Represents if importAllScripts has been run
// eslint-disable-next-line
let scriptsLoadInitiated = false;
2022-07-20 09:40:31 +02:00
const testMode = process.env.IN_TEST;
2022-07-20 09:40:31 +02:00
const loadTimeLogs = [];
2022-05-26 06:48:23 +02:00
// eslint-disable-next-line import/unambiguous
function tryImport(...fileNames) {
try {
2022-07-20 09:40:31 +02:00
const startTime = new Date().getTime();
2022-05-26 06:48:23 +02:00
// eslint-disable-next-line
importScripts(...fileNames);
2022-07-20 09:40:31 +02:00
const endTime = new Date().getTime();
loadTimeLogs.push({
name: fileNames[0],
value: endTime - startTime,
children: [],
startTime,
endTime,
});
2022-05-26 06:48:23 +02:00
return true;
} catch (e) {
console.error(e);
}
2022-07-20 09:40:31 +02:00
return false;
2022-05-26 06:48:23 +02:00
}
function importAllScripts() {
2022-07-20 09:40:31 +02:00
// Bail if we've already imported scripts
if (scriptsLoadInitiated) {
2022-07-20 09:40:31 +02:00
return;
}
scriptsLoadInitiated = true;
2022-07-20 09:40:31 +02:00
const files = [];
// In testMode individual files are imported, this is to help capture load time stats
const loadFile = (fileName) => {
if (testMode) {
tryImport(fileName);
} else {
files.push(fileName);
}
};
2022-05-26 06:48:23 +02:00
const startImportScriptsTime = Date.now();
// value of applyLavaMoat below is dynamically replaced at build time with actual value
const applyLavaMoat = process.env.APPLY_LAVAMOAT;
if (typeof applyLavaMoat !== 'boolean') {
throw new Error('Missing APPLY_LAVAMOAT environment variable');
}
2022-05-26 06:48:23 +02:00
2022-07-20 09:40:31 +02:00
loadFile('./globalthis.js');
loadFile('./sentry-install.js');
2022-05-26 06:48:23 +02:00
// Always apply LavaMoat in e2e test builds, so that we can capture initialization stats
if (testMode || applyLavaMoat) {
2022-07-20 09:40:31 +02:00
loadFile('./runtime-lavamoat.js');
loadFile('./lockdown-more.js');
loadFile('./policy-load.js');
2022-05-26 06:48:23 +02:00
} else {
2022-07-20 09:40:31 +02:00
loadFile('./init-globals.js');
loadFile('./lockdown-install.js');
loadFile('./lockdown-run.js');
loadFile('./lockdown-more.js');
loadFile('./runtime-cjs.js');
2022-05-26 06:48:23 +02:00
}
// This environment variable is set to a string of comma-separated relative file paths.
const rawFileList = process.env.FILE_NAMES;
const fileList = rawFileList.split(',');
2022-07-20 09:40:31 +02:00
fileList.forEach((fileName) => loadFile(fileName));
// Import all required resources
tryImport(...files);
const endImportScriptsTime = Date.now();
2022-05-26 06:48:23 +02:00
// for performance metrics/reference
console.log(
`SCRIPTS IMPORT COMPLETE in Seconds: ${
(Date.now() - startImportScriptsTime) / 1000
}`,
);
2022-07-20 09:40:31 +02:00
// In testMode load time logs are output to console
if (testMode) {
console.log(
`Time for each import: ${JSON.stringify(
{
name: 'Total',
children: loadTimeLogs,
startTime: startImportScriptsTime,
endTime: endImportScriptsTime,
value: endImportScriptsTime - startImportScriptsTime,
version: 1,
},
undefined,
' ',
)}`,
);
}
2022-05-26 06:48:23 +02:00
}
// Ref: https://stackoverflow.com/questions/66406672/chrome-extension-mv3-modularize-service-worker-js-file
2022-07-20 09:40:31 +02:00
// eslint-disable-next-line no-undef
self.addEventListener('install', importAllScripts);
/*
* A keepalive message listener to prevent Service Worker getting shut down due to inactivity.
* UI sends the message periodically, in a setInterval.
* Chrome will revive the service worker if it was shut down, whenever a new message is sent, but only if a listener was defined here.
*
2022-07-20 09:40:31 +02:00
* chrome below needs to be replaced by cross-browser object,
* but there is issue in importing webextension-polyfill into service worker.
* chrome does seems to work in at-least all chromium based browsers
*/
chrome.runtime.onMessage.addListener(() => {
importAllScripts();
return false;
});
Keep memstore contents after service worker restarts (#15913) * Add all controllers in memstore to store Add methods to controller to reset memstore Reset memstore when popup or tab is closed. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * When profile is loaded, set isFirstTime to true.. After resetting the controllers, set the flag to false. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Remove console.logs Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * For some reason programmatically computing the store is not working. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Proper check for browser.storage Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * do a list of rest methods instead of reset controllers. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Mock controller resetStates and localstore get/set Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Comments about TLC Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * bind this. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * use globalThis instead of locastore to store first time state. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Test to check that resetStates is not called a second time Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Set init state in GasFeeController and other controllers so that their state is persisted accross SW restarts Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Revert localstore changes Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * wrap the reset states changes in MV3 flag Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Remove localstore from metamask-controller Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Always reset state on MMController start in MV2. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Use relative path for import of isManifestV3 Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Fix unit test Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>
2022-11-22 17:56:26 +01:00
chrome.runtime.onStartup.addListener(() => {
globalThis.isFirstTimeProfileLoaded = true;
});
/*
* This content script is injected programmatically because
* MAIN world injection does not work properly via manifest
* https://bugs.chromium.org/p/chromium/issues/detail?id=634381
*/
const registerInPageContentScript = async () => {
try {
await chrome.scripting.registerContentScripts([
{
id: 'inpage',
matches: ['file://*/*', 'http://*/*', 'https://*/*'],
js: ['inpage.js'],
runAt: 'document_start',
world: 'MAIN',
},
]);
} catch (err) {
/**
* An error occurs when app-init.js is reloaded. Attempts to avoid the duplicate script error:
* 1. registeringContentScripts inside runtime.onInstalled - This caused a race condition
* in which the provider might not be loaded in time.
* 2. await chrome.scripting.getRegisteredContentScripts() to check for an existing
* inpage script before registering - The provider is not loaded on time.
*/
console.warn(`Dropped attempt to register inpage content script. ${err}`);
}
};
registerInPageContentScript();