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.i18n2Service);
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.folderService, this.cipherService, this.cryptoService, this.collectionService,
this.storageService, this.messagingService, (expired: boolean) => this.logout(expired));
@ -214,23 +217,27 @@ export default class MainBackground {
await this.actionSetIcon(this.sidebarAction, suffix);
}
async refreshBadgeAndMenu() {
async refreshBadgeAndMenu(forLocked: boolean = false) {
if (this.isSafari || !chrome.windows || !chrome.contextMenus) {
return;
}
const tab = await BrowserApi.getTabFromCurrentWindowId();
if (!tab) {
const menuDisabled = await this.storageService.get<boolean>(ConstantsService.disableContextMenuItemKey);
if (!menuDisabled) {
await this.buildContextMenu();
} else {
await this.contextMenusRemoveAll();
}
if (forLocked) {
await this.loadMenuAndUpdateBadgeForLockedState(!menuDisabled);
this.onUpdatedRan = this.onReplacedRan = false;
return;
}
const disabled = await this.storageService.get<boolean>(ConstantsService.disableContextMenuItemKey);
if (!disabled) {
await this.buildContextMenu();
await this.contextMenuReady(tab, true);
} else {
await this.contextMenusRemoveAll();
await this.contextMenuReady(tab, false);
const tab = await BrowserApi.getTabFromCurrentWindow();
if (tab) {
await this.contextMenuReady(tab, !menuDisabled);
}
}
@ -406,11 +413,23 @@ export default class MainBackground {
this.browserActionSetBadgeText(theText, tabId);
this.sidebarActionSetBadgeText(theText, tabId);
} catch (e) {
if (contextMenuEnabled) {
await this.loadNoLoginsContextMenuOptions(this.i18nService.vaultLocked);
}
this.browserActionSetBadgeText('', tabId);
this.sidebarActionSetBadgeText('', tabId);
await this.loadMenuAndUpdateBadgeForLockedState(contextMenuEnabled);
}
}
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 'locked':
await this.main.setIcon();
await this.main.refreshBadgeAndMenu();
await this.main.refreshBadgeAndMenu(msg.command === 'locked');
break;
case 'logout':
await this.main.logout(msg.expired);

View File

@ -1,6 +1,6 @@
export class BrowserApi {
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 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[]> {
if (BrowserApi.isChromeApi) {
return new Promise((resolve) => {
@ -29,23 +35,28 @@ export class BrowserApi {
});
});
} else if (BrowserApi.isSafariApi) {
let win: any = null;
let wins: any[] = [];
if (options.currentWindow) {
win = safari.application.activeBrowserWindow;
}
if (!win || !win.tabs || !win.tabs.length) {
return Promise.resolve([]);
}
const tabs: any[] = [];
if (options.active && win.activeTab) {
tabs.push(win.activeTab);
if (safari.application.activeBrowserWindow) {
wins.push(safari.application.activeBrowserWindow);
}
} else {
wins = safari.application.browserWindows;
}
const returnedTabs: any[] = [];
tabs.forEach((tab: any) => {
returnedTabs.push(BrowserApi.makeTabObject(tab));
wins.forEach((win: any) => {
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);