mirror of
https://github.com/bitwarden/browser.git
synced 2024-12-20 16:07:45 +01:00
abstract MessagingService
This commit is contained in:
parent
0fbbc4a0b9
commit
d39c5b37dc
@ -14,6 +14,7 @@ import WindowsBackground from './windows.background';
|
|||||||
import ApiService from '../services/api.service';
|
import ApiService from '../services/api.service';
|
||||||
import AppIdService from '../services/appId.service';
|
import AppIdService from '../services/appId.service';
|
||||||
import AutofillService from '../services/autofill.service';
|
import AutofillService from '../services/autofill.service';
|
||||||
|
import BrowserMessagingService from '../services/browserMessaging.service';
|
||||||
import BrowserUtilsService from '../services/browserUtils.service';
|
import BrowserUtilsService from '../services/browserUtils.service';
|
||||||
import ChromeStorageService from '../services/chromeStorage.service';
|
import ChromeStorageService from '../services/chromeStorage.service';
|
||||||
import CipherService from '../services/cipher.service';
|
import CipherService from '../services/cipher.service';
|
||||||
@ -33,9 +34,11 @@ import TotpService from '../services/totp.service';
|
|||||||
import UserService from '../services/user.service';
|
import UserService from '../services/user.service';
|
||||||
import UtilsService from '../services/utils.service';
|
import UtilsService from '../services/utils.service';
|
||||||
|
|
||||||
|
import { MessagingService } from '../services/abstractions/messaging.service';
|
||||||
import { StorageService } from '../services/abstractions/storage.service';
|
import { StorageService } from '../services/abstractions/storage.service';
|
||||||
|
|
||||||
export default class MainBackground {
|
export default class MainBackground {
|
||||||
|
messagingService: MessagingService;
|
||||||
storageService: StorageService;
|
storageService: StorageService;
|
||||||
i18nService: any;
|
i18nService: any;
|
||||||
browserUtilsService: BrowserUtilsService;
|
browserUtilsService: BrowserUtilsService;
|
||||||
@ -76,9 +79,10 @@ export default class MainBackground {
|
|||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
// Services
|
// Services
|
||||||
this.storageService = new ChromeStorageService();
|
|
||||||
this.utilsService = new UtilsService();
|
this.utilsService = new UtilsService();
|
||||||
this.browserUtilsService = new BrowserUtilsService();
|
this.browserUtilsService = new BrowserUtilsService();
|
||||||
|
this.messagingService = new BrowserMessagingService(this.browserUtilsService);
|
||||||
|
this.storageService = new ChromeStorageService();
|
||||||
this.i18nService = i18nService(this.browserUtilsService);
|
this.i18nService = i18nService(this.browserUtilsService);
|
||||||
this.constantsService = new ConstantsService(this.i18nService, this.browserUtilsService);
|
this.constantsService = new ConstantsService(this.i18nService, this.browserUtilsService);
|
||||||
this.cryptoService = ContainerService.cryptoService = new CryptoService(this.storageService,
|
this.cryptoService = ContainerService.cryptoService = new CryptoService(this.storageService,
|
||||||
@ -100,7 +104,7 @@ export default class MainBackground {
|
|||||||
() => this.setIcon(), () => this.refreshBadgeAndMenu());
|
() => this.setIcon(), () => this.refreshBadgeAndMenu());
|
||||||
this.syncService = new SyncService(this.userService, this.apiService, this.settingsService,
|
this.syncService = new SyncService(this.userService, this.apiService, this.settingsService,
|
||||||
this.folderService, this.cipherService, this.cryptoService, this.collectionService,
|
this.folderService, this.cipherService, this.cryptoService, this.collectionService,
|
||||||
this.storageService, (expired: boolean) => this.logout(expired));
|
this.storageService, this.messagingService, (expired: boolean) => this.logout(expired));
|
||||||
this.passwordGenerationService = new PasswordGenerationService(this.cryptoService, this.storageService);
|
this.passwordGenerationService = new PasswordGenerationService(this.cryptoService, this.storageService);
|
||||||
this.totpService = new TotpService(this.storageService);
|
this.totpService = new TotpService(this.storageService);
|
||||||
this.autofillService = new AutofillService(this.cipherService, this.tokenService,
|
this.autofillService = new AutofillService(this.cipherService, this.tokenService,
|
||||||
@ -187,9 +191,7 @@ export default class MainBackground {
|
|||||||
this.passwordGenerationService.clear(),
|
this.passwordGenerationService.clear(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
chrome.runtime.sendMessage({
|
this.messagingService.send('doneLoggingOut', { expired: expired });
|
||||||
command: 'doneLoggingOut', expired: expired,
|
|
||||||
});
|
|
||||||
|
|
||||||
await this.setIcon();
|
await this.setIcon();
|
||||||
await this.refreshBadgeAndMenu();
|
await this.refreshBadgeAndMenu();
|
||||||
|
@ -265,8 +265,9 @@ angular
|
|||||||
}
|
}
|
||||||
|
|
||||||
const userService = trans.injector().get('userService');
|
const userService = trans.injector().get('userService');
|
||||||
|
const messagingService = trans.injector().get('messagingService');
|
||||||
|
|
||||||
if (!userService) {
|
if (!userService || !messagingService) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -278,7 +279,7 @@ angular
|
|||||||
}
|
}
|
||||||
else if (toState.data && toState.data.authorize) {
|
else if (toState.data && toState.data.authorize) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
chrome.runtime.sendMessage({ command: 'logout' });
|
messagingService.send('logout');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -3,6 +3,7 @@ import * as template from './lock.component.html';
|
|||||||
|
|
||||||
import { BrowserUtilsService } from '../../../services/abstractions/browserUtils.service';
|
import { BrowserUtilsService } from '../../../services/abstractions/browserUtils.service';
|
||||||
import { CryptoService } from '../../../services/abstractions/crypto.service';
|
import { CryptoService } from '../../../services/abstractions/crypto.service';
|
||||||
|
import { MessagingService } from '../../../services/abstractions/messaging.service';
|
||||||
|
|
||||||
export class LockController {
|
export class LockController {
|
||||||
i18n: any;
|
i18n: any;
|
||||||
@ -10,7 +11,7 @@ export class LockController {
|
|||||||
|
|
||||||
constructor(public $state: any, public i18nService: any, private $timeout: any,
|
constructor(public $state: any, public i18nService: any, private $timeout: any,
|
||||||
private browserUtilsService: BrowserUtilsService, public cryptoService: CryptoService, public toastr: any,
|
private browserUtilsService: BrowserUtilsService, public cryptoService: CryptoService, public toastr: any,
|
||||||
public userService: any, public SweetAlert: any) {
|
public userService: any, public messagingService: MessagingService, public SweetAlert: any) {
|
||||||
this.i18n = i18nService;
|
this.i18n = i18nService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,7 +31,7 @@ export class LockController {
|
|||||||
cancelButtonText: this.i18nService.cancel,
|
cancelButtonText: this.i18nService.cancel,
|
||||||
}, (confirmed: boolean) => {
|
}, (confirmed: boolean) => {
|
||||||
if (confirmed) {
|
if (confirmed) {
|
||||||
chrome.runtime.sendMessage({ command: 'logout' });
|
this.messagingService.send('logout');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -48,7 +49,7 @@ export class LockController {
|
|||||||
|
|
||||||
if (storedKeyHash != null && keyHash != null && storedKeyHash === keyHash) {
|
if (storedKeyHash != null && keyHash != null && storedKeyHash === keyHash) {
|
||||||
await this.cryptoService.setKey(key);
|
await this.cryptoService.setKey(key);
|
||||||
chrome.runtime.sendMessage({ command: 'unlocked' });
|
this.messagingService.send('unlocked');
|
||||||
this.$state.go('tabs.current');
|
this.$state.go('tabs.current');
|
||||||
} else {
|
} else {
|
||||||
this.toastr.error(this.i18nService.invalidMasterPassword, this.i18nService.errorsOccurred);
|
this.toastr.error(this.i18nService.invalidMasterPassword, this.i18nService.errorsOccurred);
|
||||||
|
@ -3,11 +3,13 @@ import { TokenRequest } from '../../../models/request/tokenRequest';
|
|||||||
|
|
||||||
import { BrowserUtilsService } from '../../../services/abstractions/browserUtils.service';
|
import { BrowserUtilsService } from '../../../services/abstractions/browserUtils.service';
|
||||||
import { CryptoService } from '../../../services/abstractions/crypto.service';
|
import { CryptoService } from '../../../services/abstractions/crypto.service';
|
||||||
|
import { MessagingService } from '../../../services/abstractions/messaging.service';
|
||||||
|
|
||||||
class AuthService {
|
class AuthService {
|
||||||
constructor(public cryptoService: CryptoService, public apiService: any, public userService: any,
|
constructor(public cryptoService: CryptoService, public apiService: any, public userService: any,
|
||||||
public tokenService: any, public $rootScope: any, public appIdService: any,
|
public tokenService: any, public $rootScope: any, public appIdService: any,
|
||||||
public browserUtilsService: BrowserUtilsService, public constantsService: any) {
|
public browserUtilsService: BrowserUtilsService, public constantsService: any,
|
||||||
|
public messagingService: MessagingService) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async logIn(email: string, masterPassword: string, twoFactorProvider?: number,
|
async logIn(email: string, masterPassword: string, twoFactorProvider?: number,
|
||||||
@ -57,7 +59,7 @@ class AuthService {
|
|||||||
await this.cryptoService.setEncKey(response.key);
|
await this.cryptoService.setEncKey(response.key);
|
||||||
await this.cryptoService.setEncPrivateKey(response.privateKey);
|
await this.cryptoService.setEncPrivateKey(response.privateKey);
|
||||||
|
|
||||||
chrome.runtime.sendMessage({ command: 'loggedIn' });
|
this.messagingService.send('loggedIn');
|
||||||
return {
|
return {
|
||||||
twoFactor: false,
|
twoFactor: false,
|
||||||
twoFactorProviders: null,
|
twoFactorProviders: null,
|
||||||
|
@ -4,12 +4,17 @@ import * as backgroundServices from './background.service';
|
|||||||
import StateService from './state.service';
|
import StateService from './state.service';
|
||||||
import { ValidationService } from './validation.service';
|
import { ValidationService } from './validation.service';
|
||||||
|
|
||||||
|
import BrowserMessagingService from '../../../services/browserMessaging.service';
|
||||||
|
|
||||||
|
const messagingService = new BrowserMessagingService(backgroundServices.browserUtilsService());
|
||||||
|
|
||||||
export default angular
|
export default angular
|
||||||
.module('bit.services', ['toastr'])
|
.module('bit.services', ['toastr'])
|
||||||
.service('stateService', StateService)
|
.service('stateService', StateService)
|
||||||
.service('validationService', ValidationService)
|
.service('validationService', ValidationService)
|
||||||
.service('authService', AuthService)
|
.service('authService', AuthService)
|
||||||
|
|
||||||
|
.factory('messagingService', () => messagingService)
|
||||||
.factory('storageService', backgroundServices.storageService)
|
.factory('storageService', backgroundServices.storageService)
|
||||||
.factory('tokenService', backgroundServices.tokenService)
|
.factory('tokenService', backgroundServices.tokenService)
|
||||||
.factory('cryptoService', backgroundServices.cryptoService)
|
.factory('cryptoService', backgroundServices.cryptoService)
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import * as angular from 'angular';
|
import * as angular from 'angular';
|
||||||
import { BrowserUtilsService } from '../../../services/abstractions/browserUtils.service';
|
import { BrowserUtilsService } from '../../../services/abstractions/browserUtils.service';
|
||||||
|
import { MessagingService } from '../../../services/abstractions/messaging.service';
|
||||||
import { StorageService } from '../../../services/abstractions/storage.service';
|
import { StorageService } from '../../../services/abstractions/storage.service';
|
||||||
import StateService from '../services/state.service';
|
import StateService from '../services/state.service';
|
||||||
import * as template from './options.component.html';
|
import * as template from './options.component.html';
|
||||||
@ -15,7 +16,8 @@ export class OptionsController {
|
|||||||
|
|
||||||
constructor(private i18nService: any, private $analytics: any, private constantsService: any,
|
constructor(private i18nService: any, private $analytics: any, private constantsService: any,
|
||||||
private browserUtilsService: BrowserUtilsService, private totpService: any, private stateService: StateService,
|
private browserUtilsService: BrowserUtilsService, private totpService: any, private stateService: StateService,
|
||||||
private storageService: StorageService, private $timeout: ng.ITimeoutService) {
|
private storageService: StorageService, public messagingService: MessagingService,
|
||||||
|
private $timeout: ng.ITimeoutService) {
|
||||||
this.i18n = i18nService;
|
this.i18n = i18nService;
|
||||||
|
|
||||||
$timeout(() => {
|
$timeout(() => {
|
||||||
@ -64,9 +66,7 @@ export class OptionsController {
|
|||||||
updateDisableContextMenuItem() {
|
updateDisableContextMenuItem() {
|
||||||
this.storageService.save(this.constantsService.disableContextMenuItemKey,
|
this.storageService.save(this.constantsService.disableContextMenuItemKey,
|
||||||
this.disableContextMenuItem).then(() => {
|
this.disableContextMenuItem).then(() => {
|
||||||
chrome.runtime.sendMessage({
|
this.messagingService.send('bgUpdateContextMenu');
|
||||||
command: 'bgUpdateContextMenu',
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
this.callAnalytics('Context Menu Item', !this.disableContextMenuItem);
|
this.callAnalytics('Context Menu Item', !this.disableContextMenuItem);
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ import * as angular from 'angular';
|
|||||||
import { BrowserType } from '../../../enums/browserType.enum';
|
import { BrowserType } from '../../../enums/browserType.enum';
|
||||||
import { BrowserUtilsService } from '../../../services/abstractions/browserUtils.service';
|
import { BrowserUtilsService } from '../../../services/abstractions/browserUtils.service';
|
||||||
import { CryptoService } from '../../../services/abstractions/crypto.service';
|
import { CryptoService } from '../../../services/abstractions/crypto.service';
|
||||||
|
import { MessagingService } from '../../../services/abstractions/messaging.service';
|
||||||
import { StorageService } from '../../../services/abstractions/storage.service';
|
import { StorageService } from '../../../services/abstractions/storage.service';
|
||||||
import ConstantsService from '../../../services/constants.service';
|
import ConstantsService from '../../../services/constants.service';
|
||||||
|
|
||||||
@ -30,7 +31,7 @@ export class SettingsController {
|
|||||||
constructor(private $state: any, private SweetAlert: any, private browserUtilsService: BrowserUtilsService,
|
constructor(private $state: any, private SweetAlert: any, private browserUtilsService: BrowserUtilsService,
|
||||||
private $analytics: any, private i18nService: any, private constantsService: ConstantsService,
|
private $analytics: any, private i18nService: any, private constantsService: ConstantsService,
|
||||||
private cryptoService: CryptoService, private lockService: any, private storageService: StorageService,
|
private cryptoService: CryptoService, private lockService: any, private storageService: StorageService,
|
||||||
private $timeout: ng.ITimeoutService) {
|
public messagingService: MessagingService, private $timeout: ng.ITimeoutService) {
|
||||||
this.i18n = i18nService;
|
this.i18n = i18nService;
|
||||||
|
|
||||||
$timeout(() => {
|
$timeout(() => {
|
||||||
@ -68,7 +69,7 @@ export class SettingsController {
|
|||||||
}, (confirmed: boolean) => {
|
}, (confirmed: boolean) => {
|
||||||
if (confirmed) {
|
if (confirmed) {
|
||||||
this.cryptoService.toggleKey();
|
this.cryptoService.toggleKey();
|
||||||
chrome.runtime.sendMessage({ command: 'logout' });
|
this.messagingService.send('logout');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -93,7 +94,7 @@ export class SettingsController {
|
|||||||
cancelButtonText: this.i18nService.cancel,
|
cancelButtonText: this.i18nService.cancel,
|
||||||
}, (confirmed: boolean) => {
|
}, (confirmed: boolean) => {
|
||||||
if (confirmed) {
|
if (confirmed) {
|
||||||
chrome.runtime.sendMessage({ command: 'logout' });
|
this.messagingService.send('logout');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
3
src/services/abstractions/messaging.service.ts
Normal file
3
src/services/abstractions/messaging.service.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export interface MessagingService {
|
||||||
|
send(subscriber: string, arg?: any): void;
|
||||||
|
}
|
13
src/services/browserMessaging.service.ts
Normal file
13
src/services/browserMessaging.service.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import { BrowserUtilsService } from './abstractions/browserUtils.service';
|
||||||
|
import { MessagingService as MessagingServiceInterface } from './abstractions/messaging.service';
|
||||||
|
|
||||||
|
export default class BrowserMessagingService implements MessagingServiceInterface {
|
||||||
|
constructor(private browserUtilsService: BrowserUtilsService) {
|
||||||
|
}
|
||||||
|
|
||||||
|
send(subscriber: string, arg: any = {}) {
|
||||||
|
// if safari, else
|
||||||
|
const message = Object.assign({}, { command: subscriber }, arg);
|
||||||
|
chrome.runtime.sendMessage(message);
|
||||||
|
}
|
||||||
|
}
|
@ -17,6 +17,7 @@ import FolderService from './folder.service';
|
|||||||
import SettingsService from './settings.service';
|
import SettingsService from './settings.service';
|
||||||
import UserService from './user.service';
|
import UserService from './user.service';
|
||||||
|
|
||||||
|
import { MessagingService } from './abstractions/messaging.service';
|
||||||
import { StorageService } from './abstractions/storage.service';
|
import { StorageService } from './abstractions/storage.service';
|
||||||
|
|
||||||
const Keys = {
|
const Keys = {
|
||||||
@ -30,7 +31,7 @@ export default class SyncService {
|
|||||||
private settingsService: SettingsService, private folderService: FolderService,
|
private settingsService: SettingsService, private folderService: FolderService,
|
||||||
private cipherService: CipherService, private cryptoService: CryptoService,
|
private cipherService: CipherService, private cryptoService: CryptoService,
|
||||||
private collectionService: CollectionService, private storageService: StorageService,
|
private collectionService: CollectionService, private storageService: StorageService,
|
||||||
private logoutCallback: Function) {
|
private messagingService: MessagingService, private logoutCallback: Function) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async getLastSync() {
|
async getLastSync() {
|
||||||
@ -50,13 +51,12 @@ export default class SyncService {
|
|||||||
|
|
||||||
syncStarted() {
|
syncStarted() {
|
||||||
this.syncInProgress = true;
|
this.syncInProgress = true;
|
||||||
chrome.runtime.sendMessage({ command: 'syncStarted' });
|
this.messagingService.send('syncStarted');
|
||||||
}
|
}
|
||||||
|
|
||||||
syncCompleted(successfully: boolean) {
|
syncCompleted(successfully: boolean) {
|
||||||
this.syncInProgress = false;
|
this.syncInProgress = false;
|
||||||
// tslint:disable-next-line
|
this.messagingService.send('syncCompleted', { successfully: successfully });
|
||||||
chrome.runtime.sendMessage({ command: 'syncCompleted', successfully: successfully });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fullSync(forceSync: boolean) {
|
async fullSync(forceSync: boolean) {
|
||||||
|
Loading…
Reference in New Issue
Block a user