mirror of
https://github.com/finos/SymphonyElectron.git
synced 2024-12-27 01:11:13 -06:00
SDA-4177 Windows tel/sms protocols bugfix + macOS protocols registration delegated to Electron (#1895)
* Adding protocol in classes * macOS default protocol apply instantly bugfix
This commit is contained in:
parent
dcbe7a0b74
commit
893d288f8b
@ -1,13 +1,9 @@
|
|||||||
import { exec } from 'child_process';
|
|
||||||
import { app } from 'electron';
|
import { app } from 'electron';
|
||||||
import * as fs from 'fs';
|
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import { PhoneNumberProtocol } from '../common/api-interface';
|
import { PhoneNumberProtocol } from '../common/api-interface';
|
||||||
import { isDevEnv, isMac, isWindowsOS } from '../common/env';
|
import { isDevEnv, isMac, isWindowsOS } from '../common/env';
|
||||||
import { logger } from '../common/logger';
|
import { logger } from '../common/logger';
|
||||||
|
|
||||||
const LS_REGISTER_PATH =
|
|
||||||
'/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister';
|
|
||||||
enum REGISTRY_PATHS {
|
enum REGISTRY_PATHS {
|
||||||
Classes = '\\Software\\Classes',
|
Classes = '\\Software\\Classes',
|
||||||
Capabilities = '\\Software\\Symphony\\Capabilities',
|
Capabilities = '\\Software\\Symphony\\Capabilities',
|
||||||
@ -80,6 +76,18 @@ class VoiceHandler {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const protocolClassRegKey = new Registry({
|
||||||
|
hive: Registry.HKCU,
|
||||||
|
key: `${REGISTRY_PATHS.Classes}\\${protocol}`,
|
||||||
|
});
|
||||||
|
await protocolClassRegKey.set(
|
||||||
|
'',
|
||||||
|
Registry.REG_SZ,
|
||||||
|
`URL:${protocol}`,
|
||||||
|
errorCallback,
|
||||||
|
);
|
||||||
|
|
||||||
await applicationCapabilitiesRegKey.set(
|
await applicationCapabilitiesRegKey.set(
|
||||||
'ApplicationName',
|
'ApplicationName',
|
||||||
Registry.REG_SZ,
|
Registry.REG_SZ,
|
||||||
@ -183,128 +191,28 @@ class VoiceHandler {
|
|||||||
* Registers app on macOS
|
* Registers app on macOS
|
||||||
*/
|
*/
|
||||||
private registerAppOnMacOS(protocols: PhoneNumberProtocol[]) {
|
private registerAppOnMacOS(protocols: PhoneNumberProtocol[]) {
|
||||||
this.readLaunchServicesPlist((plist) => {
|
const allowedProtocols = [PhoneNumberProtocol.Sms, PhoneNumberProtocol.Tel];
|
||||||
for (const protocol of protocols) {
|
for (const protocol of protocols) {
|
||||||
const itemIdx = plist.LSHandlers.findIndex(
|
const idx = allowedProtocols.indexOf(protocol);
|
||||||
(lsHandler) => lsHandler.LSHandlerURLScheme === protocol,
|
if (idx > -1) {
|
||||||
);
|
app.setAsDefaultProtocolClient(protocol);
|
||||||
// macOS allows only one app being declared as able to make calls
|
allowedProtocols.splice(idx, 1);
|
||||||
if (itemIdx !== -1) {
|
|
||||||
plist.splice(itemIdx, 1);
|
|
||||||
}
|
|
||||||
const plistEntry = {
|
|
||||||
LSHandlerURLScheme: protocol,
|
|
||||||
LSHandlerRoleAll: 'com.symphony.electron-desktop',
|
|
||||||
LSHandlerPreferredVersions: {
|
|
||||||
LSHandlerRoleAll: '-',
|
|
||||||
},
|
|
||||||
};
|
|
||||||
plist.LSHandlers.push(plistEntry);
|
|
||||||
}
|
}
|
||||||
this.updateLaunchServicesPlist(plist);
|
}
|
||||||
});
|
if (allowedProtocols.length) {
|
||||||
|
for (const unsupportedProtocol of allowedProtocols) {
|
||||||
|
app.removeAsDefaultProtocolClient(unsupportedProtocol);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unregisters app for tel/sms on macOS
|
* Unregisters app for tel/sms on macOS
|
||||||
*/
|
*/
|
||||||
private unregisterAppOnMacOS(protocols: PhoneNumberProtocol[]) {
|
private unregisterAppOnMacOS(protocols: PhoneNumberProtocol[]) {
|
||||||
this.readLaunchServicesPlist((plist) => {
|
for (const protocol of protocols) {
|
||||||
if (plist) {
|
app.removeAsDefaultProtocolClient(protocol);
|
||||||
const filteredList = plist.LSHandlers.filter(
|
|
||||||
(lsHandler) => protocols.indexOf(lsHandler.LSHandlerURLScheme) === -1,
|
|
||||||
);
|
|
||||||
plist.LSHandlers = filteredList;
|
|
||||||
this.updateLaunchServicesPlist(plist);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reads macOS launch services plist
|
|
||||||
* @param callback
|
|
||||||
*/
|
|
||||||
private readLaunchServicesPlist(callback) {
|
|
||||||
const plistPath = this.getLaunchServicesPlistPath();
|
|
||||||
const tmpPath = `${plistPath}.${Math.random()}`;
|
|
||||||
exec(`plutil -convert json "${plistPath}" -o "${tmpPath}"`, (err) => {
|
|
||||||
if (err) {
|
|
||||||
logger.error(
|
|
||||||
'voice-handler: error while converting binary file: ',
|
|
||||||
err,
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
fs.readFile(tmpPath, (readErr, data) => {
|
|
||||||
if (readErr) {
|
|
||||||
logger.error('voice-handler: error while reading tmp file:');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
const plistContent = JSON.parse(data.toString());
|
|
||||||
callback(plistContent);
|
|
||||||
fs.unlink(tmpPath, (err) => {
|
|
||||||
if (err) {
|
|
||||||
logger.error('voice-handler: error clearing tmp file ', err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (e) {
|
|
||||||
logger.error('voice-handler: unexpected error occured ', err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Updates launch services plist file
|
|
||||||
* @param defaults
|
|
||||||
*/
|
|
||||||
private updateLaunchServicesPlist(defaults) {
|
|
||||||
const plistPath = this.getLaunchServicesPlistPath();
|
|
||||||
const tmpPath = `${plistPath}.${Math.random()}`;
|
|
||||||
try {
|
|
||||||
fs.writeFileSync(tmpPath, JSON.stringify(defaults));
|
|
||||||
} catch (e) {
|
|
||||||
logger.error('voice-handler: error while creating tmp plist ', e);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
exec(`plutil -convert binary1 "${tmpPath}" -o "${plistPath}"`, () => {
|
|
||||||
fs.unlink(tmpPath, (err) => {
|
|
||||||
if (err) {
|
|
||||||
logger.error(`voice-handler: error while clearing ${tmpPath}: `, err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
// Relaunch Launch Services so it take into consideration updated plist file
|
|
||||||
exec(
|
|
||||||
`${LS_REGISTER_PATH} -kill -r -domain local -domain system -domain user`,
|
|
||||||
(registerErr) => {
|
|
||||||
if (registerErr) {
|
|
||||||
logger.error(
|
|
||||||
'voice-handler: error relaunching Launch Services ',
|
|
||||||
registerErr,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns Launch services plist filepath
|
|
||||||
* @param callback
|
|
||||||
*/
|
|
||||||
private getLaunchServicesPlistPath() {
|
|
||||||
const secureLaunchServicesPlist = `${process.env.HOME}/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist`;
|
|
||||||
const insecureLaunchServicesPlist = `${process.env.HOME}/Library/Preferences/com.apple.LaunchServices.plist`;
|
|
||||||
|
|
||||||
const secureLaunchServicesPlistExists = fs.existsSync(
|
|
||||||
secureLaunchServicesPlist,
|
|
||||||
);
|
|
||||||
if (secureLaunchServicesPlistExists) {
|
|
||||||
return secureLaunchServicesPlist;
|
|
||||||
}
|
|
||||||
return insecureLaunchServicesPlist;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user