2021-06-07 19:25:37 +02:00
|
|
|
import { CipherRepromptType } from 'jslib-common/enums/cipherRepromptType';
|
2021-09-17 15:44:27 +02:00
|
|
|
import { CipherType } from 'jslib-common/enums/cipherType';
|
2018-01-09 20:26:20 +01:00
|
|
|
|
2021-10-18 15:32:38 +02:00
|
|
|
import { CipherView } from 'jslib-common/models/view/cipherView';
|
|
|
|
|
2021-09-17 15:44:27 +02:00
|
|
|
import { ApiService } from 'jslib-common/services/api.service';
|
|
|
|
import { AppIdService } from 'jslib-common/services/appId.service';
|
|
|
|
import { AuditService } from 'jslib-common/services/audit.service';
|
|
|
|
import { AuthService } from 'jslib-common/services/auth.service';
|
|
|
|
import { CipherService } from 'jslib-common/services/cipher.service';
|
|
|
|
import { CollectionService } from 'jslib-common/services/collection.service';
|
2021-06-07 19:25:37 +02:00
|
|
|
import { ConsoleLogService } from 'jslib-common/services/consoleLog.service';
|
2021-09-17 15:44:27 +02:00
|
|
|
import { ConstantsService } from 'jslib-common/services/constants.service';
|
|
|
|
import { ContainerService } from 'jslib-common/services/container.service';
|
|
|
|
import { EnvironmentService } from 'jslib-common/services/environment.service';
|
2021-06-07 19:25:37 +02:00
|
|
|
import { EventService } from 'jslib-common/services/event.service';
|
|
|
|
import { ExportService } from 'jslib-common/services/export.service';
|
|
|
|
import { FileUploadService } from 'jslib-common/services/fileUpload.service';
|
2021-09-17 15:44:27 +02:00
|
|
|
import { FolderService } from 'jslib-common/services/folder.service';
|
2021-11-09 18:59:51 +01:00
|
|
|
import { KeyConnectorService } from 'jslib-common/services/keyConnector.service';
|
2021-06-07 19:25:37 +02:00
|
|
|
import { NotificationsService } from 'jslib-common/services/notifications.service';
|
2021-09-17 15:44:27 +02:00
|
|
|
import { PasswordGenerationService } from 'jslib-common/services/passwordGeneration.service';
|
2021-06-07 19:25:37 +02:00
|
|
|
import { PolicyService } from 'jslib-common/services/policy.service';
|
|
|
|
import { SearchService } from 'jslib-common/services/search.service';
|
|
|
|
import { SendService } from 'jslib-common/services/send.service';
|
2021-09-17 15:44:27 +02:00
|
|
|
import { SettingsService } from 'jslib-common/services/settings.service';
|
|
|
|
import { StateService } from 'jslib-common/services/state.service';
|
|
|
|
import { SyncService } from 'jslib-common/services/sync.service';
|
2021-06-07 19:25:37 +02:00
|
|
|
import { SystemService } from 'jslib-common/services/system.service';
|
2021-09-17 15:44:27 +02:00
|
|
|
import { TokenService } from 'jslib-common/services/token.service';
|
|
|
|
import { TotpService } from 'jslib-common/services/totp.service';
|
|
|
|
import { UserService } from 'jslib-common/services/user.service';
|
2021-11-16 11:50:58 +01:00
|
|
|
import { UserVerificationService } from 'jslib-common/services/userVerification.service';
|
2021-06-07 19:25:37 +02:00
|
|
|
import { WebCryptoFunctionService } from 'jslib-common/services/webCryptoFunction.service';
|
2018-01-09 20:26:20 +01:00
|
|
|
|
2021-09-17 15:44:27 +02:00
|
|
|
import { ApiService as ApiServiceAbstraction } from 'jslib-common/abstractions/api.service';
|
|
|
|
import { AppIdService as AppIdServiceAbstraction } from 'jslib-common/abstractions/appId.service';
|
|
|
|
import { AuditService as AuditServiceAbstraction } from 'jslib-common/abstractions/audit.service';
|
|
|
|
import { AuthService as AuthServiceAbstraction } from 'jslib-common/abstractions/auth.service';
|
|
|
|
import { CipherService as CipherServiceAbstraction } from 'jslib-common/abstractions/cipher.service';
|
|
|
|
import { CollectionService as CollectionServiceAbstraction } from 'jslib-common/abstractions/collection.service';
|
|
|
|
import { CryptoService as CryptoServiceAbstraction } from 'jslib-common/abstractions/crypto.service';
|
2021-06-07 19:25:37 +02:00
|
|
|
import { CryptoFunctionService as CryptoFunctionServiceAbstraction } from 'jslib-common/abstractions/cryptoFunction.service';
|
2021-09-17 15:44:27 +02:00
|
|
|
import { EnvironmentService as EnvironmentServiceAbstraction } from 'jslib-common/abstractions/environment.service';
|
2021-06-07 19:25:37 +02:00
|
|
|
import { EventService as EventServiceAbstraction } from 'jslib-common/abstractions/event.service';
|
|
|
|
import { ExportService as ExportServiceAbstraction } from 'jslib-common/abstractions/export.service';
|
|
|
|
import { FileUploadService as FileUploadServiceAbstraction } from 'jslib-common/abstractions/fileUpload.service';
|
2021-09-17 15:44:27 +02:00
|
|
|
import { FolderService as FolderServiceAbstraction } from 'jslib-common/abstractions/folder.service';
|
|
|
|
import { I18nService as I18nServiceAbstraction } from 'jslib-common/abstractions/i18n.service';
|
2021-11-09 18:59:51 +01:00
|
|
|
import { KeyConnectorService as KeyConnectorServiceAbstraction } from 'jslib-common/abstractions/keyConnector.service';
|
2021-09-17 15:44:27 +02:00
|
|
|
import { LogService as LogServiceAbstraction } from 'jslib-common/abstractions/log.service';
|
|
|
|
import { MessagingService as MessagingServiceAbstraction } from 'jslib-common/abstractions/messaging.service';
|
2021-06-07 19:25:37 +02:00
|
|
|
import { NotificationsService as NotificationsServiceAbstraction } from 'jslib-common/abstractions/notifications.service';
|
2021-09-17 15:44:27 +02:00
|
|
|
import { PasswordGenerationService as PasswordGenerationServiceAbstraction } from 'jslib-common/abstractions/passwordGeneration.service';
|
|
|
|
import { PlatformUtilsService as PlatformUtilsServiceAbstraction } from 'jslib-common/abstractions/platformUtils.service';
|
2021-06-07 19:25:37 +02:00
|
|
|
import { PolicyService as PolicyServiceAbstraction } from 'jslib-common/abstractions/policy.service';
|
|
|
|
import { SearchService as SearchServiceAbstraction } from 'jslib-common/abstractions/search.service';
|
|
|
|
import { SendService as SendServiceAbstraction } from 'jslib-common/abstractions/send.service';
|
2021-09-17 15:44:27 +02:00
|
|
|
import { SettingsService as SettingsServiceAbstraction } from 'jslib-common/abstractions/settings.service';
|
|
|
|
import { StateService as StateServiceAbstraction } from 'jslib-common/abstractions/state.service';
|
|
|
|
import { StorageService as StorageServiceAbstraction } from 'jslib-common/abstractions/storage.service';
|
|
|
|
import { SyncService as SyncServiceAbstraction } from 'jslib-common/abstractions/sync.service';
|
2021-06-07 19:25:37 +02:00
|
|
|
import { SystemService as SystemServiceAbstraction } from 'jslib-common/abstractions/system.service';
|
2021-09-17 15:44:27 +02:00
|
|
|
import { TokenService as TokenServiceAbstraction } from 'jslib-common/abstractions/token.service';
|
|
|
|
import { TotpService as TotpServiceAbstraction } from 'jslib-common/abstractions/totp.service';
|
|
|
|
import { UserService as UserServiceAbstraction } from 'jslib-common/abstractions/user.service';
|
2021-11-16 11:50:58 +01:00
|
|
|
import { UserVerificationService as UserVerificationServiceAbstraction } from 'jslib-common/abstractions/userVerification.service';
|
2021-09-17 15:44:27 +02:00
|
|
|
import { VaultTimeoutService as VaultTimeoutServiceAbstraction } from 'jslib-common/abstractions/vaultTimeout.service';
|
2021-11-16 11:50:58 +01:00
|
|
|
|
2021-05-03 20:56:38 +02:00
|
|
|
import { AutofillService as AutofillServiceAbstraction } from '../services/abstractions/autofill.service';
|
2017-12-05 23:04:30 +01:00
|
|
|
|
2018-01-12 17:09:30 +01:00
|
|
|
import { BrowserApi } from '../browser/browserApi';
|
2019-08-12 18:31:32 +02:00
|
|
|
import { SafariApp } from '../browser/safariApp';
|
2017-12-07 21:36:24 +01:00
|
|
|
|
2017-12-07 21:06:37 +01:00
|
|
|
import CommandsBackground from './commands.background';
|
2017-12-07 22:02:15 +01:00
|
|
|
import ContextMenusBackground from './contextMenus.background';
|
2018-01-04 23:04:26 +01:00
|
|
|
import IdleBackground from './idle.background';
|
2020-11-23 14:27:36 +01:00
|
|
|
import { NativeMessagingBackground } from './nativeMessaging.background';
|
2021-10-15 15:03:25 +02:00
|
|
|
import NotificationBackground from './notification.background';
|
2017-12-07 21:06:37 +01:00
|
|
|
import RuntimeBackground from './runtime.background';
|
|
|
|
import TabsBackground from './tabs.background';
|
2017-12-07 03:54:38 +01:00
|
|
|
import WebRequestBackground from './webRequest.background';
|
2017-12-07 21:06:37 +01:00
|
|
|
import WindowsBackground from './windows.background';
|
2017-12-07 03:54:38 +01:00
|
|
|
|
2021-02-10 16:40:15 +01:00
|
|
|
import { PopupUtilsService } from '../popup/services/popup-utils.service';
|
2017-12-05 23:04:30 +01:00
|
|
|
import AutofillService from '../services/autofill.service';
|
2021-06-22 22:11:29 +02:00
|
|
|
import { BrowserCryptoService } from '../services/browserCrypto.service';
|
2018-01-04 22:06:00 +01:00
|
|
|
import BrowserMessagingService from '../services/browserMessaging.service';
|
2018-01-05 22:38:50 +01:00
|
|
|
import BrowserPlatformUtilsService from '../services/browserPlatformUtils.service';
|
2018-01-04 22:15:06 +01:00
|
|
|
import BrowserStorageService from '../services/browserStorage.service';
|
2018-04-11 20:52:49 +02:00
|
|
|
import I18nService from '../services/i18n.service';
|
2021-02-20 11:10:33 +01:00
|
|
|
import VaultTimeoutService from '../services/vaultTimeout.service';
|
2018-01-04 16:51:08 +01:00
|
|
|
|
2017-12-05 23:04:30 +01:00
|
|
|
export default class MainBackground {
|
2018-01-09 20:26:20 +01:00
|
|
|
messagingService: MessagingServiceAbstraction;
|
|
|
|
storageService: StorageServiceAbstraction;
|
2018-01-12 06:06:53 +01:00
|
|
|
secureStorageService: StorageServiceAbstraction;
|
2018-04-11 20:52:49 +02:00
|
|
|
i18nService: I18nServiceAbstraction;
|
2018-01-09 20:26:20 +01:00
|
|
|
platformUtilsService: PlatformUtilsServiceAbstraction;
|
2017-12-05 23:04:30 +01:00
|
|
|
constantsService: ConstantsService;
|
2021-07-23 20:22:53 +02:00
|
|
|
logService: LogServiceAbstraction;
|
2018-01-09 20:26:20 +01:00
|
|
|
cryptoService: CryptoServiceAbstraction;
|
2020-09-18 22:03:08 +02:00
|
|
|
cryptoFunctionService: CryptoFunctionServiceAbstraction;
|
2018-01-09 22:20:15 +01:00
|
|
|
tokenService: TokenServiceAbstraction;
|
|
|
|
appIdService: AppIdServiceAbstraction;
|
|
|
|
apiService: ApiServiceAbstraction;
|
2018-01-10 02:20:45 +01:00
|
|
|
environmentService: EnvironmentServiceAbstraction;
|
2018-01-09 23:37:40 +01:00
|
|
|
userService: UserServiceAbstraction;
|
2018-01-10 04:47:53 +01:00
|
|
|
settingsService: SettingsServiceAbstraction;
|
2018-01-10 05:05:46 +01:00
|
|
|
cipherService: CipherServiceAbstraction;
|
2018-01-10 04:39:38 +01:00
|
|
|
folderService: FolderServiceAbstraction;
|
2018-01-10 05:12:14 +01:00
|
|
|
collectionService: CollectionServiceAbstraction;
|
2020-04-06 17:40:16 +02:00
|
|
|
vaultTimeoutService: VaultTimeoutServiceAbstraction;
|
2018-01-10 05:27:15 +01:00
|
|
|
syncService: SyncServiceAbstraction;
|
2018-01-09 23:55:28 +01:00
|
|
|
passwordGenerationService: PasswordGenerationServiceAbstraction;
|
2018-01-09 23:45:17 +01:00
|
|
|
totpService: TotpServiceAbstraction;
|
2018-01-11 20:45:27 +01:00
|
|
|
autofillService: AutofillServiceAbstraction;
|
2018-01-07 06:15:12 +01:00
|
|
|
containerService: ContainerService;
|
2018-03-21 14:38:03 +01:00
|
|
|
auditService: AuditServiceAbstraction;
|
2020-08-11 22:25:07 +02:00
|
|
|
authService: AuthServiceAbstraction;
|
2018-05-17 16:57:52 +02:00
|
|
|
exportService: ExportServiceAbstraction;
|
2018-08-13 17:53:16 +02:00
|
|
|
searchService: SearchServiceAbstraction;
|
2018-08-20 23:40:39 +02:00
|
|
|
notificationsService: NotificationsServiceAbstraction;
|
2020-08-11 22:25:07 +02:00
|
|
|
stateService: StateServiceAbstraction;
|
2019-02-27 15:28:16 +01:00
|
|
|
systemService: SystemServiceAbstraction;
|
2019-07-09 19:56:13 +02:00
|
|
|
eventService: EventServiceAbstraction;
|
2020-01-29 04:35:40 +01:00
|
|
|
policyService: PolicyServiceAbstraction;
|
2020-08-14 19:48:50 +02:00
|
|
|
popupUtilsService: PopupUtilsService;
|
2020-11-18 22:54:29 +01:00
|
|
|
sendService: SendServiceAbstraction;
|
2021-04-07 20:43:07 +02:00
|
|
|
fileUploadService: FileUploadServiceAbstraction;
|
2021-11-09 18:59:51 +01:00
|
|
|
keyConnectorService: KeyConnectorServiceAbstraction;
|
2021-11-16 11:50:58 +01:00
|
|
|
userVerificationService: UserVerificationServiceAbstraction;
|
2017-12-05 23:04:30 +01:00
|
|
|
|
2017-12-07 21:06:37 +01:00
|
|
|
onUpdatedRan: boolean;
|
|
|
|
onReplacedRan: boolean;
|
2021-10-18 15:32:38 +02:00
|
|
|
loginToAutoFill: CipherView = null;
|
2017-12-07 21:06:37 +01:00
|
|
|
|
|
|
|
private commandsBackground: CommandsBackground;
|
2017-12-07 22:02:15 +01:00
|
|
|
private contextMenusBackground: ContextMenusBackground;
|
2018-01-04 23:04:26 +01:00
|
|
|
private idleBackground: IdleBackground;
|
2021-10-18 16:41:42 +02:00
|
|
|
private notificationBackground: NotificationBackground;
|
2017-12-07 21:06:37 +01:00
|
|
|
private runtimeBackground: RuntimeBackground;
|
|
|
|
private tabsBackground: TabsBackground;
|
2017-12-07 03:54:38 +01:00
|
|
|
private webRequestBackground: WebRequestBackground;
|
2017-12-07 21:06:37 +01:00
|
|
|
private windowsBackground: WindowsBackground;
|
2017-12-07 03:54:38 +01:00
|
|
|
|
2017-12-05 23:04:30 +01:00
|
|
|
private sidebarAction: any;
|
|
|
|
private buildingContextMenu: boolean;
|
|
|
|
private menuOptionsLoaded: any[] = [];
|
2018-04-06 17:48:45 +02:00
|
|
|
private syncTimeout: any;
|
2018-01-12 04:36:22 +01:00
|
|
|
private isSafari: boolean;
|
2020-10-11 20:45:25 +02:00
|
|
|
private nativeMessagingBackground: NativeMessagingBackground;
|
2017-12-05 23:04:30 +01:00
|
|
|
|
2017-12-06 19:51:49 +01:00
|
|
|
constructor() {
|
2017-12-05 23:04:30 +01:00
|
|
|
// Services
|
2018-04-10 20:20:03 +02:00
|
|
|
this.messagingService = new BrowserMessagingService();
|
2021-10-04 22:30:31 +02:00
|
|
|
this.storageService = new BrowserStorageService();
|
|
|
|
this.platformUtilsService = new BrowserPlatformUtilsService(this.messagingService, this.storageService,
|
2019-02-27 15:28:16 +01:00
|
|
|
(clipboardValue, clearMs) => {
|
|
|
|
if (this.systemService != null) {
|
|
|
|
this.systemService.clearClipboard(clipboardValue, clearMs);
|
|
|
|
}
|
2020-10-12 18:01:34 +02:00
|
|
|
},
|
2020-11-30 13:41:08 +01:00
|
|
|
async () => {
|
2020-10-12 18:01:34 +02:00
|
|
|
if (this.nativeMessagingBackground != null) {
|
2020-10-21 17:18:04 +02:00
|
|
|
const promise = this.nativeMessagingBackground.getResponse();
|
2020-12-09 18:23:44 +01:00
|
|
|
|
2020-11-30 13:41:08 +01:00
|
|
|
try {
|
2020-12-09 21:11:25 +01:00
|
|
|
await this.nativeMessagingBackground.send({ command: 'biometricUnlock' });
|
2020-11-30 13:41:08 +01:00
|
|
|
} catch (e) {
|
|
|
|
return Promise.reject(e);
|
|
|
|
}
|
|
|
|
|
2021-02-10 16:40:15 +01:00
|
|
|
return promise.then(result => result.response === 'unlocked');
|
2020-10-12 18:01:34 +02:00
|
|
|
}
|
|
|
|
});
|
2021-02-03 20:36:05 +01:00
|
|
|
this.secureStorageService = new BrowserStorageService();
|
2019-08-14 22:54:40 +02:00
|
|
|
this.i18nService = new I18nService(BrowserApi.getUILanguage(window));
|
2020-09-18 22:03:08 +02:00
|
|
|
this.cryptoFunctionService = new WebCryptoFunctionService(window, this.platformUtilsService);
|
2021-07-23 20:22:53 +02:00
|
|
|
this.logService = new ConsoleLogService(false);
|
2021-06-22 22:11:29 +02:00
|
|
|
this.cryptoService = new BrowserCryptoService(this.storageService, this.secureStorageService,
|
2021-07-23 20:22:53 +02:00
|
|
|
this.cryptoFunctionService, this.platformUtilsService, this.logService);
|
2018-01-04 16:51:08 +01:00
|
|
|
this.tokenService = new TokenService(this.storageService);
|
|
|
|
this.appIdService = new AppIdService(this.storageService);
|
2021-07-23 22:32:42 +02:00
|
|
|
this.environmentService = new EnvironmentService(this.storageService);
|
|
|
|
this.apiService = new ApiService(this.tokenService, this.platformUtilsService, this.environmentService,
|
2018-08-28 14:38:29 +02:00
|
|
|
(expired: boolean) => this.logout(expired));
|
2018-01-04 16:51:08 +01:00
|
|
|
this.userService = new UserService(this.tokenService, this.storageService);
|
|
|
|
this.settingsService = new SettingsService(this.userService, this.storageService);
|
2021-07-23 20:22:53 +02:00
|
|
|
this.fileUploadService = new FileUploadService(this.logService, this.apiService);
|
2017-12-06 05:28:31 +01:00
|
|
|
this.cipherService = new CipherService(this.cryptoService, this.userService, this.settingsService,
|
2021-10-21 11:10:46 +02:00
|
|
|
this.apiService, this.fileUploadService, this.storageService, this.i18nService, () => this.searchService,
|
|
|
|
this.logService);
|
2018-06-25 20:56:29 +02:00
|
|
|
this.folderService = new FolderService(this.cryptoService, this.userService, this.apiService,
|
|
|
|
this.storageService, this.i18nService, this.cipherService);
|
2018-01-27 04:38:54 +01:00
|
|
|
this.collectionService = new CollectionService(this.cryptoService, this.userService, this.storageService,
|
2018-04-11 20:52:49 +02:00
|
|
|
this.i18nService);
|
2021-07-23 20:22:53 +02:00
|
|
|
this.searchService = new SearchService(this.cipherService, this.logService, this.i18nService);
|
2021-03-29 17:16:31 +02:00
|
|
|
this.sendService = new SendService(this.cryptoService, this.userService, this.apiService, this.fileUploadService,
|
|
|
|
this.storageService, this.i18nService, this.cryptoFunctionService);
|
2020-08-11 22:25:07 +02:00
|
|
|
this.stateService = new StateService();
|
2021-10-21 11:10:46 +02:00
|
|
|
this.policyService = new PolicyService(this.userService, this.storageService, this.apiService);
|
2021-11-09 18:59:51 +01:00
|
|
|
this.keyConnectorService = new KeyConnectorService(this.storageService, this.userService, this.cryptoService,
|
2021-11-16 11:50:58 +01:00
|
|
|
this.apiService, this.tokenService, this.logService);
|
2020-04-06 17:40:16 +02:00
|
|
|
this.vaultTimeoutService = new VaultTimeoutService(this.cipherService, this.folderService,
|
|
|
|
this.collectionService, this.cryptoService, this.platformUtilsService, this.storageService,
|
2021-09-14 13:36:34 +02:00
|
|
|
this.messagingService, this.searchService, this.userService, this.tokenService, this.policyService,
|
2021-11-09 18:59:51 +01:00
|
|
|
this.keyConnectorService,
|
2020-04-06 19:44:28 +02:00
|
|
|
async () => {
|
2018-08-23 03:47:30 +02:00
|
|
|
if (this.notificationsService != null) {
|
2018-08-23 15:26:07 +02:00
|
|
|
this.notificationsService.updateConnection(false);
|
2018-08-23 03:47:30 +02:00
|
|
|
}
|
2018-03-04 04:48:38 +01:00
|
|
|
await this.setIcon();
|
|
|
|
await this.refreshBadgeAndMenu(true);
|
2019-02-27 15:28:16 +01:00
|
|
|
if (this.systemService != null) {
|
|
|
|
this.systemService.startProcessReload();
|
2019-02-27 17:57:29 +01:00
|
|
|
await this.systemService.clearPendingClipboard();
|
2019-02-27 15:28:16 +01:00
|
|
|
}
|
2020-04-06 17:40:16 +02:00
|
|
|
}, async () => await this.logout(false));
|
2017-12-05 23:04:30 +01:00
|
|
|
this.syncService = new SyncService(this.userService, this.apiService, this.settingsService,
|
2017-12-07 04:28:33 +01:00
|
|
|
this.folderService, this.cipherService, this.cryptoService, this.collectionService,
|
2020-11-18 22:54:29 +01:00
|
|
|
this.storageService, this.messagingService, this.policyService, this.sendService,
|
2021-11-09 18:59:51 +01:00
|
|
|
this.logService, this.tokenService, this.keyConnectorService,
|
|
|
|
async (expired: boolean) => await this.logout(expired));
|
2019-07-12 20:54:17 +02:00
|
|
|
this.eventService = new EventService(this.storageService, this.apiService, this.userService,
|
2021-10-21 11:10:46 +02:00
|
|
|
this.cipherService, this.logService);
|
2020-02-28 18:43:27 +01:00
|
|
|
this.passwordGenerationService = new PasswordGenerationService(this.cryptoService, this.storageService,
|
|
|
|
this.policyService);
|
2021-10-21 11:10:46 +02:00
|
|
|
this.totpService = new TotpService(this.storageService, this.cryptoFunctionService, this.logService);
|
2019-07-12 20:54:17 +02:00
|
|
|
this.autofillService = new AutofillService(this.cipherService, this.userService, this.totpService,
|
2021-10-21 11:10:46 +02:00
|
|
|
this.eventService, this.logService);
|
2018-10-14 04:52:49 +02:00
|
|
|
this.containerService = new ContainerService(this.cryptoService);
|
2020-09-18 22:03:08 +02:00
|
|
|
this.auditService = new AuditService(this.cryptoFunctionService, this.apiService);
|
2021-05-13 02:51:07 +02:00
|
|
|
this.exportService = new ExportService(this.folderService, this.cipherService, this.apiService,
|
|
|
|
this.cryptoService);
|
2018-08-23 03:47:30 +02:00
|
|
|
this.notificationsService = new NotificationsService(this.userService, this.syncService, this.appIdService,
|
2021-07-23 22:32:42 +02:00
|
|
|
this.apiService, this.vaultTimeoutService, this.environmentService, () => this.logout(true), this.logService);
|
2020-08-14 19:48:50 +02:00
|
|
|
this.popupUtilsService = new PopupUtilsService(this.platformUtilsService);
|
2020-04-06 17:40:16 +02:00
|
|
|
this.systemService = new SystemService(this.storageService, this.vaultTimeoutService,
|
2019-02-27 15:28:16 +01:00
|
|
|
this.messagingService, this.platformUtilsService, () => {
|
2019-04-04 17:50:49 +02:00
|
|
|
const forceWindowReload = this.platformUtilsService.isSafari() ||
|
|
|
|
this.platformUtilsService.isFirefox() || this.platformUtilsService.isOpera();
|
|
|
|
BrowserApi.reloadExtension(forceWindowReload ? window : null);
|
2019-02-27 15:28:16 +01:00
|
|
|
return Promise.resolve();
|
|
|
|
});
|
2021-11-16 11:50:58 +01:00
|
|
|
this.userVerificationService = new UserVerificationService(this.cryptoService, this.i18nService,
|
|
|
|
this.apiService);
|
2017-12-05 23:04:30 +01:00
|
|
|
|
|
|
|
// Other fields
|
2018-01-12 04:36:22 +01:00
|
|
|
this.isSafari = this.platformUtilsService.isSafari();
|
|
|
|
this.sidebarAction = this.isSafari ? null : (typeof opr !== 'undefined') && opr.sidebarAction ?
|
2017-12-05 23:18:20 +01:00
|
|
|
opr.sidebarAction : (window as any).chrome.sidebarAction;
|
2017-12-07 03:54:38 +01:00
|
|
|
|
|
|
|
// Background
|
2021-10-15 15:03:25 +02:00
|
|
|
this.runtimeBackground = new RuntimeBackground(this, this.autofillService,
|
2018-04-10 20:20:03 +02:00
|
|
|
this.platformUtilsService as BrowserPlatformUtilsService, this.storageService, this.i18nService,
|
2021-10-21 11:10:46 +02:00
|
|
|
this.notificationsService, this.systemService, this.environmentService, this.messagingService,
|
|
|
|
this.logService);
|
2020-10-19 16:50:25 +02:00
|
|
|
this.nativeMessagingBackground = new NativeMessagingBackground(this.storageService, this.cryptoService, this.cryptoFunctionService,
|
2021-04-07 17:39:59 +02:00
|
|
|
this.vaultTimeoutService, this.runtimeBackground, this.i18nService, this.userService, this.messagingService, this.appIdService,
|
|
|
|
this.platformUtilsService);
|
2018-01-17 15:12:16 +01:00
|
|
|
this.commandsBackground = new CommandsBackground(this, this.passwordGenerationService,
|
2021-04-14 23:43:09 +02:00
|
|
|
this.platformUtilsService, this.vaultTimeoutService);
|
2021-10-15 15:03:25 +02:00
|
|
|
this.notificationBackground = new NotificationBackground(this, this.autofillService, this.cipherService,
|
2021-11-16 05:14:48 +01:00
|
|
|
this.storageService, this.vaultTimeoutService, this.policyService, this.folderService, this.userService);
|
2018-01-12 18:24:34 +01:00
|
|
|
|
2021-10-15 15:09:13 +02:00
|
|
|
this.tabsBackground = new TabsBackground(this, this.notificationBackground);
|
2021-04-14 23:43:09 +02:00
|
|
|
this.contextMenusBackground = new ContextMenusBackground(this, this.cipherService, this.passwordGenerationService,
|
|
|
|
this.platformUtilsService, this.vaultTimeoutService, this.eventService, this.totpService);
|
2021-02-03 20:36:05 +01:00
|
|
|
this.idleBackground = new IdleBackground(this.vaultTimeoutService, this.storageService,
|
|
|
|
this.notificationsService);
|
|
|
|
this.webRequestBackground = new WebRequestBackground(this.platformUtilsService, this.cipherService,
|
|
|
|
this.vaultTimeoutService);
|
|
|
|
this.windowsBackground = new WindowsBackground(this);
|
2021-03-17 22:14:26 +01:00
|
|
|
|
|
|
|
const that = this;
|
|
|
|
this.authService = new AuthService(this.cryptoService, this.apiService, this.userService,
|
|
|
|
this.tokenService, this.appIdService, this.i18nService, this.platformUtilsService,
|
|
|
|
new class extends MessagingServiceAbstraction {
|
|
|
|
// AuthService should send the messages to the background not popup.
|
|
|
|
send = (subscriber: string, arg: any = {}) => {
|
|
|
|
const message = Object.assign({}, { command: subscriber }, arg);
|
|
|
|
that.runtimeBackground.processMessage(message, that, null);
|
|
|
|
}
|
2021-11-09 18:59:51 +01:00
|
|
|
}(), this.vaultTimeoutService, this.logService, this.cryptoFunctionService, this.environmentService,
|
|
|
|
this.keyConnectorService);
|
2017-12-05 23:04:30 +01:00
|
|
|
}
|
|
|
|
|
2017-12-06 05:37:32 +01:00
|
|
|
async bootstrap() {
|
2018-01-07 06:15:12 +01:00
|
|
|
this.containerService.attachToWindow(window);
|
|
|
|
|
2021-03-17 22:14:26 +01:00
|
|
|
(this.authService as AuthService).init();
|
2020-04-06 17:40:16 +02:00
|
|
|
await (this.vaultTimeoutService as VaultTimeoutService).init(true);
|
2018-04-11 21:59:39 +02:00
|
|
|
await (this.i18nService as I18nService).init();
|
2019-07-09 19:56:13 +02:00
|
|
|
await (this.eventService as EventService).init(true);
|
2018-01-12 18:22:55 +01:00
|
|
|
await this.runtimeBackground.init();
|
2021-10-15 15:03:25 +02:00
|
|
|
await this.notificationBackground.init();
|
2018-01-17 15:12:16 +01:00
|
|
|
await this.commandsBackground.init();
|
2018-01-13 21:09:05 +01:00
|
|
|
|
2021-02-03 20:36:05 +01:00
|
|
|
await this.tabsBackground.init();
|
|
|
|
await this.contextMenusBackground.init();
|
|
|
|
await this.idleBackground.init();
|
|
|
|
await this.webRequestBackground.init();
|
|
|
|
await this.windowsBackground.init();
|
2017-12-07 03:54:38 +01:00
|
|
|
|
2021-03-29 17:16:31 +02:00
|
|
|
return new Promise<void>(resolve => {
|
2018-01-19 17:52:05 +01:00
|
|
|
setTimeout(async () => {
|
|
|
|
await this.environmentService.setUrlsFromStorage();
|
|
|
|
await this.setIcon();
|
2018-08-20 23:40:39 +02:00
|
|
|
this.fullSync(true);
|
2021-07-23 22:32:42 +02:00
|
|
|
setTimeout(() => this.notificationsService.init(), 2500);
|
2018-01-19 17:52:05 +01:00
|
|
|
resolve();
|
|
|
|
}, 500);
|
|
|
|
});
|
2017-12-06 05:28:31 +01:00
|
|
|
}
|
|
|
|
|
2017-12-07 22:02:15 +01:00
|
|
|
async setIcon() {
|
2021-02-03 20:36:05 +01:00
|
|
|
if (!chrome.browserAction && !this.sidebarAction) {
|
2017-12-07 22:02:15 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const isAuthenticated = await this.userService.isAuthenticated();
|
2020-04-06 17:40:16 +02:00
|
|
|
const locked = await this.vaultTimeoutService.isLocked();
|
2017-12-07 22:02:15 +01:00
|
|
|
|
|
|
|
let suffix = '';
|
|
|
|
if (!isAuthenticated) {
|
|
|
|
suffix = '_gray';
|
2019-02-14 06:46:28 +01:00
|
|
|
} else if (locked) {
|
2017-12-07 22:02:15 +01:00
|
|
|
suffix = '_locked';
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.actionSetIcon(chrome.browserAction, suffix);
|
|
|
|
await this.actionSetIcon(this.sidebarAction, suffix);
|
|
|
|
}
|
|
|
|
|
2018-03-04 04:48:38 +01:00
|
|
|
async refreshBadgeAndMenu(forLocked: boolean = false) {
|
2021-02-03 20:36:05 +01:00
|
|
|
if (!chrome.windows || !chrome.contextMenus) {
|
2017-12-07 22:02:15 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-04 04:48:38 +01:00
|
|
|
const menuDisabled = await this.storageService.get<boolean>(ConstantsService.disableContextMenuItemKey);
|
|
|
|
if (!menuDisabled) {
|
2017-12-07 22:02:15 +01:00
|
|
|
await this.buildContextMenu();
|
|
|
|
} else {
|
|
|
|
await this.contextMenusRemoveAll();
|
2018-03-04 04:48:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (forLocked) {
|
2020-04-08 02:19:47 +02:00
|
|
|
await this.loadMenuAndUpdateBadgeForNoAccessState(!menuDisabled);
|
2018-03-04 04:48:38 +01:00
|
|
|
this.onUpdatedRan = this.onReplacedRan = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const tab = await BrowserApi.getTabFromCurrentWindow();
|
|
|
|
if (tab) {
|
|
|
|
await this.contextMenuReady(tab, !menuDisabled);
|
2017-12-07 22:02:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async logout(expired: boolean) {
|
2019-07-09 19:56:13 +02:00
|
|
|
await this.eventService.uploadEvents();
|
2017-12-07 22:02:15 +01:00
|
|
|
const userId = await this.userService.getUserId();
|
|
|
|
|
|
|
|
await Promise.all([
|
2019-07-09 19:56:13 +02:00
|
|
|
this.eventService.clearEvents(),
|
2017-12-07 22:02:15 +01:00
|
|
|
this.syncService.setLastSync(new Date(0)),
|
|
|
|
this.tokenService.clearToken(),
|
|
|
|
this.cryptoService.clearKeys(),
|
|
|
|
this.userService.clear(),
|
|
|
|
this.settingsService.clear(userId),
|
|
|
|
this.cipherService.clear(userId),
|
|
|
|
this.folderService.clear(userId),
|
2018-05-16 05:27:06 +02:00
|
|
|
this.collectionService.clear(userId),
|
2020-01-29 04:35:40 +01:00
|
|
|
this.policyService.clear(userId),
|
2017-12-07 22:02:15 +01:00
|
|
|
this.passwordGenerationService.clear(),
|
2020-04-06 17:40:16 +02:00
|
|
|
this.vaultTimeoutService.clear(),
|
2021-11-09 18:59:51 +01:00
|
|
|
this.keyConnectorService.clear(),
|
2017-12-07 22:02:15 +01:00
|
|
|
]);
|
|
|
|
|
2018-08-13 20:09:26 +02:00
|
|
|
this.searchService.clearIndex();
|
2018-01-04 22:06:00 +01:00
|
|
|
this.messagingService.send('doneLoggingOut', { expired: expired });
|
2017-12-07 22:02:15 +01:00
|
|
|
|
|
|
|
await this.setIcon();
|
|
|
|
await this.refreshBadgeAndMenu();
|
2019-02-13 17:34:42 +01:00
|
|
|
await this.reseedStorage();
|
2018-08-23 15:26:07 +02:00
|
|
|
this.notificationsService.updateConnection(false);
|
2019-02-27 15:28:16 +01:00
|
|
|
this.systemService.startProcessReload();
|
2019-02-27 17:57:29 +01:00
|
|
|
await this.systemService.clearPendingClipboard();
|
2017-12-07 22:02:15 +01:00
|
|
|
}
|
|
|
|
|
2019-03-06 22:50:04 +01:00
|
|
|
async collectPageDetailsForContentScript(tab: any, sender: string, frameId: number = null) {
|
2017-12-07 22:02:15 +01:00
|
|
|
if (tab == null || !tab.id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const options: any = {};
|
|
|
|
if (frameId != null) {
|
|
|
|
options.frameId = frameId;
|
|
|
|
}
|
|
|
|
|
2018-01-12 20:44:44 +01:00
|
|
|
BrowserApi.tabSendMessage(tab, {
|
2017-12-07 22:02:15 +01:00
|
|
|
command: 'collectPageDetails',
|
|
|
|
tab: tab,
|
|
|
|
sender: sender,
|
2018-01-12 20:44:44 +01:00
|
|
|
}, options);
|
2017-12-07 22:02:15 +01:00
|
|
|
}
|
|
|
|
|
2018-01-18 22:17:58 +01:00
|
|
|
async openPopup() {
|
|
|
|
// Chrome APIs cannot open popup
|
2019-08-13 21:47:03 +02:00
|
|
|
|
|
|
|
// TODO: Do we need to open this popup?
|
2019-08-19 21:57:34 +02:00
|
|
|
if (!this.isSafari) {
|
|
|
|
return;
|
|
|
|
}
|
2019-09-21 15:50:57 +02:00
|
|
|
await SafariApp.sendMessageToApp('showPopover', null, true);
|
2018-01-18 22:17:58 +01:00
|
|
|
}
|
|
|
|
|
2019-02-13 17:34:42 +01:00
|
|
|
async reseedStorage() {
|
|
|
|
if (!this.platformUtilsService.isChrome() && !this.platformUtilsService.isVivaldi() &&
|
|
|
|
!this.platformUtilsService.isOpera()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-06 17:40:16 +02:00
|
|
|
const currentVaultTimeout = await this.storageService.get<number>(ConstantsService.vaultTimeoutKey);
|
|
|
|
if (currentVaultTimeout == null) {
|
2019-02-13 17:34:42 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-10 16:40:15 +01:00
|
|
|
const getStorage = (): Promise<any> => new Promise(resolve => {
|
2019-02-13 17:34:42 +01:00
|
|
|
chrome.storage.local.get(null, (o: any) => resolve(o));
|
|
|
|
});
|
|
|
|
|
2021-02-10 16:40:15 +01:00
|
|
|
const clearStorage = (): Promise<void> => new Promise(resolve => {
|
2019-02-13 17:34:42 +01:00
|
|
|
chrome.storage.local.clear(() => resolve());
|
|
|
|
});
|
|
|
|
|
|
|
|
const storage = await getStorage();
|
|
|
|
await clearStorage();
|
|
|
|
|
|
|
|
for (const key in storage) {
|
|
|
|
if (!storage.hasOwnProperty(key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
await this.storageService.save(key, storage[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-05 23:04:30 +01:00
|
|
|
private async buildContextMenu() {
|
2021-02-03 20:36:05 +01:00
|
|
|
if (!chrome.contextMenus || this.buildingContextMenu) {
|
2017-12-05 23:04:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.buildingContextMenu = true;
|
|
|
|
await this.contextMenusRemoveAll();
|
|
|
|
|
|
|
|
await this.contextMenusCreate({
|
|
|
|
type: 'normal',
|
|
|
|
id: 'root',
|
|
|
|
contexts: ['all'],
|
2018-02-27 19:37:57 +01:00
|
|
|
title: 'Bitwarden',
|
2017-12-05 23:04:30 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
await this.contextMenusCreate({
|
|
|
|
type: 'normal',
|
|
|
|
id: 'autofill',
|
|
|
|
parentId: 'root',
|
|
|
|
contexts: ['all'],
|
2018-04-11 20:52:49 +02:00
|
|
|
title: this.i18nService.t('autoFill'),
|
2017-12-05 23:04:30 +01:00
|
|
|
});
|
|
|
|
|
2020-09-15 16:50:45 +02:00
|
|
|
await this.contextMenusCreate({
|
|
|
|
type: 'normal',
|
|
|
|
id: 'copy-username',
|
|
|
|
parentId: 'root',
|
|
|
|
contexts: ['all'],
|
|
|
|
title: this.i18nService.t('copyUsername'),
|
|
|
|
});
|
2020-03-03 16:40:06 +01:00
|
|
|
|
2020-09-15 16:50:45 +02:00
|
|
|
await this.contextMenusCreate({
|
|
|
|
type: 'normal',
|
|
|
|
id: 'copy-password',
|
|
|
|
parentId: 'root',
|
|
|
|
contexts: ['all'],
|
|
|
|
title: this.i18nService.t('copyPassword'),
|
|
|
|
});
|
2017-12-05 23:04:30 +01:00
|
|
|
|
2020-09-15 16:50:45 +02:00
|
|
|
if (await this.userService.canAccessPremium()) {
|
2017-12-05 23:04:30 +01:00
|
|
|
await this.contextMenusCreate({
|
|
|
|
type: 'normal',
|
2020-09-15 16:50:45 +02:00
|
|
|
id: 'copy-totp',
|
2017-12-05 23:04:30 +01:00
|
|
|
parentId: 'root',
|
|
|
|
contexts: ['all'],
|
2020-09-15 16:50:45 +02:00
|
|
|
title: this.i18nService.t('copyVerificationCode'),
|
2017-12-05 23:04:30 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-15 16:50:45 +02:00
|
|
|
await this.contextMenusCreate({
|
|
|
|
type: 'separator',
|
|
|
|
parentId: 'root',
|
|
|
|
});
|
|
|
|
|
|
|
|
await this.contextMenusCreate({
|
|
|
|
type: 'normal',
|
|
|
|
id: 'generate-password',
|
|
|
|
parentId: 'root',
|
|
|
|
contexts: ['all'],
|
|
|
|
title: this.i18nService.t('generatePasswordCopied'),
|
|
|
|
});
|
|
|
|
|
2021-09-01 23:51:43 +02:00
|
|
|
await this.contextMenusCreate({
|
|
|
|
type: 'normal',
|
|
|
|
id: 'copy-identifier',
|
|
|
|
parentId: 'root',
|
|
|
|
contexts: ['all'],
|
|
|
|
title: this.i18nService.t('copyElementIdentifier'),
|
|
|
|
});
|
|
|
|
|
2017-12-05 23:04:30 +01:00
|
|
|
this.buildingContextMenu = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async contextMenuReady(tab: any, contextMenuEnabled: boolean) {
|
|
|
|
await this.loadMenuAndUpdateBadge(tab.url, tab.id, contextMenuEnabled);
|
|
|
|
this.onUpdatedRan = this.onReplacedRan = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async loadMenuAndUpdateBadge(url: string, tabId: number, contextMenuEnabled: boolean) {
|
|
|
|
if (!url || (!chrome.browserAction && !this.sidebarAction)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.actionSetBadgeBackgroundColor(chrome.browserAction);
|
|
|
|
this.actionSetBadgeBackgroundColor(this.sidebarAction);
|
|
|
|
|
|
|
|
this.menuOptionsLoaded = [];
|
2020-04-06 17:40:16 +02:00
|
|
|
const locked = await this.vaultTimeoutService.isLocked();
|
2019-03-06 22:50:04 +01:00
|
|
|
if (!locked) {
|
|
|
|
try {
|
|
|
|
const ciphers = await this.cipherService.getAllDecryptedForUrl(url);
|
|
|
|
ciphers.sort((a, b) => this.cipherService.sortCiphersByLastUsedThenName(a, b));
|
2017-12-05 23:04:30 +01:00
|
|
|
|
|
|
|
if (contextMenuEnabled) {
|
2021-02-10 16:40:15 +01:00
|
|
|
ciphers.forEach(cipher => {
|
2019-03-06 22:50:04 +01:00
|
|
|
this.loadLoginContextMenuOptions(cipher);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-06 17:18:11 +01:00
|
|
|
const disableBadgeCounter = await this.storageService.get<boolean>(ConstantsService.disableBadgeCounterKey);
|
2019-03-06 22:50:04 +01:00
|
|
|
let theText = '';
|
2017-12-05 23:04:30 +01:00
|
|
|
|
2021-02-21 23:35:14 +01:00
|
|
|
if (!disableBadgeCounter) {
|
2021-03-06 17:18:11 +01:00
|
|
|
if (ciphers.length > 0 && ciphers.length <= 9) {
|
|
|
|
theText = ciphers.length.toString();
|
|
|
|
} else if (ciphers.length > 0) {
|
|
|
|
theText = '9+';
|
2019-03-06 22:50:04 +01:00
|
|
|
}
|
2017-12-05 23:04:30 +01:00
|
|
|
}
|
|
|
|
|
2021-03-09 18:02:57 +01:00
|
|
|
if (contextMenuEnabled && ciphers.length === 0) {
|
|
|
|
await this.loadNoLoginsContextMenuOptions(this.i18nService.t('noMatchingLogins'));
|
|
|
|
}
|
|
|
|
|
2019-03-06 22:50:04 +01:00
|
|
|
this.sidebarActionSetBadgeText(theText, tabId);
|
2021-03-06 17:18:11 +01:00
|
|
|
this.browserActionSetBadgeText(theText, tabId);
|
|
|
|
|
2019-03-06 22:50:04 +01:00
|
|
|
return;
|
2021-10-21 11:10:46 +02:00
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(e);
|
|
|
|
}
|
2018-03-04 04:48:38 +01:00
|
|
|
}
|
2019-03-06 22:50:04 +01:00
|
|
|
|
2020-04-08 02:19:47 +02:00
|
|
|
await this.loadMenuAndUpdateBadgeForNoAccessState(contextMenuEnabled);
|
2018-03-04 04:48:38 +01:00
|
|
|
}
|
|
|
|
|
2020-04-08 02:19:47 +02:00
|
|
|
private async loadMenuAndUpdateBadgeForNoAccessState(contextMenuEnabled: boolean) {
|
2018-03-04 04:48:38 +01:00
|
|
|
if (contextMenuEnabled) {
|
2020-04-08 02:19:47 +02:00
|
|
|
const authed = await this.userService.isAuthenticated();
|
|
|
|
await this.loadNoLoginsContextMenuOptions(this.i18nService.t(authed ? 'vaultLocked' : 'vaultLoggedOut'));
|
2018-03-04 04:48:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const tabs = await BrowserApi.getActiveTabs();
|
|
|
|
if (tabs != null) {
|
2021-02-10 16:40:15 +01:00
|
|
|
tabs.forEach(tab => {
|
2018-03-04 04:48:38 +01:00
|
|
|
if (tab.id != null) {
|
|
|
|
this.browserActionSetBadgeText('', tab.id);
|
|
|
|
this.sidebarActionSetBadgeText('', tab.id);
|
|
|
|
}
|
|
|
|
});
|
2017-12-05 23:04:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async loadLoginContextMenuOptions(cipher: any) {
|
2021-05-03 20:56:38 +02:00
|
|
|
if (cipher == null || cipher.type !== CipherType.Login || cipher.reprompt !== CipherRepromptType.None) {
|
2017-12-05 23:04:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-07 03:54:38 +01:00
|
|
|
let title = cipher.name;
|
|
|
|
if (cipher.login.username && cipher.login.username !== '') {
|
|
|
|
title += (' (' + cipher.login.username + ')');
|
|
|
|
}
|
2017-12-05 23:04:30 +01:00
|
|
|
await this.loadContextMenuOptions(title, cipher.id, cipher);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async loadNoLoginsContextMenuOptions(noLoginsMessage: string) {
|
|
|
|
await this.loadContextMenuOptions(noLoginsMessage, 'noop', null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async loadContextMenuOptions(title: string, idSuffix: string, cipher: any) {
|
|
|
|
if (!chrome.contextMenus || this.menuOptionsLoaded.indexOf(idSuffix) > -1 ||
|
2018-01-09 20:26:20 +01:00
|
|
|
(cipher != null && cipher.type !== CipherType.Login)) {
|
2017-12-05 23:04:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.menuOptionsLoaded.push(idSuffix);
|
|
|
|
|
2017-12-07 04:10:02 +01:00
|
|
|
if (cipher == null || (cipher.login.password && cipher.login.password !== '')) {
|
2017-12-05 23:04:30 +01:00
|
|
|
await this.contextMenusCreate({
|
|
|
|
type: 'normal',
|
|
|
|
id: 'autofill_' + idSuffix,
|
|
|
|
parentId: 'autofill',
|
|
|
|
contexts: ['all'],
|
2020-04-06 17:25:07 +02:00
|
|
|
title: this.sanitizeContextMenuTitle(title),
|
2017-12-05 23:04:30 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-07 04:10:02 +01:00
|
|
|
if (cipher == null || (cipher.login.username && cipher.login.username !== '')) {
|
2017-12-05 23:04:30 +01:00
|
|
|
await this.contextMenusCreate({
|
|
|
|
type: 'normal',
|
|
|
|
id: 'copy-username_' + idSuffix,
|
|
|
|
parentId: 'copy-username',
|
|
|
|
contexts: ['all'],
|
2020-04-06 17:25:07 +02:00
|
|
|
title: this.sanitizeContextMenuTitle(title),
|
2017-12-05 23:04:30 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-24 19:59:49 +02:00
|
|
|
if (cipher == null || (cipher.login.password && cipher.login.password !== '' && cipher.viewPassword)) {
|
2017-12-05 23:04:30 +01:00
|
|
|
await this.contextMenusCreate({
|
|
|
|
type: 'normal',
|
|
|
|
id: 'copy-password_' + idSuffix,
|
|
|
|
parentId: 'copy-password',
|
|
|
|
contexts: ['all'],
|
2020-04-06 17:25:07 +02:00
|
|
|
title: this.sanitizeContextMenuTitle(title),
|
2017-12-05 23:04:30 +01:00
|
|
|
});
|
|
|
|
}
|
2020-03-03 16:40:06 +01:00
|
|
|
|
2020-05-26 20:53:17 +02:00
|
|
|
const canAccessPremium = await this.userService.canAccessPremium();
|
|
|
|
if (canAccessPremium && (cipher == null || (cipher.login.totp && cipher.login.totp !== ''))) {
|
2020-03-03 16:40:06 +01:00
|
|
|
await this.contextMenusCreate({
|
|
|
|
type: 'normal',
|
|
|
|
id: 'copy-totp_' + idSuffix,
|
|
|
|
parentId: 'copy-totp',
|
|
|
|
contexts: ['all'],
|
2020-04-06 17:25:07 +02:00
|
|
|
title: this.sanitizeContextMenuTitle(title),
|
2020-03-03 16:40:06 +01:00
|
|
|
});
|
|
|
|
}
|
2017-12-05 23:04:30 +01:00
|
|
|
}
|
|
|
|
|
2020-04-06 17:25:07 +02:00
|
|
|
private sanitizeContextMenuTitle(title: string): string {
|
|
|
|
return title.replace(/&/g, '&&');
|
|
|
|
}
|
|
|
|
|
2017-12-06 05:28:31 +01:00
|
|
|
private async fullSync(override: boolean = false) {
|
|
|
|
const syncInternal = 6 * 60 * 60 * 1000; // 6 hours
|
|
|
|
const lastSync = await this.syncService.getLastSync();
|
|
|
|
|
|
|
|
let lastSyncAgo = syncInternal + 1;
|
|
|
|
if (lastSync != null) {
|
|
|
|
lastSyncAgo = new Date().getTime() - lastSync.getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (override || lastSyncAgo >= syncInternal) {
|
|
|
|
await this.syncService.fullSync(override);
|
|
|
|
this.scheduleNextSync();
|
|
|
|
} else {
|
|
|
|
this.scheduleNextSync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private scheduleNextSync() {
|
|
|
|
if (this.syncTimeout) {
|
|
|
|
clearTimeout(this.syncTimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.syncTimeout = setTimeout(async () => await this.fullSync(), 5 * 60 * 1000); // check every 5 minutes
|
|
|
|
}
|
|
|
|
|
|
|
|
// Browser API Helpers
|
2017-12-05 23:04:30 +01:00
|
|
|
|
|
|
|
private contextMenusRemoveAll() {
|
2021-03-29 17:16:31 +02:00
|
|
|
return new Promise<void>(resolve => {
|
2017-12-06 05:28:31 +01:00
|
|
|
chrome.contextMenus.removeAll(() => {
|
2017-12-05 23:04:30 +01:00
|
|
|
resolve();
|
|
|
|
if (chrome.runtime.lastError) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private contextMenusCreate(options: any) {
|
2021-03-29 17:16:31 +02:00
|
|
|
return new Promise<void>(resolve => {
|
2017-12-06 05:28:31 +01:00
|
|
|
chrome.contextMenus.create(options, () => {
|
2017-12-05 23:04:30 +01:00
|
|
|
resolve();
|
|
|
|
if (chrome.runtime.lastError) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-07 03:54:38 +01:00
|
|
|
private async actionSetIcon(theAction: any, suffix: string): Promise<any> {
|
2017-12-05 23:04:30 +01:00
|
|
|
if (!theAction || !theAction.setIcon) {
|
2017-12-07 03:54:38 +01:00
|
|
|
return;
|
2017-12-05 23:04:30 +01:00
|
|
|
}
|
|
|
|
|
2017-12-07 03:54:38 +01:00
|
|
|
const options = {
|
|
|
|
path: {
|
|
|
|
19: 'images/icon19' + suffix + '.png',
|
|
|
|
38: 'images/icon38' + suffix + '.png',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-01-05 22:30:15 +01:00
|
|
|
if (this.platformUtilsService.isFirefox()) {
|
2017-12-07 03:54:38 +01:00
|
|
|
await theAction.setIcon(options);
|
2021-03-01 17:32:18 +01:00
|
|
|
} else if (this.platformUtilsService.isSafari()) {
|
|
|
|
// Workaround since Safari 14.0.3 returns a pending promise
|
|
|
|
// which doesn't resolve within a reasonable time.
|
|
|
|
theAction.setIcon(options);
|
2017-12-07 03:54:38 +01:00
|
|
|
} else {
|
2021-03-29 17:16:31 +02:00
|
|
|
return new Promise<void>(resolve => {
|
2017-12-07 03:54:38 +01:00
|
|
|
theAction.setIcon(options, () => resolve());
|
2017-12-05 23:04:30 +01:00
|
|
|
});
|
2017-12-07 03:54:38 +01:00
|
|
|
}
|
2017-12-05 23:04:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private actionSetBadgeBackgroundColor(action: any) {
|
|
|
|
if (action && action.setBadgeBackgroundColor) {
|
|
|
|
action.setBadgeBackgroundColor({ color: '#294e5f' });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private browserActionSetBadgeText(text: string, tabId: number) {
|
|
|
|
if (chrome.browserAction && chrome.browserAction.setBadgeText) {
|
|
|
|
chrome.browserAction.setBadgeText({
|
|
|
|
text: text,
|
2017-12-06 05:28:31 +01:00
|
|
|
tabId: tabId,
|
2017-12-05 23:04:30 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private sidebarActionSetBadgeText(text: string, tabId: number) {
|
|
|
|
if (!this.sidebarAction) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.sidebarAction.setBadgeText) {
|
|
|
|
this.sidebarAction.setBadgeText({
|
|
|
|
text: text,
|
2017-12-06 05:28:31 +01:00
|
|
|
tabId: tabId,
|
2017-12-05 23:04:30 +01:00
|
|
|
});
|
|
|
|
} else if (this.sidebarAction.setTitle) {
|
2018-02-27 19:37:57 +01:00
|
|
|
let title = 'Bitwarden';
|
2017-12-05 23:04:30 +01:00
|
|
|
if (text && text !== '') {
|
|
|
|
title += (' [' + text + ']');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.sidebarAction.setTitle({
|
|
|
|
title: title,
|
2017-12-06 05:28:31 +01:00
|
|
|
tabId: tabId,
|
2017-12-05 23:04:30 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|