Leviathan  0.8.0.0
Leviathan game engine
Leviathan::GlobalCEFHandler Class Reference

Singleton class for handling CEF initialization that needs to be done right away. More...

#include <GlobalCEFHandler.h>

Static Public Member Functions

static DLLEXPORT bool CEFFirstCheckChildProcess (int argcount, char *args[], int &returnvalue, std::shared_ptr< CEFApplicationKeeper > &keeper, const std::string &logname)
 This is the first function called in the Engine to handle CEF child processes. More...
 
static DLLEXPORT void CEFLastThingInProgram ()
 
static DLLEXPORT CEFApplicationKeeperGetCEFObjects ()
 
static DLLEXPORT void DoCEFMessageLoopWork ()
 
static DLLEXPORT void RegisterCustomJavaScriptQueryHandler (std::shared_ptr< GUI::JSAsyncCustom > ptr)
 Registers a new JavaScript query handler. More...
 
static DLLEXPORT void UnRegisterCustomJavaScriptQueryHandler (GUI::JSAsyncCustom *toremove)
 Unregisters a registered query handler. More...
 
static DLLEXPORT const std::vector< std::shared_ptr< GUI::JSAsyncCustom > > & GetRegisteredCustomHandlers ()
 Mainly allows LeviathanJavaScriptAsync to access the list of handlers. More...
 
static DLLEXPORT void RegisterCustomExtension (std::shared_ptr< GUI::CustomExtension > extension)
 Registers a custom extension for all render processes to load as a V8 extension. More...
 
static DLLEXPORT void RegisterJSAsync (GUI::LeviathanJavaScriptAsync *ptr)
 Registers a LeviathanJavaScriptAsync to receive notifications about JSAsyncCustom changes. More...
 
static DLLEXPORT void UnRegisterJSAsync (GUI::LeviathanJavaScriptAsync *ptr)
 Unregisters a registered LeviathanJavaScriptAsync. More...
 
static DLLEXPORT bool HandleCustomExtensionProcessMessage (CefRefPtr< CefBrowser > browser, CefProcessId source_process, CefRefPtr< CefProcessMessage > message)
 Handles passing a custom process message. More...
 

Static Protected Attributes

static bool CEFInitialized = false
 A flag for making sure that functions are only ran if CEF is actually used. More...
 
static CEFApplicationKeeperAccessToThese = NULL
 
static std::vector< std::shared_ptr< GUI::JSAsyncCustom > > CustomJSHandlers
 Stores all the custom handlers. More...
 
static std::vector< GUI::LeviathanJavaScriptAsync * > JSAsynToNotify
 Stored to be able to notify all LeviathanJavaScriptAsync objects. More...
 
static std::recursive_mutex JSCustomMutex
 
static std::vector< std::shared_ptr< GUI::CustomExtension > > CustomExtensions
 Custom extension storage here to allow routing messages to the right one. More...
 

Detailed Description

Singleton class for handling CEF initialization that needs to be done right away.

Definition at line 37 of file GlobalCEFHandler.h.

Member Function Documentation

◆ CEFFirstCheckChildProcess()

DLLEXPORT bool Leviathan::GlobalCEFHandler::CEFFirstCheckChildProcess ( int  argcount,
char *  args[],
int &  returnvalue,
std::shared_ptr< CEFApplicationKeeper > &  keeper,
const std::string &  logname 
)
static

This is the first function called in the Engine to handle CEF child processes.

This function will check command line arguments and possibly just run a subprocess or continue with the main application Passing command line argument of "--nogui" will skip this step and CEF initialization

Definition at line 15 of file GlobalCEFHandler.cpp.

