bitwarden-estensione-browser/src/popup/services/services.module.ts

184 lines
9.0 KiB
TypeScript
Raw Normal View History

2018-04-04 04:14:54 +02:00
import {
APP_INITIALIZER,
2018-08-02 15:30:33 +02:00
LOCALE_ID,
2018-04-04 04:14:54 +02:00
NgModule,
} from '@angular/core';
import { ToasterModule } from 'angular2-toaster';
2018-04-10 23:14:10 +02:00
import { LaunchGuardService } from './launch-guard.service';
2018-04-04 14:27:31 +02:00
import { AuthGuardService } from 'jslib/angular/services/auth-guard.service';
2018-04-06 21:33:20 +02:00
import { BroadcasterService } from 'jslib/angular/services/broadcaster.service';
2018-04-04 14:27:31 +02:00
import { ValidationService } from 'jslib/angular/services/validation.service';
import { BrowserApi } from '../../browser/browserApi';
import { ApiService } from 'jslib/abstractions/api.service';
import { AppIdService } from 'jslib/abstractions/appId.service';
import { AuditService } from 'jslib/abstractions/audit.service';
2018-04-11 05:49:46 +02:00
import { AuthService as AuthServiceAbstraction } from 'jslib/abstractions/auth.service';
2018-04-04 14:27:31 +02:00
import { CipherService } from 'jslib/abstractions/cipher.service';
import { CollectionService } from 'jslib/abstractions/collection.service';
import { CryptoFunctionService } from 'jslib/abstractions/cryptoFunction.service';
2018-04-04 14:27:31 +02:00
import { CryptoService } from 'jslib/abstractions/crypto.service';
import { EnvironmentService } from 'jslib/abstractions/environment.service';
import { EventService } from 'jslib/abstractions/event.service';
2018-05-17 16:57:52 +02:00
import { ExportService } from 'jslib/abstractions/export.service';
2018-04-04 14:27:31 +02:00
import { FolderService } from 'jslib/abstractions/folder.service';
import { I18nService } from 'jslib/abstractions/i18n.service';
import { MessagingService } from 'jslib/abstractions/messaging.service';
2018-08-20 23:40:39 +02:00
import { NotificationsService } from 'jslib/abstractions/notifications.service';
2018-04-04 14:27:31 +02:00
import { PasswordGenerationService } from 'jslib/abstractions/passwordGeneration.service';
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
import { PolicyService } from 'jslib/abstractions/policy.service';
import { SearchService as SearchServiceAbstraction } from 'jslib/abstractions/search.service';
2018-04-04 14:27:31 +02:00
import { SettingsService } from 'jslib/abstractions/settings.service';
import { StateService as StateServiceAbstraction } from 'jslib/abstractions/state.service';
2018-04-04 14:27:31 +02:00
import { StorageService } from 'jslib/abstractions/storage.service';
import { SyncService } from 'jslib/abstractions/sync.service';
import { TokenService } from 'jslib/abstractions/token.service';
import { TotpService } from 'jslib/abstractions/totp.service';
import { UserService } from 'jslib/abstractions/user.service';
import { VaultTimeoutService } from 'jslib/abstractions/vaultTimeout.service';
2018-04-04 14:27:31 +02:00
import { AutofillService } from '../../services/abstractions/autofill.service';
import BrowserMessagingService from '../../services/browserMessaging.service';
import { AuthService } from 'jslib/services/auth.service';
import { ConstantsService } from 'jslib/services/constants.service';
import { SearchService } from 'jslib/services/search.service';
import { StateService } from 'jslib/services/state.service';
2018-04-13 20:36:25 +02:00
import { Analytics } from 'jslib/misc/analytics';
import { PopupSearchService } from './popup-search.service';
import { PopupUtilsService } from './popup-utils.service';
2018-04-04 14:27:31 +02:00
function getBgService<T>(service: string) {
return (): T => {
const page = BrowserApi.getBackgroundPage();
return page ? page.bitwardenMain[service] as T : null;
};
}
2018-04-06 17:48:45 +02:00
export const stateService = new StateService();
2018-04-10 20:20:03 +02:00
export const messagingService = new BrowserMessagingService();
2018-04-06 17:48:45 +02:00
export const authService = new AuthService(getBgService<CryptoService>('cryptoService')(),
2018-04-04 14:27:31 +02:00
getBgService<ApiService>('apiService')(), getBgService<UserService>('userService')(),
getBgService<TokenService>('tokenService')(), getBgService<AppIdService>('appIdService')(),
2018-04-11 20:52:49 +02:00
getBgService<I18nService>('i18nService')(), getBgService<PlatformUtilsService>('platformUtilsService')(),
2020-09-15 22:27:28 +02:00
messagingService, getBgService<VaultTimeoutService>('vaultTimeoutService')());
export const searchService = new PopupSearchService(getBgService<SearchService>('searchService')(),
2020-09-15 16:50:45 +02:00
getBgService<CipherService>('cipherService')());
2018-04-04 04:14:54 +02:00
2018-04-10 04:25:03 +02:00
export function initFactory(i18nService: I18nService, storageService: StorageService,
popupUtilsService: PopupUtilsService): Function {
2018-04-04 04:14:54 +02:00
return async () => {
2018-04-10 04:25:03 +02:00
if (!popupUtilsService.inPopup(window)) {
window.document.body.classList.add('body-full');
} else if (window.screen.availHeight < 600) {
window.document.body.classList.add('body-xs');
2018-04-06 21:52:53 +02:00
} else if (window.screen.availHeight <= 800) {
2018-04-10 04:25:03 +02:00
window.document.body.classList.add('body-sm');
2018-04-06 21:52:53 +02:00
}
2018-04-13 20:36:25 +02:00
if (BrowserApi.getBackgroundPage() != null) {
2018-04-14 04:08:24 +02:00
stateService.save(ConstantsService.disableFaviconKey,
await storageService.get<boolean>(ConstantsService.disableFaviconKey));
2018-05-30 23:26:43 +02:00
let theme = await storageService.get<string>(ConstantsService.themeKey);
if (theme == null) {
theme = 'light';
}
2018-04-06 21:52:53 +02:00
window.document.documentElement.classList.add('locale_' + i18nService.translationLocale);
2018-05-30 23:26:43 +02:00
window.document.documentElement.classList.add('theme_' + theme);
2018-04-04 14:27:31 +02:00
authService.init();
2018-04-13 21:14:04 +02:00
const analytics = new Analytics(window, () => BrowserApi.gaFilter(), null, null, null, () => {
2018-04-13 20:36:25 +02:00
const bgPage = BrowserApi.getBackgroundPage();
if (bgPage == null || bgPage.bitwardenMain == null) {
2018-04-13 21:14:04 +02:00
throw new Error('Cannot resolve background page main.');
2018-04-13 20:36:25 +02:00
}
return bgPage.bitwardenMain;
});
}
2018-04-04 04:14:54 +02:00
};
}
@NgModule({
imports: [
ToasterModule,
],
declarations: [],
providers: [
ValidationService,
AuthGuardService,
2018-04-10 23:14:10 +02:00
LaunchGuardService,
PopupUtilsService,
2018-04-06 21:33:20 +02:00
BroadcasterService,
2018-04-04 14:27:31 +02:00
{ provide: MessagingService, useValue: messagingService },
{ provide: AuthServiceAbstraction, useValue: authService },
{ provide: StateServiceAbstraction, useValue: stateService },
{ provide: SearchServiceAbstraction, useValue: searchService },
2018-04-04 14:27:31 +02:00
{ provide: AuditService, useFactory: getBgService<AuditService>('auditService'), deps: [] },
{ provide: CipherService, useFactory: getBgService<CipherService>('cipherService'), deps: [] },
{
provide: CryptoFunctionService,
useFactory: getBgService<CryptoFunctionService>('cryptoFunctionService'),
deps: [],
},
2018-04-04 14:27:31 +02:00
{ provide: FolderService, useFactory: getBgService<FolderService>('folderService'), deps: [] },
{ provide: CollectionService, useFactory: getBgService<CollectionService>('collectionService'), deps: [] },
{ provide: EnvironmentService, useFactory: getBgService<EnvironmentService>('environmentService'), deps: [] },
{ provide: TotpService, useFactory: getBgService<TotpService>('totpService'), deps: [] },
{ provide: TokenService, useFactory: getBgService<TokenService>('tokenService'), deps: [] },
2018-04-11 20:52:49 +02:00
{ provide: I18nService, useFactory: getBgService<I18nService>('i18nService'), deps: [] },
2018-04-04 14:27:31 +02:00
{ provide: CryptoService, useFactory: getBgService<CryptoService>('cryptoService'), deps: [] },
{ provide: EventService, useFactory: getBgService<EventService>('eventService'), deps: [] },
{ provide: PolicyService, useFactory: getBgService<PolicyService>('policyService'), deps: [] },
2018-04-04 14:27:31 +02:00
{
provide: PlatformUtilsService,
useFactory: getBgService<PlatformUtilsService>('platformUtilsService'),
2018-04-11 05:49:46 +02:00
deps: [],
2018-04-04 14:27:31 +02:00
},
{
provide: PasswordGenerationService,
useFactory: getBgService<PasswordGenerationService>('passwordGenerationService'),
2018-04-11 05:49:46 +02:00
deps: [],
2018-04-04 14:27:31 +02:00
},
{ provide: ApiService, useFactory: getBgService<ApiService>('apiService'), deps: [] },
{ provide: SyncService, useFactory: getBgService<SyncService>('syncService'), deps: [] },
{ provide: UserService, useFactory: getBgService<UserService>('userService'), deps: [] },
{ provide: SettingsService, useFactory: getBgService<SettingsService>('settingsService'), deps: [] },
{ provide: StorageService, useFactory: getBgService<StorageService>('storageService'), deps: [] },
{ provide: AppIdService, useFactory: getBgService<AppIdService>('appIdService'), deps: [] },
{ provide: AutofillService, useFactory: getBgService<AutofillService>('autofillService'), deps: [] },
2018-08-13 17:53:16 +02:00
{ provide: ExportService, useFactory: getBgService<ExportService>('exportService'), deps: [] },
{
provide: VaultTimeoutService,
useFactory: getBgService<VaultTimeoutService>('vaultTimeoutService'),
deps: [],
},
2018-08-20 23:40:39 +02:00
{
provide: NotificationsService,
useFactory: getBgService<NotificationsService>('notificationsService'),
deps: [],
},
2018-04-04 04:14:54 +02:00
{
provide: APP_INITIALIZER,
useFactory: initFactory,
2018-04-10 04:25:03 +02:00
deps: [I18nService, StorageService, PopupUtilsService],
2018-04-04 04:14:54 +02:00
multi: true,
},
2018-08-02 15:30:33 +02:00
{
provide: LOCALE_ID,
useFactory: () => getBgService<I18nService>('i18nService')().translationLocale,
deps: [],
},
2018-04-04 04:14:54 +02:00
],
})
export class ServicesModule {
}