From 76bac52a2dff4abd0667c3c4a85e19c813968b63 Mon Sep 17 00:00:00 2001
From: Svornost <11434-svornost@users.noreply.gitgud.io>
Date: Sat, 20 Mar 2021 00:36:26 -0700
Subject: [PATCH] CefExecuteProcess needs the app pointer otherwise
 initialization in multi-process mode doesn't work correctly

---
 FCHost/fchost/fchost_app.cc   | 8 ++------
 FCHost/fchost/fchost_app.h    | 5 +++--
 FCHost/fchost/fchost_linux.cc | 8 ++++++--
 FCHost/fchost/fchost_win.cc   | 8 ++++++--
 4 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/FCHost/fchost/fchost_app.cc b/FCHost/fchost/fchost_app.cc
index 3c5608a42c6..4dfc4cefaac 100644
--- a/FCHost/fchost/fchost_app.cc
+++ b/FCHost/fchost/fchost_app.cc
@@ -163,10 +163,6 @@ void FCHostApp::OnContextCreated(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFra
 
 int FCHostApp::main(const CefMainArgs& args, CefCommandLine* commandLine)
 {
-  // It will create the first browser instance in OnContextInitialized() after
-  // CEF has initialized.
-  CefRefPtr<FCHostApp> app(new FCHostApp);
-
   // Specify CEF global settings here.
   CefSettings settings;
 
@@ -176,7 +172,7 @@ int FCHostApp::main(const CefMainArgs& args, CefCommandLine* commandLine)
   }
 
   // Cache location is required for local storage
-  cef_string_from_path(app->GetLocalStorePath(), &settings.cache_path);
+  cef_string_from_path(this->GetLocalStorePath(), &settings.cache_path);
 
   // When generating projects with CMake the CEF_USE_SANDBOX value will be defined
   // automatically. Pass -DUSE_SANDBOX=OFF to the CMake command-line to disable
@@ -186,7 +182,7 @@ int FCHostApp::main(const CefMainArgs& args, CefCommandLine* commandLine)
 #endif
 
   // Initialize CEF for the browser process.
-  CefInitialize(args, settings, app.get(), nullptr);
+  CefInitialize(args, settings, this, nullptr);
 
   // Run the CEF message loop. This will block until CefQuitMessageLoop() is
   // called.
diff --git a/FCHost/fchost/fchost_app.h b/FCHost/fchost/fchost_app.h
index 52887c8754c..d5ffe9f4190 100644
--- a/FCHost/fchost/fchost_app.h
+++ b/FCHost/fchost/fchost_app.h
@@ -14,6 +14,8 @@ class CefCommandLine;
 // Implement application-level callbacks for the browser process.
 class FCHostApp : public CefApp, public CefBrowserProcessHandler, public CefRenderProcessHandler {
  public:
+  FCHostApp();
+
   // CefApp methods:
   virtual CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler()
       OVERRIDE {
@@ -30,10 +32,9 @@ class FCHostApp : public CefApp, public CefBrowserProcessHandler, public CefRend
 
   std::filesystem::path GetLocalStorePath();
 
-  static int main(const CefMainArgs& args, CefCommandLine* commandLine);
+  int main(const CefMainArgs& args, CefCommandLine* commandLine);
 
  private:
-  FCHostApp();
   // Include the default reference counting implementation.
   IMPLEMENT_REFCOUNTING(FCHostApp);
 };
diff --git a/FCHost/fchost/fchost_linux.cc b/FCHost/fchost/fchost_linux.cc
index c79746c8968..c1c7b81f940 100644
--- a/FCHost/fchost/fchost_linux.cc
+++ b/FCHost/fchost/fchost_linux.cc
@@ -43,11 +43,15 @@ int XIOErrorHandlerImpl(Display* display) {
 int main(int argc, char* argv[]) {
   // Provide CEF with command-line arguments.
   CefMainArgs main_args(argc, argv);
+  
+  // It will create the first browser instance in OnContextInitialized() after
+  // CEF has initialized.
+  CefRefPtr<FCHostApp> app(new FCHostApp);
 
   // CEF applications have multiple sub-processes (render, plugin, GPU, etc)
   // that share the same executable. This function checks the command-line and,
   // if this is a sub-process, executes the appropriate logic.
-  int exit_code = CefExecuteProcess(main_args, nullptr, nullptr);
+  int exit_code = CefExecuteProcess(main_args, app, nullptr);
   if (exit_code >= 0) {
     // The sub-process has completed so return here.
     return exit_code;
@@ -63,5 +67,5 @@ int main(int argc, char* argv[]) {
   CefRefPtr<CefCommandLine> command_line = CefCommandLine::CreateCommandLine();
   command_line->InitFromArgv(argc, argv);
 
-  return FCHostApp::main(main_args, command_line);
+  return app->main(main_args, command_line);
 }
diff --git a/FCHost/fchost/fchost_win.cc b/FCHost/fchost/fchost_win.cc
index 575f5340fb0..aaf6975ad31 100644
--- a/FCHost/fchost/fchost_win.cc
+++ b/FCHost/fchost/fchost_win.cc
@@ -31,10 +31,14 @@ int APIENTRY wWinMain(HINSTANCE hInstance,
   // Provide CEF with command-line arguments.
   CefMainArgs main_args(hInstance);
 
+  // It will create the first browser instance in OnContextInitialized() after
+  // CEF has initialized.
+  CefRefPtr<FCHostApp> app(new FCHostApp);
+
   // CEF applications have multiple sub-processes (render, plugin, GPU, etc)
   // that share the same executable. This function checks the command-line and,
   // if this is a sub-process, executes the appropriate logic.
-  int exit_code = CefExecuteProcess(main_args, nullptr, nullptr);
+  int exit_code = CefExecuteProcess(main_args, app, nullptr);
   if (exit_code >= 0) {
     // The sub-process has completed so return here.
     return exit_code;
@@ -43,5 +47,5 @@ int APIENTRY wWinMain(HINSTANCE hInstance,
   CefRefPtr<CefCommandLine> command_line = CefCommandLine::CreateCommandLine();
   command_line->InitFromString(lpCmdLine);
 
-  return FCHostApp::main(main_args, command_line);
+  return app->main(main_args, command_line);
 }
-- 
GitLab