I recently learned something about the android browser plug-in:
I need to know the following:
1. what is the plug-in
2. how to load plug-ins and create instances in the android browser
3 interaction between browser plug-ins and scripting languages
4 internal data stream of the plug-in
overview of browser plug-ins:
1.1 Overview
browser plug-ins are essentially a functional module and an extension of browser functions. Its carrier is a DLL or so file. It depends on the browser to complete a specific function. Plug-ins need to implement some functions defined by the browser. These functions call the npapi. It is the plug-in that implements these functions to interact with the browser. The browser also provides some functions for the plug-in. There are also some proprietary functions on the Android platform. Their function names have conventions. The plug-in provides NPP _ headers. The method provided by the browser is, and the function provided by Android starts with ANP.
when is the plug-in loaded as a shared library. What interfaces have been exported for the browser to call.
the browser plug-in is loaded by the browser. WebKit is also loaded in Android. Generally, a shared library is loaded by calling the loadlibray function, and then using the getentrypoint function to obtain the address of the shared library export function. The plug-in is also loaded by the browser. To call the loadlibrary function, we must know the location of the shared library in the system. Windows, Mac, and Linux systems can all be stored in a fixed location. In Linux, you can also set the path by setting environment variables. But this is not the case in Android. There is a fixed path data/COM. Android. Browser/plug-in 1.5, but the path is not saved after 2.0. It is stored in the Lib folder under the installation directory of the plug-in APK package. For example, if the package name of the plug-in is com. Android. plugin, the storage path of the shared library should be data/COM. Android. plugin/lib /. For different plug-in paths, the following describes how the browser finds these lib paths.
1.2 Android browser plug-in
the android browser plug-in is released in the form of an APK package. In this project, we define a service. This service can respond to plugin_action. This is set in androidmainefest. xml. The registration of plug-ins is also completed through the service.
2. Loading plug-ins in the android Browser
2.1 Summary
1.1 mentioned that the browser loading plug-in is loaded in loadlibary mode. And you need to know the path. In fact, the browser load plug-in is divided into three steps:
1. the browser seeks the plug-in path, which is found by the service that runs when the plug-in APK package is installed.
2. the browser obtains the plug-in information. Obtain the plug-in name, description, and mime information and save it to your own plugindatabase class instance.
3. Create a plug-in instance in the browser. This process creates a plug-in instance and initializes the internal data of the plug-in.
The three processes are described in detail below:
2.2 The browser obtains the plug-in path.
Every time the browser starts refreshing the page, it will refresh its own plug-in information library to update its own plugindatabase. Then, the reindatabase function refresh will call the plugin manager function to obtain the plug-in path. The plugin manager uses pack manager to find all services that can respond to the PLUGIN-ACTION intent. Then, you can find the plug-in by getting the package name through each service information. For more information, see frameworks/base/CORE/Java/Android/WebKit/pluginmanager. java.
Figure 1 describes the process of obtaining the plug-in path from the browser. For details, refer to the source code. Path:/frameworks/base/CORE/Java/Android/WebKit
External/WebKit/WebCore/plugins
External/WebKit/Android/JNI
2.3 The browser obtains the plug-in information
After obtaining the plug-in path, we can get the function address exported by the plug-in. First, let's take a look at the functions exported by the plug-in and their functions.
Figure 2 shows a function exported from a plug-in shared library. The functions of each function are described in detail below.
After the browser calls refresh, it will call np_getvalue to obtain the plug-in name and description. Call np_getmimedescription to obtain the MIME type of the plug-in and the extension and description of supported files. Save the information to plugindatabase. Np_shutdown is called when pluginview is destroyed to release plug-in resources. The np_initialize function is called only when the plug-in instance is created. The specific process is shown in step 3.
When the mimetype cannot be found, the browser matches the plug-in based on the data file extension.
2.4 create a plug-in instance in the browser:
The np_initialize function is important. It is the key to interaction between browsers and plug-ins. There are three main functions:
1. Obtain the browser-defined 2_function address.
2. Return the NPP _ FUNCTION address defined by the plug-in to the browser.
3. Obtain the ANP functions provided by Android.
These functions are listed below:
// NPP function, main function for interaction between plug-ins and browsers
The above functions all have an NPP instance parameter, which is actually a pointer to the pluginview member variable. The member variable has two pointers: pdata and ndata. Pdata points to our plug-in npoject object. You can see this value in npp_new. Ndata is stored as the this inview's own this pointer.
// Functions provided by Android
The above functions are initialized using npn_getvalue.
// Other functions provided by the plug-in. These functions are mainly used for interaction between the plug-in and javasript.
After calling the np_initialize function, the browser will know the address of the npp_new function. The browser calls this function to create a browser instance. This function is called in pluginview.
In npp_new, we create the npobject instance of the plug-in. Npobject is created by calling the npn_create function of the browser. In this create function, it is determined that npobject does not provide the create method (the address of the creat method is passed through the second npn_create parameter). If not, the browser calls malloc to create the instance. And save the npobject function address provided by the plug-in (the static function listed above) in the npobject object. The plug-in instance has been created.
3. Interaction between browser plug-ins and scripting languages
The browser provides a mechanism for interaction between plug-ins and JavaScript.
First, let's take a look at how Java Script calls the plug-in method.
The browser first calls nperror npp_getvalue (NPP instance, nppvariable variable, void * value) to obtain the address of the npobject object. The variable parameter is nppvpluginscriptablenpobject. After obtaining this object, the browser can call the npclass function provided by the plug-in. The main functions are as follows:
Pluginhasmethod: ask if the plug-in supports a js method.
Pluginhasproperty: queries whether the plug-in has a certain attribute.
Plugininvoke: When the plug-in supports a method, the browser will call the method provided by the function execution plug-in for Js. How to differentiate many provided method plug-ins in this function.
Let's analyze the function:
Static bool plugininvoke (npobject * OBJ, npidentifier name, const npvariant * ARGs, uint32_t argcount, npvariant * result );
OBJ is the npobject object address in the plug-in.
Name indicates the name of the method provided by the plug-in. You can compare this parameter to distinguish the different methods provided by the plug-in.
ARGs and argcount indicate the parameter addresses and number of parameters sent from JS respectively.
Result.
Let's take a look at how the plug-in calls the methods provided by JS:
JS can set the callback function for the plug-in two ways. PseudoCodeAs follows:
<Script language = JavaScript>
Plugin. onfun = fun; // Method 1: Pass in the callback function address by setting the plug-in property
Plugin. onfun (fun); // Method 2: Call the plug-in function to input the callback function address
Function fun (){}
</SCRIPT>
Inside the plug-in, when the JS function address is uploaded to the plug-in, the browser encapsulates it as an npobject object, which contains the function address.
Method 1: Inside the plug-in, the browser calls pluginhasproperty to check whether the plug-in has this property. If so, the browser calls the pluginsetproperty function. The second parameter of pluginsetproperty (npobject * OBJ, npidentifier name, const npvariant * variant) determines the attribute, and the third parameter is the address of the npobject object.
Method 2: Inside the plug-in, the browser will call pluginhasmethod to determine whether this method is supported. Call plugininvoke. The args parameter contains the npobject address of the callback function.
After the callback function is set in JS, the plug-in can call the function. Use npn_invokedefault. The sample code is as follows:
Bool Bret = gbrowser-> invokedefault (NPP, callbacknpobject, & PV, 1, & result );
In addition, the plug-in can also directly call functions in Js. Call the geturl function of the browser within the plug-in. The specific format is as follows:
Gbrowser-> geturl (insT (), "javascript: function ()", "_ Self ");
If you want to input an integer parameter, the second parameter of the above function should be written as "javascript: function (" + num + ")".
If a string parameter is input, the second parameter of the preceding function is "javascript: function (/'" + "string" + "/')". URL encode is required if the string contains Chinese characters.
Iv. Plug-in Data Flow
the plug-in can request data from the server in a browser or send data to the server.
If the plug-in needs to request data from the server, you can call the browser function npn_geturl to send a request to the server. If the target parameter is set to null, the data can be passed to the plug-in on this page. When the request is successful, the browser calls the npp_newstream function of the plug-in.
when a stream is created through npp_newstream, a streaming mode parameter is passed. Plug-In sets this parameter when it returns, the default value is np_normal. the stream is deleted through npp_destroystream. plug-in can also call npn_destroystream to delete a stream. the three modes are as follows:
normal mode. this mode is used when the parameter is set to np_normal. When data can be sent, Netscape sends the data to plug-in, which may arrive in an abnormal order. The browser sends data by calling a series of npp_writeready and npp_write. The browser uses Len to tell plug-in how much data it will send. Netscape calls npp_writeready to determine how many bytes of data plug-in will receive each time, and then calls npp_write to send data. this mode is more efficient.
random access mode. this mode is used if the Boolean seekable parameter is set to true when npp_newstream is called. in this case, plug-in calls npn_requestread to specify the data range to be obtained, and then the browser calls npp_writeready and npp_write to transmit the data to plug-in. this mode requires the support of the remote server or the browser to first save the stream data to a local temporary file. in this mode, the user can read the desired records from the server's data files, just as reading a record from the local hard disk.
file mode. set the parameter to np_asfile. the browser first saves the whole URL data to a local file, and then uses the npp_st Reamasfile transfers the file name to plug-in. Plug-in can obtain the required data through file operations.
V. Summary
For browser plug-in development, refer to the source code example, development/samples/browseplugin instance. Relevant knowledge can refer to https://developer.mozilla.org/en/Gecko_Plugin_API_Reference
Reprinted from: http://blog.csdn.net/ownerwu/article/details/6429072