27 {
28  // Check for no graphics mode //
29  for(int i = 0; i < argcount; ++i) {
30  if(std::string_view(args[i]).find("--nogui") != std::string_view::npos) {
31  // No gui commandline specified //
32  std::cout << "Not using CEF because --nogui is specified" << std::endl;
33  return false;
34  }
35  }
36 
37  // Run CEF startup //
38  keeper = std::make_shared<CEFApplicationKeeper>();
39 
40  void* windows_sandbox_info = nullptr;
41 
42 #ifdef CEF_ENABLE_SANDBOX
43  windows_sandbox_info = &sandbox;
44 #endif
45 
46  // Provide CEF with command-line arguments //
47 #ifdef _WIN32
48  CefMainArgs main_args(hInstance);
49 #else
50  CefMainArgs main_args(argcount, args);
51 #endif // _WIN32
52 
53  // Callback object //
54  keeper->CEFApp = CefRefPtr<GUI::CefApplication>(new GUI::CefApplication());
55 
56  // Check are we a sub process //
57  int exit_code = CefExecuteProcess(main_args, keeper->CEFApp.get(), windows_sandbox_info);
58  if(exit_code >= 0) {
59  // This was a sub process //
60  returnvalue = exit_code;
61 
62  return true;
63  }
64 
65  // Specify CEF global settings here.
66  CefSettings settings;
67 
68  // Apparently this "just works" on non-windows platforms
69 #if !defined(CEF_ENABLE_SANDBOX) && defined(_WIN32)
70  settings.no_sandbox = true;
71 #endif
72 
73  try {
74  CefString(&settings.locales_dir_path) =
75  boost::filesystem::canonical("locales").wstring();
76 
77  const auto currentCanonical = boost::filesystem::canonical("./").wstring();
78 
79  CefString(&settings.resources_dir_path) = currentCanonical;
80 
81  CefString(&settings.log_file) =
82  currentCanonical + Convert::Utf8ToUtf16("/" + logname + "LogCEF.txt");
83 
84  const auto cachePath = currentCanonical + L"/Data/Cache/CEF";
85  boost::filesystem::create_directories(cachePath);
86 
87  CefString(&settings.cache_path) = cachePath;
88 
89  } catch(const boost::filesystem::filesystem_error& e) {
90 
91  std::stringstream msg;
92  msg << "Error missing file or accessing cache location: " << e.what() << "\n";
93 
94 
95  std::ofstream write(std::string("Leviathan_start_failure_") +
96 #ifdef __linux
97  std::to_string(::getpid()) +
98 #endif //__linux
99  ".txt");
100  write << msg.str();
101  write << "Args are (" << argcount << ")" << std::endl;
102  for(int i = 0; i < argcount; ++i)
103  write << args[i] << std::endl;
104  write << std::endl;
105  write.close();
106 
107  std::cout << msg.str();
108  abort();
109  }
110 
111  // TODO: log_severity
112  // settings.log_severity = cef_log_severity_t::LOGSEVERITY_DEBUG;
113 
114  // TODO: user agent
115 
116  settings.windowless_rendering_enabled = true;
117 
118  settings.external_message_pump = true;
119 
120  // Apparently this is missing from the windows version but not the linux version. For some
121  // reason?
122  // settings.single_process = false;
123 
124  // // Enable remote debugging
125  // settings.remote_debugging_port = 9090;
126 
127  // Only works on windows
128  // And the OnPaint assumes it is on the main thread so this doesn't work at all
129  settings.multi_threaded_message_loop = false;
130 
131  // Initialize CEF.
132  CefInitialize(main_args, settings, keeper->CEFApp.get(), windows_sandbox_info);
133 
134  CEFInitialized = true;
135 
136  AccessToThese = keeper.get();
137 
138  // Wasn't a sub process //
139  return false;
140 }
static DLLEXPORT std::wstring Utf8ToUtf16(const std::string &utf8str)
Decodes an UTF8 string to an UTF16 string (wide string/wstring)
Definition: Convert.cpp:76
Handler for new render processes.
static bool CEFInitialized
A flag for making sure that functions are only ran if CEF is actually used.
static CEFApplicationKeeper * AccessToThese

◆ CEFLastThingInProgram()

DLLEXPORT void Leviathan::GlobalCEFHandler::CEFLastThingInProgram ( )
static

Definition at line 142 of file GlobalCEFHandler.cpp.

143 {
144  if(!CEFInitialized)
145  return;
146 
147  // Close it down //
148  CefShutdown();
149 
150  // The automatic templates remove the need for this message, which won't be logged anyway
151  // std::cout << "CEF shutdown called" << std::endl;
152 }
static bool CEFInitialized
A flag for making sure that functions are only ran if CEF is actually used.

◆ DoCEFMessageLoopWork()

DLLEXPORT void Leviathan::GlobalCEFHandler::DoCEFMessageLoopWork ( )
static

Definition at line 154 of file GlobalCEFHandler.cpp.

