From c67b63a452abd9dd324608b826ca6ac169ea853d Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 23 Apr 2018 13:04:11 -0400 Subject: [PATCH] refactor utils service to utils --- jslib | 2 +- src/background/commands.background.ts | 4 +-- src/background/contextMenus.background.ts | 14 +++++------ src/background/main.background.ts | 8 ++---- src/background/runtime.background.ts | 7 +++--- src/popup/services/services.module.ts | 2 -- src/popup/vault/current-tab.component.ts | 8 +++--- src/services/autofill.service.ts | 3 --- src/services/browserPlatformUtils.service.ts | 26 +++++++++++++++++--- 9 files changed, 41 insertions(+), 33 deletions(-) diff --git a/jslib b/jslib index 5e7115f78d..0fa9fc58eb 160000 --- a/jslib +++ b/jslib @@ -1 +1 @@ -Subproject commit 5e7115f78d8a87b241a43fdcd6d53b67ccf0b605 +Subproject commit 0fa9fc58eb82c98fedc729eef30521c88e105520 diff --git a/src/background/commands.background.ts b/src/background/commands.background.ts index 3542f931da..e7d31b7319 100644 --- a/src/background/commands.background.ts +++ b/src/background/commands.background.ts @@ -9,8 +9,6 @@ import { PlatformUtilsService, } from 'jslib/abstractions'; -import { UtilsService } from 'jslib/services/utils.service'; - export default class CommandsBackground { private commands: any; private isSafari: boolean; @@ -67,7 +65,7 @@ export default class CommandsBackground { const options = await this.passwordGenerationService.getOptions(); const password = await this.passwordGenerationService.generatePassword(options); - UtilsService.copyToClipboard(password); + this.platformUtilsService.copyToClipboard(password); this.passwordGenerationService.addHistory(password); this.analytics.ga('send', { diff --git a/src/background/contextMenus.background.ts b/src/background/contextMenus.background.ts index 2da29845ce..248d39590b 100644 --- a/src/background/contextMenus.background.ts +++ b/src/background/contextMenus.background.ts @@ -7,15 +7,15 @@ import { Analytics } from 'jslib/misc'; import { CipherService, PasswordGenerationService, + PlatformUtilsService, } from 'jslib/abstractions'; -import { UtilsService } from 'jslib/services/utils.service'; - export default class ContextMenusBackground { private contextMenus: any; constructor(private main: MainBackground, private cipherService: CipherService, - private passwordGenerationService: PasswordGenerationService, private analytics: Analytics) { + private passwordGenerationService: PasswordGenerationService, private analytics: Analytics, + private platformUtilsService: PlatformUtilsService) { this.contextMenus = chrome.contextMenus; } @@ -36,8 +36,8 @@ export default class ContextMenusBackground { private async generatePasswordToClipboard() { const options = await this.passwordGenerationService.getOptions(); - const password = this.passwordGenerationService.generatePassword(options); - UtilsService.copyToClipboard(password); + const password = await this.passwordGenerationService.generatePassword(options); + this.platformUtilsService.copyToClipboard(password); this.passwordGenerationService.addHistory(password); this.analytics.ga('send', { @@ -73,13 +73,13 @@ export default class ContextMenusBackground { hitType: 'event', eventAction: 'Copied Username From Context Menu', }); - UtilsService.copyToClipboard(cipher.login.username); + this.platformUtilsService.copyToClipboard(cipher.login.username); } else if (info.parentMenuItemId === 'copy-password') { this.analytics.ga('send', { hitType: 'event', eventAction: 'Copied Password From Context Menu', }); - UtilsService.copyToClipboard(cipher.login.password); + this.platformUtilsService.copyToClipboard(cipher.login.password); } break; diff --git a/src/background/main.background.ts b/src/background/main.background.ts index b3bdd30aee..8828180e10 100644 --- a/src/background/main.background.ts +++ b/src/background/main.background.ts @@ -18,7 +18,6 @@ import { TokenService, TotpService, UserService, - UtilsService, } from 'jslib/services'; import { WebCryptoFunctionService } from 'jslib/services/webCryptoFunction.service'; @@ -42,7 +41,6 @@ import { TokenService as TokenServiceAbstraction, TotpService as TotpServiceAbstraction, UserService as UserServiceAbstraction, - UtilsService as UtilsServiceAbstraction, } from 'jslib/abstractions'; import { CryptoFunctionService as CryptoFunctionServiceAbstraction } from 'jslib/abstractions/cryptoFunction.service'; @@ -72,7 +70,6 @@ export default class MainBackground { secureStorageService: StorageServiceAbstraction; i18nService: I18nServiceAbstraction; platformUtilsService: PlatformUtilsServiceAbstraction; - utilsService: UtilsServiceAbstraction; constantsService: ConstantsService; cryptoService: CryptoServiceAbstraction; tokenService: TokenServiceAbstraction; @@ -114,7 +111,6 @@ export default class MainBackground { constructor() { // Services - this.utilsService = new UtilsService(); this.messagingService = new BrowserMessagingService(); this.platformUtilsService = new BrowserPlatformUtilsService(this.messagingService); this.storageService = new BrowserStorageService(this.platformUtilsService, false); @@ -131,7 +127,7 @@ export default class MainBackground { this.userService = new UserService(this.tokenService, this.storageService); this.settingsService = new SettingsService(this.userService, this.storageService); this.cipherService = new CipherService(this.cryptoService, this.userService, this.settingsService, - this.apiService, this.storageService, this.i18nService, this.platformUtilsService, this.utilsService); + this.apiService, this.storageService, this.i18nService, this.platformUtilsService); this.folderService = new FolderService(this.cryptoService, this.userService, () => this.i18nService.t('noneFolder'), this.apiService, this.storageService, this.i18nService); this.collectionService = new CollectionService(this.cryptoService, this.userService, this.storageService, @@ -168,7 +164,7 @@ export default class MainBackground { if (!this.isSafari) { this.contextMenusBackground = new ContextMenusBackground(this, this.cipherService, - this.passwordGenerationService, this.analytics); + this.passwordGenerationService, this.analytics, this.platformUtilsService); this.idleBackground = new IdleBackground(this, this.lockService, this.storageService); this.webRequestBackground = new WebRequestBackground(this.platformUtilsService, this.cipherService); this.windowsBackground = new WindowsBackground(this); diff --git a/src/background/runtime.background.ts b/src/background/runtime.background.ts index 1b96c8be9b..e029f25405 100644 --- a/src/background/runtime.background.ts +++ b/src/background/runtime.background.ts @@ -5,7 +5,6 @@ import { LoginUriView } from 'jslib/models/view/loginUriView'; import { LoginView } from 'jslib/models/view/loginView'; import { ConstantsService } from 'jslib/services/constants.service'; -import { UtilsService } from 'jslib/services/utils.service'; import { I18nService } from 'jslib/abstractions/i18n.service'; @@ -23,6 +22,8 @@ import MainBackground from './main.background'; import { AutofillService } from '../services/abstractions/autofill.service'; import BrowserPlatformUtilsService from '../services/browserPlatformUtils.service'; +import { Utils } from 'jslib/misc/utils'; + export default class RuntimeBackground { private runtime: any; private autofillTimeout: any; @@ -200,7 +201,7 @@ export default class RuntimeBackground { loginModel.username = loginInfo.username; loginModel.password = loginInfo.password; const model = new CipherView(); - model.name = UtilsService.getHostname(loginInfo.uri) || loginInfo.domain; + model.name = Utils.getHostname(loginInfo.uri) || loginInfo.domain; model.type = CipherType.Login; model.login = loginModel; @@ -228,7 +229,7 @@ export default class RuntimeBackground { } this.main.loginsToAdd.splice(i, 1); - const hostname = UtilsService.getHostname(tab.url); + const hostname = Utils.getHostname(tab.url); await this.cipherService.saveNeverDomain(hostname); BrowserApi.tabSendMessageData(tab, 'closeNotificationBar'); } diff --git a/src/popup/services/services.module.ts b/src/popup/services/services.module.ts index 9205487030..398520b35a 100644 --- a/src/popup/services/services.module.ts +++ b/src/popup/services/services.module.ts @@ -34,7 +34,6 @@ 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 { UtilsService } from 'jslib/abstractions/utils.service'; import { AutofillService } from '../../services/abstractions/autofill.service'; import BrowserMessagingService from '../../services/browserMessaging.service'; @@ -113,7 +112,6 @@ export function initFactory(i18nService: I18nService, storageService: StorageSer { provide: TotpService, useFactory: getBgService('totpService'), deps: [] }, { provide: TokenService, useFactory: getBgService('tokenService'), deps: [] }, { provide: I18nService, useFactory: getBgService('i18nService'), deps: [] }, - { provide: UtilsService, useFactory: getBgService('utilsService'), deps: [] }, { provide: CryptoService, useFactory: getBgService('cryptoService'), deps: [] }, { provide: PlatformUtilsService, diff --git a/src/popup/vault/current-tab.component.ts b/src/popup/vault/current-tab.component.ts index c36ea4f6b5..b2cca4262b 100644 --- a/src/popup/vault/current-tab.component.ts +++ b/src/popup/vault/current-tab.component.ts @@ -22,12 +22,13 @@ import { CipherService } from 'jslib/abstractions/cipher.service'; import { I18nService } from 'jslib/abstractions/i18n.service'; import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service'; import { SyncService } from 'jslib/abstractions/sync.service'; -import { UtilsService } from 'jslib/abstractions/utils.service'; import { AutofillService } from '../../services/abstractions/autofill.service'; import { PopupUtilsService } from '../services/popup-utils.service'; +import { Utils } from 'jslib/misc/utils'; + const BroadcasterSubscriptionId = 'CurrentTabComponent'; @Component({ @@ -55,8 +56,7 @@ export class CurrentTabComponent implements OnInit, OnDestroy { private analytics: Angulartics2, private toasterService: ToasterService, private i18nService: I18nService, private router: Router, private ngZone: NgZone, private broadcasterService: BroadcasterService, - private changeDetectorRef: ChangeDetectorRef, private syncService: SyncService, - private utilsService: UtilsService) { } + private changeDetectorRef: ChangeDetectorRef, private syncService: SyncService) { } async ngOnInit() { this.showLeftHeader = !this.platformUtilsService.isSafari(); @@ -187,7 +187,7 @@ export class CurrentTabComponent implements OnInit, OnDestroy { return; } - this.hostname = this.utilsService.getHostname(this.url); + this.hostname = Utils.getHostname(this.url); BrowserApi.tabSendMessage(tab, { command: 'collectPageDetails', tab: tab, diff --git a/src/services/autofill.service.ts b/src/services/autofill.service.ts index ebca28e563..07d3cd4933 100644 --- a/src/services/autofill.service.ts +++ b/src/services/autofill.service.ts @@ -9,8 +9,6 @@ import AutofillScript from '../models/autofillScript'; import { BrowserApi } from '../browser/browserApi'; -import { UtilsService } from 'jslib/services'; - import { AutofillService as AutofillServiceInterface } from './abstractions/autofill.service'; import { @@ -18,7 +16,6 @@ import { PlatformUtilsService, TokenService, TotpService, - UtilsService as UtilsServiceAbstraction, } from 'jslib/abstractions'; const CardAttributes: string[] = ['autoCompleteType', 'data-stripe', 'htmlName', 'htmlID', 'label-tag', diff --git a/src/services/browserPlatformUtils.service.ts b/src/services/browserPlatformUtils.service.ts index 70c1d3b2d8..62014c73a3 100644 --- a/src/services/browserPlatformUtils.service.ts +++ b/src/services/browserPlatformUtils.service.ts @@ -7,8 +7,6 @@ import { DeviceType } from 'jslib/enums/deviceType'; import { MessagingService } from 'jslib/abstractions/messaging.service'; import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service'; -import { UtilsService } from 'jslib/services/utils.service'; - const AnalyticsIds = { [DeviceType.Chrome]: 'UA-81915606-6', [DeviceType.Firefox]: 'UA-81915606-7', @@ -194,8 +192,28 @@ export default class BrowserPlatformUtilsService implements PlatformUtilsService } copyToClipboard(text: string, options?: any): void { - const doc = options ? options.doc : null; - UtilsService.copyToClipboard(text, doc); + const doc = options ? options.doc : window.document; + if ((window as any).clipboardData && (window as any).clipboardData.setData) { + // IE specific code path to prevent textarea being shown while dialog is visible. + (window as any).clipboardData.setData('Text', text); + } else if (doc.queryCommandSupported && doc.queryCommandSupported('copy')) { + const textarea = doc.createElement('textarea'); + textarea.textContent = text; + // Prevent scrolling to bottom of page in MS Edge. + textarea.style.position = 'fixed'; + doc.body.appendChild(textarea); + textarea.select(); + + try { + // Security exception may be thrown by some browsers. + doc.execCommand('copy'); + } catch (e) { + // tslint:disable-next-line + console.warn('Copy to clipboard failed.', e); + } finally { + doc.body.removeChild(textarea); + } + } } resolveDialogPromise(dialogId: number, confirmed: boolean) {