flutter: depth communication - receiving end

Environment: Flutter sdk v1.5.4-hotfix.1 @stable

对应 flutter engine: 52c7a1e849a170be4b2b2fe34142ca2c0a6fea1f

Foreword

By PlatformChannel Examples of internet layer of the receiving end we have learned DartMessengerby the interface in response handleMessageFromDartto the message Dart layer / layer operation of the transmitter to the platform, and this is PlatformMessageHandlerthe interface object, the object is held interface instance FlutterJNI.

As one of the passive call, the platform layer waiting to receive a message, do not know the source and purpose of the message, so we just need to find what you want, find out the caller, you can track the flow of the reception process is complete.

Trace

Easy to see that FlutterJN.handlePlatformMessagecall handleMessageFromDart, this function is marked @SuppressWarnings("unused"), and very likely related to the C ++ layer, the name really search method in ` 中找到" handlePlatformMessage " , 函数签名是" (Ljava / lang / String; [BI) V " 正是些方法,方法对象被全局变量g_handle_platform_message_method 持有,又被FlutterViewHandlePlatformMessage` references, and so far into the C ++ layer.

Here HandlePlatformMessagename is really misleading, feels like C ++ layer message processing platform layer message sent, but actually it is passed Dart layer to the platform, though handlePlatformXXXthis style have said message processing Dart layer and remains good, but still not receiveXXXbe simple, straightforward and clear.

To facilitate understanding of the following is the sequence of calls

DartMessenger.handleMessageFromDart => PlatformMessageHandler
  FlutterJNI.handlePlatformMessage => g_handle_platform_message_method
    FlutterViewHandlePlatformMessage 
      PlatformViewAndroid::HandlePlatformMessage <= PlatformView::HandlePlatformMessage
       ...Shell::OnEngineHandlePlatformMessage <= PlatformView::Delegate::OnEngineHandlePlatformMessage
         Engine::HandlePlatformMessage <= RuntimeDelegate::HandlePlatformMessage
           RuntimeController::HandlePlatformMessage <= WindowClient::HandlePlatformMessage
             ::SendPlatformMessage
             ...tonic::DartCallStatic(::_SendPlatformMessage
             ...Window::RegisterNatives

This is exactly the same sequence of levels of the transmitting side, top to bottom are Shell -> PlatformView -> Engine -> RuntimeController -> Window.

Seen FlutterViewHandlePlatformMessageis the end of the call C ++, global variables g_handle_platform_message_methodis actually a platform java method, we need to know when java method associated with the C ++ method, that is, g_handle_platform_message_methodwhen it was set:
The following is the sequence of calls

g_handle_platform_message_method = env->GetMethodID(,"handlePlatformMessage",)
  ::RegisterApi
    PlatformViewAndroid::Register
      JNI_OnLoad
        System.loadLibrary("flutter") (library_loader.cc:23)
          FlutterMain.startInitialization (FlutterMain.java:161)
            FlutterApplication.onCreate (FlutterApplication.java:22)

JNI_OnLoadIs declared in the linker script (android_exports.lst) in representing the operations performed when loaded.

Epilogue

Call details before 2 binding assay (accurate to function), and create some kind of key opportunity to make a brief flutter channel data communication class diagram as follows:
on the left is a java class, the right is a C ++ class

FlutterChannel

Guess you like

Origin www.cnblogs.com/lindeer/p/11119775.html