Android browser plug-in development

Source: Internet
Author: User

I recently learned something about the android browser plug-in:
You need to know the following knowledge:
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 flow of the plug-in

Introduction to 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.
As a shared library, the plug-in is loaded when it is used. 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
Android browser plug-ins are released in the form of apk packages. 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/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. The pseudocode is as 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, it 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. If the request is successful, the browser calls the NPP_newstream function of the plug-in,
When a stream is created through NPP_Newstream, the mode parameter of the stream is passed. When plug-in is returned, this parameter is set. 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 passes the file name to plug-in through NPP_StreamAsFile. 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

 

From the column of ownerwu

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.