初始化了一个浏览器

This commit is contained in:
JackLee 2025-03-25 20:05:49 +08:00
parent b6f180b053
commit 48ac8e2f61
2 changed files with 118 additions and 50 deletions

View File

@ -114,5 +114,16 @@ const QMap<QString, QString> fullToHalfMap = {
{" ", " "} // 全角空格转换为半角空格
};
typedef struct libcef_options
{
//用户代理
QString user_agent="Mozilla/5.0 (Windows NT 10.0; CEF/3.2272.2035) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 SoftwareInformer/1.6.1400";
int log_level=0;//日志级别
QString bridge_object_name="CallBridge";//网桥对象名称
QString builtin_scheme_name="CefView";//方案名称
bool window_less_rendering=true;//离屏渲染
QString single_process="single-process";//单进程模式
QString multi_threaded_message_loop={};
QString external_message_pump={};
} cef_opts;
#endif // CTAI_BASE_H

View File

@ -1,3 +1,4 @@
#include <windows.h>
#include <iostream>
#include <QTranslator>
#include "sui_base_config.h"
@ -7,58 +8,114 @@
#include <QCefContext.h>
#include "QCefWidget/CefWidget.h"
#include <include/cef_app.h>
#include <include/cef_client.h>
#include <include/cef_render_handler.h>
#include <include/cef_browser.h>
// 自定义 CefApp 类
class CefAppQt : public CefApp, public CefBrowserProcessHandler
{
public:
CefAppQt() {}
// 重写 CefApp 方法
CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler() override
{
return this;
}
// 重写 CefBrowserProcessHandler 方法
void OnContextInitialized() override
{
CefWindowInfo window_info;
CefBrowserSettings browser_settings;
browser_settings.windowless_frame_rate = 60;
CefBrowserHost::CreateBrowser(window_info, nullptr, "https://html5test.opensuse.org", browser_settings, nullptr, nullptr);
}
IMPLEMENT_REFCOUNTING(CefAppQt);
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QCefConfig config;
config.setUserAgent("Mozilla/5.0 (Windows NT 10.0; CEF/3.2272.2035) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 SoftwareInformer/1.6.1400");
config.setLogLevel(QCefConfig::LOGSEVERITY_VERBOSE);
config.setBridgeObjectName("CallBridge");
config.setBuiltinSchemeName("CefView");
config.setWindowlessRenderingEnabled(true);
config.addCommandLineSwitch("single-process");
// config.addCommandLineSwitch("use-alloy-style");
// config.addCommandLineSwitch("off-screen-rendering-enabled");
// config.addCommandLineSwitch("multi-threaded-message-loop");
// config.addCommandLineSwitch("external-message-pump");
// config.addCommandLineSwitch("enable-chrome-runtime");
config.addCommandLineSwitch("enable-gpu");
// config.addCommandLineSwitch("use-views");
//config.addCommandLineSwitch("site-per-process");
config.addCommandLineSwitch("no-sandbox");
//config.addCommandLineSwitch("in-process-gpu");
// config.addCommandLineSwitch("use-mock-keychain");
// config.addCommandLineSwitch("enable-chrome-runtime");
// config.addCommandLineSwitch("start-stack-profiler");
//config.addCommandLineSwitchWithValue("type","gpu-process");
// config.addCommandLineSwitchWithValue("ignore-certificate-errors","true");
// config.addCommandLineSwitchWithValue("enable-gpu-rasterizatio","true");
// config.addCommandLineSwitchWithValue("enable-accelerated-video","true");
// config.addCommandLineSwitchWithValue("use-fake-ui-for-media-stream","true");
// config.addCommandLineSwitchWithValue("use-fake-device-for-media-stream","true");
//config.addCommandLineSwitchWithValue("renderer-process-limit", "1");
//config.addCommandLineSwitchWithValue("disable-features", "BlinkGenPropertyTrees,TranslateUI,site-per-process");
QCefContext cefContext(&a, argc, argv, &config);
CefWidget cw;
cw.show();
qputenv("QT_QPA_PLATFORM", "windows:fontengine=freetype");
// 加载语言包
QTranslator qtBaseTranslator;
if (!qtBaseTranslator.load(QStringLiteral(":/res/translator/qtbase_zh_CN.qm")))
HINSTANCE hInstance = GetModuleHandle(NULL);
CefMainArgs main_args(hInstance);
// 处理多进程逻辑
CefRefPtr<CefAppQt> app(new CefAppQt);
int exit_code = CefExecuteProcess(main_args, app, nullptr);
if (exit_code >= 0)
{
// 处理加载翻译文件失败的情况
qDebug() << "Failed to load translation file.";
return -1;
std::cerr << "exit code:" << exit_code << std::endl;
return exit_code;
}
a.installTranslator(&qtBaseTranslator);
// 读取窗体的配置并初始化
sui_init_config();
ctai x;
x.init_layout();
// 仅限于windows平台
#if defined(__WIN32__)
x.title()->set_type(QD_TYPE::QD_EXIT);
#endif
x.show();
// 初始化 CEF
CefSettings settings;
settings.windowless_rendering_enabled = true;
settings.multi_threaded_message_loop = false;
settings.external_message_pump = false;
settings.no_sandbox = true;
// 自定义 root_cache_path
QString cache = QDir::currentPath() + "/usercache";
CefString(&settings.root_cache_path) = cache.toStdString();
if (!CefInitialize(main_args, settings, app, nullptr))
{
std::cerr << "CEF initialization failed." << std::endl;
return 1;
}
std::cout << "init CefWidget" << std::endl;
// 初始化 Qt 应用
QApplication a(argc, argv);
std::cout << "CefRunMessageLoop" << std::endl;
// 运行 CEF 消息循环
CefRunMessageLoop();
std::cout << "CefShutdown" << std::endl;
// 关闭 CEF
CefShutdown();
return a.exec();
}
// int main(int argc, char *argv[])
// {
// QApplication a(argc, argv);
// QCefConfig config;
// config.setUserAgent("Mozilla/5.0 (Windows NT 10.0; CEF/3.2272.2035) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 SoftwareInformer/1.6.1400");
// config.setLogLevel(QCefConfig::LOGSEVERITY_VERBOSE);
// config.setBridgeObjectName("CallBridge");
// config.setBuiltinSchemeName("CefView");
// //config.setWindowlessRenderingEnabled(true);
// //config.addCommandLineSwitch("single-process");
// //config.addCommandLineSwitch("enable-gpu");
// //禁用沙盒
// //config.addCommandLineSwitchWithValue("no-sandbox","ture");
// //禁止多线程会话循环(单进程为true,多进程为false);
// //config.addCommandLineSwitchWithValue("external-message-pump","false");
// //config.addCommandLineSwitchWithValue("multi-threaded-message-loop","false");
// QCefContext cefContext(&a, argc, argv, &config);
// CefWidget cw;
// cw.show();
// qputenv("QT_QPA_PLATFORM", "windows:fontengine=freetype");
// // 加载语言包
// QTranslator qtBaseTranslator;
// if (!qtBaseTranslator.load(QStringLiteral(":/res/translator/qtbase_zh_CN.qm")))
// {
// // 处理加载翻译文件失败的情况
// qDebug() << "Failed to load translation file.";
// return -1;
// }
// a.installTranslator(&qtBaseTranslator);
// // 读取窗体的配置并初始化
// sui_init_config();
// ctai x;
// x.init_layout();
// // 仅限于windows平台
// #if defined(__WIN32__)
// x.title()->set_type(QD_TYPE::QD_EXIT);
// #endif
// x.show();
// return a.exec();
// }