1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-01-29 22:31:29 +01:00

set badge for all windows when locked

This commit is contained in:
Kyle Spearrin 2018-03-03 22:48:38 -05:00
parent f952cd5642
commit 5ef72091db
3 changed files with 61 additions and 31 deletions

View File

@ -135,7 +135,10 @@ export default class MainBackground {
this.collectionService = new CollectionService(this.cryptoService, this.userService, this.storageService, this.collectionService = new CollectionService(this.cryptoService, this.userService, this.storageService,
this.i18n2Service); this.i18n2Service);
this.lockService = new LockService(this.cipherService, this.folderService, this.collectionService, this.lockService = new LockService(this.cipherService, this.folderService, this.collectionService,
this.cryptoService, this.platformUtilsService, this.storageService, this.messagingService); this.cryptoService, this.platformUtilsService, this.storageService, this.messagingService, async () => {
await this.setIcon();
await this.refreshBadgeAndMenu(true);
});
this.syncService = new SyncService(this.userService, this.apiService, this.settingsService, this.syncService = new SyncService(this.userService, this.apiService, this.settingsService,
this.folderService, this.cipherService, this.cryptoService, this.collectionService, this.folderService, this.cipherService, this.cryptoService, this.collectionService,
this.storageService, this.messagingService, (expired: boolean) => this.logout(expired)); this.storageService, this.messagingService, (expired: boolean) => this.logout(expired));
@ -214,23 +217,27 @@ export default class MainBackground {
await this.actionSetIcon(this.sidebarAction, suffix); await this.actionSetIcon(this.sidebarAction, suffix);
} }
async refreshBadgeAndMenu() { async refreshBadgeAndMenu(forLocked: boolean = false) {
if (this.isSafari || !chrome.windows || !chrome.contextMenus) { if (this.isSafari || !chrome.windows || !chrome.contextMenus) {
return; return;
} }
const tab = await BrowserApi.getTabFromCurrentWindowId(); const menuDisabled = await this.storageService.get<boolean>(ConstantsService.disableContextMenuItemKey);
if (!tab) { if (!menuDisabled) {
await this.buildContextMenu();
} else {
await this.contextMenusRemoveAll();
}
if (forLocked) {
await this.loadMenuAndUpdateBadgeForLockedState(!menuDisabled);
this.onUpdatedRan = this.onReplacedRan = false;
return; return;
} }
const disabled = await this.storageService.get<boolean>(ConstantsService.disableContextMenuItemKey); const tab = await BrowserApi.getTabFromCurrentWindow();
if (!disabled) { if (tab) {
await this.buildContextMenu(); await this.contextMenuReady(tab, !menuDisabled);
await this.contextMenuReady(tab, true);
} else {
await this.contextMenusRemoveAll();
await this.contextMenuReady(tab, false);
} }
} }
@ -406,11 +413,23 @@ export default class MainBackground {
this.browserActionSetBadgeText(theText, tabId); this.browserActionSetBadgeText(theText, tabId);
this.sidebarActionSetBadgeText(theText, tabId); this.sidebarActionSetBadgeText(theText, tabId);
} catch (e) { } catch (e) {
if (contextMenuEnabled) { await this.loadMenuAndUpdateBadgeForLockedState(contextMenuEnabled);
await this.loadNoLoginsContextMenuOptions(this.i18nService.vaultLocked); }
} }
this.browserActionSetBadgeText('', tabId);
this.sidebarActionSetBadgeText('', tabId); private async loadMenuAndUpdateBadgeForLockedState(contextMenuEnabled: boolean) {
if (contextMenuEnabled) {
await this.loadNoLoginsContextMenuOptions(this.i18nService.vaultLocked);
}
const tabs = await BrowserApi.getActiveTabs();
if (tabs != null) {
tabs.forEach((tab) => {
if (tab.id != null) {
this.browserActionSetBadgeText('', tab.id);
this.sidebarActionSetBadgeText('', tab.id);
}
});
} }
} }

View File

@ -77,7 +77,7 @@ export default class RuntimeBackground {
case 'unlocked': case 'unlocked':
case 'locked': case 'locked':
await this.main.setIcon(); await this.main.setIcon();
await this.main.refreshBadgeAndMenu(); await this.main.refreshBadgeAndMenu(msg.command === 'locked');
break; break;
case 'logout': case 'logout':
await this.main.logout(msg.expired); await this.main.logout(msg.expired);

View File

@ -1,6 +1,6 @@
export class BrowserApi { export class BrowserApi {
static isSafariApi: boolean = (typeof safari !== 'undefined') && static isSafariApi: boolean = (typeof safari !== 'undefined') &&
navigator.userAgent.indexOf(' Safari/') !== -1 && navigator.userAgent.indexOf('Chrome') === -1; navigator.userAgent.indexOf(' Safari/') !== -1 && navigator.userAgent.indexOf('Chrome') === -1;
static isChromeApi: boolean = !BrowserApi.isSafariApi && (typeof chrome !== 'undefined'); static isChromeApi: boolean = !BrowserApi.isSafariApi && (typeof chrome !== 'undefined');
static async getTabFromCurrentWindowId(): Promise<any> { static async getTabFromCurrentWindowId(): Promise<any> {
@ -21,6 +21,12 @@ export class BrowserApi {
}); });
} }
static async getActiveTabs(): Promise<any[]> {
return await BrowserApi.tabsQuery({
active: true,
});
}
static tabsQuery(options: any): Promise<any[]> { static tabsQuery(options: any): Promise<any[]> {
if (BrowserApi.isChromeApi) { if (BrowserApi.isChromeApi) {
return new Promise((resolve) => { return new Promise((resolve) => {
@ -29,23 +35,28 @@ export class BrowserApi {
}); });
}); });
} else if (BrowserApi.isSafariApi) { } else if (BrowserApi.isSafariApi) {
let win: any = null; let wins: any[] = [];
if (options.currentWindow) { if (options.currentWindow) {
win = safari.application.activeBrowserWindow; if (safari.application.activeBrowserWindow) {
} wins.push(safari.application.activeBrowserWindow);
}
if (!win || !win.tabs || !win.tabs.length) { } else {
return Promise.resolve([]); wins = safari.application.browserWindows;
}
const tabs: any[] = [];
if (options.active && win.activeTab) {
tabs.push(win.activeTab);
} }
const returnedTabs: any[] = []; const returnedTabs: any[] = [];
tabs.forEach((tab: any) => { wins.forEach((win: any) => {
returnedTabs.push(BrowserApi.makeTabObject(tab)); if (!win.tabs) {
return;
}
if (options.active && win.activeTab) {
returnedTabs.push(BrowserApi.makeTabObject(win.activeTab));
} else if (!options.active) {
win.tabs.forEach((tab: any) => {
returnedTabs.push(BrowserApi.makeTabObject(tab));
});
}
}); });
return Promise.resolve(returnedTabs); return Promise.resolve(returnedTabs);