Android built-in browser-webkit
1. webkit Architecture
1.1 Introduction
The Webkit module of the android platform can be divided into Java and WebKit libraries.
1.2 Webkit directory structure
WebKit Module Directory structure |
Root directory frameworks \ base \ core \ 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. |
1.3 Overall Framework Structure
JNI and Bridge play an intermediary role.
1.4 top Java Layer
1. WebView class
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.
2. WebViewDatabase encapsulation class
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.
3. WebViewCore Interaction Class
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.
4. CallbackProxy proxy
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.
5. 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.
6. 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.
1.5 bottom C layer java layer ------> c layer
WebView WebViewNative
MJavaGlue in WebViewNative saves the property ID and method ID defined in WebView. The construction method WebViewNative is responsible for initialization, and assigns the pointer of the constructed WebViewNative object to the variable mNativeClass.
LoadListener struct resourceloader_t
The struct resourceloader_t structure stores the LoaderListener Object ID and cancelMethod ID. When a cancel or download event is generated, WebCore calls back the CancelMethod or DownloadFileMethod class.
BrowserFrame FrameBriage
The FrameBriage class encapsulates the local method defined in the BrowserFrame class for callback by the Dalvik virtual machine. In the FameBriage constructor, the offset of the callback method of the BrowserFrame class initializes the fields constructed by JavaBrowserFrame.
2. WebView2.1 webpage access
webview=(webView)findViewById(R.id.wb);webview.loadUrl(url);
Show Internet
webview,loadUrl("http://www.baidu.com");
Show local files
webview.loadUrl("file://android_asset/index.html");
2.2 Common settings
AddJavascriptInterface (Object object, StringinterfaceName) // use this function to bind the Javascript of an Object. This method can access Javascript
SetDefaultFontsize () // set the font
Setdefazoom zoom (); // sets the screen zoom level.
SetPluginsEnabled (); // sets the function that allows the Gears plug-in to display flash animation.
SetjavaScriptEnabled (); // you can specify whether to execute a Javascript script.
SetBuildInZoomControls (); // The setting supports scaling.
2.3 WebView implementation
1. declare WebView 2 in the layout file. instantiate WebView in Activity. Set requirement 3. call the loadUrl () method of WebView 4. let WebView respond to the hyperlink, call the setWebViewClient () method, and set WebView 5. (Note) after WebView clicks on many pages, the onKeyDown () method of the Activity needs to be overwritten to support the rollback function of WebView. If not processed, click the system back key, the entire browser will call finish () instead of rolling back to the previous page. 6. add permissions in androidMainfest. xml
Eg1.webview implementation
Public class MainActivity extends ActionBarActivity {private WebView webView; String url = "http://www.51cto.com"; @ SuppressLint ({"SetJavaScriptEnabled", "NewApi"}) @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); webView = (WebView) findViewById (R. id. webView); webView. getSettings (). setJavaScriptEnabled (true); webView. getSettings (). setAllowContentAccess (true); webView. getSettings (). setAllowFileAccess (true); webView. getSettings (). setCacheMode (WebSettings. LOAD_NO_CACHE); webView. setLayerType (View. LAYER_TYPE_SOFTWARE, null); webView. loadUrl (url); webView. setWebViewClient (new HelloWebViewClient (); // enter each newly opened activity in the web View} public boolean onKeyDown (int keycode, KeyEvent key) {if (keycode = KeyEvent. KEYCODE_BACK) & webView. canGoBack () {webView. goBack (); return true;} return false;} public class HelloWebViewClient extends WebViewClient {public boolean shouldOverrideUrlLoading (WebView view, String url) // The {view. loadUrl (url); return true ;}@overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml.int id = item. getItemId (); if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item );}}