1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-12-22 16:29:09 +01:00

process messages in popup

This commit is contained in:
Kyle Spearrin 2018-04-06 15:33:20 -04:00
parent 1a9cbb03e0
commit 94a79c80db
6 changed files with 209 additions and 23 deletions

View File

@ -1,4 +1,10 @@
import { Component } from '@angular/core'; import {
ChangeDetectorRef,
Component,
NgZone,
OnDestroy,
OnInit,
} from '@angular/core';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
@ -16,8 +22,12 @@ 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 { BroadcasterService } from 'jslib/angular/services/broadcaster.service';
import { TwoFactorComponent as BaseTwoFactorComponent } from 'jslib/angular/components/two-factor.component'; import { TwoFactorComponent as BaseTwoFactorComponent } from 'jslib/angular/components/two-factor.component';
const BroadcasterSubscriptionId = 'TwoFactorComponent';
@Component({ @Component({
selector: 'app-two-factor', selector: 'app-two-factor',
templateUrl: 'two-factor.component.html', templateUrl: 'two-factor.component.html',
@ -29,13 +39,30 @@ export class TwoFactorComponent extends BaseTwoFactorComponent {
analytics: Angulartics2, toasterService: ToasterService, analytics: Angulartics2, toasterService: ToasterService,
i18nService: I18nService, apiService: ApiService, i18nService: I18nService, apiService: ApiService,
platformUtilsService: PlatformUtilsService, syncService: SyncService, platformUtilsService: PlatformUtilsService, syncService: SyncService,
environmentService: EnvironmentService) { environmentService: EnvironmentService, private ngZone: NgZone,
private broadcasterService: BroadcasterService, private changeDetectorRef: ChangeDetectorRef) {
super(authService, router, analytics, toasterService, i18nService, apiService, super(authService, router, analytics, toasterService, i18nService, apiService,
platformUtilsService, syncService, window, environmentService); platformUtilsService, syncService, window, environmentService);
this.successRoute = '/tabs/vault'; this.successRoute = '/tabs/vault';
} }
async ngOnInit() { async ngOnInit() {
this.broadcasterService.subscribe(BroadcasterSubscriptionId, (message: any) => {
this.ngZone.run(async () => {
switch (message.command) {
case '2faPageResponse':
if (message.type === 'duo') {
this.token = message.data.sigValue;
this.submitWithTab(message.webExtSender.tab);
}
default:
break;
}
this.changeDetectorRef.detectChanges();
})
});
this.showNewWindowMessage = this.platformUtilsService.isSafari(); this.showNewWindowMessage = this.platformUtilsService.isSafari();
await super.ngOnInit(); await super.ngOnInit();
@ -62,11 +89,26 @@ export class TwoFactorComponent extends BaseTwoFactorComponent {
} }
}); });
}, 500); }, 500);
}
// TODO: listen for duo data message response ngOnDestroy() {
this.broadcasterService.unsubscribe(BroadcasterSubscriptionId);
super.ngOnDestroy();
} }
anotherMethod() { anotherMethod() {
this.router.navigate(['2fa-options']); this.router.navigate(['2fa-options']);
} }
async submitWithTab(sendSuccessToTab: any) {
await super.submit();
if (sendSuccessToTab != null) {
window.setTimeout(() => {
BrowserApi.tabSendMessage(sendSuccessToTab, {
command: '2faPageData',
data: { type: 'success' }
});
}, 1000);
}
}
} }

View File