155 {
156  if(!CEFInitialized) {
157  LOG_ERROR("DoCEFMessageLoopWork called before CEF is initialized");
158  return;
159  }
160 
161  CefDoMessageLoopWork();
162 }
#define LOG_ERROR(x)
Definition: Define.h:83
static bool CEFInitialized
A flag for making sure that functions are only ran if CEF is actually used.

◆ GetCEFObjects()

DLLEXPORT CEFApplicationKeeper * Leviathan::GlobalCEFHandler::GetCEFObjects ( )
static

Definition at line 164 of file GlobalCEFHandler.cpp.

165 {
166  return AccessToThese;
167 }
static CEFApplicationKeeper * AccessToThese

◆ GetRegisteredCustomHandlers()

DLLEXPORT const std::vector< std::shared_ptr< GUI::JSAsyncCustom > > & Leviathan::GlobalCEFHandler::GetRegisteredCustomHandlers ( )
static

Mainly allows LeviathanJavaScriptAsync to access the list of handlers.

Definition at line 208 of file GlobalCEFHandler.cpp.

209 {
210  return CustomJSHandlers;
211 }
static std::vector< std::shared_ptr< GUI::JSAsyncCustom > > CustomJSHandlers
Stores all the custom handlers.

◆ HandleCustomExtensionProcessMessage()

DLLEXPORT bool GlobalCEFHandler::HandleCustomExtensionProcessMessage ( CefRefPtr< CefBrowser >  browser,
CefProcessId  source_process,
CefRefPtr< CefProcessMessage >  message 
)
static

Handles passing a custom process message.

Definition at line 253 of file GlobalCEFHandler.cpp.

256 {
257  // Pass to the extensions until it is handled //
258  for(const auto& ext : CustomExtensions) {
259 
260  if(ext->MessageHandler) {
261 
262  if(ext->MessageHandler->OnProcessMessageReceived(browser, source_process, message))
263  return true;
264  }
265  }
266 
267  return false;
268 }
static std::vector< std::shared_ptr< GUI::CustomExtension > > CustomExtensions
Custom extension storage here to allow routing messages to the right one.

◆ RegisterCustomExtension()

DLLEXPORT void GlobalCEFHandler::RegisterCustomExtension ( std::shared_ptr< GUI::CustomExtension extension)
static

Registers a custom extension for all render processes to load as a V8 extension.

Note
Only one should ever be registered, for performance reasons

Definition at line 246 of file GlobalCEFHandler.cpp.

248 {
249  CustomExtensions.push_back(extension);
250  AccessToThese->CEFApp->RegisterCustomExtension(extension);
251 }
static std::vector< std::shared_ptr< GUI::CustomExtension > > CustomExtensions
Custom extension storage here to allow routing messages to the right one.
CefRefPtr< GUI::CefApplication > CEFApp
static CEFApplicationKeeper * AccessToThese

◆ RegisterCustomJavaScriptQueryHandler()

DLLEXPORT void Leviathan::GlobalCEFHandler::RegisterCustomJavaScriptQueryHandler ( std::shared_ptr< GUI::JSAsyncCustom ptr)
static

Registers a new JavaScript query handler.

Parameters
newdptrPass a newed object or NULL
Todo:
Add support for removing existing ones

Definition at line 169 of file GlobalCEFHandler.cpp.

171 {
172  std::unique_lock<std::recursive_mutex> guard(JSCustomMutex);
173 
174  // Add it //
175  CustomJSHandlers.push_back(ptr);
176 
177  // Notify all //
178  for(size_t i = 0; i < JSAsynToNotify.size(); i++) {
180  JSAsynToNotify[i]->RegisterNewCustom(guard2, ptr);
181  }
182 
183  // Things created after this will automatically retrieve the ones that are registered
184  // before it is created
185 }
static std::vector< GUI::LeviathanJavaScriptAsync * > JSAsynToNotify
Stored to be able to notify all LeviathanJavaScriptAsync objects.
#define GUARD_LOCK_OTHER_NAME(x, y)
Definition: ThreadSafe.h:113
static std::recursive_mutex JSCustomMutex
static std::vector< std::shared_ptr< GUI::JSAsyncCustom > > CustomJSHandlers
Stores all the custom handlers.

◆ RegisterJSAsync()

DLLEXPORT void Leviathan::GlobalCEFHandler::RegisterJSAsync ( GUI::LeviathanJavaScriptAsync ptr)
static

