WebKit-WebKit for Android

Source: Internet
Author: User
Tags http authentication browser cache dataloader

If you need to reprint it, please indicate the source!
Website: http://www.jjos.org/
Author: Jiang linuxemacs@gmail.com.
QQ: 457283

This is a work document I wrote a year ago.

1. Introduction to WebKit

WebKit is an open-source browser web page layout engine, including The WebCore Layout Engine and jscore engine.

The WebCore and jscore engines are from the khtml and KJS open-source projects of the KDE project. Android Web

The engine framework uses the WebCore and jscore parts of the WebKit project. The upper layer is encapsulated by the Java language and

The API is provided to Android app developers, while the underlying WebKit core library (WebCore and jscore) is used

Web page layout.

Ii. WebKit directory structure

The WebKit module of the Android platform is divided into Java and WebKit libraries. The directory structure is shown in the following table:

WebKitModule Directory structure

JavaLayer (root directoryDevice/Java/Android/WebKit)

Browserframe. Java
Browserframe is a Java layer encapsulation of the frame object in the WebCore library. It is used to create a frame defined in WebCore and provides a Java layer callback method for the frame object.

Bytearraybuilder. Java
Bytearraybuilder auxiliary object, used for processing byte block linked list.

Cachloader. Java
URL cache loader object, which implements the streadloader abstract base class and is used to load content data through cacheresult objects.

Cachemanager. Java
Cache Management object, responsible for cache object management at the Java Layer

Cachesyncmanager. Java
Cache synchronously Manages objects and synchronizes the browser cache data between RAM and flash. The actual physical data operations are completed in the websyncmanager object.

Callbackproxy. Java
This object is a proxy class used to process WebCore and UI thread messages. When a Web event is generated, The WebCore thread calls the callback proxy class. The proxy class notifies the UI thread by message and calls the callback function of the Set Client object.

Celllist. Java
Celllist defines cells in an image set and manages cell image rendering, state changes, and indexing.

Cookiemanager. Java
Manage cookies according to rfc2109 specifications

Cookiesyncmanager. Java
The cookie synchronization management object, which is used to synchronize cookies between RAM and flash. The actual physical data operations are completed in the base class websyncmanager.

Dataloader. Java
The Data Loader object used to load webpage data.

Datesorter. Java
Not used

Downloadlistener. Java
Download listener Interface

Downloadmanagercore. Java
Download Manager object to manage the download list. This object runs in the WebKit thread and interacts with the UI thread through the callbackproxy object.

Fileloader. Java
File loader to Load file data into the frame.

Frameloader. Java
Frame loader for loading webpage frame data

Httpauthhandler. Java
HTTP authentication processing object, which is passed as a parameter to the browsercallback. displayhttpauthdialog method to interact with users.

Httpdatatime. Java
This is a secondary object for processing HTTP dates.

Jsconfirmresult. Java
JS validation request object

Jspromptresult. Java
JS result prompt object, used to prompt the user for JavaScript running results.

Jsresult. Java
JS result object for user interaction

Jwebcorejavabridge. Java
Bridge Code that interacts with timer and cookies in the WebCore library using Java.

Loadlistener. Java
The loader listener is used to process the listener messages of the loader.

Network. Java
This object encapsulates the network connection logic and provides callers with more advanced network connection interfaces.

Panzoom. Java
Used to process image scaling, moving, and other operations

Panzoomcelllist. Java
Cell used to save moving and scaling Images

Perfchecker. Java
Function objects used for Efficiency Testing ???

Sslerrorhandler. Java
Used to process SSL error messages.

Streamloader. Java
The streamloader abstract class is the base class of all content loader objects. This class is a message-controlled state machine used to load data into a frame.

Textdialog. Java
It is used to handle text area overlays in HTML. You can use the special edittext control defined by standard text editing.

Urlutil. Java
URL Processing functions are used to encode and decode URL strings and provide additional URL type analysis functions.

Webbackforwardlist. Java
This object contains the historical data displayed in the webview object.

