Skip to content
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
e35c6ae
fix: use single PersistenceManager for hooks once background is ready
gauthierpetetin Feb 27, 2026
38e8ec1
Merge branch 'main' into fix/single-persistence-manager-instance
gauthierpetetin Mar 2, 2026
fe5f3c4
Merge branch 'main' into fix/single-persistence-manager-instance
gauthierpetetin Mar 2, 2026
d25c3aa
refactor: single PersistenceManager per context
gauthierpetetin Mar 2, 2026
2578d11
Merge branch 'main' into fix/single-persistence-manager-instance
gauthierpetetin Mar 2, 2026
0313461
Merge branch 'main' into fix/single-persistence-manager-instance
gauthierpetetin Mar 2, 2026
e044f2d
Revert unrelated navigator change in getSentryState
gauthierpetetin Mar 2, 2026
ef1544a
Use export instead of global for persistenceManager
gauthierpetetin Mar 2, 2026
f0c07da
Simplify isBackgroundContext for MV2 and MV3
gauthierpetetin Mar 2, 2026
c8673b5
Fix E2E: load setup-initial-state-hooks first so fixture starts early
gauthierpetetin Mar 2, 2026
33191ac
fix: simplify isBackgroundContext for E2E Chrome MV3 and Firefox MV2
gauthierpetetin Mar 2, 2026
378ffe3
refactor: use only globalThis.self for href in isBackgroundContext
gauthierpetetin Mar 2, 2026
2aa42bb
refactor: drop forced first import of setup-initial-state-hooks in ba…
gauthierpetetin Mar 2, 2026
bc79461
Merge branch 'main' into fix/single-persistence-manager-instance
gauthierpetetin Mar 2, 2026
0485959
fix: restore setup-initial-state-hooks as first import for Sentry
gauthierpetetin Mar 2, 2026
ea26cfb
fix: detect webpack MV3 service worker in isBackgroundContext
gauthierpetetin Mar 3, 2026
f389610
Merge branch 'main' into fix/single-persistence-manager-instance
gauthierpetetin Mar 3, 2026
8f6e95d
test: add unit tests for setup-initial-state-hooks
gauthierpetetin Mar 3, 2026
099bdbb
Merge branch 'main' into fix/single-persistence-manager-instance
gauthierpetetin Mar 3, 2026
e4c69ad
fix: lint errors in setup-initial-state-hooks tests
gauthierpetetin Mar 3, 2026
8a07355
test: add getSentryState branch coverage for setup-initial-state-hooks
gauthierpetetin Mar 3, 2026
08c1d57
Merge branch 'main' into fix/single-persistence-manager-instance
gauthierpetetin Mar 3, 2026
085ee4b
fix: lint errors in setup-initial-state-hooks tests
gauthierpetetin Mar 3, 2026
c56cd35
refactor: extract getEnvironmentType to dedicated module
gauthierpetetin Mar 4, 2026
4b5295e
Merge branch 'main' into fix/single-persistence-manager-instance
gauthierpetetin Mar 4, 2026
08a9a7b
fix: return EnvironmentType instead of string from getEnvironmentType
gauthierpetetin Mar 4, 2026
76c3be4
Duplicate getEnvironmentType in setup-initial-state-hooks to avoid La…
gauthierpetetin Mar 4, 2026
1c750a4
Merge branch 'main' into fix/single-persistence-manager-instance
gauthierpetetin Mar 4, 2026
313ab84
Revert util.ts to match main (eliminate diff)
gauthierpetetin Mar 4, 2026
ab98739
Align util.ts with origin/main (remove BN/hexToBn/BnMultiplyByFraction)
gauthierpetetin Mar 4, 2026
aa80a0d
Fix Webpack UI treating empty href as background (E2E Sentry test)
gauthierpetetin Mar 6, 2026
ad592e3
Merge branch 'main' into fix/single-persistence-manager-instance
gauthierpetetin Mar 6, 2026
e9b620e
refactor: move getEnvironmentTypeForHooks into setup-initial-state-hooks
gauthierpetetin Mar 6, 2026
656a270
Merge branch 'main' into fix/single-persistence-manager-instance
gauthierpetetin Mar 6, 2026
bed1714
DEBUG COMMIT TO BE REVERTED BEFORE PR GETS MERGED
gauthierpetetin Mar 16, 2026
a073850
Merge branch 'main' into fix/single-persistence-manager-instance
gauthierpetetin Mar 16, 2026
6e12cb5
Revert "DEBUG COMMIT TO BE REVERTED BEFORE PR GETS MERGED"
gauthierpetetin Mar 16, 2026
81d4159
refactor(setup-initial-state-hooks): guard location check to fixture …
gauthierpetetin Mar 16, 2026
4468792
refactor: introduce createLocalStore() for local store creation
gauthierpetetin Mar 17, 2026
1ec00b8
Merge branch 'main' into fix/single-persistence-manager-instance
gauthierpetetin Mar 17, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions app/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

