起始cpp,我删掉了一些不必要的注释
#include <windows.h>
#include "cefsimple/simple_app.h"
#include "include/cef_sandbox_win.h"
//入口函数,请查阅main()、_tWinMain(),wWinMain()的区别
int APIENTRY wWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow) {
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// Enable High-DPI support on Windows 7 or newer.
//支持win7及更高版本的DPI(分辨率)
CefEnableHighDPISupport();
//沙箱指针,沙箱是一种虚拟环境,可把需要执行的远端代码拉到虚拟环境运行
void* sandbox_info = NULL;
#if defined(CEF_USE_SANDBOX)
sandbox_info = scoped_sandbox.sandbox_info();
#endif
// Provide CEF with command-line arguments.
//获取函数参数
CefMainArgs main_args(hInstance);
// 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.
//创建主进程,第一次创建返回-1,之后均跳过不会进入函数
int exit_code = CefExecuteProcess(main_args, NULL, sandbox_info);
if (exit_code >= 0)
{
// The sub-process has completed so return here.
return exit_code;
}
// Specify CEF global settings here.
//结构体存的是对浏览器的全局设置
CefSettings settings;
//是否使用沙箱环境,true为禁用
#if !defined(CEF_USE_SANDBOX)
settings.no_sandbox = true;
#endif
//创建cef实例
CefRefPtr<SimpleApp> app(new SimpleApp);
// 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;
}
然后两个比较重要的.h文件
(1)simple_app.h
#ifndef CEF_TESTS_CEFSIMPLE_SIMPLE_APP_H_
#define CEF_TESTS_CEFSIMPLE_SIMPLE_APP_H_
#include "include/cef_app.h"
// Implement application-level callbacks for the browser process.
class SimpleApp : public CefApp,
public CefBrowserProcessHandler
{
public:
SimpleApp();
// 获取浏览器句柄:
virtual CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler()
OVERRIDE { return this; }
// CefBrowserProcessHandler methods:
//上下文初始化
virtual void OnContextInitialized() OVERRIDE;
private:
// Include the default reference counting implementation.
//引用技术,用AddRef()和Release()处理计数的增减
IMPLEMENT_REFCOUNTING(SimpleApp);
};
#endif // CEF_TESTS_CEFSIMPLE_SIMPLE_APP_H_
(2)simple_handler.h
#ifndef CEF_TESTS_CEFSIMPLE_SIMPLE_HANDLER_H_
#define CEF_TESTS_CEFSIMPLE_SIMPLE_HANDLER_H_
#include "include/cef_client.h"
#include <list>
//需要处理哪些事件就继承哪个类
class SimpleHandler : public CefClient,
public CefDisplayHandler,
public CefLifeSpanHandler,
public CefLoadHandler {
public:
explicit SimpleHandler(bool use_views);
~SimpleHandler();
// Provide access to the single global instance of this object.
//创建为单例模式
static SimpleHandler* GetInstance();
// CefClient methods:
//继承自CefClient 里的虚函数,有了这个函数才能告诉系统你需要自己处理CefDisplayHandler里的OnTitleChange()函数,CefLifeSpanHandler里的OnAfterCreated()等。
virtual CefRefPtr<CefDisplayHandler> GetDisplayHandler() OVERRIDE {
return this;
}
virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE {
return this;
}
virtual CefRefPtr<CefLoadHandler> GetLoadHandler() OVERRIDE {
return this;
}
// CefDisplayHandler methods:
//标题改变,CefDisplayHandler 里还有其他如地址栏改变,图标改变等
virtual void OnTitleChange(CefRefPtr<CefBrowser> browser,
const CefString& title) OVERRIDE;
//下面这些on开头的都是回调函数,发生此事件后你可自定义处理方法,然后交还系统
// CefLifeSpanHandler methods:
//这个函数很重要,每次创建一个浏览器就会调用这个函数
virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE;
//返回值bool,true为同意关闭
virtual bool DoClose(CefRefPtr<CefBrowser> browser) OVERRIDE;
//在浏览器关闭之前调用此函数,销毁变量、回收资源等
virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) OVERRIDE;
// CefLoadHandler methods:
virtual void OnLoadError(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
ErrorCode errorCode,
const CefString& errorText,
const CefString& failedUrl) OVERRIDE;
// Request that all existing browser windows close.
void CloseAllBrowsers(bool force_close);
bool IsClosing() const { return is_closing_; }
private:
// Platform-specific implementation.
void PlatformTitleChange(CefRefPtr<CefBrowser> browser,
const CefString& title);
// True if the application is using the Views framework.
const bool use_views_;
// List of existing browser windows. Only accessed on the CEF UI thread.
typedef std::list<CefRefPtr<CefBrowser> > BrowserList;
BrowserList browser_list_;
bool is_closing_;
// Include the default reference counting implementation.
//引用计数
IMPLEMENT_REFCOUNTING(SimpleHandler);
};
#endif // CEF_TESTS_CEFSIMPLE_SIMPLE_HANDLER_H_
大叔问我simple_app.h与simple_handler.h很像,他们有什么区别,
我觉得区别是:
SimpleApp用于管理与进程,命令行参数,代理,资源管理相关的回调类
SimpleHandler 是回调管理类、主要用于处理事件
还有就是在simple_app.h中应添加一个函数:
virtual void OnBeforeCommandLineProcessing(const CefString& process_type, CefRefPtr<CefCommandLine> command_line);
用于在程序启动之前改变其命令行参数
simple_app.cpp和simple_handler.cpp是对应头文件函数声明的实现。