From 357df98a67671cb7c42b336547c07b364274c851 Mon Sep 17 00:00:00 2001 From: Loic Sharma Date: Fri, 20 Jan 2023 16:08:03 -0800 Subject: [PATCH 1/2] Clarify messenger APIs --- .../client_wrapper/include/flutter/method_channel.h | 5 ++--- shell/platform/common/public/flutter_messenger.h | 2 ++ shell/platform/windows/flutter_windows.cc | 9 +++++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/shell/platform/common/client_wrapper/include/flutter/method_channel.h b/shell/platform/common/client_wrapper/include/flutter/method_channel.h index e9ed6161c4bc4..3d873827c26f6 100644 --- a/shell/platform/common/client_wrapper/include/flutter/method_channel.h +++ b/shell/platform/common/client_wrapper/include/flutter/method_channel.h @@ -91,9 +91,8 @@ class MethodChannel { // Registers a handler that should be called any time a method call is // received on this channel. A null handler will remove any previous handler. // - // Note that the MethodChannel does not own the handler, and will not - // unregister it on destruction, so the caller is responsible for - // unregistering explicitly if it should no longer be called. + // The handler will be owned by the underlying BinaryMessageHandler. + // Destroying the MethodChannel will not unregister the handler. void SetMethodCallHandler(MethodCallHandler handler) const { if (!handler) { messenger_->SetMessageHandler(name_, nullptr); diff --git a/shell/platform/common/public/flutter_messenger.h b/shell/platform/common/public/flutter_messenger.h index 019267cbe7dfa..b364a573bec39 100644 --- a/shell/platform/common/public/flutter_messenger.h +++ b/shell/platform/common/public/flutter_messenger.h @@ -58,6 +58,8 @@ FLUTTER_EXPORT bool FlutterDesktopMessengerSend( const uint8_t* message, const size_t message_size); +// Sends a binary message to the Flutter side on the specified channel. +// The |reply| callback will be executed when a response is received. FLUTTER_EXPORT bool FlutterDesktopMessengerSendWithReply( FlutterDesktopMessengerRef messenger, const char* channel, diff --git a/shell/platform/windows/flutter_windows.cc b/shell/platform/windows/flutter_windows.cc index 76aa1e6854fdb..50bc538f1f8e0 100644 --- a/shell/platform/windows/flutter_windows.cc +++ b/shell/platform/windows/flutter_windows.cc @@ -265,6 +265,9 @@ bool FlutterDesktopMessengerSendWithReply(FlutterDesktopMessengerRef messenger, const size_t message_size, const FlutterDesktopBinaryReply reply, void* user_data) { + FML_DCHECK(FlutterDesktopMessengerIsAvailable(messenger)) + << "Messenger must reference a running engine to send a message"; + return flutter::FlutterDesktopMessenger::FromRef(messenger) ->GetEngine() ->SendPlatformMessage(channel, message, message_size, reply, user_data); @@ -283,6 +286,9 @@ void FlutterDesktopMessengerSendResponse( const FlutterDesktopMessageResponseHandle* handle, const uint8_t* data, size_t data_length) { + FML_DCHECK(FlutterDesktopMessengerIsAvailable(messenger)) + << "Messenger must reference a running engine to send a response"; + flutter::FlutterDesktopMessenger::FromRef(messenger) ->GetEngine() ->SendPlatformMessageResponse(handle, data, data_length); @@ -292,6 +298,9 @@ void FlutterDesktopMessengerSetCallback(FlutterDesktopMessengerRef messenger, const char* channel, FlutterDesktopMessageCallback callback, void* user_data) { + FML_DCHECK(FlutterDesktopMessengerIsAvailable(messenger)) + << "Messenger must reference a running engine to set a callback"; + flutter::FlutterDesktopMessenger::FromRef(messenger) ->GetEngine() ->message_dispatcher() From b9cb5044ae18b2fefe0b6dc465914b95126842fb Mon Sep 17 00:00:00 2001 From: Loic Sharma Date: Tue, 24 Jan 2023 14:12:50 -0800 Subject: [PATCH 2/2] Feedback --- .../common/client_wrapper/include/flutter/method_channel.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/shell/platform/common/client_wrapper/include/flutter/method_channel.h b/shell/platform/common/client_wrapper/include/flutter/method_channel.h index 3d873827c26f6..6e39a644f6e08 100644 --- a/shell/platform/common/client_wrapper/include/flutter/method_channel.h +++ b/shell/platform/common/client_wrapper/include/flutter/method_channel.h @@ -92,7 +92,9 @@ class MethodChannel { // received on this channel. A null handler will remove any previous handler. // // The handler will be owned by the underlying BinaryMessageHandler. - // Destroying the MethodChannel will not unregister the handler. + // Destroying the MethodChannel will not unregister the handler, so + // the caller is responsible for unregistering explicitly if the handler + // stops being valid before the engine is destroyed. void SetMethodCallHandler(MethodCallHandler handler) const { if (!handler) { messenger_->SetMessageHandler(name_, nullptr);