// This import sets up global functions required for Sentry to function.
// It must be run first in case an error is thrown later during initialization.
import './lib/setup-initial-state-hooks';
// eslint-disable-next-line import/order -- intentional first import for Sentry
import { persistenceManager } from './lib/setup-initial-state-hooks';

// Import this very early, so globalThis.INFURA_PROJECT_ID_FROM_MANIFEST_FLAGS is always defined
import '../../shared/constants/infura-project-id';
Expand Down Expand Up @@ -57,12 +58,7 @@ import {
CorruptionHandler,
hasVault,
} from './lib/state-corruption/state-corruption-recovery';
import {
backedUpStateKeys,
PersistenceManager,
} from './lib/stores/persistence-manager';
import ExtensionStore from './lib/stores/extension-store';
import { FixtureExtensionStore } from './lib/stores/fixture-extension-store';
import { backedUpStateKeys } from './lib/stores/persistence-manager';
import { useSplitStateStorage } from './lib/use-split-state-storage';
import migrations from './migrations';
import Migrator from './lib/migrator';
Expand Down Expand Up @@ -120,12 +116,6 @@ const BADGE_COLOR_APPROVAL = '#0376C9';
const BADGE_MAX_COUNT = 9;

const inTest = process.env.IN_TEST;
const useFixtureStore =
inTest && getManifestFlags().testing?.forceExtensionStore !== true;
const localStore = useFixtureStore
? new FixtureExtensionStore({ initialize: true })
: new ExtensionStore();
const persistenceManager = new PersistenceManager({ localStore });

const { safePersist, requestSafeReload, evacuate } =
getRequestSafeReload(persistenceManager);
Expand Down
36 changes: 25 additions & 11 deletions app/scripts/lib/setup-initial-state-hooks.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ENVIRONMENT_TYPE_BACKGROUND } from '../../../shared/constants/app';
import { getEnvironmentType } from '../../../shared/lib/environment-type';
import { getManifestFlags } from '../../../shared/lib/manifestFlags';
import { maskObject } from '../../../shared/lib/object.utils';
import ExtensionPlatform from '../platforms/extension';
Expand All @@ -11,20 +13,32 @@ const platform = new ExtensionPlatform();
const useFixtureStore =
process.env.IN_TEST &&
getManifestFlags().testing?.forceExtensionStore !== true;
// This instance of `localStore` is used by Sentry to get the persisted state
const sentryLocalStore = new PersistenceManager({
localStore: useFixtureStore
? new FixtureExtensionStore()
: new ExtensionStore(),
});
let isBackground = false;
if (useFixtureStore) {
// Use globalThis.self (not window) so this works in both the UI and the background/service worker, where window is undefined.
const locationHref = globalThis.self?.location?.href;
if (!locationHref) {
throw new Error(
'setup-initial-state-hooks: globalThis.self?.location?.href is not defined; expected to run in a document or service worker context.',
);
}
isBackground =
getEnvironmentType(locationHref) === ENVIRONMENT_TYPE_BACKGROUND;
}
const localStore = useFixtureStore
? new FixtureExtensionStore({ initialize: isBackground })
: new ExtensionStore();

// Single PersistenceManager per context: one in background, one per UI context.
export const persistenceManager = new PersistenceManager({ localStore });

/**
* Get the persisted wallet state.
*
* @returns The persisted wallet state.
*/
globalThis.stateHooks.getPersistedState = async function () {
return await sentryLocalStore.get({ validateVault: false });
return await persistenceManager.get({ validateVault: false });
};

/**
Expand All @@ -34,7 +48,7 @@ globalThis.stateHooks.getPersistedState = async function () {
* @returns The backup state, or null if unavailable.
*/
globalThis.stateHooks.getBackupState = async function () {
return await sentryLocalStore.getBackup();
return await persistenceManager.getBackup();
};

const persistedStateMask = {
Expand Down Expand Up @@ -65,7 +79,7 @@ globalThis.stateHooks.getSentryState = function () {
};
// If `getSentryAppState` is set, it implies that initialization has completed
if (globalThis.stateHooks.getSentryAppState) {
sentryLocalStore.cleanUpMostRecentRetrievedState();
persistenceManager.cleanUpMostRecentRetrievedState();
return {
...sentryState,
state: globalThis.stateHooks.getSentryAppState(),
Expand All @@ -74,12 +88,12 @@ globalThis.stateHooks.getSentryState = function () {
// This is truthy if Sentry has retrieved state at least once already. This
// should always be true when getting context for an error report, but can
// be unset when Sentry is performing the opt-in check.
sentryLocalStore.mostRecentRetrievedState ||
persistenceManager.mostRecentRetrievedState ||
// This is only set in the background process.
globalThis.stateHooks.getMostRecentPersistedState
) {
const persistedState =
sentryLocalStore.mostRecentRetrievedState ||
persistenceManager.mostRecentRetrievedState ||
globalThis.stateHooks.getMostRecentPersistedState();
// This can be unset when this method is called in the background for an
// opt-in check, but the state hasn't been loaded yet.
Expand Down
Loading
Loading