Webbackforwardlistclient. Java
The customer interface class for browsing history processing. All the classes that need to receive browsing history changes need to implement this interface.

Webchromeclient. Java
Chrome client base class, chrome client objects will be notified when the browser document title, progress bar, and Icon change.

Webhistoryitem. Java
This object is used to save the historical data of a Web page.

Webicondatabase. Java
Graph database management object. All webviews request the same icon database object.

Websettings. Java
Webview management settings data, which is obtained from the underlying layer through the JNI interface.

Websyncmanager. Java
A data synchronization object used to synchronize Ram data and flash data.

Webview. Java
Web View objects are used for basic web page data loading, display, and other UI operations.

Webviewclient. Java
The Web View client object can be notified when an event is generated in the Web View.

Webviewcore. Java
This object encapsulates the WebCore library, sends data requests in the UI thread to WebCore for processing, and notifies the UI thread of data processing results through callbackproxy.

Webviewdatabase. Java
This object uses sqlitedatabase to provide data access operations for the WebCore module.

Iii. WebKit module framework

The WebKit module of the Android platform consists of two parts: the Java layer and the WebKit library. The Java layer communicates with Android applications, while the WebKit class library is responsible for the actual web page layout processing. The Java and C-layer Libraries call each other through JNI and bridge, as shown in:

3.1 main class relationships of Java layer framework 3.1.1

The Java layer of the WebKit module consists of 41 files. The main class relationships are shown in:

  1. Webview

The webview class is a view class at the Java layer of the WebKit module. All Android applications that require web browsing must create network resources for displaying and processing requests for this view object. Currently, the WebKit module supports HTTP, https, FTP, and JavaScript requests. As the application UI interface, webview provides users with a series of web browsing and user interaction interfaces through which the client program accesses the core WebKit code.

  1. Webviewdatabase

Webviewdatabase is the encapsulation of sqlitedatabase objects in the WebKit module. It is used to store and obtain the buffer data, historical access data, and browser configuration data stored by the browser at runtime. This object is a single instance object. You can use the getinstance method to obtain the instance of webviewdatabase. Webviewdatabase is an internal object in the WebKit module and is only used within the WebKit framework.

  1. Webviewcore

The webviewcore class is the interaction class between the Java layer and the core library of the C-layer WebKit. The client program will forward the Webpage Browsing related operations called by webview to the browserframe object. After the core library of WebKit completes actual data analysis and processing, a series of JNI interfaces defined in webviwecore will be called back. These interfaces will notify the corresponding UI objects through callbackproxy.

  1. Callbackproxy

Callbackproxy is a proxy class used for UI thread interaction with WebCore thread. This class defines a series of user-related notification methods. When WebCore completes the corresponding data processing, it will call the corresponding methods in the callbackproxy class, these methods call the Processing Methods of the corresponding object through message. The detailed processing process will be analyzed in the following sections.

  1. Browserframe

The browserframe class is responsible for URL resource loading, access history maintenance, data caching, and other operations. It directly interacts with the WebKit C-layer library through the JNI interface.

  1. Jwebcorejavabridge

This class provides timer and cookies operations related to the core part of the C-layer WebKit for Java-layer WebKit code.

  1. Downloadmanagercore

Download management core class, which is responsible for managing network resource downloads. All web download operations are managed in the same class. This type of instance runs in the WebKit thread. The interaction with the UI thread is completed by calling the corresponding method in the callbackproxy object.

  1. Websettings

This object describes the user configuration information related to Web browser access.

  1. Downloadlistener

Download the listener interface. If the client code implements this interface, the downloadmanagercore object calls the dwonloadlistener method implemented in the client code when the download starts, fails, hangs, and completes.

  1. Webbackforwardlist

The webbackforwarlist object maintains the user access history, which provides the client program with methods for accessing the browser's historical data.

  1. Webviewclient

The webviewclient class defines a series of event methods. If the Android Application sets a webviewclient derived object, when page loading, resource loading, or page access errors occur, the corresponding method of the derived object is called.

  1. Webbackforwardlistclient

