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* sandbox_info = nullptr;
41 
42 #ifdef CEF_ENABLE_SANDBOX
43  sandbox_info = &sandbox;
44 #endif
45 
46 #ifdef __linux
47  // Must force GPU disabled
48  char** oldArgs = args;
49 
50  argcount += 1;
51  args = new char*[argcount];
52 
53  std::unique_ptr<char, decltype(std::free)*> disablegpu(strdup("--disable-gpu"), std::free);
54 
55  for(int i = 0; i < argcount; ++i) {
56  if(i < argcount - 1) {
57  args[i] = oldArgs[i];
58  } else {
59  args[i] = disablegpu.get();
60  }
61  }
62 
63 #endif
64 
65  // Provide CEF with command-line arguments //
66 #ifdef _WIN32
67  CefMainArgs main_args(hInstance);
68 #else
69  CefMainArgs main_args(argcount, args);
70 #endif // _WIN32
71 
72  // Callback object //
73  keeper->CEFApp = CefRefPtr<GUI::CefApplication>(new GUI::CefApplication());
74 
75  // Check are we a sub process //
76  int exit_code = CefExecuteProcess(main_args, keeper->CEFApp.get(), sandbox_info);
77  if(exit_code >= 0) {
78  // This was a sub process //
79  returnvalue = exit_code;
80 
81 #ifdef __linux
82  delete[] args;
83 #endif
84  return true;
85  }
86 
87  // Specify CEF global settings here.
88  CefSettings settings;
89 
90  // Apparently this "just works" on non-windows platforms
91 #if !defined(CEF_ENABLE_SANDBOX) && defined(_WIN32)
92  settings.no_sandbox = true;
93 #endif
94 
95  try {
96  CefString(&settings.locales_dir_path) =
97  boost::filesystem::canonical("locales").wstring();
98 
99  const auto currentCanonical = boost::filesystem::canonical("./").wstring();
100 
101  CefString(&settings.resources_dir_path) = currentCanonical;
102 
103  CefString(&settings.log_file) =
104  currentCanonical + Convert::Utf8ToUtf16("/" + logname + "LogCEF.txt");
105 
106  const auto cachePath = currentCanonical + L"/Data/Cache/CEF";
107  boost::filesystem::create_directories(cachePath);
108 
109  CefString(&settings.cache_path) = cachePath;
110 
111  } catch(const boost::filesystem::filesystem_error& e) {
112 
113  std::cout << "Error missing file or accessing cache location: " << e.what()
114  << std::endl;
115  abort();
116  }
117 
118  // TODO: log_severity
119 
120  // TODO: user agent
121 
122  settings.windowless_rendering_enabled = true;
123 
124  settings.single_process = false;
125 
126 #ifdef _WIN32
127  // Only works on windows
128  // And the OnPaint assumes it is on the main thread so this doesn't work
129  settings.multi_threaded_message_loop = false;
130 #endif
131 
132  // Initialize CEF.
133  CefInitialize(main_args, settings, keeper->CEFApp.get(), sandbox_info);
134 
135  CEFInitialized = true;
136 
137  AccessToThese = keeper.get();
138 
139 #ifdef __linux
140  delete[] args;
141 #endif
142 
143  // Wasn't a sub process //
144  return false;
145 }
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 147 of file GlobalCEFHandler.cpp.

148 {
149  if(!CEFInitialized)
150  return;
151 
152  // Close it down //
153  CefShutdown();
154 
155  // The automatic templates remove the need for this message, which won't be logged anyway
156  // std::cout << "CEF shutdown called" << std::endl;
157 }
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 159 of file GlobalCEFHandler.cpp.

160 {
161  if(!CEFInitialized) {
162  LOG_ERROR("DoCEFMessageLoopWork called before CEF is initialized");
163  return;
164  }
165 
166  CefDoMessageLoopWork();
167 }
#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 169 of file GlobalCEFHandler.cpp.

170 {
171  return AccessToThese;
172 }
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 213 of file GlobalCEFHandler.cpp.

214 {
215  return CustomJSHandlers;
216 }
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 258 of file GlobalCEFHandler.cpp.

261 {
262  // Pass to the extensions until it is handled //
263  for(const auto& ext : CustomExtensions) {
264 
265  if(ext->MessageHandler) {
266 
267  if(ext->MessageHandler->OnProcessMessageReceived(browser, source_process, message))
268  return true;
269  }
270  }
271 
272  return false;
273 }
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 251 of file GlobalCEFHandler.cpp.

253 {
254  CustomExtensions.push_back(extension);
255  AccessToThese->CEFApp->RegisterCustomExtension(extension);
256 }
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 174 of file GlobalCEFHandler.cpp.

176 {
177  std::unique_lock<std::recursive_mutex> guard(JSCustomMutex);
178 
179  // Add it //
180  CustomJSHandlers.push_back(ptr);
181 
182  // Notify all //
183  for(size_t i = 0; i < JSAsynToNotify.size(); i++) {
185  JSAsynToNotify[i]->RegisterNewCustom(guard2, ptr);
186  }
187 
188  // Things created after this will automatically retrieve the ones that are registered
189  // before it is created
190 }
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:95
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 218 of file GlobalCEFHandler.cpp.

219 {
220  std::unique_lock<std::recursive_mutex> guard(JSCustomMutex);
221 
222  JSAsynToNotify.push_back(ptr);
223 }
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 192 of file GlobalCEFHandler.cpp.

194 {
195  boost::unique_lock<std::recursive_mutex> guard(JSCustomMutex);
196 
197  // Notify all objects //
198  for(size_t i = 0; i < JSAsynToNotify.size(); i++) {
199  JSAsynToNotify[i]->UnregisterCustom(toremove);
200  }
201 
202  // Compare pointers and remove it //
203  for(size_t i = 0; i < CustomJSHandlers.size(); i++) {
204  if(CustomJSHandlers[i].get() == toremove) {
205 
206  CustomJSHandlers.erase(CustomJSHandlers.begin() + i);
207  return;
208  }
209  }
210 }
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 225 of file GlobalCEFHandler.cpp.

227 {
228  std::unique_lock<std::recursive_mutex> guard(JSCustomMutex);
229 
230  for(size_t i = 0; i < JSAsynToNotify.size(); i++) {
231 
232  if(JSAsynToNotify[i] == ptr) {
233 
234  JSAsynToNotify.erase(JSAsynToNotify.begin() + i);
235  return;
236  }
237  }
238 }
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: