一般在一个网络应用系统中,都会一下几类事件: 网络事件, timer事件,以及设备事件,设备事件主要指鼠标,按键,以及paint等消息事件
webkit也不例外,小弟分析了一下32平台的实现,也无特殊之处
1. 设备事件
设备事件仍然是通过win32平台的消息循环来派发,消息处理函数如下:
在32平台,webView仅仅需要实现消息处理函数,然后创建窗口,将其消息处理函数注册给目的窗口
2. 网络事件
32平台是用的curl网络库,通过调用网络库的异步函数, webkit引擎与网络库的接口是ResouceHandler,发起网络请求,需要调用ResouceHandle::start(NetworkingContext* context),函数当网络事件到达,会调用如下函数:
- void didReceiveData(CFURLConnectionRef conn, CFDataRef data, CFIndex originalLength, const void* clientInfo)
- {
- ResourceHandle* handle = static_cast<ResourceHandle*>(const_cast<void*>(clientInfo));
- const UInt8* bytes = CFDataGetBytePtr(data);
- CFIndex length = CFDataGetLength(data);
- LOG(Network, "CFNet - didReceiveData(conn=%p, handle=%p, bytes=%d) (%s)", conn, handle, length, handle->firstRequest().url().string().utf8().data());
- if (handle->client())
- handle->client()->didReceiveData(handle, (const char*)bytes, length, originalLength);
- }
3.timer事件
timer事件的处理类似于设备事件,timer的平台初始化函数:
- static void initializeOffScreenTimerWindow()
- {
- if (timerWindowHandle)
- return;
- WNDCLASSEX wcex;
- memset(&wcex, 0, sizeof(WNDCLASSEX));
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.lpfnWndProc = TimerWindowWndProc;
- wcex.hInstance = WebCore::instanceHandle();
- wcex.lpszClassName = kTimerWindowClassName;
- RegisterClassEx(&wcex);
- timerWindowHandle = CreateWindow(kTimerWindowClassName, 0, 0,
- CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, HWND_MESSAGE, 0, WebCore::instanceHandle(), 0);
- timerFiredMessage = RegisterWindowMessage(L"com.apple.WebKit.TimerFired");
- }
timer的消息处理函数如下:
- LRESULT CALLBACK TimerWindowWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- #if PLATFORM(WIN)
- // Windows Media Player has a modal message loop that will deliver messages
- // to us at inappropriate times and we will crash if we handle them when
- // they are delivered. We repost all messages so that we will get to handle
- // them once the modal loop exits.
- if (PluginView::isCallingPlugin()) {
- PostMessage(hWnd, message, wParam, lParam);
- return 0;
- }
- #endif
- if (message == timerFiredMessage) {
- InterlockedExchange(&pendingTimers, 0);
- processingCustomTimerMessage = true;
- sharedTimerFiredFunction();
- processingCustomTimerMessage = false;
- } else if (message == WM_TIMER) {
- if (wParam == sharedTimerID) {
- KillTimer(timerWindowHandle, sharedTimerID);
- sharedTimerFiredFunction();
- } else if (wParam == endHighResTimerID) {
- KillTimer(timerWindowHandle, endHighResTimerID);
- highResTimerActive = false;
- timeEndPeriod(timerResolution);
- }
- } else
- return DefWindowProc(hWnd, message, wParam, lParam);
- return 0;
- }
引擎内部事件如何处理?
通过分析发现引擎内部的异步是通过timer来实现,具体请参照前面讲解timer的文章
通过上面的分析发现,webkit没有将事件的处理统一起来,如果在引擎中自己有一个事件的处理者,或者将以上事件的处理统一起来,其架构看起来会不会更加优美?