setup safari app messaging

This commit is contained in:
Kyle Spearrin 2019-08-12 12:31:32 -04:00
parent 55db185d6b
commit 73db5cd59a
4 changed files with 39 additions and 8 deletions

2
jslib

@ -1 +1 @@
Subproject commit 393f6c9c20fb7eded5008b65242ea44e69bc349c
Subproject commit 4f876fc222a0b6b5156139f43a4623f6fa083465

View File

@ -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);

View File

@ -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);
}
}
}

35
src/browser/safariApp.ts Normal file
View File

@ -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<any> {
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<number, { resolve: (value?: unknown) => 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);
}
}