From c16566cfadb839e7f710d264a8d090e2a0938121 Mon Sep 17 00:00:00 2001 From: hensm Date: Fri, 17 May 2019 15:03:27 +0100 Subject: [PATCH] Add bridge application name to options storage --- ext/src/defaultOptions.ts | 1 + ext/src/lib/getBridgeInfo.ts | 15 +++++++++++---- ext/src/main.ts | 17 +++++++++++------ .../NativeMacReceiverSelectorManager.ts | 4 +++- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/ext/src/defaultOptions.ts b/ext/src/defaultOptions.ts index ae11533..78f431e 100644 --- a/ext/src/defaultOptions.ts +++ b/ext/src/defaultOptions.ts @@ -1,6 +1,7 @@ "use strict"; export interface Options { + bridgeApplicationName: string; mediaEnabled: boolean; mediaSyncElement: boolean; mediaStopOnUnload: boolean; diff --git a/ext/src/lib/getBridgeInfo.ts b/ext/src/lib/getBridgeInfo.ts index 08edc6e..88a26af 100644 --- a/ext/src/lib/getBridgeInfo.ts +++ b/ext/src/lib/getBridgeInfo.ts @@ -1,7 +1,10 @@ "use strict"; import semver from "semver"; + import nativeMessaging from "./nativeMessaging"; +import options from "./options"; + export interface BridgeInfo { name: string; @@ -14,12 +17,16 @@ export interface BridgeInfo { } export default async function getBridgeInfo (): Promise { + const applicationName = await options.get("bridgeApplicationName"); let applicationVersion: string; + try { + const { version } = browser.runtime.getManifest(); + applicationVersion = await nativeMessaging.sendNativeMessage( - APPLICATION_NAME + applicationName , { subject: "bridge:/getInfo" - , data: EXTENSION_VERSION }); + , data: version }); } catch (err) { return null; } @@ -40,14 +47,14 @@ export default async function getBridgeInfo (): Promise { // Print compatibility info to console if (!isVersionCompatible) { - console.error(`Expecting ${APPLICATION_NAME} v${APPLICATION_VERSION}, found v${applicationVersion}.` + console.error(`Expecting ${applicationName} v${APPLICATION_VERSION}, found v${applicationVersion}.` , isVersionOlder ? "Try updating the native app to the latest version." : "Try updating the extension to the latest version"); } return { - name: APPLICATION_NAME + name: applicationName , version: applicationVersion , expectedVersion: APPLICATION_VERSION diff --git a/ext/src/main.ts b/ext/src/main.ts index 93e2764..68c8c10 100755 --- a/ext/src/main.ts +++ b/ext/src/main.ts @@ -535,8 +535,10 @@ const statusBridgeReceivers = new Map(); /** * Create status bridge, set event handlers and initialize. */ -function initStatusBridge () { - statusBridge = nativeMessaging.connectNative(APPLICATION_NAME); +async function initStatusBridge () { + const applicationName = await options.get("bridgeApplicationName"); + + statusBridge = nativeMessaging.connectNative(applicationName); statusBridge.onDisconnect.addListener(onStatusBridgeDisconnect); statusBridge.onMessage.addListener(onStatusBridgeMessage); @@ -705,11 +707,14 @@ async function onConnectShim (port: browser.runtime.Port) { shimMap.delete(shimId); } + + const applicationName = await options.get("bridgeApplicationName"); + // Spawn bridge app instance - const bridgePort = nativeMessaging.connectNative(APPLICATION_NAME); + const bridgePort = nativeMessaging.connectNative(applicationName); if (bridgePort.error) { - console.error(`Failed connect to ${APPLICATION_NAME}:` + console.error(`Failed connect to ${applicationName}:` , bridgePort.error.message); } @@ -722,10 +727,10 @@ async function onConnectShim (port: browser.runtime.Port) { bridgePort.onDisconnect.addListener(() => { if (bridgePort.error) { - console.error(`${APPLICATION_NAME} disconnected:` + console.error(`${applicationName} disconnected:` , bridgePort.error.message); } else { - console.info(`${APPLICATION_NAME} disconnected`); + console.info(`${applicationName} disconnected`); } }); diff --git a/ext/src/receiverSelectorManager/selectorManagers/NativeMacReceiverSelectorManager.ts b/ext/src/receiverSelectorManager/selectorManagers/NativeMacReceiverSelectorManager.ts index 1cbdd04..27249fb 100644 --- a/ext/src/receiverSelectorManager/selectorManagers/NativeMacReceiverSelectorManager.ts +++ b/ext/src/receiverSelectorManager/selectorManagers/NativeMacReceiverSelectorManager.ts @@ -1,5 +1,6 @@ "use strict"; +import options from "../../lib/options"; import nativeMessaging from "../../lib/nativeMessaging"; import ReceiverSelectorManager, { @@ -29,7 +30,8 @@ export default class NativeMacReceiverSelectorManager receivers: Receiver[] , defaultMediaType: ReceiverSelectorMediaType): Promise { - this.bridgePort = nativeMessaging.connectNative(APPLICATION_NAME); + const applicationName = await options.get("bridgeApplicationName"); + this.bridgePort = nativeMessaging.connectNative(applicationName); this.bridgePort.onMessage.addListener((message: Message) => { switch (message.subject) {