1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-12-19 15:57:42 +01:00

abstract MessagingService

This commit is contained in:
Kyle Spearrin 2018-01-04 16:06:00 -05:00
parent 0fbbc4a0b9
commit d39c5b37dc
10 changed files with 51 additions and 23 deletions

View File

@ -14,6 +14,7 @@ import WindowsBackground from './windows.background';
import ApiService from '../services/api.service';
import AppIdService from '../services/appId.service';
import AutofillService from '../services/autofill.service';
import BrowserMessagingService from '../services/browserMessaging.service';
import BrowserUtilsService from '../services/browserUtils.service';
import ChromeStorageService from '../services/chromeStorage.service';
import CipherService from '../services/cipher.service';
@ -33,9 +34,11 @@ import TotpService from '../services/totp.service';
import UserService from '../services/user.service';
import UtilsService from '../services/utils.service';
import { MessagingService } from '../services/abstractions/messaging.service';
import { StorageService } from '../services/abstractions/storage.service';
export default class MainBackground {
messagingService: MessagingService;
storageService: StorageService;
i18nService: any;
browserUtilsService: BrowserUtilsService;
@ -76,9 +79,10 @@ export default class MainBackground {
constructor() {
// Services
this.storageService = new ChromeStorageService();
this.utilsService = new UtilsService();
this.browserUtilsService = new BrowserUtilsService();
this.messagingService = new BrowserMessagingService(this.browserUtilsService);
this.storageService = new ChromeStorageService();
this.i18nService = i18nService(this.browserUtilsService);
this.constantsService = new ConstantsService(this.i18nService, this.browserUtilsService);
this.cryptoService = ContainerService.cryptoService = new CryptoService(this.storageService,
@ -100,7 +104,7 @@ export default class MainBackground {
() => this.setIcon(), () => this.refreshBadgeAndMenu());
this.syncService = new SyncService(this.userService, this.apiService, this.settingsService,
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.totpService = new TotpService(this.storageService);
this.autofillService = new AutofillService(this.cipherService, this.tokenService,
@ -187,9 +191,7 @@ export default class MainBackground {
this.passwordGenerationService.clear(),
]);
chrome.runtime.sendMessage({
command: 'doneLoggingOut', expired: expired,
});
this.messagingService.send('doneLoggingOut', { expired: expired });
await this.setIcon();
await this.refreshBadgeAndMenu();

View File

@ -265,8 +265,9 @@ angular
}
const userService = trans.injector().get('userService');
const messagingService = trans.injector().get('messagingService');
if (!userService) {
if (!userService || !messagingService) {
return;
}
@ -278,7 +279,7 @@ angular
}
else if (toState.data && toState.data.authorize) {
event.preventDefault();
chrome.runtime.sendMessage({ command: 'logout' });
messagingService.send('logout');
}
});
});

View File

