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  // Make a copy of the args to not have CEF mess with them
51  std::vector<char*> cefArgs;
52  std::vector<std::string> cefArgData;
53 
54  cefArgs.resize(argcount);
55  cefArgData.reserve(argcount);
56 
57  for(int i = 0; i < argcount; ++i) {
58 
59  cefArgData.push_back(args[i]);
60  cefArgs[i] = cefArgData.back().data();
61  }
62 
63  CefMainArgs main_args(cefArgs.size(), cefArgs.data());
64  // CefMainArgs main_args(argcount, args);
65 #endif // _WIN32
66 
67  // Callback object //
68  keeper->CEFApp = CefRefPtr<GUI::CefApplication>(new GUI::CefApplication());
69 
70  // Check are we a sub process //
71  int exit_code = CefExecuteProcess(main_args, keeper->CEFApp.get(), windows_sandbox_info);
72  if(exit_code >= 0) {
73  // This was a sub process //
74  returnvalue = exit_code;
75 
76  return true;
77  }
78 
79  // Specify CEF global settings here.
80  CefSettings settings;
81 
82  // Apparently this "just works" on non-windows platforms
83 #if !defined(CEF_ENABLE_SANDBOX) && defined(_WIN32)
84  settings.no_sandbox = true;
85 #endif
86 
87  try {
88  CefString(&settings.locales_dir_path) =
89  boost::filesystem::canonical("Data/locales").wstring();
90 
91  const auto currentCanonical = boost::filesystem::canonical("./").wstring();
92 
93  // CefString(&settings.resources_dir_path) = currentCanonical;
94 
95  CefString(&settings.log_file) =
96  currentCanonical + Convert::Utf8ToUtf16("/" + logname + "LogCEF.txt");
97 
98  const auto cachePath = currentCanonical + L"/Data/Cache/CEF";
99  boost::filesystem::create_directories(cachePath);
100 
101  CefString(&settings.cache_path) = cachePath;
102 
103  } catch(const boost::filesystem::filesystem_error& e) {
104 
105  std::stringstream msg;
106  msg << "Error missing file or accessing cache location: " << e.what() << "\n";
107 
108 
109  std::ofstream write(std::string("Leviathan_start_failure_") +
110 #ifdef __linux
111  std::to_string(::getpid()) +
112 #endif //__linux
113  ".txt");
114  write << msg.str();
115  write << "Args are (" << argcount << ")" << std::endl;
116  for(int i = 0; i < argcount; ++i)
117  write << args[i] << std::endl;
118  write << std::endl;
119  write.close();
120 
121  std::cout << msg.str();
122  abort();
123  }
124 
125  // TODO: log_severity
126  // settings.log_severity = cef_log_severity_t::LOGSEVERITY_DEBUG;
127 
128  // TODO: user agent
129 
130  settings.windowless_rendering_enabled = true;
131 
132  settings.external_message_pump = true;
133 
134  // Apparently this is missing from the windows version but not the linux version. For some
135  // reason?
136  // settings.single_process = false;
137 
138  // // Enable remote debugging
139  // settings.remote_debugging_port = 9090;
140 
141  // Only works on windows
142  // And the OnPaint assumes it is on the main thread so this doesn't work at all
143  settings.multi_threaded_message_loop = false;
144 
145  // Initialize CEF.
146  CefInitialize(main_args, settings, keeper->CEFApp.get(), windows_sandbox_info);
147 
148  CEFInitialized = true;
149 
150  AccessToThese = keeper.get();
151 
152  // Wasn't a sub process //
153  return false;
154 }
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.
CefRefPtr< GUI::CefApplication > CEFApp
static CEFApplicationKeeper * AccessToThese

◆ CEFLastThingInProgram()

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

Definition at line 156 of file GlobalCEFHandler.cpp.

157 {
158  if(!CEFInitialized)
159  return;
160 
161  // Close it down //
162  CefShutdown();
163 
164  // The automatic templates remove the need for this message, which won't be logged anyway
165  // std::cout << "CEF shutdown called" << std::endl;
166 }
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 168 of file GlobalCEFHandler.cpp.

169 {
170  if(!CEFInitialized) {
171  LOG_ERROR("DoCEFMessageLoopWork called before CEF is initialized");
172  return;
173  }
174 
175  CefDoMessageLoopWork();
176 }
#define LOG_ERROR(x)
Definition: Define.h:94
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 178 of file GlobalCEFHandler.cpp.

179 {
180  return AccessToThese;
181 }
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 222 of file GlobalCEFHandler.cpp.

223 {
224  return CustomJSHandlers;
225 }
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 267 of file GlobalCEFHandler.cpp.

270 {
271  // Pass to the extensions until it is handled //
272  for(const auto& ext : CustomExtensions) {
273 
274  if(ext->MessageHandler) {
275 
276  if(ext->MessageHandler->OnProcessMessageReceived(browser, source_process, message))
277  return true;
278  }
279  }
280 
281  return false;
282 }
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 260 of file GlobalCEFHandler.cpp.

262 {
263  CustomExtensions.push_back(extension);
264  AccessToThese->CEFApp->RegisterCustomExtension(extension);
265 }
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 183 of file GlobalCEFHandler.cpp.

185 {
186  std::unique_lock<std::recursive_mutex> guard(JSCustomMutex);
187 
188  // Add it //
189  CustomJSHandlers.push_back(ptr);
190 
191  // Notify all //
192  for(size_t i = 0; i < JSAsynToNotify.size(); i++) {
194  JSAsynToNotify[i]->RegisterNewCustom(guard2, ptr);
195  }
196 
197  // Things created after this will automatically retrieve the ones that are registered
198  // before it is created
199 }
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:127
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 227 of file GlobalCEFHandler.cpp.

228 {
229  std::unique_lock<std::recursive_mutex> guard(JSCustomMutex);
230 
231  JSAsynToNotify.push_back(ptr);
232 }
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 201 of file GlobalCEFHandler.cpp.

203 {
204  std::unique_lock<std::recursive_mutex> guard(JSCustomMutex);
205 
206  // Notify all objects //
207  for(size_t i = 0; i < JSAsynToNotify.size(); i++) {
208  JSAsynToNotify[i]->UnregisterCustom(toremove);
209  }
210 
211  // Compare pointers and remove it //
212  for(size_t i = 0; i < CustomJSHandlers.size(); i++) {
213  if(CustomJSHandlers[i].get() == toremove) {
214 
215  CustomJSHandlers.erase(CustomJSHandlers.begin() + i);
216  return;
217  }
218  }
219 }
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 234 of file GlobalCEFHandler.cpp.

236 {
237  std::unique_lock<std::recursive_mutex> guard(JSCustomMutex);
238 
239  for(size_t i = 0; i < JSAsynToNotify.size(); i++) {
240 
241  if(JSAsynToNotify[i] == ptr) {
242 
243  JSAsynToNotify.erase(JSAsynToNotify.begin() + i);
244  return;
245  }
246  }
247 }
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: