]> git.sesse.net Git - nageru/blob - nageru/nageru_cef_app.cpp
Set CEF autoplay policy to be more lenient.
[nageru] / nageru / nageru_cef_app.cpp
1 #include <cef_app.h>
2 #include <cef_browser.h>
3 #include <cef_command_line.h>
4 #include <cef_task.h>
5 #include <QTimer>
6 #include <QWidget>
7 #include <mutex>
8 #include <thread>
9
10 #include "nageru_cef_app.h"
11
12 using namespace std;
13
14 void NageruCefApp::OnBeforeCommandLineProcessing(
15         const CefString& process_type,
16         CefRefPtr<CefCommandLine> command_line)
17 {
18         command_line->AppendSwitch("disable-gpu");
19         command_line->AppendSwitch("disable-gpu-compositing");
20         command_line->AppendSwitch("enable-begin-frame-scheduling");
21         command_line->AppendSwitchWithValue("autoplay-policy", "no-user-gesture-required");
22
23         // https://bitbucket.org/chromiumembedded/cef/issues/2717/xmlhttprequest-empty-responsetext
24         command_line->AppendSwitch("disable-web-security");
25 }
26
27 void NageruCefApp::initialize_cef()
28 {
29         unique_lock<mutex> lock(cef_mutex);
30         if (cef_thread_refcount++ == 0) {
31                 cef_thread = thread(&NageruCefApp::cef_thread_func, this);
32         }
33         cef_initialized_cond.wait(lock, [this]{ return cef_initialized; });
34 }
35
36 void NageruCefApp::close_browser(CefRefPtr<CefBrowser> browser)
37 {
38         lock_guard<mutex> lock(cef_mutex);
39         browser->GetHost()->CloseBrowser(/*force_close=*/true);
40 }
41
42 void NageruCefApp::unref_cef()
43 {
44         unique_lock<mutex> lock(cef_mutex);
45         if (--cef_thread_refcount == 0) {
46                 CefPostTask(TID_UI, new CEFTaskAdapter(&CefQuitMessageLoop));
47                 lock.unlock();
48                 cef_thread.join();
49         }
50 }
51
52 void NageruCefApp::cef_thread_func()
53 {
54         CefMainArgs main_args;
55         CefSettings settings;
56         //settings.log_severity = LOGSEVERITY_VERBOSE;
57         settings.windowless_rendering_enabled = true;
58         settings.no_sandbox = true;
59         settings.command_line_args_disabled = false;
60         CefInitialize(main_args, settings, this, nullptr);
61
62         {
63                 lock_guard<mutex> lock(cef_mutex);
64                 cef_initialized = true;
65         }
66         cef_initialized_cond.notify_all();
67
68         CefRunMessageLoop();
69
70         CefShutdown();
71 }
72