bitwarden-estensione-browser/apps/browser/src/background/idle.background.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

74 lines
2.4 KiB
TypeScript
Raw Normal View History

2022-06-14 17:10:53 +02:00
import { NotificationsService } from "@bitwarden/common/abstractions/notifications.service";
import { VaultTimeoutService } from "@bitwarden/common/abstractions/vaultTimeout/vaultTimeout.service";
import { VaultTimeoutAction } from "@bitwarden/common/enums/vault-timeout-action.enum";
2021-09-17 15:44:27 +02:00
import { BrowserStateService } from "../platform/services/abstractions/browser-state.service";
2018-08-23 03:10:23 +02:00
const IdleInterval = 60 * 5; // 5 minutes
export default class IdleBackground {
private idle: any;
2018-08-23 03:10:23 +02:00
private idleTimer: number = null;
private idleState = "active";
constructor(
private vaultTimeoutService: VaultTimeoutService,
[PS-1854] Split services between background and visualizations (#4075) * Elevate Map <-> Record JSON helpers to Utils * Build Account from a StateService provided AccountDeserializer * Allow Manifest V2 usage of session sync Expands use of SessionSyncer to all Subject types. Correctly handles replay buffer for each type to ignore the flood of data upon subscription to each Subject type. * Create browser-synced Policy Service * Move BrowserFolderService * Libs account serialization improvements * Serialize Browser Accounts * Separate StateService in background/visualizations Visualizer state services share storages with background page, which nicely emulates mv3 synchronization through session/local storage. There should not be multithreading issues since all of these services are still running through a single thread, we just now have multiple places we are reading/writing data from. Smaller improvements * Rename browser's state service to BrowserStateService * Remove unused WithPrototype decorator :celebrate: * Removed conversion on withPrototypeForArrayMembers. It's reasonable to think that if the type is maintained, it doesn't need conversion. Eventually, we should be able to remove the withPrototypeForArrayMembers decorator as well, but that will require a bit more work on (de)serialization of the Accounts.data property. * Make Record <-> Map idempotent Should we get in a situation where we _think_ an object has been jsonified, but hasn't been, we need to correctly deal with the object received to create our target. * Check all requirements while duck typing * Name client services after the client * Use union type to limit initialize options * Fixup usages of `initializeAs` * Add OrganizationService to synced services Co-Authored-By: Daniel James Smith <djsmith85@users.noreply.github.com> * Add Settings service to synced services Co-Authored-By: Daniel James Smith <djsmith85@users.noreply.github.com> * Add missing BrowserStateService * Fix factories to use browser-specific service overides * Fix org-service registration in services.module * Revert "Add missing BrowserStateService" This reverts commit 81cf384e872718e86e84f9c54731e78a083e1ee3. * Fix session syncer tests * Fix synced item metadata tests * Early return null json objects * Prefer abstract service dependencies * Prefer minimal browser service overrides * [SG-632] - Change forwarded providers radio buttons list to dropdown (#4045) * SG-632 - Changed forwarded providers list of radio buttons to dropdown * SG-632 - Added role attributes to improve accessibility. * SG-632 - Added sorting to array and empty option * SG-632 - Fix styling to match standards. * rename cipehrs component to vault items component (#4081) * Update the version hash for the QA Web build artifact to follow SemVer syntax (#4102) * Remove extra call to toJSON() (#4101) Co-authored-by: Daniel James Smith <djsmith85@users.noreply.github.com> Co-authored-by: Daniel James Smith <djsmith@web.de> Co-authored-by: Carlos Gonçalves <carlosmaccam@gmail.com> Co-authored-by: Jake Fink <jfink@bitwarden.com> Co-authored-by: Joseph Flinn <58369717+joseph-flinn@users.noreply.github.com> Co-authored-by: Robyn MacCallum <robyntmaccallum@gmail.com>
2022-11-23 23:26:57 +01:00
private stateService: BrowserStateService,
2018-08-23 03:10:23 +02:00
private notificationsService: NotificationsService
) {
2019-06-29 05:33:53 +02:00
this.idle = chrome.idle || (browser != null ? browser.idle : null);
2021-12-21 15:43:35 +01:00
}
async init() {
if (!this.idle) {
2021-12-21 15:43:35 +01:00
return;
}
const idleHandler = (newState: string) => {
2018-08-23 03:10:23 +02:00
if (newState === "active") {
this.notificationsService.reconnectFromActivity();
2021-12-21 15:43:35 +01:00
} else {
2018-08-23 03:10:23 +02:00
this.notificationsService.disconnectFromInactivity();
2021-12-21 15:43:35 +01:00
}
};
if (this.idle.onStateChanged && this.idle.setDetectionInterval) {
this.idle.setDetectionInterval(IdleInterval);
2018-08-23 03:10:23 +02:00
this.idle.onStateChanged.addListener(idleHandler);
} else {
2018-08-23 03:10:23 +02:00
this.pollIdle(idleHandler);
2021-12-21 15:43:35 +01:00
}
2018-08-23 03:10:23 +02:00
if (this.idle.onStateChanged) {
this.idle.onStateChanged.addListener(async (newState: string) => {
if (newState === "locked") {
// If the screen is locked or the screensaver activates
[Account Switching] Base changes for account switching (#2250) * Pull in jslib * Create new state models * Create browser specific stateService * Remove registration deprecated services, register stateService * Replace usage of deprecated services (user, constants) * Add missing properties to BrowserGroupingsComponentState * Remove StorageService from initFactory * Clear the correct state * Add null check when restoring send-grouping state * add remember email * Initialize stateservice in services.module * Fix 'lock now' not working * Comment to remove setting defaults on install * Pull jslib * Remove setting defaults on install * Bump jslib * Pass the current userId to services when logging out * Bump jslib * Override vaultTimeout default on account addition * Pull latest jslib * Retrieve vaultTimeout from stateService * Record activity per Account * Add userId to logout and add fallback if not present * Register AccountFactory * Pass userId in messages * Base changes for account switching di fixes (#2280) * [bug] Null checks on Account init * [bug] Use same stateService instance for all operations We override the stateService in browser, but currently don't pull the background service into popup and allow jslib to create its own instance of the base StateService for jslib services. This causes a split in in memory state between the three isntances that results in many errors, namely locking not working. * [chore] Update jslib * Pull in jslib * Pull in jslib * Pull in latest jslib to multiple stateservice inits * Check vault states before executing processReload * Adjust iterator * Update native messaging to include the userId (#2290) * Re-Add UserVerificationService * Fix email not being remembered by base component * Improve readability of reloadProcess * Removed unneeded null check * Fix constructor dependency (stateService) * Added missing await * Simplify dependency registration * Fixed typos * Reverted back to simple loop * Use vaultTimeoutService to retrieve Timeout Co-authored-by: Addison Beck <abeck@bitwarden.com> Co-authored-by: Oscar Hinton <oscar@oscarhinton.com>
2022-01-27 22:22:51 +01:00
const timeout = await this.stateService.getVaultTimeout();
if (timeout === -2) {
// On System Lock vault timeout option
[Account Switching] Base changes for account switching (#2250) * Pull in jslib * Create new state models * Create browser specific stateService * Remove registration deprecated services, register stateService * Replace usage of deprecated services (user, constants) * Add missing properties to BrowserGroupingsComponentState * Remove StorageService from initFactory * Clear the correct state * Add null check when restoring send-grouping state * add remember email * Initialize stateservice in services.module * Fix 'lock now' not working * Comment to remove setting defaults on install * Pull jslib * Remove setting defaults on install * Bump jslib * Pass the current userId to services when logging out * Bump jslib * Override vaultTimeout default on account addition * Pull latest jslib * Retrieve vaultTimeout from stateService * Record activity per Account * Add userId to logout and add fallback if not present * Register AccountFactory * Pass userId in messages * Base changes for account switching di fixes (#2280) * [bug] Null checks on Account init * [bug] Use same stateService instance for all operations We override the stateService in browser, but currently don't pull the background service into popup and allow jslib to create its own instance of the base StateService for jslib services. This causes a split in in memory state between the three isntances that results in many errors, namely locking not working. * [chore] Update jslib * Pull in jslib * Pull in jslib * Pull in latest jslib to multiple stateservice inits * Check vault states before executing processReload * Adjust iterator * Update native messaging to include the userId (#2290) * Re-Add UserVerificationService * Fix email not being remembered by base component * Improve readability of reloadProcess * Removed unneeded null check * Fix constructor dependency (stateService) * Added missing await * Simplify dependency registration * Fixed typos * Reverted back to simple loop * Use vaultTimeoutService to retrieve Timeout Co-authored-by: Addison Beck <abeck@bitwarden.com> Co-authored-by: Oscar Hinton <oscar@oscarhinton.com>
2022-01-27 22:22:51 +01:00
const action = await this.stateService.getVaultTimeoutAction();
if (action === VaultTimeoutAction.LogOut) {
2018-08-23 03:10:23 +02:00
await this.vaultTimeoutService.logOut();
} else {
await this.vaultTimeoutService.lock();
2018-08-23 03:10:23 +02:00
}
}
}
2021-12-21 15:43:35 +01:00
});
}
2021-12-21 15:43:35 +01:00
}
2018-08-23 03:10:23 +02:00
private pollIdle(handler: (newState: string) => void) {
if (this.idleTimer != null) {
window.clearTimeout(this.idleTimer);
this.idleTimer = null;
}
this.idle.queryState(IdleInterval, (state: string) => {
if (state !== this.idleState) {
this.idleState = state;
handler(state);
2021-12-21 15:43:35 +01:00
}
2018-08-23 03:10:23 +02:00
this.idleTimer = window.setTimeout(() => this.pollIdle(handler), 5000);
2021-12-21 15:43:35 +01:00
});
}
}