1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-09-18 02:41:15 +02:00

refactor utils service to utils

This commit is contained in:
Kyle Spearrin 2018-04-23 13:04:11 -04:00
parent 97835f7627
commit c67b63a452
9 changed files with 41 additions and 33 deletions

2
jslib

@ -1 +1 @@
Subproject commit 5e7115f78d8a87b241a43fdcd6d53b67ccf0b605 Subproject commit 0fa9fc58eb82c98fedc729eef30521c88e105520

View File

@ -9,8 +9,6 @@ import {
PlatformUtilsService, PlatformUtilsService,
} from 'jslib/abstractions'; } from 'jslib/abstractions';
import { UtilsService } from 'jslib/services/utils.service';
export default class CommandsBackground { export default class CommandsBackground {
private commands: any; private commands: any;
private isSafari: boolean; private isSafari: boolean;
@ -67,7 +65,7 @@ export default class CommandsBackground {
const options = await this.passwordGenerationService.getOptions(); const options = await this.passwordGenerationService.getOptions();
const password = await this.passwordGenerationService.generatePassword(options); const password = await this.passwordGenerationService.generatePassword(options);
UtilsService.copyToClipboard(password); this.platformUtilsService.copyToClipboard(password);
this.passwordGenerationService.addHistory(password); this.passwordGenerationService.addHistory(password);
this.analytics.ga('send', { this.analytics.ga('send', {

View File

@ -7,15 +7,15 @@ import { Analytics } from 'jslib/misc';
import { import {
CipherService, CipherService,
PasswordGenerationService, PasswordGenerationService,
PlatformUtilsService,
} from 'jslib/abstractions'; } from 'jslib/abstractions';
import { UtilsService } from 'jslib/services/utils.service';
export default class ContextMenusBackground { export default class ContextMenusBackground {
private contextMenus: any; private contextMenus: any;
constructor(private main: MainBackground, private cipherService: CipherService, 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; this.contextMenus = chrome.contextMenus;
} }
@ -36,8 +36,8 @@ export default class ContextMenusBackground {
private async generatePasswordToClipboard() { private async generatePasswordToClipboard() {
const options = await this.passwordGenerationService.getOptions(); const options = await this.passwordGenerationService.getOptions();
const password = this.passwordGenerationService.generatePassword(options); const password = await this.passwordGenerationService.generatePassword(options);
UtilsService.copyToClipboard(password); this.platformUtilsService.copyToClipboard(password);
this.passwordGenerationService.addHistory(password); this.passwordGenerationService.addHistory(password);
this.analytics.ga('send', { this.analytics.ga('send', {
@ -73,13 +73,13 @@ export default class ContextMenusBackground {
hitType: 'event', hitType: 'event',
eventAction: 'Copied Username From Context Menu', eventAction: 'Copied Username From Context Menu',
}); });
UtilsService.copyToClipboard(cipher.login.username); this.platformUtilsService.copyToClipboard(cipher.login.username);
} else if (info.parentMenuItemId === 'copy-password') { } else if (info.parentMenuItemId === 'copy-password') {
this.analytics.ga('send', { this.analytics.ga('send', {
hitType: 'event', hitType: 'event',
eventAction: 'Copied Password From Context Menu', eventAction: 'Copied Password From Context Menu',
}); });
UtilsService.copyToClipboard(cipher.login.password); this.platformUtilsService.copyToClipboard(cipher.login.password);
} }
break; break;

View File

@ -18,7 +18,6 @@ import {
TokenService, TokenService,
TotpService, TotpService,
UserService, UserService,
UtilsService,
} from 'jslib/services'; } from 'jslib/services';
import { WebCryptoFunctionService } from 'jslib/services/webCryptoFunction.service'; import { WebCryptoFunctionService } from 'jslib/services/webCryptoFunction.service';
@ -42,7 +41,6 @@ import {
TokenService as TokenServiceAbstraction, TokenService as TokenServiceAbstraction,
TotpService as TotpServiceAbstraction, TotpService as TotpServiceAbstraction,
UserService as UserServiceAbstraction, UserService as UserServiceAbstraction,
UtilsService as UtilsServiceAbstraction,
} from 'jslib/abstractions'; } from 'jslib/abstractions';
import { CryptoFunctionService as CryptoFunctionServiceAbstraction } from 'jslib/abstractions/cryptoFunction.service'; import { CryptoFunctionService as CryptoFunctionServiceAbstraction } from 'jslib/abstractions/cryptoFunction.service';
@ -72,7 +70,6 @@ export default class MainBackground {
secureStorageService: StorageServiceAbstraction; secureStorageService: StorageServiceAbstraction;
i18nService: I18nServiceAbstraction; i18nService: I18nServiceAbstraction;
platformUtilsService: PlatformUtilsServiceAbstraction; platformUtilsService: PlatformUtilsServiceAbstraction;
utilsService: UtilsServiceAbstraction;
constantsService: ConstantsService; constantsService: ConstantsService;
cryptoService: CryptoServiceAbstraction; cryptoService: CryptoServiceAbstraction;
tokenService: TokenServiceAbstraction; tokenService: TokenServiceAbstraction;
@ -114,7 +111,6 @@ export default class MainBackground {
constructor() { constructor() {
// Services // Services
this.utilsService = new UtilsService();
this.messagingService = new BrowserMessagingService(); this.messagingService = new BrowserMessagingService();
this.platformUtilsService = new BrowserPlatformUtilsService(this.messagingService); this.platformUtilsService = new BrowserPlatformUtilsService(this.messagingService);
this.storageService = new BrowserStorageService(this.platformUtilsService, false); this.storageService = new BrowserStorageService(this.platformUtilsService, false);
@ -131,7 +127,7 @@ export default class MainBackground {
this.userService = new UserService(this.tokenService, this.storageService); this.userService = new UserService(this.tokenService, this.storageService);
this.settingsService = new SettingsService(this.userService, this.storageService); this.settingsService = new SettingsService(this.userService, this.storageService);
this.cipherService = new CipherService(this.cryptoService, this.userService, this.settingsService, 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.folderService = new FolderService(this.cryptoService, this.userService,
() => this.i18nService.t('noneFolder'), this.apiService, this.storageService, this.i18nService); () => this.i18nService.t('noneFolder'), this.apiService, this.storageService, this.i18nService);
this.collectionService = new CollectionService(this.cryptoService, this.userService, this.storageService, this.collectionService = new CollectionService(this.cryptoService, this.userService, this.storageService,
@ -168,7 +164,7 @@ export default class MainBackground {
if (!this.isSafari) { if (!this.isSafari) {
this.contextMenusBackground = new ContextMenusBackground(this, this.cipherService, 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.idleBackground = new IdleBackground(this, this.lockService, this.storageService);
this.webRequestBackground = new WebRequestBackground(this.platformUtilsService, this.cipherService); this.webRequestBackground = new WebRequestBackground(this.platformUtilsService, this.cipherService);
this.windowsBackground = new WindowsBackground(this); this.windowsBackground = new WindowsBackground(this);

View File

@ -5,7 +5,6 @@ import { LoginUriView } from 'jslib/models/view/loginUriView';
import { LoginView } from 'jslib/models/view/loginView'; import { LoginView } from 'jslib/models/view/loginView';
import { ConstantsService } from 'jslib/services/constants.service'; import { ConstantsService } from 'jslib/services/constants.service';
import { UtilsService } from 'jslib/services/utils.service';
import { I18nService } from 'jslib/abstractions/i18n.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 { AutofillService } from '../services/abstractions/autofill.service';
import BrowserPlatformUtilsService from '../services/browserPlatformUtils.service'; import BrowserPlatformUtilsService from '../services/browserPlatformUtils.service';
import { Utils } from 'jslib/misc/utils';
export default class RuntimeBackground { export default class RuntimeBackground {
private runtime: any; private runtime: any;
private autofillTimeout: any; private autofillTimeout: any;
@ -200,7 +201,7 @@ export default class RuntimeBackground {
loginModel.username = loginInfo.username; loginModel.username = loginInfo.username;
loginModel.password = loginInfo.password; loginModel.password = loginInfo.password;
const model = new CipherView(); 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.type = CipherType.Login;
model.login = loginModel; model.login = loginModel;
@ -228,7 +229,7 @@ export default class RuntimeBackground {
} }
this.main.loginsToAdd.splice(i, 1); this.main.loginsToAdd.splice(i, 1);
const hostname = UtilsService.getHostname(tab.url); const hostname = Utils.getHostname(tab.url);
await this.cipherService.saveNeverDomain(hostname); await this.cipherService.saveNeverDomain(hostname);
BrowserApi.tabSendMessageData(tab, 'closeNotificationBar'); BrowserApi.tabSendMessageData(tab, 'closeNotificationBar');
} }

View File

@ -34,7 +34,6 @@ import { SyncService } from 'jslib/abstractions/sync.service';
import { TokenService } from 'jslib/abstractions/token.service'; import { TokenService } from 'jslib/abstractions/token.service';
import { TotpService } from 'jslib/abstractions/totp.service'; import { TotpService } from 'jslib/abstractions/totp.service';
import { UserService } from 'jslib/abstractions/user.service'; import { UserService } from 'jslib/abstractions/user.service';
import { UtilsService } from 'jslib/abstractions/utils.service';
import { AutofillService } from '../../services/abstractions/autofill.service'; import { AutofillService } from '../../services/abstractions/autofill.service';
import BrowserMessagingService from '../../services/browserMessaging.service'; import BrowserMessagingService from '../../services/browserMessaging.service';
@ -113,7 +112,6 @@ export function initFactory(i18nService: I18nService, storageService: StorageSer
{ provide: TotpService, useFactory: getBgService<TotpService>('totpService'), deps: [] }, { provide: TotpService, useFactory: getBgService<TotpService>('totpService'), deps: [] },
{ provide: TokenService, useFactory: getBgService<TokenService>('tokenService'), deps: [] }, { provide: TokenService, useFactory: getBgService<TokenService>('tokenService'), deps: [] },
{ provide: I18nService, useFactory: getBgService<I18nService>('i18nService'), deps: [] }, { provide: I18nService, useFactory: getBgService<I18nService>('i18nService'), deps: [] },
{ provide: UtilsService, useFactory: getBgService<UtilsService>('utilsService'), deps: [] },
{ provide: CryptoService, useFactory: getBgService<CryptoService>('cryptoService'), deps: [] }, { provide: CryptoService, useFactory: getBgService<CryptoService>('cryptoService'), deps: [] },
{ {
provide: PlatformUtilsService, provide: PlatformUtilsService,

View File

@ -22,12 +22,13 @@ import { CipherService } from 'jslib/abstractions/cipher.service';
import { I18nService } from 'jslib/abstractions/i18n.service'; import { I18nService } from 'jslib/abstractions/i18n.service';
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service'; import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
import { SyncService } from 'jslib/abstractions/sync.service'; import { SyncService } from 'jslib/abstractions/sync.service';
import { UtilsService } from 'jslib/abstractions/utils.service';
import { AutofillService } from '../../services/abstractions/autofill.service'; import { AutofillService } from '../../services/abstractions/autofill.service';
import { PopupUtilsService } from '../services/popup-utils.service'; import { PopupUtilsService } from '../services/popup-utils.service';
import { Utils } from 'jslib/misc/utils';
const BroadcasterSubscriptionId = 'CurrentTabComponent'; const BroadcasterSubscriptionId = 'CurrentTabComponent';
@Component({ @Component({
@ -55,8 +56,7 @@ export class CurrentTabComponent implements OnInit, OnDestroy {
private analytics: Angulartics2, private toasterService: ToasterService, private analytics: Angulartics2, private toasterService: ToasterService,
private i18nService: I18nService, private router: Router, private i18nService: I18nService, private router: Router,
private ngZone: NgZone, private broadcasterService: BroadcasterService, private ngZone: NgZone, private broadcasterService: BroadcasterService,
private changeDetectorRef: ChangeDetectorRef, private syncService: SyncService, private changeDetectorRef: ChangeDetectorRef, private syncService: SyncService) { }
private utilsService: UtilsService) { }
async ngOnInit() { async ngOnInit() {
this.showLeftHeader = !this.platformUtilsService.isSafari(); this.showLeftHeader = !this.platformUtilsService.isSafari();
@ -187,7 +187,7 @@ export class CurrentTabComponent implements OnInit, OnDestroy {
return; return;
} }
this.hostname = this.utilsService.getHostname(this.url); this.hostname = Utils.getHostname(this.url);
BrowserApi.tabSendMessage(tab, { BrowserApi.tabSendMessage(tab, {
command: 'collectPageDetails', command: 'collectPageDetails',
tab: tab, tab: tab,

View File

@ -9,8 +9,6 @@ import AutofillScript from '../models/autofillScript';
import { BrowserApi } from '../browser/browserApi'; import { BrowserApi } from '../browser/browserApi';
import { UtilsService } from 'jslib/services';
import { AutofillService as AutofillServiceInterface } from './abstractions/autofill.service'; import { AutofillService as AutofillServiceInterface } from './abstractions/autofill.service';
import { import {
@ -18,7 +16,6 @@ import {
PlatformUtilsService, PlatformUtilsService,
TokenService, TokenService,
TotpService, TotpService,
UtilsService as UtilsServiceAbstraction,
} from 'jslib/abstractions'; } from 'jslib/abstractions';
const CardAttributes: string[] = ['autoCompleteType', 'data-stripe', 'htmlName', 'htmlID', 'label-tag', const CardAttributes: string[] = ['autoCompleteType', 'data-stripe', 'htmlName', 'htmlID', 'label-tag',

View File

@ -7,8 +7,6 @@ import { DeviceType } from 'jslib/enums/deviceType';
import { MessagingService } from 'jslib/abstractions/messaging.service'; import { MessagingService } from 'jslib/abstractions/messaging.service';
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service'; import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
import { UtilsService } from 'jslib/services/utils.service';
const AnalyticsIds = { const AnalyticsIds = {
[DeviceType.Chrome]: 'UA-81915606-6', [DeviceType.Chrome]: 'UA-81915606-6',
[DeviceType.Firefox]: 'UA-81915606-7', [DeviceType.Firefox]: 'UA-81915606-7',
@ -194,8 +192,28 @@ export default class BrowserPlatformUtilsService implements PlatformUtilsService
} }
copyToClipboard(text: string, options?: any): void { copyToClipboard(text: string, options?: any): void {
const doc = options ? options.doc : null; const doc = options ? options.doc : window.document;
UtilsService.copyToClipboard(text, doc); 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) { resolveDialogPromise(dialogId: number, confirmed: boolean) {