diff --git a/src/background/main.background.ts b/src/background/main.background.ts index 61ce6002da..a3e076c433 100644 --- a/src/background/main.background.ts +++ b/src/background/main.background.ts @@ -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(); diff --git a/src/popup/app/config.js b/src/popup/app/config.js index 8995d4cc9f..0965bf04c1 100644 --- a/src/popup/app/config.js +++ b/src/popup/app/config.js @@ -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'); } }); }); diff --git a/src/popup/app/lock/lock.component.ts b/src/popup/app/lock/lock.component.ts index 3016dfe9d2..71e67fdc0d 100644 --- a/src/popup/app/lock/lock.component.ts +++ b/src/popup/app/lock/lock.component.ts @@ -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); diff --git a/src/popup/app/services/auth.service.ts b/src/popup/app/services/auth.service.ts index 907bcf5b8e..518f63ce48 100644 --- a/src/popup/app/services/auth.service.ts +++ b/src/popup/app/services/auth.service.ts @@ -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, diff --git a/src/popup/app/services/services.module.ts b/src/popup/app/services/services.module.ts index 760f4d6f89..8a85db642a 100644 --- a/src/popup/app/services/services.module.ts +++ b/src/popup/app/services/services.module.ts @@ -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) diff --git a/src/popup/app/settings/options.component.ts b/src/popup/app/settings/options.component.ts index 4dd7cf2f19..e474051ad7 100644 --- a/src/popup/app/settings/options.component.ts +++ b/src/popup/app/settings/options.component.ts @@ -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); } diff --git a/src/popup/app/settings/settings.component.ts b/src/popup/app/settings/settings.component.ts index 5b24df06f0..d665886965 100644 --- a/src/popup/app/settings/settings.component.ts +++ b/src/popup/app/settings/settings.component.ts @@ -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'); } }); } diff --git a/src/services/abstractions/messaging.service.ts b/src/services/abstractions/messaging.service.ts new file mode 100644 index 0000000000..6bafce93e4 --- /dev/null +++ b/src/services/abstractions/messaging.service.ts @@ -0,0 +1,3 @@ +export interface MessagingService { + send(subscriber: string, arg?: any): void; +} diff --git a/src/services/browserMessaging.service.ts b/src/services/browserMessaging.service.ts new file mode 100644 index 0000000000..fffa94c70c --- /dev/null +++ b/src/services/browserMessaging.service.ts @@ -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); + } +} diff --git a/src/services/sync.service.ts b/src/services/sync.service.ts index 5789990e36..3dfb37a938 100644 --- a/src/services/sync.service.ts +++ b/src/services/sync.service.ts @@ -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) {