dbus-vis: Use IPC for open file dialog

Previously, the "open file" dialog is opened in the renderer thread
running "initialization.js".

This change moves the dialog to the main thread (main.js) so that the
renderer thread emits a "file-request" request and the main thread
answers with a "filename" response.

This IPC paradigm is required for newer versions of Electron, because
the "dialog" module cannot be imported in renderer threads in those
newer versions.

Signed-off-by: Sui Chen <suichen@google.com>
Change-Id: Ifd324688bb8a4dbdc6a5f86082fb3d205af0d77a
diff --git a/dbus-vis/initialization.js b/dbus-vis/initialization.js
index fb150e4..bc85c8f 100644
--- a/dbus-vis/initialization.js
+++ b/dbus-vis/initialization.js
@@ -1,15 +1,9 @@
+const { ipcRenderer } = require('electron');
 const { spawnSync } = require('child_process');
 const md5File = require('md5-file');
 const https = require('https');
 
-function OpenFileHandler() {
-  console.log('Will open a dialog box ...');
-  const options = {
-    title: 'Open a file or folder',
-  };
-  let x = dialog.showOpenDialogSync(options) + '';  // Convert to string
-  console.log('file name: ' + x)
-
+ipcRenderer.on('filename', (event, x) => {
   // Determine file type
   let is_asio_log = false;
   const data = fs.readFileSync(x, {encoding: 'utf-8'});
@@ -38,8 +32,11 @@
   }
 
   OpenDBusPcapFile(x);
-
   UpdateLayout();
+});
+
+function OpenFileHandler() {
+  ipcRenderer.send('file-request');
 }
 
 // The file may be either DBus dump or Boost Asio handler log
diff --git a/dbus-vis/ipmi_timeline_vis.js b/dbus-vis/ipmi_timeline_vis.js
index 0b3aa74..fd7ec03 100644
--- a/dbus-vis/ipmi_timeline_vis.js
+++ b/dbus-vis/ipmi_timeline_vis.js
@@ -13,7 +13,7 @@
 //    picks up the updated Intervals and Titles arrays and draws on the canvas
 //    accordingly.
 
-const {dialog} = require('electron').remote;
+const {dialog} = require('electron');
 const {fs} = require('file-system');
 const {util} = require('util');
 const {exec} = require('child_process');
diff --git a/dbus-vis/main.js b/dbus-vis/main.js
index 705ce4e..860fc31 100644
--- a/dbus-vis/main.js
+++ b/dbus-vis/main.js
@@ -1,6 +1,16 @@
 // Modules to control application life and create native browser window
-const {app, BrowserWindow} = require('electron');
+const {app, BrowserWindow, dialog, ipcMain } = require('electron');
 const path = require('path');
+const {fs} = require('file-system');
+
+// Open-file dialog
+ipcMain.on('file-request', (event) => {
+  const options = {
+    title: 'Open a file or folder',
+  };
+  const x = dialog.showOpenDialogSync(options) + '';  // Convert to string
+  event.reply("filename", x);
+});
 
 function createWindow() {
   // Create the browser window.
@@ -10,7 +20,7 @@
     webPreferences: {
       nodeIntegration:
           true,  // For opening file dialog from the renderer process
-      enableRemoteModule: true  // For require('electron').remote
+      contextIsolation: false,
     }
   });