1 #ifndef _NAGERU_CEF_APP_H
2 #define _NAGERU_CEF_APP_H 1
4 // NageruCefApp deals with global state around CEF, in particular the global
5 // CEF event loop. CEF is pretty picky about which threads everything runs on;
6 // in particular, the documentation says CefExecute, CefInitialize and
7 // CefRunMessageLoop must all be on the main thread (ie., the first thread
8 // created). However, Qt wants to run _its_ event loop on this thread, too,
9 // and integrating the two has proved problematic (see also the comment in
10 // main.cpp). It seems that as long as you don't have two GLib loops running,
11 // it's completely fine in practice to have a separate thread for the main loop
12 // (running CefInitialize, CefRunMessageLoop, and finally CefDestroy).
13 // Many other tasks (like most things related to interacting with browsers)
14 // have to be run from the message loop, but that's fine; CEF gives us tools
15 // to post tasks to it.
20 #include <cef_browser.h>
21 #include <cef_client.h>
22 #include <cef_version.h>
25 #include <condition_variable>
28 #include <unordered_set>
32 // Takes in arbitrary lambdas and converts them to something CefPostTask() will accept.
33 class CEFTaskAdapter : public CefTask
36 CEFTaskAdapter(const std::function<void()>&& func)
37 : func(std::move(func)) {}
38 void Execute() override { func(); }
41 std::function<void()> func;
43 IMPLEMENT_REFCOUNTING(CEFTaskAdapter);
46 // Runs and stops the CEF event loop, and also makes some startup tasks.
47 class NageruCefApp : public CefApp, public CefRenderProcessHandler, public CefBrowserProcessHandler {
51 // Starts up the CEF main loop if it does not already run, and blocks until
52 // CEF is properly initialized. You can call initialize_ref() multiple times,
53 // which will then increase the refcount.
54 void initialize_cef();
56 // If the refcount goes to zero, shut down the main loop and uninitialize CEF.
59 // Closes the given browser, and blocks until it is done closing.
61 // NOTE: We can't call unref_cef() from close_browser(), since
62 // CefRefPtr<T> does not support move semantics, so it would have a
63 // refcount of either zero or two going into close_browser (not one,
64 // as it should). The latter means the caller would hold on to an extra
65 // reference to the browser (which triggers an assert failure), and the
66 // former would mean that the browser gets deleted before it's closed.
67 void close_browser(CefRefPtr<CefBrowser> browser);
69 CefRefPtr<CefRenderProcessHandler> GetRenderProcessHandler() override
74 CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler() override
79 void OnBeforeCommandLineProcessing(const CefString& process_type, CefRefPtr<CefCommandLine> command_line) override;
82 void cef_thread_func();
84 std::thread cef_thread;
86 int cef_thread_refcount = 0; // Under <cef_mutex>.
87 bool cef_initialized = false; // Under <cef_mutex>.
88 std::condition_variable cef_initialized_cond;
90 IMPLEMENT_REFCOUNTING(NageruCefApp);
93 #endif // !defined(_NAGERU_CEF_APP_H)