Cordova Source code In-depth analysis-fifth lecture __cordova

Source: Internet
Author: User

In the last lecture, the main process and framework have been introduced,

Looking at the whole code again, I found that there were some details that I didn't enumerate,

For example, before just introduced addjavascript one way to achieve communication, in fact, there is another way of communication is the prompt way, the introduction of this story again.

Prompt has several advantages:

1. Can avoid the Android 4.2 below, JS security issues

2. Can realize synchronous call, return value directly

The following begins parsing the code logic:

Repeat position, no longer tired. Come straight to cordova.js.

Define ("Cordova/android/nativeapiprovider", function (require, exports, module) {

var nativeapi = this._ cordovanative | | Require (' Cordova/android/promptbasednativeapi '); here in fact, if there is no definition _cordovanative will go prompt
var currentapi = Nativeapi;

Module.exports = {
    get:function () {return currentapi;},
    setpreferprompt:function (value) {
        CURRENTAPI = VA Lue? Require (' Cordova/android/promptbasednativeapi '): Nativeapi;
    //Used only by tests.
    Set:function (value) {
        Currentapi = value;

And in the use of prompt way, the method is the same.

Define ("Cordova/android/promptbasednativeapi", function (require, exports, module) {

module.exports = {
    exec: function (Bridgesecret, service, action, Callbackid, Argsjson) {return
        prompt Argsjson, ' Gap: ' +json.stringify ([ Bridgesecret, service, Action, Callbackid]);/directly into the Java side, code fragment 1
    setnativetojsbridgemode:function ( Bridgesecret, value) {
        prompt (value, ' Gap_bridge_mode: ' + Bridgesecret);
    retrievejsmessages:function (Bridgesecret, Fromonlineevent) {return
        prompt (+fromonlineevent, ' gap_poll: ' + Bridgesecret);}}


Code Snippet

    @Override Public
    Boolean onjsprompt (WebView view, string origin, String message, String defaultvalue, Final jspromptr Esult result) {
        //Unlike the @JavascriptInterface bridge, this is always called on the UI thread.
        String Handledret = ParentEngine.bridge.promptOnJsPrompt (origin, message, defaultvalue);//code Fragment 2
        if (Handledret!= NULL) {
            result.confirm (Handledret);
        } else {
            dialogshelper.showprompt (message, DefaultValue, new Cordovadialogshelper.result () {
                @Override public
                void Gotresult (Boolean success, String value) {
                    if ( Success) {
                        result.confirm (value);
                    } else {
                        result.cancel ();}}}
        return true;

Code fragment

    public string Promptonjsprompt (string origin, String message, string defaultvalue) {if (DefaultValue!= null
            && defaultvalue.length () > 3 && defaultvalue.startswith ("Gap:")) {Jsonarray array;
                try {array = new Jsonarray (defaultvalue.substring (4));
                int bridgesecret = array.getint (0);
                String service = array.getstring (1);
                String action = array.getstring (2);
                String Callbackid = array.getstring (3);  String r = jsexec (Bridgesecret, Service, action, Callbackid, message);//execute return r = NULL in the same process as the logic described earlier ?
            "": r;
            catch (Jsonexception e) {e.printstacktrace ();
            catch (Illegalaccessexception e) {e.printstacktrace ();
        Return "";
        }//Sets the Native->js bridge mode. else if (defaultvalue!= null && DefaultvaluE.startswith ("Gap_bridge_mode:")) {try {int bridgesecret = Integer.parseint (defaultvalue.sub
                String (16));
            Jssetnativetojsbridgemode (Bridgesecret, integer.parseint (message));
            catch (NumberFormatException e) {e.printstacktrace ();
            catch (Illegalaccessexception e) {e.printstacktrace ();
        Return ""; }//Polling for JavaScript messages else if (defaultvalue!= null && defaultvalue.startswith ("Gap
            _poll: ")" {int bridgesecret = Integer.parseint (defaultvalue.substring (9));
                try {String r = jsretrievejsmessages (Bridgesecret, "1". Equals (message)); return r = null?
            "": r;
            catch (Illegalaccessexception e) {e.printstacktrace ();
        Return ""; else if (defaultvalue!= null && defaultvalue.startswith ("Gap_Init: ")) {//Protect against random iframes being able to talk the bridge.
            Trust only pages which the app would have been allowed to navigate to anyway.  if (Pluginmanager.shouldallowbridgeaccess (Origin)) {//Enable the bridge int bridgemode =
                Integer.parseint (defaultvalue.substring (9));
                Jsmessagequeue.setbridgemode (Bridgemode);
                Tell JS the bridge secret.
                int secret = Generatebridgesecret ();
            Return "" +secret;
            else {LOG.E (Log_tag, "gap_init called from restricted Origin:" + origin);
        Return "";
    return null; }
Here, the whole process is connected again, no more nonsense.

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: 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.