diff --git a/jslib b/jslib index 393f6c9c20..4f876fc222 160000 --- a/jslib +++ b/jslib @@ -1 +1 @@ -Subproject commit 393f6c9c20fb7eded5008b65242ea44e69bc349c +Subproject commit 4f876fc222a0b6b5156139f43a4623f6fa083465 diff --git a/src/background/main.background.ts b/src/background/main.background.ts index 3f0c30d2cd..06ed09810a 100644 --- a/src/background/main.background.ts +++ b/src/background/main.background.ts @@ -57,6 +57,7 @@ import { Analytics } from 'jslib/misc'; import { Utils } from 'jslib/misc/utils'; import { BrowserApi } from '../browser/browserApi'; +import { SafariApp } from '../browser/safariApp'; import CommandsBackground from './commands.background'; import ContextMenusBackground from './contextMenus.background'; @@ -216,6 +217,7 @@ export default class MainBackground { } async bootstrap() { + SafariApp.init(); this.analytics.ga('send', 'pageview', '/background.html'); this.containerService.attachToWindow(window); diff --git a/src/browser/browserApi.ts b/src/browser/browserApi.ts index d7d645ec5f..73c589e008 100644 --- a/src/browser/browserApi.ts +++ b/src/browser/browserApi.ts @@ -154,7 +154,7 @@ export class BrowserApi { return chrome.runtime.getManifest().version; } else if (BrowserApi.isSafariApi) { // TODO - return null; + return 'TODO'; } else { return null; } @@ -291,10 +291,4 @@ export class BrowserApi { return chrome.runtime.reload(); } } - - static sendSafariMessageToApp(message: any, response: (data: any) => {} = null) { - if (this.isSafariApi) { - (window as any).webkit.messageHandlers.bitwardenApp.postMessage(message); - } - } } diff --git a/src/browser/safariApp.ts b/src/browser/safariApp.ts new file mode 100644 index 0000000000..6c78cddaa9 --- /dev/null +++ b/src/browser/safariApp.ts @@ -0,0 +1,35 @@ +import { BrowserApi } from './browserApi'; + +export class SafariApp { + static init() { + if (BrowserApi.isSafariApi) { + (window as any).bitwardenSafariAppMessageReceiver = (message: any) => + SafariApp.receiveMessageFromApp(message); + } + } + + static sendMessageToApp(command: string, data: any = null): Promise { + if (!BrowserApi.isSafariApi) { + return Promise.resolve(null); + } + return new Promise((resolve) => { + const messageId = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER); + (window as any).webkit.messageHandlers.bitwardenApp.postMessage({ + id: messageId, + command: command, + data: data, + }); + SafariApp.requests.set(messageId, { resolve: resolve, date: new Date() }); + }); + } + + private static requests = new Map void, date: Date }>(); + + private static receiveMessageFromApp(message: any) { + if (message == null || message.id == null || !SafariApp.requests.has(message.id)) { + return; + } + const p = SafariApp.requests.get(message.id); + p.resolve(message.data); + } +}