From 7c1124f1e5218367d5fdac683d4fe00ffa12e899 Mon Sep 17 00:00:00 2001 From: sadick254 Date: Tue, 12 Oct 2021 09:13:18 +0300 Subject: [PATCH] Replace remote module with invoke calls. The remote module has been deprecated and is set to be removed on electron version 11. We have two options to replace the remote module 1. use @electron/remote module 2. Send messages using send/invoke. I am using invoke since it is the [recommended](https://www.npmjs.com/package/@electron/remote) option to use. --- src/main-process/atom-application.js | 7 +++++++ src/protocol-handler-installer.js | 30 +++++++++++++++------------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/main-process/atom-application.js b/src/main-process/atom-application.js index 0346aaa498e..f05d4924496 100644 --- a/src/main-process/atom-application.js +++ b/src/main-process/atom-application.js @@ -133,6 +133,13 @@ const decryptOptions = (optionsMessage, secret) => { return JSON.parse(message); }; +ipcMain.handle('isDefaultProtocolClient', (_, { protocol, path, args }) => { + return app.isDefaultProtocolClient(protocol, path, args); +}); + +ipcMain.handle('setAsDefaultProtocolClient', (_, { protocol, path, args }) => { + return app.setAsDefaultProtocolClient(protocol, path, args); +}); // The application's singleton class. // // It's the entry point into the Atom application and maintains the global state diff --git a/src/protocol-handler-installer.js b/src/protocol-handler-installer.js index 2f3c0740df1..6b904fc7a49 100644 --- a/src/protocol-handler-installer.js +++ b/src/protocol-handler-installer.js @@ -1,4 +1,4 @@ -const { remote } = require('electron'); +const { ipcRenderer } = require('electron'); const SETTING = 'core.uriHandlerRegistration'; const PROMPT = 'prompt'; @@ -10,26 +10,28 @@ module.exports = class ProtocolHandlerInstaller { return ['win32', 'darwin'].includes(process.platform); } - isDefaultProtocolClient() { - return remote.app.isDefaultProtocolClient('atom', process.execPath, [ - '--uri-handler', - '--' - ]); + async isDefaultProtocolClient() { + return ipcRenderer.invoke('isDefaultProtocolClient', { + protocol: 'atom', + path: process.execPath, + args: ['--uri-handler', '--'] + }); } - setAsDefaultProtocolClient() { + async setAsDefaultProtocolClient() { // This Electron API is only available on Windows and macOS. There might be some // hacks to make it work on Linux; see https://github.com/electron/electron/issues/6440 return ( this.isSupported() && - remote.app.setAsDefaultProtocolClient('atom', process.execPath, [ - '--uri-handler', - '--' - ]) + ipcRenderer.invoke('setAsDefaultProtocolClient', { + protocol: 'atom', + path: process.execPath, + args: ['--uri-handler', '--'] + }) ); } - initialize(config, notifications) { + async initialize(config, notifications) { if (!this.isSupported()) { return; } @@ -37,12 +39,12 @@ module.exports = class ProtocolHandlerInstaller { const behaviorWhenNotProtocolClient = config.get(SETTING); switch (behaviorWhenNotProtocolClient) { case PROMPT: - if (!this.isDefaultProtocolClient()) { + if (await !this.isDefaultProtocolClient()) { this.promptToBecomeProtocolClient(config, notifications); } break; case ALWAYS: - if (!this.isDefaultProtocolClient()) { + if (await !this.isDefaultProtocolClient()) { this.setAsDefaultProtocolClient(); } break;