The webbackforwardlistclient object defines the event interfaces that may occur when you perform access history operations. When you implement this interface, you can perform access history operations (such as removing access history and clearing access history) the user will be notified.

  1. Webchromeclient

The webchromeclient class defines events related to window decoration. For example, when the title, icon, and progress change are received, the corresponding webchromeclient method is called.

3.1.2 Design of Main categories 3.1.2.1 design of the Data Loader

The Java framework of the WebKit module uses the data loader to load corresponding types of data. Currently, there are three types of loaders: cacheloader, dataloader, and fileloader, which are used to process cache data and memory data respectively, and file data loading. All loaders in the Java layer (WebKit module) are inherited from streamloader (its parent class is handler). Because the base class of the streamloader class is handler class, when constructing a loader, an event processing thread is enabled. This thread is responsible for the actual data loading operations, and the request thread drives data loading through messages. Is the class graph structure of the related classes of the Data Loader:

The streamloader class defines four different messages (msg_status, msg_headers, msg_data, and msg_end), which respectively indicate sending status messages, sending header messages, sending data messages, and sending completion messages. This class provides two abstract protection methods and a common method: The setupstreamandsendstatus protection method is mainly used to construct data streams related to communication protocols and send statuses to loadlistener. The buildheaders method is used to construct a message header of a specific protocol to a subclass. All loaders have only one common method (load). Therefore, you can call this method when you need to load data. Classes related to the data loading process include loaderlistener and browserframe. When a data loading event occurs, the WebKit C library updates the loading progress and notifies browserframe, after broserframe receives a progress bar change event, it notifies the view progress bar data change through the callbackproxy object. The following uses the dataloader class as an example to describe the process of data loading and interaction with the UI:

When browserframe processes a progress change event, it calls the callbackproxy object to notify the view of the change status. This is omitted here. The gray part indicates the C-Layer Code while the white part indicates the Java-layer code.

3.2 Relationship between C-layer framework 3.2.1 C-class and Java-class

1.Browserframe

The C ++ class corresponding to the browserframe Java class is framebridge, which encapsulates the local methods defined in the browserframe class for Dalvik Virtual Machine callback. The C-layer structure corresponding to the callback function (Java layer) in browserframe is defined as follows:

This structure is a member variable (mjavaframe) of framebridge (layer C). In the framebridge constructor, browserframe (Java layer) is used) class callback method offset to initialize each field of the javabrowserframe structure. After initialization, when WebCore (layer C) analyzes webpage data, there is a frame-related resource change. For example, if the subject of a Web page changes, the mjavaframe structure is used, call the corresponding method of the specified browserframe object to notify the Java layer for processing.

2.Jwebcorejavabridge

The C-layer object corresponding to this object is javabridge. The javabridge object inherits the timerclient and cookieclient classes and is responsible for timer and cookie Management in WebCore. The method offset in the jwebcorejavabridge class in the Java layer is related to several member variables in javabridege. When constructing a javabridge object, these member variables are initialized, and timer or cookies are generated later, webCore calls back corresponding methods of jwebcorejavabridge through these ID values.

3.Loadlistener

The C-layer structure related to this object is struct resourceloader_t, which stores the loadlistener Object ID, cancelmethod ID, and downloadfiledmethod ID values. When a cancel or download event is generated, WebCore calls back cancelmethod or downloadfilemethod in the loadlistener class.

4.Webviewcore

The C Class Related to webviewcore is webcoreviewimpl. The webviewcoreimpl class has a javaglue object as a member variable. When constructing a webcoreviewimpl object, use the method id value in webviewcore (Java layer) to initialize the member variable. The constructed webcoreviewimpl Object Pointer is copied to the mnativeclass of webviewcore (Java layer), so that the webviewcore (Java layer) and webviewcoreimple (C layer) are associated.

5.Websettings

The C-layer structure related to websettings is struct fieldids, which stores the property IDs and method IDs defined in the websettings class. During WebCore initialization (system is used in the static webviewcore method. loadlibrary loading) sets the ID values of these methods and attributes.

6.Webview