Registers a LeviathanJavaScriptAsync to receive notifications about JSAsyncCustom changes.

Definition at line 213 of file GlobalCEFHandler.cpp.

214 {
215  std::unique_lock<std::recursive_mutex> guard(JSCustomMutex);
216 
217  JSAsynToNotify.push_back(ptr);
218 }
static std::vector< GUI::LeviathanJavaScriptAsync * > JSAsynToNotify
Stored to be able to notify all LeviathanJavaScriptAsync objects.
static std::recursive_mutex JSCustomMutex

◆ UnRegisterCustomJavaScriptQueryHandler()

DLLEXPORT void Leviathan::GlobalCEFHandler::UnRegisterCustomJavaScriptQueryHandler ( GUI::JSAsyncCustom toremove)
static

Unregisters a registered query handler.

See also
RegisterCustomJavaScriptQueryHandler

Definition at line 187 of file GlobalCEFHandler.cpp.

189 {
190  boost::unique_lock<std::recursive_mutex> guard(JSCustomMutex);
191 
192  // Notify all objects //
193  for(size_t i = 0; i < JSAsynToNotify.size(); i++) {
194  JSAsynToNotify[i]->UnregisterCustom(toremove);
195  }
196 
197  // Compare pointers and remove it //
198  for(size_t i = 0; i < CustomJSHandlers.size(); i++) {
199  if(CustomJSHandlers[i].get() == toremove) {
200 
201  CustomJSHandlers.erase(CustomJSHandlers.begin() + i);
202  return;
203  }
204  }
205 }
static std::vector< GUI::LeviathanJavaScriptAsync * > JSAsynToNotify
Stored to be able to notify all LeviathanJavaScriptAsync objects.
static std::recursive_mutex JSCustomMutex
static std::vector< std::shared_ptr< GUI::JSAsyncCustom > > CustomJSHandlers
Stores all the custom handlers.

◆ UnRegisterJSAsync()

DLLEXPORT void Leviathan::GlobalCEFHandler::UnRegisterJSAsync ( GUI::LeviathanJavaScriptAsync ptr)
static

Unregisters a registered LeviathanJavaScriptAsync.

See also
RegisterJSAsync

Definition at line 220 of file GlobalCEFHandler.cpp.

222 {
223  std::unique_lock<std::recursive_mutex> guard(JSCustomMutex);
224 
225  for(size_t i = 0; i < JSAsynToNotify.size(); i++) {
226 
227  if(JSAsynToNotify[i] == ptr) {
228 
229  JSAsynToNotify.erase(JSAsynToNotify.begin() + i);
230  return;
231  }
232  }
233 }
static std::vector< GUI::LeviathanJavaScriptAsync * > JSAsynToNotify
Stored to be able to notify all LeviathanJavaScriptAsync objects.
static std::recursive_mutex JSCustomMutex

Member Data Documentation

◆ AccessToThese

CEFApplicationKeeper * Leviathan::GlobalCEFHandler::AccessToThese = NULL
staticprotected

Definition at line 99 of file GlobalCEFHandler.h.

◆ CEFInitialized

bool Leviathan::GlobalCEFHandler::CEFInitialized = false
staticprotected

A flag for making sure that functions are only ran if CEF is actually used.

Definition at line 98 of file GlobalCEFHandler.h.

◆ CustomExtensions

std::vector< std::shared_ptr< GUI::CustomExtension > > GlobalCEFHandler::CustomExtensions
staticprotected

Custom extension storage here to allow routing messages to the right one.

Definition at line 110 of file GlobalCEFHandler.h.

◆ CustomJSHandlers

std::vector< std::shared_ptr< GUI::JSAsyncCustom > > Leviathan::GlobalCEFHandler::CustomJSHandlers
staticprotected

Stores all the custom handlers.

Definition at line 102 of file GlobalCEFHandler.h.

◆ JSAsynToNotify

std::vector< GUI::LeviathanJavaScriptAsync * > Leviathan::GlobalCEFHandler::JSAsynToNotify
staticprotected

Stored to be able to notify all LeviathanJavaScriptAsync objects.

Definition at line 105 of file GlobalCEFHandler.h.

◆ JSCustomMutex

std::recursive_mutex Leviathan::GlobalCEFHandler::JSCustomMutex
staticprotected

Definition at line 107 of file GlobalCEFHandler.h.


The documentation for this class was generated from the following files: