From b2b5257f5c28c014ecb0475f6124e07f0e304452 Mon Sep 17 00:00:00 2001
From: ezsh <ezsh.junk@gmail.com>
Date: Tue, 16 Mar 2021 17:26:39 +0100
Subject: [PATCH] FCHost: refactor main inplementation

Consolidate the general, platform-agnostic part of the main()/WinMain()
in static FCHost::main() function to avoid code duplication.
---
 FCHost/fchost/CMakeLists.txt       |  2 ++
 FCHost/fchost/fchost_app.cc        | 41 ++++++++++++++++++++++++++++-
 FCHost/fchost/fchost_app.h         | 12 ++++++---
 FCHost/fchost/fchost_handler.cc    |  4 +--
 FCHost/fchost/fchost_linux.cc      | 42 +++---------------------------
 FCHost/fchost/fchost_storage.cc    |  8 ++----
 FCHost/fchost/fchost_storage.h     |  2 +-
 FCHost/fchost/fchost_storage_js.cc |  2 +-
 FCHost/fchost/fchost_storage_js.h  |  2 +-
 FCHost/fchost/fchost_win.cc        | 33 +++++------------------
 FCHost/fchost/utility.cc           | 16 ++++++++++++
 FCHost/fchost/utility.h            |  8 ++++++
 12 files changed, 92 insertions(+), 80 deletions(-)
 create mode 100644 FCHost/fchost/utility.cc
 create mode 100644 FCHost/fchost/utility.h