@ -3,6 +3,7 @@ import * as template from './lock.component.html';
import { BrowserUtilsService } from '../../../services/abstractions/browserUtils.service';
import { CryptoService } from '../../../services/abstractions/crypto.service';
import { MessagingService } from '../../../services/abstractions/messaging.service';
export class LockController {
i18n: any;
@ -10,7 +11,7 @@ export class LockController {
constructor(public $state: any, public i18nService: any, private $timeout: 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;
}
@ -30,7 +31,7 @@ export class LockController {
cancelButtonText: this.i18nService.cancel,
}, (confirmed: boolean) => {
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) {
await this.cryptoService.setKey(key);
chrome.runtime.sendMessage({ command: 'unlocked' });
this.messagingService.send('unlocked');
this.$state.go('tabs.current');
} else {
this.toastr.error(this.i18nService.invalidMasterPassword, this.i18nService.errorsOccurred);

View File

@ -3,11 +3,13 @@ import { TokenRequest } from '../../../models/request/tokenRequest';
import { BrowserUtilsService } from '../../../services/abstractions/browserUtils.service';
import { CryptoService } from '../../../services/abstractions/crypto.service';
import { MessagingService } from '../../../services/abstractions/messaging.service';
class AuthService {
constructor(public cryptoService: CryptoService, public apiService: any, public userService: 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,
@ -57,7 +59,7 @@ class AuthService {
await this.cryptoService.setEncKey(response.key);
await this.cryptoService.setEncPrivateKey(response.privateKey);
chrome.runtime.sendMessage({ command: 'loggedIn' });
this.messagingService.send('loggedIn');
return {
twoFactor: false,
twoFactorProviders: null,

View File

@ -4,12 +4,17 @@ import * as backgroundServices from './background.service';
import StateService from './state.service';
import { ValidationService } from './validation.service';
import BrowserMessagingService from '../../../services/browserMessaging.service';
const messagingService = new BrowserMessagingService(backgroundServices.browserUtilsService());
export default angular
.module('bit.services', ['toastr'])
.service('stateService', StateService)
.service('validationService', ValidationService)
.service('authService', AuthService)
.factory('messagingService', () => messagingService)
.factory('storageService', backgroundServices.storageService)
.factory('tokenService', backgroundServices.tokenService)
.factory('cryptoService', backgroundServices.cryptoService)

View File

@ -1,5 +1,6 @@
import * as angular from 'angular';
import { BrowserUtilsService } from '../../../services/abstractions/browserUtils.service';
import { MessagingService } from '../../../services/abstractions/messaging.service';
import { StorageService } from '../../../services/abstractions/storage.service';
import StateService from '../services/state.service';
import * as template from './options.component.html';
@ -15,7 +16,8 @@ export class OptionsController {
constructor(private i18nService: any, private $analytics: any, private constantsService: any,
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;
$timeout(() => {
@ -64,9 +66,7 @@ export class OptionsController {
updateDisableContextMenuItem() {
this.storageService.save(this.constantsService.disableContextMenuItemKey,
this.disableContextMenuItem).then(() => {
chrome.runtime.sendMessage({
command: 'bgUpdateContextMenu',
});
this.messagingService.send('bgUpdateContextMenu');
});
this.callAnalytics('Context Menu Item', !this.disableContextMenuItem);
}

View File

@ -2,6 +2,7 @@ import * as angular from 'angular';
import { BrowserType } from '../../../enums/browserType.enum';
import { BrowserUtilsService } from '../../../services/abstractions/browserUtils.service';
import { CryptoService } from '../../../services/abstractions/crypto.service';
import { MessagingService } from '../../../services/abstractions/messaging.service';
import { StorageService } from '../../../services/abstractions/storage.service';
import ConstantsService from '../../../services/constants.service';
@ -30,7 +31,7 @@ export class SettingsController {
constructor(private $state: any, private SweetAlert: any, private browserUtilsService: BrowserUtilsService,
private $analytics: any, private i18nService: any, private constantsService: ConstantsService,
private cryptoService: CryptoService, private lockService: any, private storageService: StorageService,
private $timeout: ng.ITimeoutService) {
public messagingService: MessagingService, private $timeout: ng.ITimeoutService) {
this.i18n = i18nService;
$timeout(() => {
@ -68,7 +69,7 @@ export class SettingsController {
}, (confirmed: boolean) => {
if (confirmed) {
this.cryptoService.toggleKey();
chrome.runtime.sendMessage({ command: 'logout' });
this.messagingService.send('logout');
}
});
}
@ -93,7 +94,7 @@ export class SettingsController {
cancelButtonText: this.i18nService.cancel,
}, (confirmed: boolean) => {
if (confirmed) {
chrome.runtime.sendMessage({ command: 'logout' });
this.messagingService.send('logout');
}
});
}

View File

@ -0,0 +1,3 @@
export interface MessagingService {
send(subscriber: string, arg?: any): void;
}

View 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);
}
}

View File

@ -17,6 +17,7 @@ import FolderService from './folder.service';
import SettingsService from './settings.service';
import UserService from './user.service';
import { MessagingService } from './abstractions/messaging.service';
import { StorageService } from './abstractions/storage.service';
const Keys = {
@ -30,7 +31,7 @@ export default class SyncService {
private settingsService: SettingsService, private folderService: FolderService,
private cipherService: CipherService, private cryptoService: CryptoService,
private collectionService: CollectionService, private storageService: StorageService,
private logoutCallback: Function) {
private messagingService: MessagingService, private logoutCallback: Function) {
}
async getLastSync() {
@ -50,13 +51,12 @@ export default class SyncService {
syncStarted() {
this.syncInProgress = true;
chrome.runtime.sendMessage({ command: 'syncStarted' });
this.messagingService.send('syncStarted');
}
syncCompleted(successfully: boolean) {
this.syncInProgress = false;
// tslint:disable-next-line
chrome.runtime.sendMessage({ command: 'syncCompleted', successfully: successfully });
this.messagingService.send('syncCompleted', { successfully: successfully });
}
async fullSync(forceSync: boolean) {