mirror of
https://github.com/bitwarden/browser
synced 2025-12-17 08:43:33 +00:00
* Register alarms and listen to them * Wire up alarms and actions Register actions(commands) which can be executed by an alarm Create methods in alarm-state to persists actions and execution times Flesh out AlarmListener to iterate over registered commands and check if they need to execute Simplify clearClipboard action as it only handles the action instead of also worrying if it should fire. Enable previously disabled clear-clipboard tests (#3532) Adjust clear-clipboard tests to new simpler execution Co-authored-by: Matt Gibson <mgibson@bitwarden.com> * Make linter happy * Revert accidentally commited with merging master * Add jsdoc per PR comment * Fixed types to simplify adding new alarm actions Create a new alarm action (i.e `clear-clipboard.ts`) Export a name for the alarm action (`clearClipboardAlarmName`) `alarm-state.ts` Import alarm action name Extend `alarmKeys` and `alarmState` `on-alarm-listener` Import alarm action method and alarm action name Add it to the switch case * Add comment to clearClipboard action Add comment to replace clearClipboard impl once clipboardApi's are accessible by service-workers https://bugs.chromium.org/p/chromium/issues/detail?id=1160302 Co-authored-by: Matt Gibson <mgibson@bitwarden.com>
67 lines
2.4 KiB
TypeScript
67 lines
2.4 KiB
TypeScript
import { BrowserApi } from "../browser/browserApi";
|
|
import { clearClipboardAlarmName } from "../clipboard";
|
|
|
|
export const alarmKeys = [clearClipboardAlarmName] as const;
|
|
export type AlarmKeys = typeof alarmKeys[number];
|
|
|
|
type AlarmState = { [T in AlarmKeys]: number | undefined };
|
|
|
|
const alarmState: AlarmState = {
|
|
clearClipboard: null,
|
|
//TODO once implemented vaultTimeout: null;
|
|
//TODO once implemented checkNotifications: null;
|
|
//TODO once implemented (if necessary) processReload: null;
|
|
};
|
|
|
|
/**
|
|
* Retrieves the set alarm time (planned execution) for a give an commandName {@link AlarmState}
|
|
* @param commandName A command that has been previously registered with {@link AlarmState}
|
|
* @returns {Promise<number>} null or Unix epoch timestamp when the alarm action is supposed to execute
|
|
* @example
|
|
* // getAlarmTime(clearClipboard)
|
|
*/
|
|
export async function getAlarmTime(commandName: AlarmKeys): Promise<number> {
|
|
let alarmTime: number;
|
|
if (BrowserApi.manifestVersion == 3) {
|
|
const fromSessionStore = await chrome.storage.session.get(commandName);
|
|
alarmTime = fromSessionStore[commandName];
|
|
} else {
|
|
alarmTime = alarmState[commandName];
|
|
}
|
|
|
|
return alarmTime;
|
|
}
|
|
|
|
/**
|
|
* Registers an action that should execute after the given time has passed
|
|
* @param commandName A command that has been previously registered with {@link AlarmState}
|
|
* @param delay_ms The number of ms from now in which the command should execute from
|
|
* @example
|
|
* // setAlarmTime(clearClipboard, 5000) register the clearClipboard action which will execute when at least 5 seconds from now have passed
|
|
*/
|
|
export async function setAlarmTime(commandName: AlarmKeys, delay_ms: number): Promise<void> {
|
|
if (!delay_ms || delay_ms === 0) {
|
|
await this.clearAlarmTime(commandName);
|
|
return;
|
|
}
|
|
|
|
const time = Date.now() + delay_ms;
|
|
await setAlarmTimeInternal(commandName, time);
|
|
}
|
|
|
|
/**
|
|
* Clears the time currently set for a given command
|
|
* @param commandName A command that has been previously registered with {@link AlarmState}
|
|
*/
|
|
export async function clearAlarmTime(commandName: AlarmKeys): Promise<void> {
|
|
await setAlarmTimeInternal(commandName, null);
|
|
}
|
|
|
|
async function setAlarmTimeInternal(commandName: AlarmKeys, time: number): Promise<void> {
|
|
if (BrowserApi.manifestVersion == 3) {
|
|
await chrome.storage.session.set({ [commandName]: time });
|
|
} else {
|
|
alarmState[commandName] = time;
|
|
}
|
|
}
|