]> git.sesse.net Git - nageru/blob - nageru_cef_app.h
Release Nageru 1.7.2.
[nageru] / nageru_cef_app.h
1 #ifndef _NAGERU_CEF_APP_H
2 #define _NAGERU_CEF_APP_H 1
3
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.
16
17 #include <stdio.h>
18
19 #include <cef_app.h>
20 #include <cef_browser.h>
21 #include <cef_client.h>
22 #include <cef_version.h>
23
24 #include <atomic>
25 #include <condition_variable>
26 #include <functional>
27 #include <mutex>
28 #include <unordered_set>
29 #include <thread>
30 #include <vector>
31
32 // Takes in arbitrary lambdas and converts them to something CefPostTask() will accept.
33 class CEFTaskAdapter : public CefTask
34 {
35 public:
36         CEFTaskAdapter(const std::function<void()>&& func)
37                 : func(std::move(func)) {}
38         void Execute() override { func(); }
39
40 private:
41         std::function<void()> func;
42
43         IMPLEMENT_REFCOUNTING(CEFTaskAdapter);
44 };
45
46 // Runs and stops the CEF event loop, and also makes some startup tasks.
47 class NageruCefApp : public CefApp, public CefRenderProcessHandler, public CefBrowserProcessHandler {
48 public:
49         NageruCefApp() {}
50
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();
55
56         // If the refcount goes to zero, shut down the main loop and uninitialize CEF.
57         void unref_cef();
58
59         // Closes the given browser, and blocks until it is done closing.
60         //
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);
68
69         CefRefPtr<CefRenderProcessHandler> GetRenderProcessHandler() override
70         {
71                 return this;
72         }
73
74         CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler() override
75         {
76                 return this;
77         }
78
79         void OnBeforeCommandLineProcessing(const CefString& process_type, CefRefPtr<CefCommandLine> command_line) override;
80
81 private:
82         void cef_thread_func();
83
84         std::thread cef_thread;
85         std::mutex cef_mutex;
86         int cef_thread_refcount = 0;  // Under <cef_mutex>.
87         bool cef_initialized = false;  // Under <cef_mutex>.
88         std::condition_variable cef_initialized_cond;
89
90         IMPLEMENT_REFCOUNTING(NageruCefApp);
91 };
92
93 #endif  // !defined(_NAGERU_CEF_APP_H)