@ -6,19 +6,23 @@ import { Angulartics2GoogleAnalytics } from 'angulartics2/ga';
import { import {
Component, Component,
ComponentFactoryResolver,
NgZone,
OnDestroy,
OnInit, OnInit,
Type,
ViewChild,
ViewContainerRef,
} from '@angular/core'; } from '@angular/core';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { ToasterService } from 'angular2-toaster'; import { ToasterService } from 'angular2-toaster';
import { Angulartics2 } from 'angulartics2'; import { Angulartics2 } from 'angulartics2';
import { BrowserApi } from '../browser/browserApi';
import { BroadcasterService } from 'jslib/angular/services/broadcaster.service';
import { AuthService } from 'jslib/abstractions/auth.service';
import { I18nService } from 'jslib/abstractions/i18n.service';
import { StorageService } from 'jslib/abstractions/storage.service';
import { ConstantsService } from 'jslib/services/constants.service';
@Component({ @Component({
selector: 'app-root', selector: 'app-root',
styles: [], styles: [],
@ -26,7 +30,7 @@ import { Angulartics2 } from 'angulartics2';
<toaster-container [toasterconfig]="toasterConfig"></toaster-container> <toaster-container [toasterconfig]="toasterConfig"></toaster-container>
<router-outlet></router-outlet>`, <router-outlet></router-outlet>`,
}) })
export class AppComponent { export class AppComponent implements OnInit {
toasterConfig: ToasterConfig = new ToasterConfig({ toasterConfig: ToasterConfig = new ToasterConfig({
showCloseButton: true, showCloseButton: true,
mouseoverTimerStop: true, mouseoverTimerStop: true,
@ -36,6 +40,47 @@ export class AppComponent {
newestOnTop: false newestOnTop: false
}); });
private lastActivity: number = null;
constructor(private angulartics2GoogleAnalytics: Angulartics2GoogleAnalytics, private analytics: Angulartics2, constructor(private angulartics2GoogleAnalytics: Angulartics2GoogleAnalytics, private analytics: Angulartics2,
private toasterService: ToasterService) { } private toasterService: ToasterService, private storageService: StorageService,
private broadcasterService: BroadcasterService, private authService: AuthService,
private i18nService: I18nService, private router: Router) { }
ngOnInit() {
window.onmousemove = () => this.recordActivity();
window.onmousedown = () => this.recordActivity();
window.ontouchstart = () => this.recordActivity();
window.onclick = () => this.recordActivity();
window.onscroll = () => this.recordActivity();
window.onkeypress = () => this.recordActivity();
(window as any).bitwardenPopupMainMessageListener = (msg: any, sender: any, sendResponse: any) => {
if (msg.command === 'doneLoggingOut') {
this.authService.logOut(() => {
this.analytics.eventTrack.next({ action: 'Logged Out' });
if (msg.expired) {
this.toasterService.popAsync('warning', this.i18nService.t('loggedOut'),
this.i18nService.t('loginExpired'));
}
this.router.navigate(['home']);
});
} else {
msg.webExtSender = sender;
this.broadcasterService.send(msg);
}
};
BrowserApi.messageListener((window as any).bitwardenPopupMainMessageListener);
}
private async recordActivity() {
const now = (new Date()).getTime();
if (this.lastActivity != null && now - this.lastActivity < 250) {
return;
}
this.lastActivity = now;
this.storageService.save(ConstantsService.lastActiveKey, now);
}
} }

View File

@ -6,6 +6,7 @@ import {
import { ToasterModule } from 'angular2-toaster'; import { ToasterModule } from 'angular2-toaster';
import { AuthGuardService } from 'jslib/angular/services/auth-guard.service'; import { AuthGuardService } from 'jslib/angular/services/auth-guard.service';
import { BroadcasterService } from 'jslib/angular/services/broadcaster.service';
import { ValidationService } from 'jslib/angular/services/validation.service'; import { ValidationService } from 'jslib/angular/services/validation.service';
import { BrowserApi } from '../../browser/browserApi'; import { BrowserApi } from '../../browser/browserApi';
@ -79,6 +80,7 @@ export function initFactory(i18nService: I18nService, storageService: StorageSer
ValidationService, ValidationService,
AuthGuardService, AuthGuardService,
PopupUtilsService, PopupUtilsService,
BroadcasterService,
{ provide: MessagingService, useValue: messagingService }, { provide: MessagingService, useValue: messagingService },
{ provide: AuthServiceAbstraction, useValue: authService }, { provide: AuthServiceAbstraction, useValue: authService },
{ provide: StateServiceAbstraction, useValue: stateService }, { provide: StateServiceAbstraction, useValue: stateService },

View File

@ -1,6 +1,9 @@
import { Location } from '@angular/common'; import { Location } from '@angular/common';
import { import {
ChangeDetectorRef,
Component, Component,
NgZone,
OnDestroy,
OnInit, OnInit,
} from '@angular/core'; } from '@angular/core';
import { import {
@ -12,15 +15,21 @@ import { CipherService } from 'jslib/abstractions/cipher.service';
import { CipherView } from 'jslib/models/view/cipherView'; import { CipherView } from 'jslib/models/view/cipherView';
import { BroadcasterService } from 'jslib/angular/services/broadcaster.service';
import { CiphersComponent as BaseCiphersComponent } from 'jslib/angular/components/ciphers.component'; import { CiphersComponent as BaseCiphersComponent } from 'jslib/angular/components/ciphers.component';
const BroadcasterSubscriptionId = 'CiphersComponent';
@Component({ @Component({
selector: 'app-vault-ciphers', selector: 'app-vault-ciphers',
templateUrl: 'ciphers.component.html', templateUrl: 'ciphers.component.html',
}) })
export class CiphersComponent extends BaseCiphersComponent implements OnInit { export class CiphersComponent extends BaseCiphersComponent implements OnInit, OnDestroy {
constructor(cipherService: CipherService, private route: ActivatedRoute, constructor(cipherService: CipherService, private route: ActivatedRoute,
private router: Router, private location: Location) { private router: Router, private location: Location,
private ngZone: NgZone, private broadcasterService: BroadcasterService,
private changeDetectorRef: ChangeDetectorRef) {
super(cipherService); super(cipherService);
} }
@ -37,6 +46,26 @@ export class CiphersComponent extends BaseCiphersComponent implements OnInit {
await super.load(); await super.load();
} }
}); });
this.broadcasterService.subscribe(BroadcasterSubscriptionId, (message: any) => {
this.ngZone.run(async () => {
switch (message.command) {
case 'syncCompleted':
window.setTimeout(() => {
this.load();
}, 500);
break;
default:
break;
}
this.changeDetectorRef.detectChanges();
})
});
}
ngOnDestroy() {
this.broadcasterService.unsubscribe(BroadcasterSubscriptionId);
} }
selectCipher(cipher: CipherView) { selectCipher(cipher: CipherView) {

View File

@ -1,12 +1,9 @@
import { import {
ChangeDetectorRef,
Component, Component,
ComponentFactoryResolver,
NgZone, NgZone,
OnDestroy, OnDestroy,
OnInit, OnInit,
Type,
ViewChild,
ViewContainerRef,
} from '@angular/core'; } from '@angular/core';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
@ -15,6 +12,8 @@ import { Angulartics2 } from 'angulartics2';
import { BrowserApi } from '../../browser/browserApi'; import { BrowserApi } from '../../browser/browserApi';
import { BroadcasterService } from 'jslib/angular/services/broadcaster.service';
import { CipherType } from 'jslib/enums/cipherType'; import { CipherType } from 'jslib/enums/cipherType';
import { CipherView } from 'jslib/models/view/cipherView'; import { CipherView } from 'jslib/models/view/cipherView';
@ -26,12 +25,15 @@ import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.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 { setTimeout } from 'timers';
const BroadcasterSubscriptionId = 'CurrentTabComponent';
@Component({ @Component({
selector: 'app-current-tab', selector: 'app-current-tab',
templateUrl: 'current-tab.component.html', templateUrl: 'current-tab.component.html',
}) })
export class CurrentTabComponent implements OnInit { export class CurrentTabComponent implements OnInit, OnDestroy {
pageDetails: any[] = []; pageDetails: any[] = [];
cardCiphers: CipherView[]; cardCiphers: CipherView[];
identityCiphers: CipherView[]; identityCiphers: CipherView[];
@ -48,16 +50,49 @@ export class CurrentTabComponent implements OnInit {
constructor(private platformUtilsService: PlatformUtilsService, private cipherService: CipherService, constructor(private platformUtilsService: PlatformUtilsService, private cipherService: CipherService,
private popupUtilsService: PopupUtilsService, private autofillService: AutofillService, private popupUtilsService: PopupUtilsService, private autofillService: AutofillService,
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 changeDetectorRef: ChangeDetectorRef) {
this.inSidebar = popupUtilsService.inSidebar(window); this.inSidebar = popupUtilsService.inSidebar(window);
this.showPopout = !this.inSidebar && !platformUtilsService.isSafari(); this.showPopout = !this.inSidebar && !platformUtilsService.isSafari();
this.disableSearch = platformUtilsService.isEdge(); this.disableSearch = platformUtilsService.isEdge();
} }
ngOnInit() { ngOnInit() {
this.broadcasterService.subscribe(BroadcasterSubscriptionId, (message: any) => {
this.ngZone.run(async () => {
switch (message.command) {
case 'syncCompleted':
if (this.loaded) {
setTimeout(() => {
this.load();
}, 500);
}
break;
case 'collectPageDetailsResponse':
if (message.sender === BroadcasterSubscriptionId) {
this.pageDetails.push({
frameId: message.webExtSender.frameId,
tab: message.tab,
details: message.details,
});
}
break;
default:
break;
}
this.changeDetectorRef.detectChanges();
})
});
this.load(); this.load();
} }
ngOnDestroy() {
this.broadcasterService.unsubscribe(BroadcasterSubscriptionId);
}
async refresh() { async refresh() {
await this.load(); await this.load();
} }
@ -117,7 +152,7 @@ export class CurrentTabComponent implements OnInit {
BrowserApi.tabSendMessage(tab, { BrowserApi.tabSendMessage(tab, {
command: 'collectPageDetails', command: 'collectPageDetails',
tab: tab, tab: tab,
sender: 'currentController', sender: BroadcasterSubscriptionId,
}).then(() => { }).then(() => {
this.canAutofill = true; this.canAutofill = true;
}); });

View File

@ -1,5 +1,8 @@
import { import {
ChangeDetectorRef,
Component, Component,
NgZone,
OnDestroy,
OnInit, OnInit,
} from '@angular/core'; } from '@angular/core';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
@ -14,13 +17,17 @@ import { CollectionService } from 'jslib/abstractions/collection.service';
import { CipherService } from 'jslib/abstractions/cipher.service'; import { CipherService } from 'jslib/abstractions/cipher.service';
import { FolderService } from 'jslib/abstractions/folder.service'; import { FolderService } from 'jslib/abstractions/folder.service';
import { BroadcasterService } from 'jslib/angular/services/broadcaster.service';
import { GroupingsComponent as BaseGroupingsComponent } from 'jslib/angular/components/groupings.component'; import { GroupingsComponent as BaseGroupingsComponent } from 'jslib/angular/components/groupings.component';
const BroadcasterSubscriptionId = 'GroupingsComponent';
@Component({ @Component({
selector: 'app-vault-groupings', selector: 'app-vault-groupings',
templateUrl: 'groupings.component.html', templateUrl: 'groupings.component.html',
}) })
export class GroupingsComponent extends BaseGroupingsComponent implements OnInit { export class GroupingsComponent extends BaseGroupingsComponent implements OnInit, OnDestroy {
ciphers: CipherView[]; ciphers: CipherView[];
favoriteCiphers: CipherView[]; favoriteCiphers: CipherView[];
noFolderCiphers: CipherView[]; noFolderCiphers: CipherView[];
@ -31,11 +38,37 @@ export class GroupingsComponent extends BaseGroupingsComponent implements OnInit
showNoFolderCiphers = false; showNoFolderCiphers = false;
constructor(collectionService: CollectionService, folderService: FolderService, constructor(collectionService: CollectionService, folderService: FolderService,
private cipherService: CipherService, private router: Router) { private cipherService: CipherService, private router: Router,
private ngZone: NgZone, private broadcasterService: BroadcasterService,
private changeDetectorRef: ChangeDetectorRef) {
super(collectionService, folderService); super(collectionService, folderService);
} }
async ngOnInit() { ngOnInit() {
this.broadcasterService.subscribe(BroadcasterSubscriptionId, (message: any) => {
this.ngZone.run(async () => {
switch (message.command) {
case 'syncCompleted':
window.setTimeout(() => {
this.load();
}, 500);
break;
default:
break;
}
this.changeDetectorRef.detectChanges();
})
});
this.load();
}
ngOnDestroy() {
this.broadcasterService.unsubscribe(BroadcasterSubscriptionId);
}
async load() {
await super.load(); await super.load();
super.loaded = false; super.loaded = false;