CEF3自研究笔记 二、从简单例程开始cefsimple
上回分解到从CEF官网下载最新版的CEF3并使用CMake将其制作成适合我们VS版本的工程,这回我们开始研究CEF3中自带的两个例程中的CEFSimple(简单例程)。
CEF中的例程是基于win32程序的,没有使用MFC,使我们更容易看清楚CEF3的初始化应用过程。我使用的是CEF3的3.2171.1979_windows32版本。
// Entry point function for all processes. int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); void* sandbox_info = NULL; #if defined(CEF_USE_SANDBOX) // Manage the life span of the sandbox information object. This is necessary // for sandbox support on Windows. See cef_sandbox_win.h for complete details. CefScopedSandboxInfo scoped_sandbox; sandbox_info = scoped_sandbox.sandbox_info(); #endif // Provide CEF with command-line arguments. CefMainArgs main_args(hInstance); // SimpleApp implements application-level callbacks. It will create the first // browser instance in OnContextInitialized() after CEF has initialized. CefRefPtr<SimpleApp> app(new SimpleApp); // CEF applications have multiple sub-processes (render, plugin, GPU, etc) // that share the same executable. This function checks the command-line and, // if this is a sub-process, executes the appropriate logic. int exit_code = CefExecuteProcess(main_args, app.get(), sandbox_info); if (exit_code >= 0) { // The sub-process has completed so return here. return exit_code; } // Specify CEF global settings here. CefSettings settings; #if !defined(CEF_USE_SANDBOX) settings.no_sandbox = true; #endif // Initialize CEF. CefInitialize(main_args, settings, app.get(), sandbox_info); // Run the CEF message loop. This will block until CefQuitMessageLoop() is // called. CefRunMessageLoop(); // Shut down CEF. CefShutdown(); return 0; }
void* sandbox_info = NULL;
设置沙箱指针,沙箱据说是用来隔离层与层的接口的,起安全作用,具体什么我也不清楚。有知道的同学可以详细给我解释一下不?不过我们设成NULL 也挺好。下面的#if define就是看你是否启用沙箱模式的开关了。
</pre><pre name="code" class="cpp">CefMainArgs main_args(hInstance);
获取程序启动时的参数。
</pre><pre name="code" class="cpp">CefRefPtr<SimpleApp> app(new SimpleApp);
创建CEF3主实例的回调类
</pre><pre name="code" class="cpp"> int exit_code = CefExecuteProcess(main_args, app.get(), sandbox_info);CEF3有多个线程,这一句将多个线程统一到主实例的过程中。
</pre><pre name="code" class="cpp"> CefSettings settings; CefInitialize(main_args, settings, app.get(), sandbox_info);
这里就是初始化CEF了,settings中可以设置一些初始化的参数,具体参见CEF3的API文档 CefRunMessageLoop();
开始主程序和CEF3的消息循环。
</pre><pre name="code" class="cpp"> CefShutdown();
结束所有CEF3的线程。
程序结束并不会关闭所有CEF3的线程,所以必须调用这一句来关闭所有CEF3启动的线程,程序才会完全 退出。
是不是很简单?其实很多细节,一个没处理好就会出问题,唉。下一节我们开始研究将复杂例子应用到MFC中去。
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。