The C-layer class related to webview is webviewnative. mjavaglue in this class stores the attributes and method IDs defined in webview, initializes them in the webviewnative constructor, and pointers the constructed webviewnative object, assign the value to the mnativeclass variable of the webview class, so that the webview and the webviewnative object have a relationship.

3.2.2 main class relationships

The following table lists the C-layer classes related to the Java layer:

Class
Function Description

Chromeclientandroid
This class mainly handles frame decoration-related operations in WebCore. For example, set the status bar, scroll bar, and Javascript script prompt box. When related events are generated in the browser, corresponding methods of the chromeclientandroid class will be called, which will pass related UI events to the Java layer through bridge, the Java layer is responsible for rendering and processing user interaction.

Editorclientandroid
This class handles text-related processing on pages, such as text input, cancellation, input method data processing, text pasting, and text editing. Currently, this type only processes the time related to the buttons. Other operations are not supported.

Contextmenuclient
This class provides page-related functional menus, such as copying, reading, and searching. However, no specific functions are implemented in the current project.

Dragclient
This class defines the processing related to page dragging, but this class does not implement specific functions currently.

Frameloaderclientandroid
This class provides operations related to frame loading. When a user requests to load a page, after WebCore analyzes the webpage data, it calls the callback method of the Java layer through this class, process the components related to the UI.

Inspectorclientandroid
This class provides window-related operations, such as window display, window close, and additional window. Currently, all methods of this class are empty implementations.

Page
This class provides page-related operations, such as forward and backward operations on webpages.

Frameandroid
This class provides frame management for Android.

Framebridge
This class encapsulates the frame-related Java Layer Methods. When a frame event is generated, WebCore calls back the Java callback function through framebridge to complete the user interaction process.

Assetmanager
This class provides local resource access for browsers.

Renderskinandroid
This class is related to the control drawing. All the control to be drawn must be derived from this class. Currently, the WebKit module has three types of controls: button, combo, and radio.

The above classes will be created when a Java-layer request creates a web frame. Their relationships are shown in:

Frame Android, which is marked as dark green, is a browser frame. A browserframe object corresponds to a frameandroid object. The other eight classes marked as light green are related to the display and layout of the frame. All the core WebCore code in the WebKit module interacts with users using the bridge processing in the frameandroid object (callback corresponding Java method ).

Iv. Basic operation analysis 4.1 WebKit module initialization

The android SDK provides the webview class, which provides customers with the custom browsing display function. If you need to add browser support, you can use the instance of this class or the instance of the derived class as the view, call the setcontentview of the activity class to display it to the user. When the first webview object is generated in the client code, the WebKit Library (including the Java layer and C layer) is initialized. Then, you can operate the webview object to access the network or local resources.

The generation of webview objects mainly involves three classes: callbackproxy, webviewcore, and webviewdatabase. Among them, the callbackproxy object provides interaction between the UI thread and WebKit class library in the WebKit module. webviewcore is the core layer of WebKit and is responsible for interaction with layer C and initialization of the class library in layer C of WebKit module, webviewdatabase provides support for cache and data storage during WebKit module runtime. The WebKit module initialization process is as follows:

Webview

+-Create a callbackproxy object

+-Create a webviewcore object

1-call system. loadlibrary to load the relevant class library of WebCore (layer C)

2-if it is the first time to initialize the webviewcore object, create a webcoretherad thread

3-create an eventhub object to process webviewcore events

4-obtain the webicondatabase object instance

5-send initialization messages to webcorethread

+-Obtain a webviewdatabase instance

As described above, the first step is to call system. the loadlibrary method is used to load the relevant class library of WebCore. This process is completed by the Dalvik virtual machine. It searches for libwebcore from the dynamic link library directory. so class library, load it To the memory, and call the jni_onload method of the WebKit initialization module. The jni_onload method of the WebKit module completes the following initialization operations:

A) initialize framebridge [Register_android_webcore_framebridge]

Initialize the static variable gframeandroidfield and register the local method table in the browserframe class.

B) initialize javabridge [Register_android_webcore_javabridge]

Initialize the gjavabridge. mobject object and register the local method in the jwebcorejavabridge class.

