Android WebKit Message Processing

Source: Internet
Author: User

Android WebKit Message Processing


Preface

In Android APIs, WebView is used to provide developers with WebKit interfaces and features. WebView is actually an encapsulation and extension of WebKit. In android4.4, WebKit has been replaced with Chromium (the WebView/Chromium of android4.4 will be explained in detail in subsequent blogs ).

Establishment of WebKit Message Processing Framework

The whole WebKit is divided into two threads, one is the Ui thread, that is, the main thread where the application uses WebView, And the other WebCore thread. The message processing of WebView is mainly the interaction between the Ui thread and WebCore thread. Some Ui threads send command operation messages to WebCore, such as LOAD_URL, and others are touch messages from Ui.

Main classes involved:


Among them, WebViewClassic is the Provider of WebView, or delegate. All interfaces that involve message processing or need to interact with WebCore are directly called functions of the same name of WebViewClassic.

WebViewInputDispatcher is used to process touch events in the Ui. Later, we will explain the WebKit touch event transmission and processing process. This chapter will not be explained.

WebView Initialization

The sequence diagram is as follows:


It is actually the initialization process of WebView/WebKit. It mainly initializes WebViewClassic, WebCore, and WebViewInputDispatcher to prepare for WebKit resource requests. CreateWebView is called in the WebView constructor. In this process, the provider of WebView is created, and the actual returned object is WebViewClassic. Then, init initializes WebViewClassic. In the init of WebViewClassic, new WebViewCore () is created (), webViewCore is initialized. After WebViewCore is initialized, it indicates that WebCore can now process messages from WebView, including touch events. At this time, WebViewCore will send messages to WebViewClassic, WEBCORE_INITIALIZED_MSG_ID. After WebViewClassic receives the message, it initializes the dispatcher of the touch event: WebViewInputDispatcher. After this process is completed, the application can use WebView and loadUrl.

From this figure, we can see that new WebCoreThread will be added to the WebCore constructor. This thread is the WebCore thread mentioned above. So far, we can see two threads, one is the Ui thread of WebView, and the other is the WebCore thread.

So how does WebKit message processing work?

In WebView, Handler is used for message distribution between threads. In the WebKit message distribution mechanism, there are a total of three Handler:


MPrivateHandler:

Created in WebViewClassic to distribute and process UI-related messages, such as re-painting and touch events. It is also responsible for interacting with WebCore threads.

SWebCoreHandler:

In the middle of WebCoreThread, it is mainly responsible for WebViewCore initialization, WebViewCoreWatchDog heartbeat, and WebCore thread priority adjustment.

MHandler:

The main handler of The WebCore thread message loop. Any WebCore interface that needs to be called must be sent to the WebCore thread through mHandler.

It is new in transferMessages () of EventHub. Because transferMessages () is called in initialize () of WebViweCore, The mHandler of EventHub is also

In The WebCore thread.

The message sent by the Ui thread to The WebCore thread for the first time is not directly distributed to The WebCore thread. Instead, it is cached in the mMessages list in WebViewCore, because it is possible that the WebKit Message Processing framework has not been initialized, And the Ui thread has started to send messages to the WebCore thread. Therefore, after WebViewCore Initialization is complete, transferMessages () is called. In transferMessages, all messages in mMessages are sent to The WebCore thread through mHandler.


It should be clear here:

Messages in the Ui thread are processed by mPrivateHandler of WebViewClassic.

Messages in The WebCore thread are processed by mHandler of EventHub and sWebCoreHandler of WebViewCore. Each Handler can send messages to each other's message queue.


Copyright statement:
Reprinted articles please indicate the source of the original, any for commercial purposes, please contact me: hyman_tan@126.com


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.