上回分解到從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中去。