C) initialize the resource loader [Register_android_webcore_resource_loader]

Initialize the gresourceloader static variable and register the local method of the loadlistener class.

D) initialize webviewcore [Register_android_webkit_webviewcore]

Initialize the static gwebcoreviewimplfield variable and register the local method of the webviewcore class.

E) initialize webhistory [Register_android_webkit_webhistory]

Initialize the gwebhistoryitem structure and register the local methods of the webbackforwardlist and webhistoryitem classes.

F) initialize webicondatabase [Register_android_webkit_webicondatabase]

Register the local method of the webicondatabase class

G) initialize websettings [Register_android_webkit_websettings]

Initialize the static gfieldids variable and register the local method of the websettings class.

H) initialize webview [Register_android_webkit_webview]

Initialize the static gwebviewnativefield variable and register the local method of the webview class.

The second step is webcorethread initialization. This Initialization is only completed when the first webviewcore object is created. When the user code first generates a webview object, a webcorethread thread is created when the webviewcore class is initialized, this thread is responsible for processing WebCore initialization events. In this case, the webviewcore constructor will be blocked until a webview initialization request is complete and will be awakened in the webcorethread thread.

Step 3: Create an eventstub object to process webview events. After WebCore Initialization is complete, an event is sent to the webview object, and the eventstub object of the webview class processes the events, complete subsequent initialization.

Step 4: Obtain the webicondatabase object instance.

Step 5: Send the initialize event to webviewcore and pass the this pointer as the message content. The webview class is mainly responsible for processing UI-related events, while the webviewcore class is mainly responsible for interacting with the WebCore library. During runtime, the UI thread and WebCore Data Processing thread run in two independent threads. After the webcorethread receives the initialize thread, it will call the initialize method of the message object parameter, and then wake up the blocked webviewcore Java thread (this thread is blocked in the webviewcore constructor ). Different webview object instances have different webviewcore object instances. Therefore, the UI thread and webviewcore thread can be decoupled by means of messages. The webcorethread event processing function calls the initialize method of webviewcore instances in different webviews when processing initialize messages. In the initialize method of the webviewcore class, a browserframe object is created (this object manages the entire web form and uses frame-related events), and a webcore_initialized_msg_id message is sent to the webview object. The webview message processing function initializes the specified webviewcore Object Based on Message Parameter 1 and updates the frame buffer of webviewcore.

The sequence diagram of the initialization process is shown in:

Shows the class graph relationship between the Java layer and the C layer after initialization.

In the light green class represents the Java layer, while the gray class represents the C layer.

4.2 Data Loading 4.2.1 load network data

The client code can use the webview loadurl method to request access to the specified URL webpage data. The webview object stores the reference of webviewcore. Because webview is a UI thread and webviewcore is a background thread, when the loadurl of the webview object is called, the URL Information is transmitted to the webviewcore object by means of a message. This object calls the loadurl method of the member variable mbrowserframe, and then calls the WebKit library to load data. The call function sequence is as follows:

