From 06234f6b232cbe0a05e5e9170cd5c4c779994223 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 15 Aug 2019 16:36:49 -0400 Subject: [PATCH] wire up internal messaging for safari app --- src/background/commands.background.ts | 2 +- src/background/runtime.background.ts | 2 +- src/browser/browserApi.ts | 3 ++- src/browser/safariApp.ts | 9 +++++++++ src/popup/app.component.ts | 2 +- src/services/browserMessaging.service.ts | 3 ++- 6 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/background/commands.background.ts b/src/background/commands.background.ts index 54a7643ac8..33cac783b4 100644 --- a/src/background/commands.background.ts +++ b/src/background/commands.background.ts @@ -23,7 +23,7 @@ export default class CommandsBackground { async init() { if (this.isSafari || this.isEdge || this.isVivaldi) { - BrowserApi.messageListener(async (msg: any, sender: any, sendResponse: any) => { + BrowserApi.messageListener('commands.background', async (msg: any, sender: any, sendResponse: any) => { if (msg.command === 'keyboardShortcutTriggered' && msg.shortcut) { await this.processCommand(msg.shortcut, sender); } diff --git a/src/background/runtime.background.ts b/src/background/runtime.background.ts index a5ee4b4a20..22123d351d 100644 --- a/src/background/runtime.background.ts +++ b/src/background/runtime.background.ts @@ -76,7 +76,7 @@ export default class RuntimeBackground { await this.checkOnInstalled(); - BrowserApi.messageListener(async (msg: any, sender: any, sendResponse: any) => { + BrowserApi.messageListener('runtime.background', async (msg: any, sender: any, sendResponse: any) => { await this.processMessage(msg, sender, sendResponse); }); } diff --git a/src/browser/browserApi.ts b/src/browser/browserApi.ts index bdf2e7ec39..5c4e1826cc 100644 --- a/src/browser/browserApi.ts +++ b/src/browser/browserApi.ts @@ -196,12 +196,13 @@ export class BrowserApi { } } - static messageListener(callback: (message: any, sender: any, response: any) => void) { + static messageListener(name: string, callback: (message: any, sender: any, response: any) => void) { if (BrowserApi.isChromeApi) { chrome.runtime.onMessage.addListener((msg: any, sender: any, response: any) => { callback(msg, sender, response); }); } else if (BrowserApi.isSafariApi) { + SafariApp.addMessageListener(name, callback); // TODO /* safari.application.addEventListener('message', async (msgEvent: any) => { diff --git a/src/browser/safariApp.ts b/src/browser/safariApp.ts index 51e52a6b17..9e498f7d97 100644 --- a/src/browser/safariApp.ts +++ b/src/browser/safariApp.ts @@ -26,7 +26,16 @@ export class SafariApp { }); } + static addMessageListener(name: string, callback: (message: any, sender: any, response: any) => void) { + SafariApp.messageListeners.set(name, callback); + } + + static sendMessageToListeners(message: any, sender: any, response: any) { + SafariApp.messageListeners.forEach((f) => f(message, sender, response)); + } + private static requests = new Map void, date: Date }>(); + private static messageListeners = new Map void>(); private static receiveMessageFromApp(message: any) { if (message == null || message.id == null || !SafariApp.requests.has(message.id)) { diff --git a/src/popup/app.component.ts b/src/popup/app.component.ts index ca0d6a3d02..ae9e77671c 100644 --- a/src/popup/app.component.ts +++ b/src/popup/app.component.ts @@ -133,7 +133,7 @@ export class AppComponent implements OnInit { } }; - BrowserApi.messageListener((window as any).bitwardenPopupMainMessageListener); + BrowserApi.messageListener('app.component', (window as any).bitwardenPopupMainMessageListener); this.router.events.subscribe((event) => { if (event instanceof NavigationEnd) { diff --git a/src/services/browserMessaging.service.ts b/src/services/browserMessaging.service.ts index 2ea3b0cdfc..01f94269fb 100644 --- a/src/services/browserMessaging.service.ts +++ b/src/services/browserMessaging.service.ts @@ -5,10 +5,11 @@ import { MessagingService } from 'jslib/abstractions/messaging.service'; export default class BrowserMessagingService implements MessagingService { send(subscriber: string, arg: any = {}) { + const message = Object.assign({}, { command: subscriber }, arg); if (BrowserApi.isSafariApi) { SafariApp.sendMessageToApp(subscriber, arg); + SafariApp.sendMessageToListeners(message, 'BrowserMessagingService', null); } else { - const message = Object.assign({}, { command: subscriber }, arg); chrome.runtime.sendMessage(message); } }