1.2. chromium源代码分析 - chromiumframe - 入口函数

时间:2023-03-09 16:00:39
1.2. chromium源代码分析 - chromiumframe - 入口函数

ChromiumFrame的入口函数在main.cpp中,打开main.cpp.
中包含3个类和_tWinMain函数。
_tWinMain就是我们要找的入口函数。我做了部分注释:

 int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// 1. 支持OLE,退出管理机制,GDIPlus 初始化,本地资源管理(new)
HRESULT hRes = OleInitialize(NULL);
base::AtExitManager exit_manager;
gfx::GdiplusInitializer gdiplus_initializer;
gdiplus_initializer.Init();
ResourceBundle::InitSharedInstance(base::FilePath());
ResourceBundle::GetSharedInstance().SetIdConveter(new AppIdConveter()); // 2. 创建窗口(包含WNDCLASS注册)
view::AcceleratorHandler handler;
MessageLoop loop(MessageLoop::TYPE_UI);
MainWindowDelegate delegate;
view::Window::CreateNativeWindow(NULL, gfx::Rect(), &delegate);
delegate.window()->SetWindowBounds(gfx::Rect(, , , ), NULL);
delegate.window()->Show(); // 3. 启动消息循环
MessageLoopForUI::current()->Run(&handler); // 4. 退出处理(new)
ResourceBundle::CleanupSharedInstance();
gdiplus_initializer.UnInit();
OleUninitialize();
}

参考注释,发现这里只比1.1中提到win32典型程序的WinMain多了 支持OLE,退出管理机制,GDIPlus 初始化,本地资源管理。

1.1文中提到的1. WNDCLASS注册   2. 窗口的创建   3. 启动消息循环也被包装后,在这里都有调用。骨架未变,只是包装上了华丽的Message framework。

注册WNDCLASS和创建窗口在这里:

 view::Window::CreateNativeWindow(NULL, gfx::Rect(), &delegate);

消息循环在这里:

 MessageLoopForUI::current()->Run(&handler);

3个类分别是MainView / MainWindowDelegate / AppIdConveter

1. 最重要的就是MainWindowDelegate类,MainWindowDelegate继承自WindowDelegate,WindowDelegate是窗口类一个委托类,通过在MainWindowDelegate重载虚函数,从而达到控制窗口的行为。需要提一下的是,google这里是完全的面向对象,而非MFC中使用set方式来设置窗口的行为。这样的方式在chromium中比比皆是。不要被弄的晕头转向才好。
2. AppIdConveter类,系统资源ID与chromium内部使用的ID转换辅助类。

3. MainView控制窗口中view的行为,这里只是告知了view的大小。在MainWindowDelegate中告知窗口的包含view就是MainView,窗口的大小也就跟随了包含view的大小。(其中关系并非这么简单,但暂时可以这么理解)

接下来就由_tWinMain出发了解view framework.