-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
8021e8c
commit 9d4cf78
Showing
19 changed files
with
670 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* Copyright (C) 2024 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { modComponentRefFactory } from "@/testUtils/factories/modComponentFactories"; | ||
import { | ||
getState, | ||
setState, | ||
TEST_resetStateController, | ||
} from "@/contentScript/stateController/stateController"; | ||
import { MergeStrategies, StateNamespaces } from "@/platform/state/stateTypes"; | ||
import type { JSONSchema7Definition } from "json-schema"; | ||
import { | ||
deleteSynchronizedModVariablesForMod, | ||
deleteSynchronizedModVariablesForTab, | ||
} from "@/background/stateControllerListeners"; | ||
import { getThisFrame } from "webext-messenger"; | ||
import { registerModVariables } from "@/contentScript/stateController/modVariablePolicyController"; | ||
|
||
beforeEach(async () => { | ||
await TEST_resetStateController(); | ||
}); | ||
|
||
describe("stateControllerListeners", () => { | ||
it("deletes mod variables", async () => { | ||
const modComponentRef = modComponentRefFactory(); | ||
|
||
registerModVariables(modComponentRef.modId, { | ||
schema: { | ||
type: "object", | ||
properties: { | ||
sessionVariable: { | ||
type: "string", | ||
"x-sync-policy": "session", | ||
// Cast required because types don't support custom `x-` variables yet | ||
} as JSONSchema7Definition, | ||
tabVariable: { | ||
type: "string", | ||
"x-sync-policy": "tab", | ||
// Cast required because types don't support custom `x-` variables yet | ||
} as JSONSchema7Definition, | ||
}, | ||
}, | ||
}); | ||
|
||
await setState({ | ||
namespace: StateNamespaces.MOD, | ||
data: { | ||
sessionVariable: "sessionValue", | ||
tabVariable: "tabValue", | ||
}, | ||
mergeStrategy: MergeStrategies.DEEP, | ||
modComponentRef, | ||
}); | ||
|
||
let values = await browser.storage.session.get(); | ||
expect(Object.keys(values)).toHaveLength(2); | ||
|
||
// Delete unrelated mod variables | ||
await deleteSynchronizedModVariablesForMod(modComponentRefFactory().modId); | ||
values = await browser.storage.session.get(); | ||
expect(Object.keys(values)).toHaveLength(2); | ||
|
||
// Delete mod variables | ||
await deleteSynchronizedModVariablesForMod(modComponentRef.modId); | ||
values = await browser.storage.session.get(); | ||
expect(Object.keys(values)).toHaveLength(0); | ||
}); | ||
|
||
it("deletes tab variables", async () => { | ||
const modComponentRef = modComponentRefFactory(); | ||
|
||
registerModVariables(modComponentRef.modId, { | ||
schema: { | ||
type: "object", | ||
properties: { | ||
sessionVariable: { | ||
type: "string", | ||
"x-sync-policy": "session", | ||
// Cast required because types don't support custom `x-` variables yet | ||
} as JSONSchema7Definition, | ||
tabVariable: { | ||
type: "string", | ||
"x-sync-policy": "tab", | ||
// Cast required because types don't support custom `x-` variables yet | ||
} as JSONSchema7Definition, | ||
}, | ||
}, | ||
}); | ||
|
||
await setState({ | ||
namespace: StateNamespaces.MOD, | ||
data: { | ||
sessionVariable: "sessionValue", | ||
tabVariable: "tabValue", | ||
}, | ||
mergeStrategy: MergeStrategies.DEEP, | ||
modComponentRef, | ||
}); | ||
|
||
let values = await browser.storage.session.get(); | ||
expect(Object.keys(values)).toHaveLength(2); | ||
|
||
// Delete unrelated mod variables | ||
const { tabId } = await getThisFrame(); | ||
await deleteSynchronizedModVariablesForTab(tabId); | ||
|
||
// Smoke test for raw state | ||
values = await browser.storage.session.get(); | ||
expect(Object.keys(values)).toHaveLength(1); | ||
|
||
// State controller value check | ||
const state = await getState({ | ||
namespace: StateNamespaces.MOD, | ||
modComponentRef, | ||
}); | ||
|
||
expect(state).toStrictEqual({ | ||
sessionVariable: "sessionValue", | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright (C) 2024 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { tryParseSessionStorageKey } from "@/platform/state/stateHelpers"; | ||
import type { RegistryId } from "@/types/registryTypes"; | ||
|
||
/** | ||
* Delete synchronized mod variables for the given tab id to free up memory. | ||
*/ | ||
export async function deleteSynchronizedModVariablesForTab( | ||
tabId: number, | ||
): Promise<void> { | ||
const values = await browser.storage.session.get(); | ||
const matches = Object.keys(values).filter( | ||
(x) => tryParseSessionStorageKey(x)?.tabId === tabId, | ||
); | ||
if (matches.length > 0) { | ||
await browser.storage.session.remove(matches); | ||
} | ||
} | ||
|
||
/** | ||
* Delete synchronized mod variables for the given tab mod to free up memory. | ||
*/ | ||
export async function deleteSynchronizedModVariablesForMod( | ||
modId: RegistryId, | ||
): Promise<void> { | ||
const values = await browser.storage.session.get(); | ||
const matches = Object.keys(values).filter( | ||
(x) => tryParseSessionStorageKey(x)?.modId === modId, | ||
); | ||
if (matches.length > 0) { | ||
await browser.storage.session.remove(matches); | ||
} | ||
} | ||
|
||
export function initStateControllerListeners(): void { | ||
browser.tabs.onRemoved.addListener(deleteSynchronizedModVariablesForTab); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.