process messages in background runtime

This commit is contained in:
Kyle Spearrin 2018-01-12 15:20:19 -05:00
parent bc59281bf0
commit 497984e353
3 changed files with 91 additions and 78 deletions

View File

@ -42,6 +42,15 @@ export default class RuntimeBackground {
}
}, true);
this.runtime.addEventListener('message', async (msgEvent: any) => {
await this.processMessage(msgEvent.message, {
tab: {
id: null, // TODO
},
frameId: null,
}, () => { /* No responses in Safari */ });
}, false);
return;
}
@ -59,84 +68,88 @@ export default class RuntimeBackground {
}
this.runtime.onMessage.addListener(async (msg: any, sender: any, sendResponse: any) => {
switch (msg.command) {
case 'loggedIn':
case 'unlocked':
case 'locked':
await this.main.setIcon();
await this.main.refreshBadgeAndMenu();
break;
case 'logout':
await this.main.logout(msg.expired);
break;
case 'syncCompleted':
if (msg.successfully) {
setTimeout(async () => await this.main.refreshBadgeAndMenu(), 2000);
}
break;
case 'bgOpenNotificationBar':
await BrowserApi.tabSendMessageData(sender.tab, 'openNotificationBar', msg.data);
break;
case 'bgCloseNotificationBar':
await BrowserApi.tabSendMessageData(sender.tab, 'closeNotificationBar');
break;
case 'bgAdjustNotificationBar':
await BrowserApi.tabSendMessageData(sender.tab, 'adjustNotificationBar', msg.data);
break;
case 'bgCollectPageDetails':
this.main.collectPageDetailsForContentScript(sender.tab, msg.sender, sender.frameId);
break;
case 'bgAddLogin':
await this.addLogin(msg.login, sender.tab);
break;
case 'bgAddClose':
this.removeAddLogin(sender.tab);
break;
case 'bgAddSave':
await this.saveAddLogin(sender.tab);
break;
case 'bgNeverSave':
await this.saveNever(sender.tab);
break;
case 'bgUpdateContextMenu':
await this.main.refreshBadgeAndMenu();
break;
case 'collectPageDetailsResponse':
switch (msg.sender) {
case 'notificationBar':
const forms = this.autofillService.getFormsWithPasswordFields(msg.details);
await BrowserApi.tabSendMessageData(msg.tab, 'notificationBarPageDetails', {
details: msg.details,
forms: forms,
});
break;
case 'autofiller':
case 'autofill_cmd':
await this.autofillService.doAutoFillForLastUsedLogin([{
frameId: sender.frameId,
tab: msg.tab,
details: msg.details,
}], msg.sender === 'autofill_cmd');
break;
case 'contextMenu':
clearTimeout(this.autofillTimeout);
this.pageDetailsToAutoFill.push({
frameId: sender.frameId,
tab: msg.tab,
details: msg.details,
});
this.autofillTimeout = setTimeout(async () => await this.autofillPage(), 300);
break;
default:
break;
}
break;
default:
break;
}
await this.processMessage(msg, sender, sendResponse);
});
}
private async processMessage(msg: any, sender: any, sendResponse: any) {
switch (msg.command) {
case 'loggedIn':
case 'unlocked':
case 'locked':
await this.main.setIcon();
await this.main.refreshBadgeAndMenu();
break;
case 'logout':
await this.main.logout(msg.expired);
break;
case 'syncCompleted':
if (msg.successfully) {
setTimeout(async () => await this.main.refreshBadgeAndMenu(), 2000);
}
break;
case 'bgOpenNotificationBar':
await BrowserApi.tabSendMessageData(sender.tab, 'openNotificationBar', msg.data);
break;
case 'bgCloseNotificationBar':
await BrowserApi.tabSendMessageData(sender.tab, 'closeNotificationBar');
break;
case 'bgAdjustNotificationBar':
await BrowserApi.tabSendMessageData(sender.tab, 'adjustNotificationBar', msg.data);
break;
case 'bgCollectPageDetails':
this.main.collectPageDetailsForContentScript(sender.tab, msg.sender, sender.frameId);
break;
case 'bgAddLogin':
await this.addLogin(msg.login, sender.tab);
break;
case 'bgAddClose':
this.removeAddLogin(sender.tab);
break;
case 'bgAddSave':
await this.saveAddLogin(sender.tab);
break;
case 'bgNeverSave':
await this.saveNever(sender.tab);
break;
case 'bgUpdateContextMenu':
await this.main.refreshBadgeAndMenu();
break;
case 'collectPageDetailsResponse':
switch (msg.sender) {
case 'notificationBar':
const forms = this.autofillService.getFormsWithPasswordFields(msg.details);
await BrowserApi.tabSendMessageData(msg.tab, 'notificationBarPageDetails', {
details: msg.details,
forms: forms,
});
break;
case 'autofiller':
case 'autofill_cmd':
await this.autofillService.doAutoFillForLastUsedLogin([{
frameId: sender.frameId,
tab: msg.tab,
details: msg.details,
}], msg.sender === 'autofill_cmd');
break;
case 'contextMenu':
clearTimeout(this.autofillTimeout);
this.pageDetailsToAutoFill.push({
frameId: sender.frameId,
tab: msg.tab,
details: msg.details,
});
this.autofillTimeout = setTimeout(async () => await this.autofillPage(), 300);
break;
default:
break;
}
break;
default:
break;
}
}
private async autofillPage() {
await this.autofillService.doAutoFill({
cipher: this.main.loginToAutoFill,

View File

@ -101,11 +101,11 @@ class BrowserApi {
return Promise.reject('Window not found.');
}
if (safari.application.activeBrowserWindow.tabs.length < tab.index + 1) {
if (win.tabs.length < tab.index + 1) {
return Promise.reject('Tab not found.');
}
const t = safari.application.activeBrowserWindow.tabs[tab.index];
const t = win.tabs[tab.index];
t.page.dispatchMessage('bitwarden', obj);
return Promise.resolve();
}

View File

@ -995,7 +995,7 @@
*/
if ((typeof safari !== 'undefined')) {
safari.self.addEventListener('bitwarden', function (msgEvent) {
safari.self.addEventListener('message', function (msgEvent) {
var msg = msgEvent.message;
if (msg.command === 'collectPageDetails') {
var pageDetails = collect(document);