The network data is loaded by the Java layer and the C layer. The Java layer completes user interaction, resource download, and other operations, layer C mainly performs data analysis (DOM tree creation and page element analysis. Because the UI thread and WebCore thread run in two different threads, when a user requests to access network resources, a message is sent to the webviewcore object to load the resource request. In the WebKit module of the Java layer, all operations related to resource loading are completed by the corresponding methods in the browserframe class. These methods are local methods, it directly calls the C-layer function of The WebCore library to complete data loading requests, resource analysis, and other operations. As shown in, the frameloader class in the C layer is the resource loader of the browser framework, which checks access policies and sends download resource requests to the Java layer. In frameloader, when a user requests network resources, after a series of policy checks, the startloadingresource method of framebridge is called. This method calls back the startloadingresource method of the browserframe (Java) class, after network data is downloaded, The startloadingresource method of the browserframe (Java) class returns a loadlistener object. frameloader deletes the original frameloader object and encapsulates the loadlistener object into a resourceloadhandler object, and set it to the new frameloader. This completes a resource access request. The following task is that The WebCore database analyzes and constructs the Dom and related data structures based on the resource data.

4.2.2 load local data

The local data is represented by a URL starting with data: //. The loading process is the same as the network data, except that when the executeload method of the frameloader class is executed, it is differentiated according to the scheme type of the URL, call the requesturl method of dataloader (see section 3.1.2.1 to analyze the loader) instead of calling handlehttpload to establish an actual network communication connection.

4.2.3 Load file data

File data is a URL starting with file: //. The basic loading process is basically the same as the network data loading process. The difference is that when the executeload method of the frameloader class is run, call the requesturl method of fileloader to load the data (see section 3.1.2.1 to analyze the loader ).

4.3 refresh and draw

When you drag a scroll bar, hide a window, or trigger a page event, a background re-painting message is sent to the webviewcore (Java layer) object. This message will lead to the drawing of webpage data. WebKit data may be drawn out of efficiency considerations, not through the Java layer, but directly in the C layer using the SGL library. Java objects related to drawing on the Java layer are as follows:

  1. Picture class

This class encapsulates SGL. The variable mnativepicture is actually a pointer to the skpicture object. Two Picture objects are defined in webviewcore as dual-buffer processing. When the webkitdraw method is called, two buffers are exchanged to accelerate the refresh speed.

  1. Webview class

This class accepts operations related to user interaction. When you perform operations such as scrolling, window covering, and clicking the page button, the webview object sends the view_size_changed message to the related webviewcore object. When the webviewcore object receives the message, it refreshes the mcontentpictureb created during the build to the screen, and exchanges mcontentpicturea with it.

  1. Webviewcore class

This class encapsulates the WebKit C-Layer Code and provides the WebKit operation interface for the View class. All user requests to the WebKit library are processed by this class, this class also provides two picture objects for the View class for refreshing graphic data.

The following describes how to refresh webpage data by dragging the mouse on a web page. When a user clicks the touch screen with his or her fingers and moves his or her fingers, the touch event is triggered. The Android platform transmits the touch event to the front-end view (the dispatchtouchevent method is used for processing ). Five touch modes are defined in the webview class. When you drag a webpage by finger, mmotiondragmode is triggered and the scrollby method of the View class is called, trigger the screen rolling event and make the view invalid (re-painting will call the ondraw method of the view ). The rolling events in the webview are responded by the onscrollchanged method. This method sends the set_visible_rect event to the webviewcore object.

After the webviewcore object receives the set_visible_rect event, it passes the rectangular area of the new view stored in the message parameters to the nativesetvisiblerect method, notifying the webcoreviewimpl (layer C) view rectangle change (webcoreviewimpl :: setvisiblerect method ). In the setvisiblerect method, the contentinvalidate method of webviewcore is called through the Virtual Machine. This method will call the webkitdraw method (message through webkit_draw ). In the webkitdraw method, the mcontentpictureb object is first transmitted to the nativedraw Method for painting, and then the content of mcontentpictureb is reversed with the content of mcontentpicturea. Here, the mcontentpicturea buffer is provided for the draw method of webviewcore. If you select a control, the draw method of the webviewcore object is called when the focus box is drawn, and the drawn content is saved in mcontentpicturea, the drawpicture method of the canvas object (Java layer) will be used to draw it to the screen, while the mcontentpictureb buffer is used for built operations, in the nativedraw method, the data of the passed mcontentpictureb object is reset first, and then related elements on the layer are drawn to the canvas on the re-built mcontentpictureb canvas. As mentioned above, the content of mcontentpictureb and mcontentpicturea will be called later, so that when a re-painting event is generated (webview will be called. the ondraw method) draws the data of mcontentpicturea to the screen using the drawpicture of the canvas class. After the webkitdraw method calls the mcontentpicturea and mcontentpictureb pointer pairs, the new_picture_msg_id message is sent to the webview object, which will generate the view_size_changed message of webviewcore, this will make the current view invalid and generate an invalidate () event. This will trigger the ondraw method call and complete a Web page data drawing process.

~~~ End ~~~

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.