diff --git a/FCHost/fchost/CMakeLists.txt b/FCHost/fchost/CMakeLists.txt
index e61d463962e..e9795108fa1 100644
--- a/FCHost/fchost/CMakeLists.txt
+++ b/FCHost/fchost/CMakeLists.txt
@@ -16,6 +16,8 @@ set(FCHOST_SRCS
   fchost_storage.h
   fchost_storage_js.cc
   fchost_storage_js.h
+  utility.cc
+  utility.h
   )
 set(FCHOST_SRCS_LINUX
   fchost_linux.cc
diff --git a/FCHost/fchost/fchost_app.cc b/FCHost/fchost/fchost_app.cc
index b15304c1594..3c5608a42c6 100644
--- a/FCHost/fchost/fchost_app.cc
+++ b/FCHost/fchost/fchost_app.cc
@@ -4,6 +4,8 @@
 
 #include "fchost_app.h"
 
+#include "./utility.h"
+
 #include "include/cef_browser.h"
 #include "include/cef_command_line.h"
 #include "include/views/cef_browser_view.h"
@@ -156,5 +158,42 @@ void FCHostApp::OnContextInitialized() {
 }
 
 void FCHostApp::OnContextCreated(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Context> context) {
-	FCHostStorageRegister(GetLocalStorePath().ToWString() + L"/FCHostPersistentStorage", context->GetGlobal());
+	FCHostStorageRegister(GetLocalStorePath() / "FCHostPersistentStorage", context->GetGlobal());
+}
+
+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;
+
+  if (commandLine->HasSwitch("enable-chrome-runtime")) {
+    // Enable experimental Chrome runtime. See issue #2969 for details.
+    settings.chrome_runtime = true;
+  }
+
+  // Cache location is required for local storage
+  cef_string_from_path(app->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
+  // use of the sandbox.
+#if !defined(CEF_USE_SANDBOX)
+    settings.no_sandbox = true;
+#endif
+
+  // Initialize CEF for the browser process.
+  CefInitialize(args, settings, app.get(), nullptr);
+
+  // Run the CEF message loop. This will block until CefQuitMessageLoop() is
+  // called.
+  CefRunMessageLoop();
+
+  // Shut down CEF.
+  CefShutdown();
+
+  return 0;
 }
diff --git a/FCHost/fchost/fchost_app.h b/FCHost/fchost/fchost_app.h
index 8bf31313266..52887c8754c 100644
--- a/FCHost/fchost/fchost_app.h
+++ b/FCHost/fchost/fchost_app.h
@@ -6,11 +6,14 @@
 
 #include "include/cef_app.h"
 
+#include <filesystem>
+
+class CefMainArgs;
+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 {
@@ -25,9 +28,12 @@ class FCHostApp : public CefApp, public CefBrowserProcessHandler, public CefRend
   virtual void OnContextInitialized() OVERRIDE;
   virtual void OnContextCreated(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Context> context) OVERRIDE;
 
-  CefString GetLocalStorePath();
+  std::filesystem::path GetLocalStorePath();
+
+  static int main(const CefMainArgs& args, CefCommandLine* commandLine);
 
  private:
+  FCHostApp();
   // Include the default reference counting implementation.
   IMPLEMENT_REFCOUNTING(FCHostApp);
 };
diff --git a/FCHost/fchost/fchost_handler.cc b/FCHost/fchost/fchost_handler.cc
index 95eec1b6e56..818538ca5bc 100644
--- a/FCHost/fchost/fchost_handler.cc
+++ b/FCHost/fchost/fchost_handler.cc
@@ -209,9 +209,9 @@ bool FCHostHandler::OnPreKeyEvent(CefRefPtr<CefBrowser> browser,
   #if defined (OS_WIN)
 			windowInfo.SetAsPopup(browser->GetHost()->GetWindowHandle(), "DevTools");
   #elif defined(OS_LINUX)
-      windowInfo.SetAsChild(browser->GetHost()->GetWindowHandle(), CefRect(100, 100, 800, 600));
+			windowInfo.SetAsChild(browser->GetHost()->GetWindowHandle(), CefRect(100, 100, 800, 600));
   #else
-    #error "Platform-specific code required"
+			#error "Platform-specific code required"
   #endif
 			browser->GetHost()->ShowDevTools(windowInfo, browser->GetHost()->GetClient(), settings, point);
 
diff --git a/FCHost/fchost/fchost_linux.cc b/FCHost/fchost/fchost_linux.cc
index 837df28715b..c79746c8968 100644
--- a/FCHost/fchost/fchost_linux.cc
+++ b/FCHost/fchost/fchost_linux.cc
@@ -13,11 +13,9 @@
 
 #include <filesystem>
 
-CefString FCHostApp::GetLocalStorePath()
+std::filesystem::path FCHostApp::GetLocalStorePath()
 {
-  std::filesystem::path ret = std::filesystem::path{getenv("HOME")} / ".local" / "share" / "FreeCities_Pregmod";
-
-	return CefString(ret.string());
+  return std::filesystem::path{getenv("HOME")} / ".local" / "share" / "FreeCities_Pregmod";
 }
 
 #if defined(CEF_X11)
@@ -62,42 +60,8 @@ int main(int argc, char* argv[]) {
   XSetIOErrorHandler(XIOErrorHandlerImpl);
 #endif
 
-  // Parse command-line arguments for use in this method.
   CefRefPtr<CefCommandLine> command_line = CefCommandLine::CreateCommandLine();
   command_line->InitFromArgv(argc, argv);
 
-  // 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;
-
-  if (command_line->HasSwitch("enable-chrome-runtime")) {
-    // Enable experimental Chrome runtime. See issue #2969 for details.
-    settings.chrome_runtime = true;
-  }
-
-  // Cache location is required for local storage
-  CefString local_storage = app->GetLocalStorePath();
-  cef_string_from_utf16(local_storage.c_str(), local_storage.length(), &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
-// use of the sandbox.
-#if !defined(CEF_USE_SANDBOX)
-  settings.no_sandbox = true;
-#endif
-
-  // Initialize CEF for the browser process.
-  CefInitialize(main_args, settings, app.get(), nullptr);
-
-  // Run the CEF message loop. This will block until CefQuitMessageLoop() is
-  // called.
-  CefRunMessageLoop();
-
-  // Shut down CEF.
-  CefShutdown();
-
-  return 0;
+  return FCHostApp::main(main_args, command_line);
 }
diff --git a/FCHost/fchost/fchost_storage.cc b/FCHost/fchost/fchost_storage.cc
index 7326e838cfa..7f9c27a6fb1 100644
--- a/FCHost/fchost/fchost_storage.cc
+++ b/FCHost/fchost/fchost_storage.cc
@@ -41,12 +41,8 @@ static bool SanitizePath(std::wstring& inpath)
 }
 */
 
-FCHostPersistentStorage::FCHostPersistentStorage(const CefString& _path)
-#if defined(OS_WIN)
-	: path(_path.ToWString())
-#else
-	: path(_path.ToString())
-#endif
+FCHostPersistentStorage::FCHostPersistentStorage(const std::filesystem::path& _path)
+	: path{_path}
 {
 	ensure_folder_exists();
 	load();
diff --git a/FCHost/fchost/fchost_storage.h b/FCHost/fchost/fchost_storage.h
index db036577572..c48da54bc1a 100644
--- a/FCHost/fchost/fchost_storage.h
+++ b/FCHost/fchost/fchost_storage.h
@@ -34,7 +34,7 @@ class FCHostPersistentStorage : public FCHostSessionStorage
 {
 	using base = FCHostSessionStorage;
 public:
-	FCHostPersistentStorage(const CefString& _path);
+	FCHostPersistentStorage(const std::filesystem::path& _path);
 
 	virtual void set(const CefString& key, CefRefPtr<CefV8Value> val) override;
 	virtual bool remove(const CefString& key) override;
diff --git a/FCHost/fchost/fchost_storage_js.cc b/FCHost/fchost/fchost_storage_js.cc
index d3e76d524c6..8b0374db990 100644
--- a/FCHost/fchost/fchost_storage_js.cc
+++ b/FCHost/fchost/fchost_storage_js.cc
@@ -24,7 +24,7 @@ static void AttachStorageFunctions(CefRefPtr<CefV8Value> object, CefRefPtr<CefV8
 	REGJSFUNC("clear");
 }
 
-void FCHostStorageRegister(const CefString& persistPath, CefRefPtr<CefV8Value> object) {
+void FCHostStorageRegister(const std::filesystem::path& persistPath, CefRefPtr<CefV8Value> object) {
 	// New context, reset everything (don't try to run more than one app at once!)
 	if (persist) persist.reset(NULL);
 	if (session) session.reset(NULL);
diff --git a/FCHost/fchost/fchost_storage_js.h b/FCHost/fchost/fchost_storage_js.h
index cf40c48a05d..bb948eb5a18 100644
--- a/FCHost/fchost/fchost_storage_js.h
+++ b/FCHost/fchost/fchost_storage_js.h
@@ -20,4 +20,4 @@ private:
 	IMPLEMENT_REFCOUNTING(FCHostStorageHandler);
 };
 
-void FCHostStorageRegister(const CefString& persistPath, CefRefPtr<CefV8Value> object);
+void FCHostStorageRegister(const std::filesystem::path& persistPath, CefRefPtr<CefV8Value> object);
diff --git a/FCHost/fchost/fchost_win.cc b/FCHost/fchost/fchost_win.cc
index 0e3a3f119f7..575f5340fb0 100644
--- a/FCHost/fchost/fchost_win.cc
+++ b/FCHost/fchost/fchost_win.cc
@@ -8,14 +8,13 @@
 
 #include "fchost_app.h"
 
-CefString FCHostApp::GetLocalStorePath()
+std::filesystem::path FCHostApp::GetLocalStorePath()
 {
 	PWSTR ppath;
 	SHGetKnownFolderPath(FOLDERID_Documents, 0, NULL, &ppath);
-	std::wstring local_storage = ppath;
-	local_storage += L"\\FreeCities_Pregmod";
+	std::filesystem::path local_storage = ppath;
 	CoTaskMemFree(ppath);
-	return local_storage.c_str();
+	return local_storage / L"FreeCities_Pregmod";
 }
 
 // Entry point function for all processes.
@@ -32,35 +31,17 @@ 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, app.get(), NULL);
+  int exit_code = CefExecuteProcess(main_args, nullptr, nullptr);
   if (exit_code >= 0) {
     // The sub-process has completed so return here.
     return exit_code;
   }
 
-  // Specify CEF global settings here.
-  CefSettings settings;
-
-  // Cache location is required for local storage
-  CefString local_storage = app->GetLocalStorePath();
-  cef_string_from_utf16(local_storage.c_str(), local_storage.length(), &settings.cache_path);
-
-  // Initialize CEF.
-  CefInitialize(main_args, settings, app.get(), NULL);
-
-  // Run the CEF message loop. This will block until CefQuitMessageLoop() is
-  // called.
-  CefRunMessageLoop();
-
-  // Shut down CEF.
-  CefShutdown();
+  CefRefPtr<CefCommandLine> command_line = CefCommandLine::CreateCommandLine();
+  command_line->InitFromString(lpCmdLine);
 
-  return 0;
+  return FCHostApp::main(main_args, command_line);
 }
diff --git a/FCHost/fchost/utility.cc b/FCHost/fchost/utility.cc
new file mode 100644
index 00000000000..ff7c7cde3a2
--- /dev/null
+++ b/FCHost/fchost/utility.cc
@@ -0,0 +1,16 @@
+#include "./utility.h"
+
+CefString cef_string_from_path(const std::filesystem::path& p)
+{
+	return CefString(p.native());
+}
+
+void cef_string_from_path(const std::filesystem::path& p, cef_string_t* str)
+{
+	const auto& pstr = p.native();
+#if defined(OS_WIN)
+	cef_string_from_utf16(pstr.c_str(), pstr.size(), str);
+#else
+	cef_string_from_utf8(pstr.c_str(), pstr.size(), str);
+#endif
+}
diff --git a/FCHost/fchost/utility.h b/FCHost/fchost/utility.h
new file mode 100644
index 00000000000..83a7ceec599
--- /dev/null
+++ b/FCHost/fchost/utility.h
@@ -0,0 +1,8 @@
+#pragma once
+
+#include "include/capi/cef_base_capi.h"
+
+#include <filesystem>
+
+CefString cef_string_from_path(const std::filesystem::path& p);
+void cef_string_from_path(const std::filesystem::path& p, cef_string_t* str);
-- 
GitLab