美文网首页hybird
补充Cordova流程源码

补充Cordova流程源码

作者: summerlines | 来源:发表于2020-09-16 17:04 被阅读0次

    从js到native的流程源码补充说明:

    事件从js触发到native时,通过注入的对象[SystemExposedJsApi &@JavascriptInterface 注解标记的方法 ]
    先初始化native,然后再去loadurl
    1.注入native端xml文件中声明的 plugin【xml提供了后续扩展解耦plugin添加的入口】,解析xml并用全局map维护,指定onload为true的先行初始化,其他native端plugin 懒加载

    ... // CordovaActivity 的onCreate
    ArrayList<PluginEntry> pluginEntity = (ArrayList<PluginEntry>)getIntent().getSerializableExtra("pluginEntity"); //第一次扩展插件的添加方式
    //这里是corodva native插件的懒加载注入入口
            loadConfig(pluginEntity);
    ...
    
    
    
    // 加载xml中的native plugin入口  
    @SuppressWarnings("deprecation")
        protected void loadConfig(ArrayList<PluginEntry> list) {
            ConfigXmlParser parser = new ConfigXmlParser();
            if (!CollectionUtils.isEmpty(list)) {
                parser.setDynamicEntity(list);
            }
            parser.parse(this);
            preferences = parser.getPreferences();
            preferences.setPreferencesBundle(getIntent().getExtras());
            launchUrl = parser.getLaunchUrl();
            //xml 解析得到native xml中声明的 plugin 包装对象map 并在下面loadUrl的 init方法中传递到PluginManager中
            pluginEntries = parser.getPluginEntries();
            Config.parser = parser;
        }
    

    2.初始化webview的方法代理CordovaWebviewImpl以及 反射创建了SystemWebViewEngine[作为真正的webview和代理之间的bridge] ,SystemWebViewEngine 是Webview相关属性设置类

    public void loadUrl(String url) {
        if (appView == null) {
            init();
        }
    
        // If keepRunning
        this.keepRunning = preferences.getBoolean("KeepRunning", true);
    
        appView.loadUrlIntoView(url, true);
    }
    
    
    
    
    
    //init方法调用到engine 的 init方法, 最后调用到 com.xxx.xxx.engine.SystemWebViewEngine#exposeJsInterface
    private static void exposeJsInterface(WebView webView, CordovaBridge bridge) {
            if ((Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)) {
                LOG.i(TAG, "Disabled addJavascriptInterface() bridge since Android version is old.");
                // Bug being that Java Strings do not get converted to JS strings automatically.
                // This isn't hard to work-around on the JS side, but it's easier to just
                // use the prompt bridge instead.
                return;
            }
            SystemExposedJsApi exposedJsApi = new SystemExposedJsApi(bridge);
            //webview注入native端 SystemExposedJsApi的对象 ,并以 _cordovaNative为名在js端使用
            webView.addJavascriptInterface(exposedJsApi, "_cordovaNative");
        }
    
    
    
    // com.xxx.xxx.core.CordovaActivity#init 方法提供了子类重写init方法
    protected void init() {
            appView = makeWebView();
            createViews();
            if (!appView.isInitialized()) {
                appView.init(cordovaInterface, pluginEntries, preferences);
            }
            cordovaInterface.onCordovaInit(appView.getPluginManager());
    
            // Wire the hardware volume controls to control media if desired.
            String volumePref = preferences.getString("DefaultVolumeStream", "");
            if ("media".equals(volumePref.toLowerCase(Locale.ENGLISH))) {
                setVolumeControlStream(AudioManager.STREAM_MUSIC);
            }
        }
    
    
    
    // ContainerActivity#makeWebView
    @Override
        protected CordovaWebView makeWebView() {
    
                mSystemWebView = new NormalWebView(this);
                mEngine = new SystemWebViewEngine((NormalWebView)mSystemWebView);
                mFlWeb.addView((NormalWebView)mSystemWebView);
    
            mCordovaWebView = new CordovaWebViewImpl(mEngine);
            mSystemWebView.resetUserAgent( " " + userAgent);
            //这里抽象了 IWebview 【便于替换X5Webview等其他扩展WebView的实现】
            mSystemWebView.resetWebViewClient(this, mEngine);
            //同理在NormalWebview中注入了重写的WebViewClient和 WebChromeClient
            mSystemWebView.resetWebChromeClient(this, mEngine);
            return mCordovaWebView;
        }
    
    
    
    //证书校验调用入口
    com.xxx.xxx.impl.RedirectSystemWebViewClient#onReceivedSslError
    
    @Override
                public void onReceivedSslError(WebView webView, SslErrorHandler handler, SslError sslError) {
    //                super.onReceivedSslError(webView, handler, sslError); 注释父类实现
                //入口封装传入参数确定是否证书校验
                    if (isCheck()) {
                        //检验证书颁发机构以及证书的合法性 证书确实是发给指定域名的且为当前设备的根证书所信任
                            if (SSLChecker.chkMySSLCNCertOCFT(sslError.getCertificate())) {
                                handler.proceed();  // 如果证书一致,忽略错误
                            } else {
                                LogUtil.w("xxx", "onReceivedSslError" + "cancel" + sslError);
                                handler.cancel();
                            }
    //                    }
                    } else {
                        handler.proceed();
                    }
                    if (client != null) {
                        client.onReceivedSslError();
                    }
                    LogUtil.w("xxx", "onReceivedSslError(NormalWebView.java:102)" );
                }
            });
    

    3.初始化完成后,初始化js 部分 ….. 衔接部分

    4.js调用native native响应并产生回调部分

    // com.xxx.xxx.core.PluginManager#exec    
    public void exec(final String service, final String action, final String callbackId, final String rawArgs) {
        //懒加载,根据协定传递指定key为service,初始化对应plugin【已初始化则直接取出】
            CordovaPlugin plugin = getPlugin(service);
            if (plugin == null) {
                LOG.d(TAG, "exec() call to unknown plugin: " + service);
    //            PluginResult cr = new PluginResult(PluginResult.Status.CLASS_NOT_FOUND_EXCEPTION);
    //            app.sendPluginResult(cr, callbackId);
                return;
            }
        //根据js端生成的callbackId 生成 native端的 CallbackContenxt对象,这个对象是native 回调js端,传递数据的起点
            CallbackContext callbackContext = new CallbackContext(callbackId, app);
            try {
                long pluginStartTime = System.currentTimeMillis();
                // 子plugin实现execute的返回值,action对应事件名,这里通过java多态找到CordovaPlugin的子实现类【即service映射的类】并execute
                boolean wasValidAction = plugin.execute(action, rawArgs, callbackContext);
                long duration = System.currentTimeMillis() - pluginStartTime;
                //提示在主线程执行过长时间,建议使用线程池异步
                if (duration > SLOW_EXEC_WARNING_THRESHOLD) {
                    LOG.w(TAG, "THREAD WARNING: exec() call to " + service + "." + action + " blocked the main thread for " + duration + "ms. Plugin should use CordovaInterface.getThreadPool().");
                }//无效行为回调
                if (!wasValidAction) {
                    PluginResult cr = new PluginResult(PluginResult.Status.INVALID_ACTION);
                    callbackContext.sendPluginResult(cr);
                }
            } catch (JSONException e) {
                //异常回调
                PluginResult cr = new PluginResult(PluginResult.Status.JSON_EXCEPTION);
                callbackContext.sendPluginResult(cr);
            } catch (Exception e) {
                LOG.e(TAG, "Uncaught exception from plugin", e);
                callbackContext.error(e.getMessage());
            }
        }
    
    
    
    //任意CordovaPlugin子类完成事件触发即执行execute后,通过方法传递的 CallbackContext回调
    ...
      public void success(JSONObject message) {
            sendPluginResult(new PluginResult(PluginResult.Status.OK, message));
        }
    ...//还有很多其他重载的success方法,可以兼容传递不同类型的数据类型,最终都会在PluginResult中完成封装,转换成最终传递到js的 payload string msg
    
    
    
    public void sendPluginResult(PluginResult pluginResult) {
            synchronized (this) {
                if (finished) {
                    LOG.w(LOG_TAG, "Attempted to send a second callback for ID: " + callbackId + "\nResult was: " + pluginResult.getMessage());
                    return;
                } else {
            //如果执行过一次的事件 【finised 为 true】 下次不会再执行,如果keepcallback则可重复调用执行,回执到js端message process后也不会被移除全局的js callback数组【所以保持了两端的同步keepcallback能力】
                    finished = !pluginResult.getKeepCallback();
                }
            }
            LogUtil.d("xxx", "sendPluginResult: "+callbackId+"-----"+pluginResult );
            webView.sendPluginResult(pluginResult, callbackId);
        }
    
    
    
    //com.xxx.xxx.core.CordovaWebViewImpl#sendPluginResult
     @Override
        public void sendPluginResult(PluginResult cr, String callbackId) {
            // nativeToJsMessageQueue native生成发送到js端的消息维护类
            nativeToJsMessageQueue.addPluginResult(cr, callbackId);
        }
    
    public void addPluginResult(PluginResult result, String callbackId) {
            if (callbackId == null) {
                LogUtil.e(LOG_TAG, "Got plugin result with no callbackId", new Throwable());
                return;
            }
            // Don't send anything if there is no result and there is no need to
            // clear the callbacks.
            boolean noResult = result.getStatus() == PluginResult.Status.NO_RESULT.ordinal();
            boolean keepCallback = result.getKeepCallback();
            if (noResult && keepCallback) {
                return;
            }
         // 将PluginResult 和 callbackId 封装成 JsMessage对象 [ JsMessage 是 NativeToJsMessageQueue 的内部类,JsMessage用于生成协议规则message的类]
            JsMessage message = new JsMessage(result, callbackId);
            if (FORCE_ENCODE_USING_EVAL) {
                StringBuilder sb = new StringBuilder(message.calculateEncodedLength() + 50);
                message.encodeAsJsMessage(sb);
                message = new JsMessage(sb.toString());
            }
    
            enqueueMessage(message);
        }
    
     private void enqueueMessage(JsMessage message) {
            synchronized (this) {
                if (activeBridgeMode == null) {
                    LOG.d(LOG_TAG, "Dropping Native->JS message due to disabled bridge");
                    return;
                }
                queue.add(message);
                if (!paused) {
                    activeBridgeMode.onNativeToJsMessageAvailable(this);
                }
            }
        }
    
    
            @Override
            public void onNativeToJsMessageAvailable(final NativeToJsMessageQueue queue) {
                cordova.getActivity().runOnUiThread(new Runnable() {
                    public void run() {
                        //这里在发送信息到js端前,先处理
                        String js = queue.popAndEncodeAsJs();
                        if (js != null) {
                            //最后调用 webview 的 evaluateJavascript方法,将处理过的带协议规则的信息传递到js端
                            engine.evaluateJavascript(js, null);
                        }
                    }
                });
            }
        }
    
    //完成封装的api方法
    public String popAndEncodeAsJs() {
            synchronized (this) {
                int length = queue.size();
                if (length == 0) {
                    return null;
                }
                int totalPayloadLen = 0;
                int numMessagesToSend = 0;
                for (JsMessage message : queue) {
                    //计算长度
                    int messageSize = message.calculateEncodedLength() + 50; // overestimate.
                    //MAX_PAYLOAD_SIZE最大传递信息长度
                    if (numMessagesToSend > 0 && totalPayloadLen + messageSize > MAX_PAYLOAD_SIZE && MAX_PAYLOAD_SIZE > 0) {
                        break;
                    }
                    totalPayloadLen += messageSize;
                    numMessagesToSend += 1;
                }
                boolean willSendAllMessages = numMessagesToSend == queue.size();
                StringBuilder sb = new StringBuilder(totalPayloadLen + (willSendAllMessages ? 0 : 100));
                // Wrap each statement in a try/finally so that if one throws it does
                // not affect the next.
                for (int i = 0; i < numMessagesToSend; ++i) {
                    //从队列中取出第一条JsMessage对象并移除
                    JsMessage message = queue.removeFirst();
                    if (willSendAllMessages && (i + 1 == numMessagesToSend)) {
                        message.encodeAsJsMessage(sb);
                    } else {
                        sb.append("try{");
                        message.encodeAsJsMessage(sb);
                        sb.append("}finally{");
                    }
                }
                if (!willSendAllMessages) {
                    sb.append("window.setTimeout(function(){cordova.require('cordova/plugin/android/polling').pollOnce();},0);");
                }
                for (int i = willSendAllMessages ? 1 : 0; i < numMessagesToSend; ++i) {
                    sb.append('}');
                }
                String ret = sb.toString();
                return ret;
            }
        }
    
    // 转换成js能够执行的message【并包装回调js端的信息】
    void encodeAsJsMessage(StringBuilder sb) {
                if (pluginResult == null) {
                    sb.append(jsPayloadOrCallbackId);
                } else {
                    int status = pluginResult.getStatus();
                    boolean success = (status == PluginResult.Status.OK.ordinal()) || (status == PluginResult.Status.NO_RESULT.ordinal());
                    // js端执行 cordova#callbackFromNative 该方法去处理包装的信息
                    sb.append("cordova.callbackFromNative('")
                      .append(jsPayloadOrCallbackId)
                      .append("',")
                      .append(success)
                      .append(",")
                      .append(status)
                      .append(",[");
                    switch (pluginResult.getMessageType()) {
                        case PluginResult.MESSAGE_TYPE_BINARYSTRING:
                            sb.append("atob('")
                              .append(pluginResult.getMessage())
                              .append("')");
                            break;
                        case PluginResult.MESSAGE_TYPE_ARRAYBUFFER:
                            sb.append("cordova.require('cordova/base64').toArrayBuffer('")
                              .append(pluginResult.getMessage())
                              .append("')");
                            break;
                        default:
                        sb.append(pluginResult.getMessage());
                    }
                    sb.append("],")
                      .append(pluginResult.getKeepCallback())
                      .append(");");
                }
            }
    
    // cordova.callbackFromNative('XXXMessagePlugin1365781441',true,1,["{\"latitude\":31.181945,\"longitude\":121.365825}"],false
    
    
    
    
    
    //com.xxx.xxx.core.CordovaPlugin#execute(java.lang.String, java.lang.String, com.xxx.xxx.core.CallbackContext) 
    public boolean execute(String action, String rawArgs, CallbackContext callbackContext) throws JSONException {
            JSONArray args = new JSONArray(rawArgs);
            return execute(action, args, callbackContext);
        }
    
    //com.xxx.xxx.core.CordovaPlugin#execute(java.lang.String, com.xxx.xxx.core.CordovaArgs, com.xxx.xxx.core.CallbackContext)
      public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException {
            mMCallbackContext = callbackContext;
            return true;
        }
    
    
    
    
    
    
    
    
    
    
    
         //额外添加解耦插件添加方式,xml中声明即可,在androidmanifest合并后,sdk启动时通过appinfo读取mete-data信息
             <meta-data
                android:name ="com.xxx.xxx.plugin.ThirdPartPlugin"
                android:value ="ContainerPlugin"/>
    
            <meta-data
                android:name ="com.xxx.xxx.plugin.ThirdPartPluginAdd"
                android:value ="ContainerPlugin"/>
    
    
    
     /**
         * 映射外置插件的key
         */
        public static final String PLUGIN_MODULE_VALUE = "ContainerPlugin";
    
        public ArrayList<PluginEntry> parseManifest(Context context) {
            ArrayList<PluginEntry> pluginList = new ArrayList<>();
            try {
                ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
                if (appInfo.metaData!=null) {
                    for(String key : appInfo.metaData.keySet()) {
                        if(PLUGIN_MODULE_VALUE.equals(appInfo.metaData.get(key))) {
                            String service = key.substring(key.lastIndexOf('.')+1);
                            PluginEntry entry = new PluginEntry(service, key, false);
                            pluginList.add(entry);
                        }
                    }
                }
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
            return pluginList;
        }
    

    相关文章

      网友评论

        本文标题:补充Cordova流程源码

        本文链接:https://www.haomeiwen.com/subject/xfrhyktx.html