From c1df0b84138709525a55e74ad18acf82c162f5f6 Mon Sep 17 00:00:00 2001 From: yaakovschectman <109111084+yaakovschectman@users.noreply.github.com> Date: Thu, 16 Mar 2023 18:00:30 +0000 Subject: [PATCH] Pass parameters by value to `StreamHandlerError` (flutter/engine#40311) * Pass by value/rvalue * Test pass by value * Formatting * Test nullptr * Formatting --- .../client_wrapper/event_channel_unittests.cc | 26 +++++++++++++++++++ .../include/flutter/event_channel.h | 4 +-- .../include/flutter/event_stream_handler.h | 17 +++++++----- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/engine/src/flutter/shell/platform/common/client_wrapper/event_channel_unittests.cc b/engine/src/flutter/shell/platform/common/client_wrapper/event_channel_unittests.cc index 58005fcfff..0d68b10c83 100644 --- a/engine/src/flutter/shell/platform/common/client_wrapper/event_channel_unittests.cc +++ b/engine/src/flutter/shell/platform/common/client_wrapper/event_channel_unittests.cc @@ -274,4 +274,30 @@ TEST(EventChannelTest, HandlerOutlivesEventChannel) { EXPECT_EQ(on_cancel_called, true); } +TEST(EventChannelTest, StreamHandlerErrorPassByValue) { + std::unique_ptr> error = nullptr; + + { + std::string code = "Code"; + std::string msg = "Message"; + std::unique_ptr details = + std::make_unique("Details"); + error = + std::make_unique>(code, msg, std::move(details)); + } + + ASSERT_NE(error.get(), nullptr); + EXPECT_EQ(error->error_code, "Code"); + EXPECT_EQ(error->error_message, "Message"); + EXPECT_EQ(std::get(*error->error_details), "Details"); +} + +TEST(EventChannelTest, StreamHandlerErrorNullptr) { + std::unique_ptr> error = + std::make_unique>("Code", "Message", nullptr); + + ASSERT_NE(error.get(), nullptr); + EXPECT_FALSE(error->error_details); +} + } // namespace flutter diff --git a/engine/src/flutter/shell/platform/common/client_wrapper/include/flutter/event_channel.h b/engine/src/flutter/shell/platform/common/client_wrapper/include/flutter/event_channel.h index 08c242a2ff..6a3ef09b8e 100644 --- a/engine/src/flutter/shell/platform/common/client_wrapper/include/flutter/event_channel.h +++ b/engine/src/flutter/shell/platform/common/client_wrapper/include/flutter/event_channel.h @@ -106,7 +106,7 @@ class EventChannel { if (error) { result = codec->EncodeErrorEnvelope(error->error_code, error->error_message, - error->error_details); + error->error_details.get()); } else { result = codec->EncodeSuccessEnvelope(); } @@ -119,7 +119,7 @@ class EventChannel { if (error) { result = codec->EncodeErrorEnvelope(error->error_code, error->error_message, - error->error_details); + error->error_details.get()); } else { result = codec->EncodeSuccessEnvelope(); } diff --git a/engine/src/flutter/shell/platform/common/client_wrapper/include/flutter/event_stream_handler.h b/engine/src/flutter/shell/platform/common/client_wrapper/include/flutter/event_stream_handler.h index 9eced6cf70..9c1d89418d 100644 --- a/engine/src/flutter/shell/platform/common/client_wrapper/include/flutter/event_stream_handler.h +++ b/engine/src/flutter/shell/platform/common/client_wrapper/include/flutter/event_stream_handler.h @@ -5,6 +5,9 @@ #ifndef FLUTTER_SHELL_PLATFORM_COMMON_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_STREAM_HANDLER_H_ #define FLUTTER_SHELL_PLATFORM_COMMON_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_STREAM_HANDLER_H_ +#include +#include + #include "event_sink.h" namespace flutter { @@ -13,16 +16,16 @@ class EncodableValue; template struct StreamHandlerError { - const std::string& error_code; - const std::string& error_message; - const T* error_details; + const std::string error_code; + const std::string error_message; + const std::unique_ptr error_details; - StreamHandlerError(const std::string& error_code, - const std::string& error_message, - const T* error_details) + StreamHandlerError(const std::string error_code, + const std::string error_message, + std::unique_ptr&& error_details) : error_code(error_code), error_message(error_message), - error_details(error_details) {} + error_details(std::move(error_details)) {} }; // Handler for stream setup and teardown requests.