From 44a638bf3d1444d9dc00683c58bc4065ff0f7937 Mon Sep 17 00:00:00 2001 From: Kurun Date: Sat, 7 Mar 2020 20:45:47 +0900 Subject: [PATCH 01/46] Initial commit --- ci/licenses_golden/licenses_flutter | 4 + .../common/cpp/client_wrapper/BUILD.gn | 1 + .../cpp/client_wrapper/core_wrapper_files.gni | 3 + .../client_wrapper/event_channel_unittests.cc | 98 ++++++++++ .../include/flutter/event_channel.h | 182 ++++++++++++++++++ .../include/flutter/event_sink.h | 54 ++++++ .../include/flutter/event_stream_handler.h | 42 ++++ 7 files changed, 384 insertions(+) create mode 100644 shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc create mode 100644 shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h create mode 100644 shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h create mode 100644 shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index d0182f4580ab0..1da7ac9b13ed1 100644 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -768,6 +768,9 @@ FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/ FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/method_channel.h FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/method_codec.h FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/method_result.h +FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h +FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h +FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/plugin_registrar.h FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/plugin_registry.h FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/standard_message_codec.h @@ -776,6 +779,7 @@ FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/json_message_cod FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/json_method_codec.cc FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/method_call_unittests.cc FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/method_channel_unittests.cc +FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/plugin_registrar.cc FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/plugin_registrar_unittests.cc FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/standard_codec.cc diff --git a/shell/platform/common/cpp/client_wrapper/BUILD.gn b/shell/platform/common/cpp/client_wrapper/BUILD.gn index a8d30109d2856..12efefdc1ff43 100644 --- a/shell/platform/common/cpp/client_wrapper/BUILD.gn +++ b/shell/platform/common/cpp/client_wrapper/BUILD.gn @@ -50,6 +50,7 @@ executable("client_wrapper_unittests") { "encodable_value_unittests.cc", "method_call_unittests.cc", "method_channel_unittests.cc", + "event_channel_unittests.cc", "plugin_registrar_unittests.cc", "standard_message_codec_unittests.cc", "standard_method_codec_unittests.cc", diff --git a/shell/platform/common/cpp/client_wrapper/core_wrapper_files.gni b/shell/platform/common/cpp/client_wrapper/core_wrapper_files.gni index ce91be7866717..f57e1192c4d4b 100644 --- a/shell/platform/common/cpp/client_wrapper/core_wrapper_files.gni +++ b/shell/platform/common/cpp/client_wrapper/core_wrapper_files.gni @@ -16,6 +16,9 @@ core_cpp_client_wrapper_includes = "include/flutter/method_channel.h", "include/flutter/method_codec.h", "include/flutter/method_result.h", + "include/flutter/event_channel.h", + "include/flutter/event_stream_handler.h", + "include/flutter/event_sink.h", "include/flutter/plugin_registrar.h", "include/flutter/plugin_registry.h", "include/flutter/standard_message_codec.h", diff --git a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc new file mode 100644 index 0000000000000..1c659cd3397b2 --- /dev/null +++ b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc @@ -0,0 +1,98 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h" + +#include +#include + +#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/binary_messenger.h" +#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/standard_method_codec.h" +#include "gtest/gtest.h" + +namespace flutter { + +namespace { + +class TestBinaryMessenger : public BinaryMessenger { + public: + void Send(const std::string& channel, + const uint8_t* message, + const size_t message_size) const override {} + + void Send(const std::string& channel, + const uint8_t* message, + const size_t message_size, + BinaryReply reply) const override {} + + void SetMessageHandler(const std::string& channel, + BinaryMessageHandler handler) override { + last_message_handler_channel_ = channel; + last_message_handler_ = handler; + } + + std::string last_message_handler_channel() { + return last_message_handler_channel_; + } + + BinaryMessageHandler last_message_handler() { return last_message_handler_; } + + private: + std::string last_message_handler_channel_; + BinaryMessageHandler last_message_handler_; +}; + +} // namespace + +// Tests that SetStreamHandler sets a handler that correctly interacts with +// the binary messenger. +TEST(EventChannelTest, Registration) { + TestBinaryMessenger messenger; + const std::string channel_name("some_channel"); + const StandardMethodCodec& codec = StandardMethodCodec::GetInstance(); + EventChannel channel(&messenger, channel_name, &codec); + + bool callback_called = false; + const std::string method_name("hello"); + + // TODO: fix test code + auto onListen = [](const flutter::EncodableValue* arguments, + const EventSink* event_sink){}; + auto onCancel = [](const flutter::EncodableValue* arguments){}; + channel.SetStreamHandler( + flutter::StreamHandler(onListen, onCancel)); + + EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); + EXPECT_NE(messenger.last_message_handler(), nullptr); + // Send a test message to trigger the handler test assertions. + MethodCall call(method_name, nullptr); + auto message = codec.EncodeMethodCall(call); + + messenger.last_message_handler()( + message->data(), message->size(), + [](const uint8_t* reply, const size_t reply_size) {}); + EXPECT_EQ(callback_called, true); +} + +// Tests that SetStreamHandler with a null handler unregisters the handler. +TEST(EventChannelTest, Unregistration) { + TestBinaryMessenger messenger; + const std::string channel_name("some_channel"); + EventChannel channel(&messenger, channel_name, + &flutter::StandardMethodCodec::GetInstance()); + + auto onListen = [](const flutter::EncodableValue* arguments, + const EventSink* event_sink){}; + auto onCancel = [](const flutter::EncodableValue* arguments){}; + channel.SetStreamHandler( + flutter::StreamHandler(onListen, onCancel)); + EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); + EXPECT_NE(messenger.last_message_handler(), nullptr); + + channel.SetStreamHandler(std::nullopt); + EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); + EXPECT_EQ(messenger.last_message_handler(), nullptr); +} + +} // namespace flutter diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h new file mode 100644 index 0000000000000..db042c471ff13 --- /dev/null +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h @@ -0,0 +1,182 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_CHANNEL_H_ +#define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_CHANNEL_H_ + +#include +#include +#include + +#include "binary_messenger.h" +#include "engine_method_result.h" +#include "event_stream_handler.h" +#include "event_sink.h" + +static constexpr char kOnListenMethod[] = "listen"; +static constexpr char kOnCancelMethod[] = "cancel"; + +namespace flutter { + +// A named channel for communicating with the Flutter application using asynchronous event streams. +// Incoming requests for event stream setup are decoded from binary on receipt, and C++ +// responses and events are encoded into binary before being transmitted back to Flutter. The +// MethodCodec used must be compatible with the one used by the Flutter application. This can be +// achieved by creating an +// EventChannel +// counterpart of this channel on the Dart side. The C++ type of stream configuration arguments, +// events, and error details is Object, but only values supported by the specified +// MethodCodec can be used. +// +// The logical identity of the channel is given by its name. Identically named channels will +// interfere with each other's communication. +template +class EventChannel { + public: + // Creates an instance that sends and receives event handler on the channel + // named |name|, encoded with |codec| and dispatched via |messenger|. + EventChannel(BinaryMessenger* messenger, + const std::string& name, + const MethodCodec* codec) + : messenger_(messenger), name_(name), codec_(codec) {} + ~EventChannel() = default; + + // Prevent copying. + EventChannel(EventChannel const&) = delete; + EventChannel& operator=(EventChannel const&) = delete; + + // Registers a stream handler on this channel. + // If no handler has been registered, any incoming stream setup requests will be handled + // silently by providing an empty stream. + void SetStreamHandler(std::optional> const& handler) const { + if (!handler) { + messenger_->SetMessageHandler(name_, nullptr); + return; + } + + const auto* codec = codec_; + const std::string channel_name = name_; + const auto* messenger = messenger_; + EventSinkImplementation* current_sink = current_sink_; + BinaryMessageHandler binary_handler = [handler, codec, channel_name, + current_sink, messenger]( + const uint8_t* message, + const size_t message_size, + BinaryReply reply) mutable { + std::unique_ptr> method_call = + codec->DecodeMethodCall(message, message_size); + if (!method_call) { + std::cerr << "Unable to construct method call from message on channel " + << channel_name << std::endl; + return; + } + + const std::string& method = method_call->method_name(); + if (method.compare(kOnListenMethod) == 0) { + if (current_sink) { + std::cerr << "Failed to cancel existing stream: " + << channel_name << std::endl; + handler->onCancel(method_call->arguments()); + delete current_sink; + } + + current_sink = + new EventSinkImplementation(messenger, channel_name, codec); + handler->onListen(method_call->arguments(), + static_cast*>(current_sink)); + + { + auto result = codec->EncodeSuccessEnvelope(); + uint8_t* buffer = new uint8_t[result->size()]; + std::copy(result->begin(), result->end(), buffer); + reply(buffer, result->size()); + delete[] buffer; + } + } + else if (method.compare(kOnCancelMethod) == 0) { + if (current_sink) { + handler->onCancel(method_call->arguments()); + + auto result = codec->EncodeSuccessEnvelope(); + uint8_t* buffer = new uint8_t[result->size()]; + std::copy(result->begin(), result->end(), buffer); + reply(buffer, result->size()); + delete current_sink; + } + else { + auto result = + codec->EncodeErrorEnvelope("error", + "No active stream to cancel", nullptr); + uint8_t* buffer = new uint8_t[result->size()]; + std::copy(result->begin(), result->end(), buffer); + reply(buffer, result->size()); + delete[] buffer; + } + } + else { + std::cerr << "Unknown event channel method call from message on channel: " + << channel_name << std::endl; + reply(nullptr, 0); + if (current_sink) + delete current_sink; + } + }; + messenger_->SetMessageHandler(name_, std::move(binary_handler)); + } + + private: + class EventSinkImplementation : public EventSink { + public: + // Creates an instance that EventSink send event on the channel + // named |name|, encoded with |codec| and dispatched via |messenger|. + EventSinkImplementation(const BinaryMessenger* messenger, + const std::string& name, + const MethodCodec* codec) + : messenger_(messenger), name_(name), codec_(codec) {} + ~EventSinkImplementation() = default; + + // Prevent copying. + EventSinkImplementation(EventSinkImplementation const&) = delete; + EventSinkImplementation& operator=(EventSinkImplementation const&) = delete; + + private: + const BinaryMessenger* messenger_; + const std::string name_; + const MethodCodec* codec_; + + protected: + void SuccessInternal(const T* event = nullptr) override { + auto result = codec_->EncodeSuccessEnvelope(event); + uint8_t* buffer = new uint8_t[result->size()]; + std::copy(result->begin(), result->end(), buffer); + messenger_->Send(name_, buffer, result->size()); + delete[] buffer; + } + + void ErrorInternal(const std::string& error_code, + const std::string& error_message, + const T* error_details) override { + auto result = + codec_->EncodeErrorEnvelope(error_code, error_message, error_details); + uint8_t* buffer = new uint8_t[result->size()]; + std::copy(result->begin(), result->end(), buffer); + messenger_->Send(name_, buffer, result->size()); + delete[] buffer; + } + + void EndOfStreamInternal() override { + messenger_->Send(name_, nullptr, 0); + } + }; + + BinaryMessenger* messenger_; + std::string name_; + const MethodCodec* codec_; + EventSinkImplementation* current_sink_; + +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_CHANNEL_H_ diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h new file mode 100644 index 0000000000000..2ae8761b145d0 --- /dev/null +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h @@ -0,0 +1,54 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_SINK_H_ +#define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_SINK_H_ + +namespace flutter { + +// Event callback. Supports dual use: Producers of events to be sent to Flutter act as clients of +// this interface for sending events. Consumers of events sent from Flutter implement this +// interface for handling received events. +template +class EventSink { + public: + EventSink() = default; + virtual ~EventSink() = default; + + // Prevent copying. + EventSink(EventSink const&) = delete; + EventSink& operator=(EventSink const&) = delete; + + // Consumes end of stream. Ensuing calls to Success(T) or + // Error(String, String, Object)}, if any, are ignored. + void EndOfStream() { EndOfStreamInternal(); } + + // Consumes a successful event. + void Success(const T* event = nullptr) { + SuccessInternal(event); + } + + // Consumes an error event. + void Error(const std::string& error_code, + const std::string& error_message = "", + const T* error_details = nullptr) { + ErrorInternal(error_code, error_message, error_details); + } + + protected: + // Implementation of the public interface, to be provided by subclasses. + virtual void EndOfStreamInternal() = 0; + + // Implementation of the public interface, to be provided by subclasses. + virtual void SuccessInternal(const T* event = nullptr) = 0; + + // Implementation of the public interface, to be provided by subclasses. + virtual void ErrorInternal(const std::string& error_code, + const std::string& error_message, + const T* error_details) = 0; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_SINK_H_ diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h new file mode 100644 index 0000000000000..f97bd4fccd366 --- /dev/null +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h @@ -0,0 +1,42 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_STREAM_HANDLER_H_ +#define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_STREAM_HANDLER_H_ + +#include "event_sink.h" + +namespace flutter { + +// Handler of stream setup and tear-down requests. +// Implementations must be prepared to accept sequences of alternating calls to +// onListen(T, EventSink) and onCancel(Object). Implementations should ideally +// consume no resources when the last such call is not onListen(). In typical situations, +// this means that the implementation should register itself with platform-specific event sources +// onListen() and deregister again onCancel(). +template +class StreamHandler { + public: + // Handles a request to set up an event stream. + // @param arguments stream configuration arguments, possibly null. + // @param event_sink an EventSink for emitting events to the Flutter receiver. + using OnListen = std::function* event_sink)>; + + // Handles a request to tear down the most recently created event stream. + // @param arguments stream configuration arguments, possibly null. + using OnCancel = std::function; + + OnListen onListen; + OnCancel onCancel; + + // Registers a stream handler on this channel. + // If no handler has been registered, any incoming stream setup requests will be handled + // silently by providing an empty stream. + StreamHandler( OnListen const& onListen, OnCancel const& onCancel) + : onListen(onListen), onCancel(onCancel) {} +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_STREAM_HANDLER_H_ From 395df1ef5efac15701cd7cf9ec8ca40ce316ae19 Mon Sep 17 00:00:00 2001 From: Kurun Date: Sun, 8 Mar 2020 15:41:26 +0900 Subject: [PATCH 02/46] remove std::optional (C++17) --- .../client_wrapper/event_channel_unittests.cc | 6 +++--- .../include/flutter/event_channel.h | 18 +++++++++--------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc index 1c659cd3397b2..c85414d84ebf2 100644 --- a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc +++ b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc @@ -90,9 +90,9 @@ TEST(EventChannelTest, Unregistration) { EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); EXPECT_NE(messenger.last_message_handler(), nullptr); - channel.SetStreamHandler(std::nullopt); - EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); - EXPECT_EQ(messenger.last_message_handler(), nullptr); + //channel.SetStreamHandler(std::nullopt); + //EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); + //EXPECT_EQ(messenger.last_message_handler(), nullptr); } } // namespace flutter diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h index db042c471ff13..270f6bc3976bd 100644 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h @@ -7,7 +7,6 @@ #include #include -#include #include "binary_messenger.h" #include "engine_method_result.h" @@ -49,11 +48,12 @@ class EventChannel { // Registers a stream handler on this channel. // If no handler has been registered, any incoming stream setup requests will be handled // silently by providing an empty stream. - void SetStreamHandler(std::optional> const& handler) const { - if (!handler) { - messenger_->SetMessageHandler(name_, nullptr); - return; - } + //void SetStreamHandler(std::optional> const& handler) const { + void SetStreamHandler(const StreamHandler& handler) const { + //if (!handler) { + // messenger_->SetMessageHandler(name_, nullptr); + // return; + //} const auto* codec = codec_; const std::string channel_name = name_; @@ -77,13 +77,13 @@ class EventChannel { if (current_sink) { std::cerr << "Failed to cancel existing stream: " << channel_name << std::endl; - handler->onCancel(method_call->arguments()); + handler.onCancel(method_call->arguments()); delete current_sink; } current_sink = new EventSinkImplementation(messenger, channel_name, codec); - handler->onListen(method_call->arguments(), + handler.onListen(method_call->arguments(), static_cast*>(current_sink)); { @@ -96,7 +96,7 @@ class EventChannel { } else if (method.compare(kOnCancelMethod) == 0) { if (current_sink) { - handler->onCancel(method_call->arguments()); + handler.onCancel(method_call->arguments()); auto result = codec->EncodeSuccessEnvelope(); uint8_t* buffer = new uint8_t[result->size()]; From c556c7abce6d15df96977840ad7b3dafde4a0478 Mon Sep 17 00:00:00 2001 From: Kurun Date: Sun, 8 Mar 2020 17:51:46 +0900 Subject: [PATCH 03/46] Fix unit test code, bugfix. --- .../client_wrapper/event_channel_unittests.cc | 39 +++++++++++++------ .../include/flutter/event_channel.h | 8 ++-- .../include/flutter/event_sink.h | 8 ++-- .../include/flutter/event_stream_handler.h | 4 +- 4 files changed, 38 insertions(+), 21 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc index c85414d84ebf2..4a95b38df2f19 100644 --- a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc +++ b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc @@ -53,26 +53,43 @@ TEST(EventChannelTest, Registration) { const StandardMethodCodec& codec = StandardMethodCodec::GetInstance(); EventChannel channel(&messenger, channel_name, &codec); - bool callback_called = false; - const std::string method_name("hello"); - - // TODO: fix test code - auto onListen = [](const flutter::EncodableValue* arguments, - const EventSink* event_sink){}; - auto onCancel = [](const flutter::EncodableValue* arguments){}; + bool on_listen_called = false; + auto onListen = [&on_listen_called](const flutter::EncodableValue* arguments, + EventSink* event_sink) { + event_sink->Success(); + auto message = flutter::EncodableValue(flutter::EncodableMap{ + {flutter::EncodableValue("message"), + flutter::EncodableValue("Test from Event Channel")} + }); + event_sink->Success(&message); + event_sink->Error("Event Channel Error Code", + "Error Message", + nullptr); + event_sink->EndOfStream(); + event_sink->Success(&message); + + on_listen_called = true; + }; + + bool on_cancel_called = false; + auto onCancel = [&on_cancel_called](const flutter::EncodableValue* arguments) { + on_cancel_called = true; + }; channel.SetStreamHandler( flutter::StreamHandler(onListen, onCancel)); EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); EXPECT_NE(messenger.last_message_handler(), nullptr); + // Send a test message to trigger the handler test assertions. - MethodCall call(method_name, nullptr); + MethodCall call("listen", nullptr); auto message = codec.EncodeMethodCall(call); - messenger.last_message_handler()( message->data(), message->size(), [](const uint8_t* reply, const size_t reply_size) {}); - EXPECT_EQ(callback_called, true); + + EXPECT_EQ(on_listen_called, true); + EXPECT_EQ(on_cancel_called, true); } // Tests that SetStreamHandler with a null handler unregisters the handler. @@ -83,7 +100,7 @@ TEST(EventChannelTest, Unregistration) { &flutter::StandardMethodCodec::GetInstance()); auto onListen = [](const flutter::EncodableValue* arguments, - const EventSink* event_sink){}; + EventSink* event_sink){}; auto onCancel = [](const flutter::EncodableValue* arguments){}; channel.SetStreamHandler( flutter::StreamHandler(onListen, onCancel)); diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h index 270f6bc3976bd..1b8d8a8e3aaa7 100644 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h @@ -48,7 +48,7 @@ class EventChannel { // Registers a stream handler on this channel. // If no handler has been registered, any incoming stream setup requests will be handled // silently by providing an empty stream. - //void SetStreamHandler(std::optional> const& handler) const { + //void SetStreamHandler(std::optional> const& handler) const { /* <= available for more than C++17 */ void SetStreamHandler(const StreamHandler& handler) const { //if (!handler) { // messenger_->SetMessageHandler(name_, nullptr); @@ -146,7 +146,7 @@ class EventChannel { const MethodCodec* codec_; protected: - void SuccessInternal(const T* event = nullptr) override { + void SuccessInternal(T* event = nullptr) override { auto result = codec_->EncodeSuccessEnvelope(event); uint8_t* buffer = new uint8_t[result->size()]; std::copy(result->begin(), result->end(), buffer); @@ -155,8 +155,8 @@ class EventChannel { } void ErrorInternal(const std::string& error_code, - const std::string& error_message, - const T* error_details) override { + const std::string& error_message, + T* error_details) override { auto result = codec_->EncodeErrorEnvelope(error_code, error_message, error_details); uint8_t* buffer = new uint8_t[result->size()]; diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h index 2ae8761b145d0..371b457bf21d6 100644 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h @@ -25,14 +25,14 @@ class EventSink { void EndOfStream() { EndOfStreamInternal(); } // Consumes a successful event. - void Success(const T* event = nullptr) { + void Success(T* event = nullptr) { SuccessInternal(event); } // Consumes an error event. void Error(const std::string& error_code, const std::string& error_message = "", - const T* error_details = nullptr) { + T* error_details = nullptr) { ErrorInternal(error_code, error_message, error_details); } @@ -41,12 +41,12 @@ class EventSink { virtual void EndOfStreamInternal() = 0; // Implementation of the public interface, to be provided by subclasses. - virtual void SuccessInternal(const T* event = nullptr) = 0; + virtual void SuccessInternal(T* event = nullptr) = 0; // Implementation of the public interface, to be provided by subclasses. virtual void ErrorInternal(const std::string& error_code, const std::string& error_message, - const T* error_details) = 0; + T* error_details) = 0; }; } // namespace flutter diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h index f97bd4fccd366..80472ee38c6ed 100644 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h @@ -21,7 +21,7 @@ class StreamHandler { // Handles a request to set up an event stream. // @param arguments stream configuration arguments, possibly null. // @param event_sink an EventSink for emitting events to the Flutter receiver. - using OnListen = std::function* event_sink)>; + using OnListen = std::function* event_sink)>; // Handles a request to tear down the most recently created event stream. // @param arguments stream configuration arguments, possibly null. @@ -33,7 +33,7 @@ class StreamHandler { // Registers a stream handler on this channel. // If no handler has been registered, any incoming stream setup requests will be handled // silently by providing an empty stream. - StreamHandler( OnListen const& onListen, OnCancel const& onCancel) + StreamHandler(OnListen const& onListen, OnCancel const& onCancel) : onListen(onListen), onCancel(onCancel) {} }; From 1000496b8bddac0d9bb30afc0918dc30f8db32ca Mon Sep 17 00:00:00 2001 From: Kurun Date: Sun, 8 Mar 2020 19:12:07 +0900 Subject: [PATCH 04/46] Fixed bug that the error of EventChannel register stream handler when app startup. --- .../client_wrapper/include/flutter/event_channel.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h index 1b8d8a8e3aaa7..a880ba1c6ac9f 100644 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h @@ -48,9 +48,9 @@ class EventChannel { // Registers a stream handler on this channel. // If no handler has been registered, any incoming stream setup requests will be handled // silently by providing an empty stream. - //void SetStreamHandler(std::optional> const& handler) const { /* <= available for more than C++17 */ void SetStreamHandler(const StreamHandler& handler) const { - //if (!handler) { + //void SetStreamHandler(std::optional> const& handler) const { + //if (!handler) { /* <= available for more than C++17 */ // messenger_->SetMessageHandler(name_, nullptr); // return; //} @@ -79,6 +79,7 @@ class EventChannel { << channel_name << std::endl; handler.onCancel(method_call->arguments()); delete current_sink; + current_sink = nullptr; } current_sink = @@ -103,6 +104,7 @@ class EventChannel { std::copy(result->begin(), result->end(), buffer); reply(buffer, result->size()); delete current_sink; + current_sink = nullptr; } else { auto result = @@ -118,8 +120,10 @@ class EventChannel { std::cerr << "Unknown event channel method call from message on channel: " << channel_name << std::endl; reply(nullptr, 0); - if (current_sink) + if (current_sink) { delete current_sink; + current_sink = nullptr; + } } }; messenger_->SetMessageHandler(name_, std::move(binary_handler)); @@ -173,7 +177,7 @@ class EventChannel { BinaryMessenger* messenger_; std::string name_; const MethodCodec* codec_; - EventSinkImplementation* current_sink_; + EventSinkImplementation* current_sink_ = nullptr; }; From ae81b9d3ff862b1e050de0e72e2e7e7a01230634 Mon Sep 17 00:00:00 2001 From: Kurun Date: Mon, 9 Mar 2020 19:38:58 +0900 Subject: [PATCH 05/46] Fix coding format. https://github.com/flutter/engine/pull/17015/checks?check_run_id=493248665 --- .../client_wrapper/include/flutter/event_sink.h | 4 +--- .../include/flutter/event_stream_handler.h | 17 +++++++++-------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h index 371b457bf21d6..73053dae52187 100644 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h @@ -25,9 +25,7 @@ class EventSink { void EndOfStream() { EndOfStreamInternal(); } // Consumes a successful event. - void Success(T* event = nullptr) { - SuccessInternal(event); - } + void Success(T* event = nullptr) { SuccessInternal(event); } // Consumes an error event. void Error(const std::string& error_code, diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h index 80472ee38c6ed..81e4bcc84f202 100644 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h @@ -10,18 +10,19 @@ namespace flutter { // Handler of stream setup and tear-down requests. -// Implementations must be prepared to accept sequences of alternating calls to +// Implementations must be prepared to accept sequences of alternating calls to // onListen(T, EventSink) and onCancel(Object). Implementations should ideally -// consume no resources when the last such call is not onListen(). In typical situations, -// this means that the implementation should register itself with platform-specific event sources -// onListen() and deregister again onCancel(). +// consume no resources when the last such call is not onListen(). In typical +// situations, this means that the implementation should register itself with +// platform-specific event sources onListen() and deregister again onCancel(). template class StreamHandler { public: // Handles a request to set up an event stream. // @param arguments stream configuration arguments, possibly null. // @param event_sink an EventSink for emitting events to the Flutter receiver. - using OnListen = std::function* event_sink)>; + using OnListen = + std::function* event_sink)>; // Handles a request to tear down the most recently created event stream. // @param arguments stream configuration arguments, possibly null. @@ -31,10 +32,10 @@ class StreamHandler { OnCancel onCancel; // Registers a stream handler on this channel. - // If no handler has been registered, any incoming stream setup requests will be handled - // silently by providing an empty stream. + // If no handler has been registered, any incoming stream setup requests will + // be handled silently by providing an empty stream. StreamHandler(OnListen const& onListen, OnCancel const& onCancel) - : onListen(onListen), onCancel(onCancel) {} + : onListen(onListen), onCancel(onCancel) {} }; } // namespace flutter From 9069e4c96784f656402fc439798e5910ec2291ad Mon Sep 17 00:00:00 2001 From: Kurun Date: Mon, 9 Mar 2020 19:49:47 +0900 Subject: [PATCH 06/46] Fix unit test fail. https://github.com/flutter/engine/pull/17015/checks?check_run_id=493248658 --- .../common/cpp/client_wrapper/event_channel_unittests.cc | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc index 4a95b38df2f19..373ad6bb2bd04 100644 --- a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc +++ b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc @@ -87,8 +87,14 @@ TEST(EventChannelTest, Registration) { messenger.last_message_handler()( message->data(), message->size(), [](const uint8_t* reply, const size_t reply_size) {}); - EXPECT_EQ(on_listen_called, true); + + // Send a test message to trigger the handler test assertions. + MethodCall call("cancel", nullptr); + auto message = codec.EncodeMethodCall(call); + messenger.last_message_handler()( + message->data(), message->size(), + [](const uint8_t* reply, const size_t reply_size) {}); EXPECT_EQ(on_cancel_called, true); } From 8e96c4f011c43f49697c87f14374b02443ebb00c Mon Sep 17 00:00:00 2001 From: Kurun Date: Mon, 9 Mar 2020 20:11:40 +0900 Subject: [PATCH 07/46] Fix coding format https://github.com/flutter/engine/pull/17015/checks?check_run_id=494856201 --- .../include/flutter/event_channel.h | 17 +++++++---------- .../client_wrapper/include/flutter/event_sink.h | 6 +++--- .../include/flutter/event_stream_handler.h | 2 +- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h index a880ba1c6ac9f..573242db05a1b 100644 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h @@ -82,8 +82,8 @@ class EventChannel { current_sink = nullptr; } - current_sink = - new EventSinkImplementation(messenger, channel_name, codec); + current_sink = + new EventSinkImplementation(messenger, channel_name, codec); handler.onListen(method_call->arguments(), static_cast*>(current_sink)); @@ -107,8 +107,8 @@ class EventChannel { current_sink = nullptr; } else { - auto result = - codec->EncodeErrorEnvelope("error", + auto result = + codec->EncodeErrorEnvelope("error", "No active stream to cancel", nullptr); uint8_t* buffer = new uint8_t[result->size()]; std::copy(result->begin(), result->end(), buffer); @@ -161,24 +161,21 @@ class EventChannel { void ErrorInternal(const std::string& error_code, const std::string& error_message, T* error_details) override { - auto result = - codec_->EncodeErrorEnvelope(error_code, error_message, error_details); + auto result = + codec_->EncodeErrorEnvelope(error_code, error_message, error_details); uint8_t* buffer = new uint8_t[result->size()]; std::copy(result->begin(), result->end(), buffer); messenger_->Send(name_, buffer, result->size()); delete[] buffer; } - void EndOfStreamInternal() override { - messenger_->Send(name_, nullptr, 0); - } + void EndOfStreamInternal() override { messenger_->Send(name_, nullptr, 0); } }; BinaryMessenger* messenger_; std::string name_; const MethodCodec* codec_; EventSinkImplementation* current_sink_ = nullptr; - }; } // namespace flutter diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h index 73053dae52187..4b6f8d1860137 100644 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h @@ -7,9 +7,9 @@ namespace flutter { -// Event callback. Supports dual use: Producers of events to be sent to Flutter act as clients of -// this interface for sending events. Consumers of events sent from Flutter implement this -// interface for handling received events. +// Event callback. Supports dual use: Producers of events to be sent to Flutter +// act as clients of this interface for sending events. Consumers of events sent +// from Flutter implement this interface for handling received events. template class EventSink { public: diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h index 81e4bcc84f202..b2a14a543b319 100644 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h @@ -21,7 +21,7 @@ class StreamHandler { // Handles a request to set up an event stream. // @param arguments stream configuration arguments, possibly null. // @param event_sink an EventSink for emitting events to the Flutter receiver. - using OnListen = + using OnListen = std::function* event_sink)>; // Handles a request to tear down the most recently created event stream. From b8fe5047c9ad779cd2c669670d72de78c73f814a Mon Sep 17 00:00:00 2001 From: Kurun Date: Mon, 9 Mar 2020 20:39:51 +0900 Subject: [PATCH 08/46] Fix build break. https://github.com/flutter/engine/pull/17015/checks?check_run_id=494856238 --- .../client_wrapper/event_channel_unittests.cc | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc index 373ad6bb2bd04..7ee9443f8ccdb 100644 --- a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc +++ b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc @@ -82,20 +82,24 @@ TEST(EventChannelTest, Registration) { EXPECT_NE(messenger.last_message_handler(), nullptr); // Send a test message to trigger the handler test assertions. - MethodCall call("listen", nullptr); - auto message = codec.EncodeMethodCall(call); - messenger.last_message_handler()( - message->data(), message->size(), - [](const uint8_t* reply, const size_t reply_size) {}); - EXPECT_EQ(on_listen_called, true); + { + MethodCall call("listen", nullptr); + auto message = codec.EncodeMethodCall(call); + messenger.last_message_handler()( + message->data(), message->size(), + [](const uint8_t* reply, const size_t reply_size) {}); + EXPECT_EQ(on_listen_called, true); + } // Send a test message to trigger the handler test assertions. - MethodCall call("cancel", nullptr); - auto message = codec.EncodeMethodCall(call); - messenger.last_message_handler()( - message->data(), message->size(), - [](const uint8_t* reply, const size_t reply_size) {}); - EXPECT_EQ(on_cancel_called, true); + { + MethodCall call("cancel", nullptr); + auto message = codec.EncodeMethodCall(call); + messenger.last_message_handler()( + message->data(), message->size(), + [](const uint8_t* reply, const size_t reply_size) {}); + EXPECT_EQ(on_cancel_called, true); + } } // Tests that SetStreamHandler with a null handler unregisters the handler. From 251829eb1a18a7ee9af5ee213b2664571726cca9 Mon Sep 17 00:00:00 2001 From: Kurun Date: Mon, 9 Mar 2020 20:53:17 +0900 Subject: [PATCH 09/46] Fix code format https://github.com/flutter/engine/pull/17015/checks?check_run_id=494955839 --- .../include/flutter/event_channel.h | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h index 573242db05a1b..7a579b0682a3a 100644 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h @@ -94,8 +94,7 @@ class EventChannel { reply(buffer, result->size()); delete[] buffer; } - } - else if (method.compare(kOnCancelMethod) == 0) { + } else if (method.compare(kOnCancelMethod) == 0) { if (current_sink) { handler.onCancel(method_call->arguments()); @@ -105,20 +104,18 @@ class EventChannel { reply(buffer, result->size()); delete current_sink; current_sink = nullptr; - } - else { - auto result = - codec->EncodeErrorEnvelope("error", - "No active stream to cancel", nullptr); + } else { + auto result = codec->EncodeErrorEnvelope( + "error", "No active stream to cancel", nullptr); uint8_t* buffer = new uint8_t[result->size()]; std::copy(result->begin(), result->end(), buffer); reply(buffer, result->size()); delete[] buffer; } - } - else { - std::cerr << "Unknown event channel method call from message on channel: " - << channel_name << std::endl; + } else { + std::cerr + << "Unknown event channel method call from message on channel: " + << channel_name << std::endl; reply(nullptr, 0); if (current_sink) { delete current_sink; @@ -131,12 +128,12 @@ class EventChannel { private: class EventSinkImplementation : public EventSink { - public: + public: // Creates an instance that EventSink send event on the channel // named |name|, encoded with |codec| and dispatched via |messenger|. EventSinkImplementation(const BinaryMessenger* messenger, - const std::string& name, - const MethodCodec* codec) + const std::string& name, + const MethodCodec* codec) : messenger_(messenger), name_(name), codec_(codec) {} ~EventSinkImplementation() = default; @@ -144,12 +141,12 @@ class EventChannel { EventSinkImplementation(EventSinkImplementation const&) = delete; EventSinkImplementation& operator=(EventSinkImplementation const&) = delete; - private: + private: const BinaryMessenger* messenger_; const std::string name_; const MethodCodec* codec_; - protected: + protected: void SuccessInternal(T* event = nullptr) override { auto result = codec_->EncodeSuccessEnvelope(event); uint8_t* buffer = new uint8_t[result->size()]; From 4f5e0513461869cb3d5a075360c3716a046071cf Mon Sep 17 00:00:00 2001 From: Kurun Date: Mon, 9 Mar 2020 21:57:47 +0900 Subject: [PATCH 10/46] Modified unit test scenario. --- .../client_wrapper/event_channel_unittests.cc | 29 ++++++------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc index 7ee9443f8ccdb..84ab58ba1dc0f 100644 --- a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc +++ b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc @@ -66,8 +66,6 @@ TEST(EventChannelTest, Registration) { "Error Message", nullptr); event_sink->EndOfStream(); - event_sink->Success(&message); - on_listen_called = true; }; @@ -82,24 +80,15 @@ TEST(EventChannelTest, Registration) { EXPECT_NE(messenger.last_message_handler(), nullptr); // Send a test message to trigger the handler test assertions. - { - MethodCall call("listen", nullptr); - auto message = codec.EncodeMethodCall(call); - messenger.last_message_handler()( - message->data(), message->size(), - [](const uint8_t* reply, const size_t reply_size) {}); - EXPECT_EQ(on_listen_called, true); - } - - // Send a test message to trigger the handler test assertions. - { - MethodCall call("cancel", nullptr); - auto message = codec.EncodeMethodCall(call); - messenger.last_message_handler()( - message->data(), message->size(), - [](const uint8_t* reply, const size_t reply_size) {}); - EXPECT_EQ(on_cancel_called, true); - } + MethodCall call("listen", nullptr); + auto message = codec.EncodeMethodCall(call); + messenger.last_message_handler()( + message->data(), message->size(), + [](const uint8_t* reply, const size_t reply_size) {}); + EXPECT_EQ(on_listen_called, true); + + // FIXME: add onCancel test scenario + //EXPECT_EQ(on_cancel_called, true); } // Tests that SetStreamHandler with a null handler unregisters the handler. From b821d515afdf587b85005dc1e4d8945874d9b881 Mon Sep 17 00:00:00 2001 From: Kurun Date: Mon, 9 Mar 2020 22:13:35 +0900 Subject: [PATCH 11/46] Fix code format --- .../include/flutter/event_channel.h | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h index 7a579b0682a3a..852f94ed55df7 100644 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h @@ -18,18 +18,19 @@ static constexpr char kOnCancelMethod[] = "cancel"; namespace flutter { -// A named channel for communicating with the Flutter application using asynchronous event streams. -// Incoming requests for event stream setup are decoded from binary on receipt, and C++ -// responses and events are encoded into binary before being transmitted back to Flutter. The -// MethodCodec used must be compatible with the one used by the Flutter application. This can be -// achieved by creating an -// EventChannel -// counterpart of this channel on the Dart side. The C++ type of stream configuration arguments, -// events, and error details is Object, but only values supported by the specified -// MethodCodec can be used. +// A named channel for communicating with the Flutter application using +// asynchronous event streams. Incoming requests for event stream setup are +// decoded from binary on receipt, and C++ responses and events are encoded into +// binary before being transmitted back to Flutter. The MethodCodec used must be +// compatible with the one used by the Flutter application. This can be achieved +// by creating an EventChannel +// ("https://docs.flutter.io/flutter/services/EventChannel-class.html") +// counterpart of this channel on the Dart side. The C++ type of stream +// configuration arguments, events, and error details is Object, but only values +// supported by the specified MethodCodec can be used. // -// The logical identity of the channel is given by its name. Identically named channels will -// interfere with each other's communication. +// The logical identity of the channel is given by its name. Identically named +// channels will interfere with each other's communication. template class EventChannel { public: @@ -49,7 +50,8 @@ class EventChannel { // If no handler has been registered, any incoming stream setup requests will be handled // silently by providing an empty stream. void SetStreamHandler(const StreamHandler& handler) const { - //void SetStreamHandler(std::optional> const& handler) const { + // TODO: The following is required when nullptr + // can be passed as an argument. //if (!handler) { /* <= available for more than C++17 */ // messenger_->SetMessageHandler(name_, nullptr); // return; @@ -75,8 +77,8 @@ class EventChannel { const std::string& method = method_call->method_name(); if (method.compare(kOnListenMethod) == 0) { if (current_sink) { - std::cerr << "Failed to cancel existing stream: " - << channel_name << std::endl; + std::cerr << "Failed to cancel existing stream: " << channel_name + << std::endl; handler.onCancel(method_call->arguments()); delete current_sink; current_sink = nullptr; @@ -84,8 +86,8 @@ class EventChannel { current_sink = new EventSinkImplementation(messenger, channel_name, codec); - handler.onListen(method_call->arguments(), - static_cast*>(current_sink)); + handler.onListen(method_call->arguments(), + static_cast*>(current_sink)); { auto result = codec->EncodeSuccessEnvelope(); From 7c5e963addb9c9a4c86b14a9fd657592b53ce341 Mon Sep 17 00:00:00 2001 From: Kurun Date: Mon, 9 Mar 2020 23:23:28 +0900 Subject: [PATCH 12/46] Fix code format --- .../client_wrapper/event_channel_unittests.cc | 12 ++++++------ .../include/flutter/event_channel.h | 16 ++++++++-------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc index 84ab58ba1dc0f..ecfcf8a9f99fb 100644 --- a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc +++ b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc @@ -59,7 +59,7 @@ TEST(EventChannelTest, Registration) { event_sink->Success(); auto message = flutter::EncodableValue(flutter::EncodableMap{ {flutter::EncodableValue("message"), - flutter::EncodableValue("Test from Event Channel")} + flutter::EncodableValue("Test from Event Channel")} }); event_sink->Success(&message); event_sink->Error("Event Channel Error Code", @@ -74,7 +74,7 @@ TEST(EventChannelTest, Registration) { on_cancel_called = true; }; channel.SetStreamHandler( - flutter::StreamHandler(onListen, onCancel)); + flutter::StreamHandler(onListen, onCancel)); EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); EXPECT_NE(messenger.last_message_handler(), nullptr); @@ -88,7 +88,7 @@ TEST(EventChannelTest, Registration) { EXPECT_EQ(on_listen_called, true); // FIXME: add onCancel test scenario - //EXPECT_EQ(on_cancel_called, true); + // EXPECT_EQ(on_cancel_called, true); } // Tests that SetStreamHandler with a null handler unregisters the handler. @@ -106,9 +106,9 @@ TEST(EventChannelTest, Unregistration) { EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); EXPECT_NE(messenger.last_message_handler(), nullptr); - //channel.SetStreamHandler(std::nullopt); - //EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); - //EXPECT_EQ(messenger.last_message_handler(), nullptr); + // channel.SetStreamHandler(std::nullopt); + // EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); + // EXPECT_EQ(messenger.last_message_handler(), nullptr); } } // namespace flutter diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h index 852f94ed55df7..dd7f7312a1278 100644 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h @@ -10,15 +10,15 @@ #include "binary_messenger.h" #include "engine_method_result.h" -#include "event_stream_handler.h" #include "event_sink.h" +#include "event_stream_handler.h" static constexpr char kOnListenMethod[] = "listen"; static constexpr char kOnCancelMethod[] = "cancel"; namespace flutter { -// A named channel for communicating with the Flutter application using +// A named channel for communicating with the Flutter application using // asynchronous event streams. Incoming requests for event stream setup are // decoded from binary on receipt, and C++ responses and events are encoded into // binary before being transmitted back to Flutter. The MethodCodec used must be @@ -47,15 +47,15 @@ class EventChannel { EventChannel& operator=(EventChannel const&) = delete; // Registers a stream handler on this channel. - // If no handler has been registered, any incoming stream setup requests will be handled - // silently by providing an empty stream. + // If no handler has been registered, any incoming stream setup requests will + // be handled silently by providing an empty stream. void SetStreamHandler(const StreamHandler& handler) const { // TODO: The following is required when nullptr // can be passed as an argument. - //if (!handler) { /* <= available for more than C++17 */ - // messenger_->SetMessageHandler(name_, nullptr); - // return; - //} + // if (!handler) { /* <= available for more than C++17 */ + // messenger_->SetMessageHandler(name_, nullptr); + // return; + // } const auto* codec = codec_; const std::string channel_name = name_; From 0b9446e3a186fb2518cffb26f80891162b75fd2d Mon Sep 17 00:00:00 2001 From: Kurun Date: Mon, 9 Mar 2020 23:40:49 +0900 Subject: [PATCH 13/46] Fix code format --- .../client_wrapper/event_channel_unittests.cc | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc index ecfcf8a9f99fb..424c8cf5946ee 100644 --- a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc +++ b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc @@ -54,25 +54,25 @@ TEST(EventChannelTest, Registration) { EventChannel channel(&messenger, channel_name, &codec); bool on_listen_called = false; - auto onListen = [&on_listen_called](const flutter::EncodableValue* arguments, - EventSink* event_sink) { + auto onListen = [&on_listen_called]( + const flutter::EncodableValue* arguments, + EventSink* event_sink) { event_sink->Success(); auto message = flutter::EncodableValue(flutter::EncodableMap{ - {flutter::EncodableValue("message"), - flutter::EncodableValue("Test from Event Channel")} + {flutter::EncodableValue("message"), + flutter::EncodableValue("Test from Event Channel")}}); }); event_sink->Success(&message); - event_sink->Error("Event Channel Error Code", - "Error Message", - nullptr); + event_sink->Error("Event Channel Error Code", "Error Message", nullptr); event_sink->EndOfStream(); on_listen_called = true; }; bool on_cancel_called = false; - auto onCancel = [&on_cancel_called](const flutter::EncodableValue* arguments) { - on_cancel_called = true; - }; + auto onCancel = + [&on_cancel_called](const flutter::EncodableValue* arguments) { + on_cancel_called = true; + }; channel.SetStreamHandler( flutter::StreamHandler(onListen, onCancel)); @@ -95,14 +95,14 @@ TEST(EventChannelTest, Registration) { TEST(EventChannelTest, Unregistration) { TestBinaryMessenger messenger; const std::string channel_name("some_channel"); - EventChannel channel(&messenger, channel_name, - &flutter::StandardMethodCodec::GetInstance()); + EventChannel channel( + &messenger, channel_name, &flutter::StandardMethodCodec::GetInstance()); auto onListen = [](const flutter::EncodableValue* arguments, - EventSink* event_sink){}; - auto onCancel = [](const flutter::EncodableValue* arguments){}; + EventSink* event_sink) {}; + auto onCancel = [](const flutter::EncodableValue* arguments) {}; channel.SetStreamHandler( - flutter::StreamHandler(onListen, onCancel)); + flutter::StreamHandler(onListen, onCancel)); EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); EXPECT_NE(messenger.last_message_handler(), nullptr); From 6168c6753314889478147dd9ad04f103fea2306e Mon Sep 17 00:00:00 2001 From: Kurun Date: Mon, 9 Mar 2020 23:49:29 +0900 Subject: [PATCH 14/46] Fix build error. --- .../common/cpp/client_wrapper/event_channel_unittests.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc index 424c8cf5946ee..4711645b5a3c9 100644 --- a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc +++ b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc @@ -61,7 +61,6 @@ TEST(EventChannelTest, Registration) { auto message = flutter::EncodableValue(flutter::EncodableMap{ {flutter::EncodableValue("message"), flutter::EncodableValue("Test from Event Channel")}}); - }); event_sink->Success(&message); event_sink->Error("Event Channel Error Code", "Error Message", nullptr); event_sink->EndOfStream(); From 6917341463bb24d0ce6ad7d843e215ae6fbf9326 Mon Sep 17 00:00:00 2001 From: Kurun Date: Mon, 9 Mar 2020 23:56:47 +0900 Subject: [PATCH 15/46] Fix code format --- .../common/cpp/client_wrapper/event_channel_unittests.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc index 4711645b5a3c9..b5d5000cc5ccd 100644 --- a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc +++ b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc @@ -63,7 +63,7 @@ TEST(EventChannelTest, Registration) { flutter::EncodableValue("Test from Event Channel")}}); event_sink->Success(&message); event_sink->Error("Event Channel Error Code", "Error Message", nullptr); - event_sink->EndOfStream(); + event_sink->EndOfStream(); on_listen_called = true; }; From f870a601f061bc3977cf3e51162e8b20d30d4e92 Mon Sep 17 00:00:00 2001 From: Kurun Date: Tue, 10 Mar 2020 00:03:58 +0900 Subject: [PATCH 16/46] Fix BUILD.gn format --- shell/platform/common/cpp/client_wrapper/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/platform/common/cpp/client_wrapper/BUILD.gn b/shell/platform/common/cpp/client_wrapper/BUILD.gn index 12efefdc1ff43..06a52b9ff59d0 100644 --- a/shell/platform/common/cpp/client_wrapper/BUILD.gn +++ b/shell/platform/common/cpp/client_wrapper/BUILD.gn @@ -48,9 +48,9 @@ executable("client_wrapper_unittests") { sources = [ "basic_message_channel_unittests.cc", "encodable_value_unittests.cc", + "event_channel_unittests.cc", "method_call_unittests.cc", "method_channel_unittests.cc", - "event_channel_unittests.cc", "plugin_registrar_unittests.cc", "standard_message_codec_unittests.cc", "standard_method_codec_unittests.cc", From f4f8634032953e819592ffe9657b0065270b2a9d Mon Sep 17 00:00:00 2001 From: Kurun Date: Tue, 10 Mar 2020 00:19:20 +0900 Subject: [PATCH 17/46] Fix licenses_flutter, core_wrapper_files.gni format --- ci/licenses_golden/licenses_flutter | 8 ++++---- .../common/cpp/client_wrapper/core_wrapper_files.gni | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index 1da7ac9b13ed1..d7c5b2aacb295 100644 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -756,10 +756,14 @@ FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/basic_message_ch FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/byte_stream_wrappers.h FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/encodable_value_unittests.cc FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/engine_method_result.cc +FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/basic_message_channel.h FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/binary_messenger.h FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/encodable_value.h FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/engine_method_result.h +FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h +FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h +FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/json_message_codec.h FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/json_method_codec.h FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/json_type.h @@ -768,9 +772,6 @@ FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/ FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/method_channel.h FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/method_codec.h FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/method_result.h -FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h -FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h -FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/plugin_registrar.h FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/plugin_registry.h FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/standard_message_codec.h @@ -779,7 +780,6 @@ FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/json_message_cod FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/json_method_codec.cc FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/method_call_unittests.cc FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/method_channel_unittests.cc -FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/plugin_registrar.cc FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/plugin_registrar_unittests.cc FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/standard_codec.cc diff --git a/shell/platform/common/cpp/client_wrapper/core_wrapper_files.gni b/shell/platform/common/cpp/client_wrapper/core_wrapper_files.gni index f57e1192c4d4b..e3740deec0fe6 100644 --- a/shell/platform/common/cpp/client_wrapper/core_wrapper_files.gni +++ b/shell/platform/common/cpp/client_wrapper/core_wrapper_files.gni @@ -8,6 +8,9 @@ core_cpp_client_wrapper_includes = "include/flutter/binary_messenger.h", "include/flutter/encodable_value.h", "include/flutter/engine_method_result.h", + "include/flutter/event_channel.h", + "include/flutter/event_stream_handler.h", + "include/flutter/event_sink.h", "include/flutter/json_message_codec.h", "include/flutter/json_method_codec.h", "include/flutter/json_type.h", @@ -16,9 +19,6 @@ core_cpp_client_wrapper_includes = "include/flutter/method_channel.h", "include/flutter/method_codec.h", "include/flutter/method_result.h", - "include/flutter/event_channel.h", - "include/flutter/event_stream_handler.h", - "include/flutter/event_sink.h", "include/flutter/plugin_registrar.h", "include/flutter/plugin_registry.h", "include/flutter/standard_message_codec.h", From 02b3a3e57f4dd34bdcd404f561eff44d30a2501c Mon Sep 17 00:00:00 2001 From: Kurun Date: Tue, 10 Mar 2020 00:27:42 +0900 Subject: [PATCH 18/46] Fix licenses_flutter, core_wrapper_files.gni format --- ci/licenses_golden/licenses_flutter | 2 +- shell/platform/common/cpp/client_wrapper/core_wrapper_files.gni | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index d7c5b2aacb295..a707410fe636b 100644 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -762,8 +762,8 @@ FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/ FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/encodable_value.h FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/engine_method_result.h FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h -FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h +FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/json_message_codec.h FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/json_method_codec.h FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/json_type.h diff --git a/shell/platform/common/cpp/client_wrapper/core_wrapper_files.gni b/shell/platform/common/cpp/client_wrapper/core_wrapper_files.gni index e3740deec0fe6..d48aab7b98c09 100644 --- a/shell/platform/common/cpp/client_wrapper/core_wrapper_files.gni +++ b/shell/platform/common/cpp/client_wrapper/core_wrapper_files.gni @@ -9,8 +9,8 @@ core_cpp_client_wrapper_includes = "include/flutter/encodable_value.h", "include/flutter/engine_method_result.h", "include/flutter/event_channel.h", - "include/flutter/event_stream_handler.h", "include/flutter/event_sink.h", + "include/flutter/event_stream_handler.h", "include/flutter/json_message_codec.h", "include/flutter/json_method_codec.h", "include/flutter/json_type.h", From 59c7146ae4f9b0d52d5bcc066da55f6263bf72a2 Mon Sep 17 00:00:00 2001 From: Kurun Date: Sun, 22 Mar 2020 21:38:48 +0900 Subject: [PATCH 19/46] Modify comments --- .../cpp/client_wrapper/include/flutter/event_sink.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h index 4b6f8d1860137..f503591888aa2 100644 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h @@ -7,9 +7,8 @@ namespace flutter { -// Event callback. Supports dual use: Producers of events to be sent to Flutter -// act as clients of this interface for sending events. Consumers of events sent -// from Flutter implement this interface for handling received events. +// Event callback. Events to be sent to Flutter application +// act as clients of this interface for sending events. template class EventSink { public: @@ -20,8 +19,8 @@ class EventSink { EventSink(EventSink const&) = delete; EventSink& operator=(EventSink const&) = delete; - // Consumes end of stream. Ensuing calls to Success(T) or - // Error(String, String, Object)}, if any, are ignored. + // Consumes end of stream. Ensuing calls to Success() or + // Error(), if any, are ignored. void EndOfStream() { EndOfStreamInternal(); } // Consumes a successful event. From 5222fc0725201dc2bd6d16b81b153fbff66e0d3d Mon Sep 17 00:00:00 2001 From: Kurun Date: Mon, 30 Mar 2020 00:36:21 +0900 Subject: [PATCH 20/46] Fix unit test code --- .../client_wrapper/event_channel_unittests.cc | 78 ++++++++++++------- 1 file changed, 49 insertions(+), 29 deletions(-) mode change 100644 => 100755 shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc diff --git a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc old mode 100644 new mode 100755 index b5d5000cc5ccd..722bfff1b4152 --- a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc +++ b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc @@ -56,58 +56,78 @@ TEST(EventChannelTest, Registration) { bool on_listen_called = false; auto onListen = [&on_listen_called]( const flutter::EncodableValue* arguments, - EventSink* event_sink) { - event_sink->Success(); - auto message = flutter::EncodableValue(flutter::EncodableMap{ - {flutter::EncodableValue("message"), - flutter::EncodableValue("Test from Event Channel")}}); - event_sink->Success(&message); - event_sink->Error("Event Channel Error Code", "Error Message", nullptr); - event_sink->EndOfStream(); + EventSink* events) { on_listen_called = true; }; + auto onCancel = [](const flutter::EncodableValue* arguments) {}; - bool on_cancel_called = false; - auto onCancel = - [&on_cancel_called](const flutter::EncodableValue* arguments) { - on_cancel_called = true; - }; - channel.SetStreamHandler( - flutter::StreamHandler(onListen, onCancel)); - + flutter::StreamHandler handler(onListen, onCancel); + channel.SetStreamHandler(&handler); EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); EXPECT_NE(messenger.last_message_handler(), nullptr); - // Send a test message to trigger the handler test assertions. - MethodCall call("listen", nullptr); + // Send dummy listen message + MethodCall call("listen", nullptr); auto message = codec.EncodeMethodCall(call); messenger.last_message_handler()( message->data(), message->size(), [](const uint8_t* reply, const size_t reply_size) {}); - EXPECT_EQ(on_listen_called, true); - // FIXME: add onCancel test scenario - // EXPECT_EQ(on_cancel_called, true); + // Check results + EXPECT_EQ(on_listen_called, true); } // Tests that SetStreamHandler with a null handler unregisters the handler. TEST(EventChannelTest, Unregistration) { TestBinaryMessenger messenger; const std::string channel_name("some_channel"); - EventChannel channel( - &messenger, channel_name, &flutter::StandardMethodCodec::GetInstance()); + const StandardMethodCodec& codec = StandardMethodCodec::GetInstance(); + EventChannel channel(&messenger, channel_name, &codec); auto onListen = [](const flutter::EncodableValue* arguments, - EventSink* event_sink) {}; + EventSink* events) {}; auto onCancel = [](const flutter::EncodableValue* arguments) {}; - channel.SetStreamHandler( - flutter::StreamHandler(onListen, onCancel)); + + flutter::StreamHandler handler(onListen, onCancel); + channel.SetStreamHandler(&handler); + EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); + EXPECT_NE(messenger.last_message_handler(), nullptr); + + channel.SetStreamHandler(nullptr); + EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); + EXPECT_EQ(messenger.last_message_handler(), nullptr); +} + +// Test of OnCancel callback +TEST(EventChannelTest, Cancel) { + TestBinaryMessenger messenger; + const std::string channel_name("some_channel"); + const StandardMethodCodec& codec = StandardMethodCodec::GetInstance(); + EventChannel channel(&messenger, channel_name, &codec); + + auto onListen = [](const flutter::EncodableValue* arguments, + EventSink* events) {}; + + bool on_cancel_called = false; + auto onCancel = + [&on_cancel_called](const flutter::EncodableValue* arguments) { + on_cancel_called = true; + }; + + flutter::StreamHandler handler(onListen, onCancel); + channel.SetStreamHandler(&handler); EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); EXPECT_NE(messenger.last_message_handler(), nullptr); - // channel.SetStreamHandler(std::nullopt); - // EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); - // EXPECT_EQ(messenger.last_message_handler(), nullptr); + // Send dummy cancel message + MethodCall call("cancel", nullptr); + auto message = codec.EncodeMethodCall(call); + messenger.last_message_handler()( + message->data(), message->size(), + [](const uint8_t* reply, const size_t reply_size) {}); + + // Check results + EXPECT_EQ(on_cancel_called, true); } } // namespace flutter From 50576c61e1da76d02ffefca84b16325383125c69 Mon Sep 17 00:00:00 2001 From: Kurun Date: Mon, 30 Mar 2020 00:49:09 +0900 Subject: [PATCH 21/46] Modify comment and change from class to structure --- .../include/flutter/event_stream_handler.h | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) mode change 100644 => 100755 shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h old mode 100644 new mode 100755 index b2a14a543b319..d503846f078de --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h @@ -11,29 +11,25 @@ namespace flutter { // Handler of stream setup and tear-down requests. // Implementations must be prepared to accept sequences of alternating calls to -// onListen(T, EventSink) and onCancel(Object). Implementations should ideally -// consume no resources when the last such call is not onListen(). In typical -// situations, this means that the implementation should register itself with +// onListen() and onCancel(). Implementations should ideally consume no resources +// when the last such call is not onListen(). In typical situations, +// this means that the implementation should register itself with // platform-specific event sources onListen() and deregister again onCancel(). template -class StreamHandler { - public: +struct StreamHandler { // Handles a request to set up an event stream. - // @param arguments stream configuration arguments, possibly null. - // @param event_sink an EventSink for emitting events to the Flutter receiver. + // |arguments| is stream configuration arguments and + // |events| is an EventSink for emitting events to the Flutter receiver. using OnListen = - std::function* event_sink)>; + std::function* events)>; // Handles a request to tear down the most recently created event stream. - // @param arguments stream configuration arguments, possibly null. + // |arguments| is stream configuration arguments. using OnCancel = std::function; OnListen onListen; OnCancel onCancel; - // Registers a stream handler on this channel. - // If no handler has been registered, any incoming stream setup requests will - // be handled silently by providing an empty stream. StreamHandler(OnListen const& onListen, OnCancel const& onCancel) : onListen(onListen), onCancel(onCancel) {} }; From 28cc16e44bf6fbf98d364dbf1bd3ec2ffa2fdb85 Mon Sep 17 00:00:00 2001 From: Kurun Date: Tue, 31 Mar 2020 22:49:13 +0900 Subject: [PATCH 22/46] Fix for code review result --- .../client_wrapper/event_channel_unittests.cc | 6 +- .../include/flutter/event_channel.h | 107 +++++------------- .../include/flutter/event_stream_handler.h | 2 +- 3 files changed, 35 insertions(+), 80 deletions(-) mode change 100644 => 100755 shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h diff --git a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc index 722bfff1b4152..ae9c2ff4d0453 100755 --- a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc +++ b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc @@ -56,7 +56,7 @@ TEST(EventChannelTest, Registration) { bool on_listen_called = false; auto onListen = [&on_listen_called]( const flutter::EncodableValue* arguments, - EventSink* events) { + std::unique_ptr>&& events) { on_listen_called = true; }; auto onCancel = [](const flutter::EncodableValue* arguments) {}; @@ -85,7 +85,7 @@ TEST(EventChannelTest, Unregistration) { EventChannel channel(&messenger, channel_name, &codec); auto onListen = [](const flutter::EncodableValue* arguments, - EventSink* events) {}; + std::unique_ptr>&& events) {}; auto onCancel = [](const flutter::EncodableValue* arguments) {}; flutter::StreamHandler handler(onListen, onCancel); @@ -106,7 +106,7 @@ TEST(EventChannelTest, Cancel) { EventChannel channel(&messenger, channel_name, &codec); auto onListen = [](const flutter::EncodableValue* arguments, - EventSink* events) {}; + std::unique_ptr>&& events) {}; bool on_cancel_called = false; auto onCancel = diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h old mode 100644 new mode 100755 index dd7f7312a1278..8e0c2dbe0535c --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h @@ -7,15 +7,13 @@ #include #include +#include #include "binary_messenger.h" #include "engine_method_result.h" #include "event_sink.h" #include "event_stream_handler.h" -static constexpr char kOnListenMethod[] = "listen"; -static constexpr char kOnCancelMethod[] = "cancel"; - namespace flutter { // A named channel for communicating with the Flutter application using @@ -25,12 +23,9 @@ namespace flutter { // compatible with the one used by the Flutter application. This can be achieved // by creating an EventChannel // ("https://docs.flutter.io/flutter/services/EventChannel-class.html") -// counterpart of this channel on the Dart side. The C++ type of stream -// configuration arguments, events, and error details is Object, but only values -// supported by the specified MethodCodec can be used. -// -// The logical identity of the channel is given by its name. Identically named -// channels will interfere with each other's communication. +// counterpart of this channel on the Dart side. +// The C++ type of stream configuration arguments, events, and error details are +// Template, but only values supported by the specified MethodCodec can be used. template class EventChannel { public: @@ -49,90 +44,57 @@ class EventChannel { // Registers a stream handler on this channel. // If no handler has been registered, any incoming stream setup requests will // be handled silently by providing an empty stream. - void SetStreamHandler(const StreamHandler& handler) const { - // TODO: The following is required when nullptr - // can be passed as an argument. - // if (!handler) { /* <= available for more than C++17 */ - // messenger_->SetMessageHandler(name_, nullptr); - // return; - // } - - const auto* codec = codec_; + void SetStreamHandler(StreamHandler* handler) const { + if (!handler) { + messenger_->SetMessageHandler(name_, nullptr); + return; + } + + const MethodCodec* codec = codec_; const std::string channel_name = name_; - const auto* messenger = messenger_; - EventSinkImplementation* current_sink = current_sink_; + const BinaryMessenger* messenger = messenger_; BinaryMessageHandler binary_handler = [handler, codec, channel_name, - current_sink, messenger]( + messenger]( const uint8_t* message, const size_t message_size, BinaryReply reply) mutable { + constexpr char kOnListenMethod[] = "listen"; + constexpr char kOnCancelMethod[] = "cancel"; + std::unique_ptr> method_call = codec->DecodeMethodCall(message, message_size); if (!method_call) { - std::cerr << "Unable to construct method call from message on channel " + std::cerr << "Unable to construct method call from message on channel: " << channel_name << std::endl; return; } const std::string& method = method_call->method_name(); if (method.compare(kOnListenMethod) == 0) { - if (current_sink) { - std::cerr << "Failed to cancel existing stream: " << channel_name - << std::endl; - handler.onCancel(method_call->arguments()); - delete current_sink; - current_sink = nullptr; - } - - current_sink = - new EventSinkImplementation(messenger, channel_name, codec); - handler.onListen(method_call->arguments(), - static_cast*>(current_sink)); - - { - auto result = codec->EncodeSuccessEnvelope(); - uint8_t* buffer = new uint8_t[result->size()]; - std::copy(result->begin(), result->end(), buffer); - reply(buffer, result->size()); - delete[] buffer; - } + auto sink = + std::make_unique(messenger, channel_name, codec); + handler->onListen(method_call->arguments(), std::move(sink)); + + auto result = codec->EncodeSuccessEnvelope(); + reply(result->data(), result->size()); } else if (method.compare(kOnCancelMethod) == 0) { - if (current_sink) { - handler.onCancel(method_call->arguments()); - - auto result = codec->EncodeSuccessEnvelope(); - uint8_t* buffer = new uint8_t[result->size()]; - std::copy(result->begin(), result->end(), buffer); - reply(buffer, result->size()); - delete current_sink; - current_sink = nullptr; - } else { - auto result = codec->EncodeErrorEnvelope( - "error", "No active stream to cancel", nullptr); - uint8_t* buffer = new uint8_t[result->size()]; - std::copy(result->begin(), result->end(), buffer); - reply(buffer, result->size()); - delete[] buffer; - } + handler->onCancel(method_call->arguments()); + + auto result = codec->EncodeSuccessEnvelope(); + reply(result->data(), result->size()); } else { std::cerr << "Unknown event channel method call from message on channel: " << channel_name << std::endl; reply(nullptr, 0); - if (current_sink) { - delete current_sink; - current_sink = nullptr; - } } }; messenger_->SetMessageHandler(name_, std::move(binary_handler)); } private: - class EventSinkImplementation : public EventSink { + class EventSinkImplementation : public EventSink { public: - // Creates an instance that EventSink send event on the channel - // named |name|, encoded with |codec| and dispatched via |messenger|. EventSinkImplementation(const BinaryMessenger* messenger, const std::string& name, const MethodCodec* codec) @@ -151,10 +113,7 @@ class EventChannel { protected: void SuccessInternal(T* event = nullptr) override { auto result = codec_->EncodeSuccessEnvelope(event); - uint8_t* buffer = new uint8_t[result->size()]; - std::copy(result->begin(), result->end(), buffer); - messenger_->Send(name_, buffer, result->size()); - delete[] buffer; + messenger_->Send(name_, result->data(), result->size()); } void ErrorInternal(const std::string& error_code, @@ -162,19 +121,15 @@ class EventChannel { T* error_details) override { auto result = codec_->EncodeErrorEnvelope(error_code, error_message, error_details); - uint8_t* buffer = new uint8_t[result->size()]; - std::copy(result->begin(), result->end(), buffer); - messenger_->Send(name_, buffer, result->size()); - delete[] buffer; + messenger_->Send(name_, result->data(), result->size()); } void EndOfStreamInternal() override { messenger_->Send(name_, nullptr, 0); } }; BinaryMessenger* messenger_; - std::string name_; + const std::string name_; const MethodCodec* codec_; - EventSinkImplementation* current_sink_ = nullptr; }; } // namespace flutter diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h index d503846f078de..d70ff735a57be 100755 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h @@ -21,7 +21,7 @@ struct StreamHandler { // |arguments| is stream configuration arguments and // |events| is an EventSink for emitting events to the Flutter receiver. using OnListen = - std::function* events)>; + std::function>&& events)>; // Handles a request to tear down the most recently created event stream. // |arguments| is stream configuration arguments. From 0835785a7db8d1e2126f904a842351e3f41ae265 Mon Sep 17 00:00:00 2001 From: Kurun Date: Tue, 31 Mar 2020 23:34:02 +0900 Subject: [PATCH 23/46] Fix code format --- .../cpp/client_wrapper/include/flutter/event_channel.h | 6 +++--- .../client_wrapper/include/flutter/event_stream_handler.h | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h index 8e0c2dbe0535c..58505d1560a2f 100755 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h @@ -6,8 +6,8 @@ #define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_CHANNEL_H_ #include -#include #include +#include #include "binary_messenger.h" #include "engine_method_result.h" @@ -71,8 +71,8 @@ class EventChannel { const std::string& method = method_call->method_name(); if (method.compare(kOnListenMethod) == 0) { - auto sink = - std::make_unique(messenger, channel_name, codec); + auto sink = std::make_unique( + messenger, channel_name, codec); handler->onListen(method_call->arguments(), std::move(sink)); auto result = codec->EncodeSuccessEnvelope(); diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h index d70ff735a57be..1c0278476c44a 100755 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h @@ -11,8 +11,8 @@ namespace flutter { // Handler of stream setup and tear-down requests. // Implementations must be prepared to accept sequences of alternating calls to -// onListen() and onCancel(). Implementations should ideally consume no resources -// when the last such call is not onListen(). In typical situations, +// onListen() and onCancel(). Implementations should ideally consume no +// resources when the last such call is not onListen(). In typical situations, // this means that the implementation should register itself with // platform-specific event sources onListen() and deregister again onCancel(). template @@ -20,8 +20,8 @@ struct StreamHandler { // Handles a request to set up an event stream. // |arguments| is stream configuration arguments and // |events| is an EventSink for emitting events to the Flutter receiver. - using OnListen = - std::function>&& events)>; + using OnListen = std::function>&& events)>; // Handles a request to tear down the most recently created event stream. // |arguments| is stream configuration arguments. From cc3f9132a103845b8d2a44fb25956f4634047310 Mon Sep 17 00:00:00 2001 From: Kurun Date: Tue, 31 Mar 2020 23:45:40 +0900 Subject: [PATCH 24/46] Fix code format --- .../client_wrapper/event_channel_unittests.cc | 19 ++++++++++++------- .../include/flutter/event_channel.h | 2 +- .../include/flutter/event_stream_handler.h | 2 +- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc index ae9c2ff4d0453..7fd9458537912 100755 --- a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc +++ b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc @@ -54,11 +54,10 @@ TEST(EventChannelTest, Registration) { EventChannel channel(&messenger, channel_name, &codec); bool on_listen_called = false; - auto onListen = [&on_listen_called]( - const flutter::EncodableValue* arguments, - std::unique_ptr>&& events) { - on_listen_called = true; - }; + auto onListen = [&on_listen_called] + (const flutter::EncodableValue* arguments, + std::unique_ptr>&& + events) { on_listen_called = true; }; auto onCancel = [](const flutter::EncodableValue* arguments) {}; flutter::StreamHandler handler(onListen, onCancel); @@ -84,8 +83,10 @@ TEST(EventChannelTest, Unregistration) { const StandardMethodCodec& codec = StandardMethodCodec::GetInstance(); EventChannel channel(&messenger, channel_name, &codec); - auto onListen = [](const flutter::EncodableValue* arguments, - std::unique_ptr>&& events) {}; + auto onListen = + [](const flutter::EncodableValue* arguments, + std::unique_ptr>&& + events) {}; auto onCancel = [](const flutter::EncodableValue* arguments) {}; flutter::StreamHandler handler(onListen, onCancel); @@ -107,6 +108,10 @@ TEST(EventChannelTest, Cancel) { auto onListen = [](const flutter::EncodableValue* arguments, std::unique_ptr>&& events) {}; + auto onListen = + [](const flutter::EncodableValue* arguments, + std::unique_ptr>&& + events) {}; bool on_cancel_called = false; auto onCancel = diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h index 58505d1560a2f..16614923be2c7 100755 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h @@ -93,7 +93,7 @@ class EventChannel { } private: - class EventSinkImplementation : public EventSink { + class EventSinkImplementation : public EventSink { public: EventSinkImplementation(const BinaryMessenger* messenger, const std::string& name, diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h index 1c0278476c44a..c744f0ffb7cea 100755 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h @@ -20,7 +20,7 @@ struct StreamHandler { // Handles a request to set up an event stream. // |arguments| is stream configuration arguments and // |events| is an EventSink for emitting events to the Flutter receiver. - using OnListen = std::function>&& events)>; // Handles a request to tear down the most recently created event stream. From 2210369fc7066e95fd5f80069439b0f9e19540b8 Mon Sep 17 00:00:00 2001 From: Kurun Date: Tue, 31 Mar 2020 23:51:56 +0900 Subject: [PATCH 25/46] Fix build error --- .../common/cpp/client_wrapper/event_channel_unittests.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc index 7fd9458537912..e61285c402a38 100755 --- a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc +++ b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc @@ -106,8 +106,6 @@ TEST(EventChannelTest, Cancel) { const StandardMethodCodec& codec = StandardMethodCodec::GetInstance(); EventChannel channel(&messenger, channel_name, &codec); - auto onListen = [](const flutter::EncodableValue* arguments, - std::unique_ptr>&& events) {}; auto onListen = [](const flutter::EncodableValue* arguments, std::unique_ptr>&& From 6c942dcdf3d6212872955897d78bcd072e1621e0 Mon Sep 17 00:00:00 2001 From: Kurun Date: Tue, 31 Mar 2020 23:57:29 +0900 Subject: [PATCH 26/46] Fix code format --- .../common/cpp/client_wrapper/event_channel_unittests.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc index e61285c402a38..6307eee6024e7 100755 --- a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc +++ b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc @@ -54,8 +54,9 @@ TEST(EventChannelTest, Registration) { EventChannel channel(&messenger, channel_name, &codec); bool on_listen_called = false; - auto onListen = [&on_listen_called] - (const flutter::EncodableValue* arguments, + auto onListen = + [&on_listen_called]( + const flutter::EncodableValue* arguments, std::unique_ptr>&& events) { on_listen_called = true; }; auto onCancel = [](const flutter::EncodableValue* arguments) {}; From fbaaee88bfae26c9bdfcfd3b21bc1edced9f78b4 Mon Sep 17 00:00:00 2001 From: Kurun Date: Wed, 1 Apr 2020 00:02:08 +0900 Subject: [PATCH 27/46] Fix code format --- .../common/cpp/client_wrapper/event_channel_unittests.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc index 6307eee6024e7..44c848cf0fb55 100755 --- a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc +++ b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc @@ -54,7 +54,7 @@ TEST(EventChannelTest, Registration) { EventChannel channel(&messenger, channel_name, &codec); bool on_listen_called = false; - auto onListen = + auto onListen = [&on_listen_called]( const flutter::EncodableValue* arguments, std::unique_ptr>&& From 6c3fb5fee3497708e00cd8dd67ee6408a81b6970 Mon Sep 17 00:00:00 2001 From: Kurun Date: Sat, 11 Apr 2020 14:34:07 +0900 Subject: [PATCH 28/46] Fixed the points that were pointed out. --- .../client_wrapper/event_channel_unittests.cc | 48 ++++++++----------- 1 file changed, 20 insertions(+), 28 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc index 44c848cf0fb55..2c72ff5f1fea0 100755 --- a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc +++ b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc @@ -54,26 +54,24 @@ TEST(EventChannelTest, Registration) { EventChannel channel(&messenger, channel_name, &codec); bool on_listen_called = false; - auto onListen = - [&on_listen_called]( + flutter::StreamHandler handler( + [&on_listen_called]( const flutter::EncodableValue* arguments, std::unique_ptr>&& - events) { on_listen_called = true; }; - auto onCancel = [](const flutter::EncodableValue* arguments) {}; - - flutter::StreamHandler handler(onListen, onCancel); + events) { on_listen_called = true; }, + [](const flutter::EncodableValue* arguments) {}); channel.SetStreamHandler(&handler); EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); EXPECT_NE(messenger.last_message_handler(), nullptr); - // Send dummy listen message + // Send dummy listen message. MethodCall call("listen", nullptr); auto message = codec.EncodeMethodCall(call); messenger.last_message_handler()( message->data(), message->size(), [](const uint8_t* reply, const size_t reply_size) {}); - // Check results + // Check results. EXPECT_EQ(on_listen_called, true); } @@ -84,13 +82,11 @@ TEST(EventChannelTest, Unregistration) { const StandardMethodCodec& codec = StandardMethodCodec::GetInstance(); EventChannel channel(&messenger, channel_name, &codec); - auto onListen = - [](const flutter::EncodableValue* arguments, + flutter::StreamHandler handler( + [](const flutter::EncodableValue* arguments, std::unique_ptr>&& - events) {}; - auto onCancel = [](const flutter::EncodableValue* arguments) {}; - - flutter::StreamHandler handler(onListen, onCancel); + events) {}, + [](const flutter::EncodableValue* arguments) {}); channel.SetStreamHandler(&handler); EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); EXPECT_NE(messenger.last_message_handler(), nullptr); @@ -100,37 +96,33 @@ TEST(EventChannelTest, Unregistration) { EXPECT_EQ(messenger.last_message_handler(), nullptr); } -// Test of OnCancel callback +// Test of OnCancel callback. TEST(EventChannelTest, Cancel) { TestBinaryMessenger messenger; const std::string channel_name("some_channel"); const StandardMethodCodec& codec = StandardMethodCodec::GetInstance(); EventChannel channel(&messenger, channel_name, &codec); - auto onListen = - [](const flutter::EncodableValue* arguments, - std::unique_ptr>&& - events) {}; - bool on_cancel_called = false; - auto onCancel = - [&on_cancel_called](const flutter::EncodableValue* arguments) { - on_cancel_called = true; - }; - - flutter::StreamHandler handler(onListen, onCancel); + flutter::StreamHandler handler( + [](const flutter::EncodableValue* arguments, + std::unique_ptr>&& + events) {}, + [&on_cancel_called](const flutter::EncodableValue* arguments) { + on_cancel_called = true; + }); channel.SetStreamHandler(&handler); EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); EXPECT_NE(messenger.last_message_handler(), nullptr); - // Send dummy cancel message + // Send dummy cancel message. MethodCall call("cancel", nullptr); auto message = codec.EncodeMethodCall(call); messenger.last_message_handler()( message->data(), message->size(), [](const uint8_t* reply, const size_t reply_size) {}); - // Check results + // Check results. EXPECT_EQ(on_cancel_called, true); } From 7f0e247ad21ebf6077d0365f68127fee76bef714 Mon Sep 17 00:00:00 2001 From: Kurun Date: Sat, 11 Apr 2020 14:42:31 +0900 Subject: [PATCH 29/46] Fixed the points that were pointed out. --- .../client_wrapper/include/flutter/event_sink.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h index f503591888aa2..6223ad7b6a572 100644 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h @@ -19,10 +19,6 @@ class EventSink { EventSink(EventSink const&) = delete; EventSink& operator=(EventSink const&) = delete; - // Consumes end of stream. Ensuing calls to Success() or - // Error(), if any, are ignored. - void EndOfStream() { EndOfStreamInternal(); } - // Consumes a successful event. void Success(T* event = nullptr) { SuccessInternal(event); } @@ -33,10 +29,11 @@ class EventSink { ErrorInternal(error_code, error_message, error_details); } - protected: - // Implementation of the public interface, to be provided by subclasses. - virtual void EndOfStreamInternal() = 0; + // Consumes end of stream. Ensuing calls to Success() or + // Error(), if any, are ignored. + void EndOfStream() { EndOfStreamInternal(); } + protected: // Implementation of the public interface, to be provided by subclasses. virtual void SuccessInternal(T* event = nullptr) = 0; @@ -44,6 +41,9 @@ class EventSink { virtual void ErrorInternal(const std::string& error_code, const std::string& error_message, T* error_details) = 0; + + // Implementation of the public interface, to be provided by subclasses. + virtual void EndOfStreamInternal() = 0; }; } // namespace flutter From a4e0caf10497a17d7212b7700e9ade401d53ba12 Mon Sep 17 00:00:00 2001 From: Kurun Date: Sat, 11 Apr 2020 14:52:34 +0900 Subject: [PATCH 30/46] Fix code format --- .../client_wrapper/event_channel_unittests.cc | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc index 2c72ff5f1fea0..f6ec6a0eb4483 100755 --- a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc +++ b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc @@ -55,11 +55,11 @@ TEST(EventChannelTest, Registration) { bool on_listen_called = false; flutter::StreamHandler handler( - [&on_listen_called]( + [&on_listen_called]( const flutter::EncodableValue* arguments, std::unique_ptr>&& events) { on_listen_called = true; }, - [](const flutter::EncodableValue* arguments) {}); + [](const flutter::EncodableValue* arguments) {}); channel.SetStreamHandler(&handler); EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); EXPECT_NE(messenger.last_message_handler(), nullptr); @@ -83,10 +83,10 @@ TEST(EventChannelTest, Unregistration) { EventChannel channel(&messenger, channel_name, &codec); flutter::StreamHandler handler( - [](const flutter::EncodableValue* arguments, + [](const flutter::EncodableValue* arguments, std::unique_ptr>&& events) {}, - [](const flutter::EncodableValue* arguments) {}); + [](const flutter::EncodableValue* arguments) {}); channel.SetStreamHandler(&handler); EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); EXPECT_NE(messenger.last_message_handler(), nullptr); @@ -105,12 +105,12 @@ TEST(EventChannelTest, Cancel) { bool on_cancel_called = false; flutter::StreamHandler handler( - [](const flutter::EncodableValue* arguments, + [](const flutter::EncodableValue* arguments, std::unique_ptr>&& events) {}, - [&on_cancel_called](const flutter::EncodableValue* arguments) { - on_cancel_called = true; - }); + [&on_cancel_called](const flutter::EncodableValue* arguments) { + on_cancel_called = true; + }); channel.SetStreamHandler(&handler); EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); EXPECT_NE(messenger.last_message_handler(), nullptr); From 05ce3b5332a4bfe67cb659480817498f1a14fa55 Mon Sep 17 00:00:00 2001 From: Kurun Date: Sat, 11 Apr 2020 18:09:00 +0900 Subject: [PATCH 31/46] - Add error handling of StreamHandler and fix unit test code. - Fix the points that were pointed out. --- .../client_wrapper/event_channel_unittests.cc | 60 +++++++++++++++++-- .../include/flutter/event_channel.h | 33 ++++++---- 2 files changed, 77 insertions(+), 16 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc index f6ec6a0eb4483..a055e3ff041f4 100755 --- a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc +++ b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc @@ -96,18 +96,20 @@ TEST(EventChannelTest, Unregistration) { EXPECT_EQ(messenger.last_message_handler(), nullptr); } -// Test of OnCancel callback. +// Test that OnCancel callback sequence. TEST(EventChannelTest, Cancel) { TestBinaryMessenger messenger; const std::string channel_name("some_channel"); const StandardMethodCodec& codec = StandardMethodCodec::GetInstance(); EventChannel channel(&messenger, channel_name, &codec); + bool on_listen_called = false; bool on_cancel_called = false; flutter::StreamHandler handler( - [](const flutter::EncodableValue* arguments, - std::unique_ptr>&& - events) {}, + [&on_listen_called]( + const flutter::EncodableValue* arguments, + std::unique_ptr>&& + events) { on_listen_called = true; }, [&on_cancel_called](const flutter::EncodableValue* arguments) { on_cancel_called = true; }); @@ -115,12 +117,60 @@ TEST(EventChannelTest, Cancel) { EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); EXPECT_NE(messenger.last_message_handler(), nullptr); + // Send dummy listen message. + MethodCall call_listen("listen", nullptr); + auto message = codec.EncodeMethodCall(call_listen); + messenger.last_message_handler()( + message->data(), message->size(), + [](const uint8_t* reply, const size_t reply_size) {}); + EXPECT_EQ(on_listen_called, true); + // Send dummy cancel message. - MethodCall call("cancel", nullptr); + MethodCall call_cancel("cancel", nullptr); + message = codec.EncodeMethodCall(call_cancel); + messenger.last_message_handler()( + message->data(), message->size(), + [](const uint8_t* reply, const size_t reply_size) {}); + + // Check results. + EXPECT_EQ(on_cancel_called, true); +} + +// Test that consecutive call of OnListen. +TEST(EventChannelTest, ConsecutiveListen) { + TestBinaryMessenger messenger; + const std::string channel_name("some_channel"); + const StandardMethodCodec& codec = StandardMethodCodec::GetInstance(); + EventChannel channel(&messenger, channel_name, &codec); + + bool on_listen_called = false; + bool on_cancel_called = false; + flutter::StreamHandler handler( + [&on_listen_called]( + const flutter::EncodableValue* arguments, + std::unique_ptr>&& + events) { on_listen_called = true; }, + [&on_cancel_called](const flutter::EncodableValue* arguments) { + on_cancel_called = true; + }); + channel.SetStreamHandler(&handler); + EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); + EXPECT_NE(messenger.last_message_handler(), nullptr); + + // Send dummy listen message. + MethodCall call("listen", nullptr); auto message = codec.EncodeMethodCall(call); messenger.last_message_handler()( message->data(), message->size(), [](const uint8_t* reply, const size_t reply_size) {}); + EXPECT_EQ(on_listen_called, true); + + // Send dummy listen message. + message = codec.EncodeMethodCall(call); + messenger.last_message_handler()( + message->data(), message->size(), + [](const uint8_t* reply, const size_t reply_size) {}); + EXPECT_EQ(on_listen_called, true); // Check results. EXPECT_EQ(on_cancel_called, true); diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h index 16614923be2c7..4fc3f5df23b7d 100755 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h @@ -25,7 +25,7 @@ namespace flutter { // ("https://docs.flutter.io/flutter/services/EventChannel-class.html") // counterpart of this channel on the Dart side. // The C++ type of stream configuration arguments, events, and error details are -// Template, but only values supported by the specified MethodCodec can be used. +// templated, but only values supported by the specified MethodCodec can be used. template class EventChannel { public: @@ -44,24 +44,26 @@ class EventChannel { // Registers a stream handler on this channel. // If no handler has been registered, any incoming stream setup requests will // be handled silently by providing an empty stream. - void SetStreamHandler(StreamHandler* handler) const { + void SetStreamHandler(StreamHandler* handler) { if (!handler) { messenger_->SetMessageHandler(name_, nullptr); + is_listened_ = false; return; } const MethodCodec* codec = codec_; const std::string channel_name = name_; const BinaryMessenger* messenger = messenger_; + bool& is_listend = is_listened_; BinaryMessageHandler binary_handler = [handler, codec, channel_name, - messenger]( + messenger, &is_listend]( const uint8_t* message, const size_t message_size, - BinaryReply reply) mutable { + BinaryReply reply) { constexpr char kOnListenMethod[] = "listen"; constexpr char kOnCancelMethod[] = "cancel"; - std::unique_ptr> method_call = + std::shared_ptr> method_call = codec->DecodeMethodCall(message, message_size); if (!method_call) { std::cerr << "Unable to construct method call from message on channel: " @@ -71,6 +73,10 @@ class EventChannel { const std::string& method = method_call->method_name(); if (method.compare(kOnListenMethod) == 0) { + if (is_listend) { + handler->onCancel(nullptr); + } + is_listend = true; auto sink = std::make_unique( messenger, channel_name, codec); handler->onListen(method_call->arguments(), std::move(sink)); @@ -78,14 +84,18 @@ class EventChannel { auto result = codec->EncodeSuccessEnvelope(); reply(result->data(), result->size()); } else if (method.compare(kOnCancelMethod) == 0) { - handler->onCancel(method_call->arguments()); - - auto result = codec->EncodeSuccessEnvelope(); + std::unique_ptr> result; + if (is_listend) { + handler->onCancel(method_call->arguments()); + result = codec->EncodeSuccessEnvelope(); + is_listend = false; + } else { + result = + codec->EncodeErrorEnvelope("error", + "No active stream to cancel", nullptr); + } reply(result->data(), result->size()); } else { - std::cerr - << "Unknown event channel method call from message on channel: " - << channel_name << std::endl; reply(nullptr, 0); } }; @@ -130,6 +140,7 @@ class EventChannel { BinaryMessenger* messenger_; const std::string name_; const MethodCodec* codec_; + bool is_listened_; }; } // namespace flutter From c830c9ba1fad829432eabee41316c71e79055d6b Mon Sep 17 00:00:00 2001 From: Kurun Date: Sat, 11 Apr 2020 18:19:16 +0900 Subject: [PATCH 32/46] Fix code formatting --- .../cpp/client_wrapper/event_channel_unittests.cc | 12 ++++++------ .../client_wrapper/include/flutter/event_channel.h | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc index a055e3ff041f4..480687212cb08 100755 --- a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc +++ b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc @@ -107,9 +107,9 @@ TEST(EventChannelTest, Cancel) { bool on_cancel_called = false; flutter::StreamHandler handler( [&on_listen_called]( - const flutter::EncodableValue* arguments, - std::unique_ptr>&& - events) { on_listen_called = true; }, + const flutter::EncodableValue* arguments, + std::unique_ptr>&& + events) { on_listen_called = true; }, [&on_cancel_called](const flutter::EncodableValue* arguments) { on_cancel_called = true; }); @@ -147,9 +147,9 @@ TEST(EventChannelTest, ConsecutiveListen) { bool on_cancel_called = false; flutter::StreamHandler handler( [&on_listen_called]( - const flutter::EncodableValue* arguments, - std::unique_ptr>&& - events) { on_listen_called = true; }, + const flutter::EncodableValue* arguments, + std::unique_ptr>&& + events) { on_listen_called = true; }, [&on_cancel_called](const flutter::EncodableValue* arguments) { on_cancel_called = true; }); diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h index 4fc3f5df23b7d..b3e077d9693a7 100755 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h @@ -25,7 +25,8 @@ namespace flutter { // ("https://docs.flutter.io/flutter/services/EventChannel-class.html") // counterpart of this channel on the Dart side. // The C++ type of stream configuration arguments, events, and error details are -// templated, but only values supported by the specified MethodCodec can be used. +// templated, but only values supported by the specified MethodCodec can be +// used. template class EventChannel { public: @@ -90,9 +91,8 @@ class EventChannel { result = codec->EncodeSuccessEnvelope(); is_listend = false; } else { - result = - codec->EncodeErrorEnvelope("error", - "No active stream to cancel", nullptr); + result = codec->EncodeErrorEnvelope( + "error", "No active stream to cancel", nullptr); } reply(result->data(), result->size()); } else { From 376802177427ec22ce51c63eb49d78edee59bf13 Mon Sep 17 00:00:00 2001 From: Kurun <38722979+Kurun-pan@users.noreply.github.com> Date: Fri, 17 Apr 2020 23:55:28 +0900 Subject: [PATCH 33/46] Fix build error, modify the comment and test case. --- .../common/cpp/client_wrapper/event_channel_unittests.cc | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc index 480687212cb08..354c89dcafa96 100755 --- a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc +++ b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc @@ -17,10 +17,6 @@ namespace { class TestBinaryMessenger : public BinaryMessenger { public: - void Send(const std::string& channel, - const uint8_t* message, - const size_t message_size) const override {} - void Send(const std::string& channel, const uint8_t* message, const size_t message_size, @@ -165,7 +161,9 @@ TEST(EventChannelTest, ConsecutiveListen) { [](const uint8_t* reply, const size_t reply_size) {}); EXPECT_EQ(on_listen_called, true); - // Send dummy listen message. + // Send second dummy message to test StreamHandler's onCancel + // method is called before onListen method is called. + on_listen_called = false; message = codec.EncodeMethodCall(call); messenger.last_message_handler()( message->data(), message->size(), From 825ca23be1db57b6b90bab532e7783a1ed7d7509 Mon Sep 17 00:00:00 2001 From: Kurun <38722979+Kurun-pan@users.noreply.github.com> Date: Sat, 18 Apr 2020 17:40:05 +0900 Subject: [PATCH 34/46] Add StreamHandlerError to communicate errors, and fix the points that were pointed out. --- .../client_wrapper/event_channel_unittests.cc | 46 +++++++++++++----- .../include/flutter/event_channel.h | 47 +++++++++++++------ .../include/flutter/event_stream_handler.h | 26 ++++++++-- 3 files changed, 88 insertions(+), 31 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc index 354c89dcafa96..7679608a49176 100755 --- a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc +++ b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc @@ -54,8 +54,14 @@ TEST(EventChannelTest, Registration) { [&on_listen_called]( const flutter::EncodableValue* arguments, std::unique_ptr>&& - events) { on_listen_called = true; }, - [](const flutter::EncodableValue* arguments) {}); + events) -> + std::unique_ptr> { + on_listen_called = true; + return nullptr; + }, + [](const flutter::EncodableValue* arguments) + -> std::unique_ptr> { + return nullptr; }); channel.SetStreamHandler(&handler); EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); EXPECT_NE(messenger.last_message_handler(), nullptr); @@ -81,8 +87,12 @@ TEST(EventChannelTest, Unregistration) { flutter::StreamHandler handler( [](const flutter::EncodableValue* arguments, std::unique_ptr>&& - events) {}, - [](const flutter::EncodableValue* arguments) {}); + events) -> + std::unique_ptr> + { return nullptr; }, + [](const flutter::EncodableValue* arguments) + -> std::unique_ptr> { + return nullptr; }); channel.SetStreamHandler(&handler); EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); EXPECT_NE(messenger.last_message_handler(), nullptr); @@ -105,10 +115,16 @@ TEST(EventChannelTest, Cancel) { [&on_listen_called]( const flutter::EncodableValue* arguments, std::unique_ptr>&& - events) { on_listen_called = true; }, - [&on_cancel_called](const flutter::EncodableValue* arguments) { - on_cancel_called = true; - }); + events) -> + std::unique_ptr> { + on_listen_called = true; + return nullptr; + }, + [&on_cancel_called](const flutter::EncodableValue* arguments) + -> std::unique_ptr> { + on_cancel_called = true; + return nullptr; + }); channel.SetStreamHandler(&handler); EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); EXPECT_NE(messenger.last_message_handler(), nullptr); @@ -145,10 +161,16 @@ TEST(EventChannelTest, ConsecutiveListen) { [&on_listen_called]( const flutter::EncodableValue* arguments, std::unique_ptr>&& - events) { on_listen_called = true; }, - [&on_cancel_called](const flutter::EncodableValue* arguments) { - on_cancel_called = true; - }); + events) -> + std::unique_ptr> { + on_listen_called = true; + return nullptr; + }, + [&on_cancel_called](const flutter::EncodableValue* arguments) + -> std::unique_ptr> { + on_cancel_called = true; + return nullptr; + }); channel.SetStreamHandler(&handler); EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); EXPECT_NE(messenger.last_message_handler(), nullptr); diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h index b3e077d9693a7..0f721defa5b69 100755 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h @@ -48,48 +48,65 @@ class EventChannel { void SetStreamHandler(StreamHandler* handler) { if (!handler) { messenger_->SetMessageHandler(name_, nullptr); - is_listened_ = false; + is_listening_ = false; return; } const MethodCodec* codec = codec_; const std::string channel_name = name_; const BinaryMessenger* messenger = messenger_; - bool& is_listend = is_listened_; BinaryMessageHandler binary_handler = [handler, codec, channel_name, - messenger, &is_listend]( + messenger, this]( const uint8_t* message, const size_t message_size, BinaryReply reply) { constexpr char kOnListenMethod[] = "listen"; constexpr char kOnCancelMethod[] = "cancel"; - std::shared_ptr> method_call = + std::unique_ptr> method_call = codec->DecodeMethodCall(message, message_size); if (!method_call) { std::cerr << "Unable to construct method call from message on channel: " << channel_name << std::endl; + reply(nullptr, 0); return; } const std::string& method = method_call->method_name(); if (method.compare(kOnListenMethod) == 0) { - if (is_listend) { - handler->onCancel(nullptr); + if (is_listening_) { + auto error = handler->onCancel(nullptr); + if (error) { + std::cerr << "Failed to cancel existing stream: " + << (error->error_code) + << ", " << (error->error_message) + << ", " << (error->error_details); + } } - is_listend = true; + is_listening_ = true; + + std::unique_ptr> result; auto sink = std::make_unique( messenger, channel_name, codec); - handler->onListen(method_call->arguments(), std::move(sink)); - - auto result = codec->EncodeSuccessEnvelope(); + auto error = handler->onListen(method_call->arguments(), std::move(sink)); + if (error) { + result = codec->EncodeErrorEnvelope( + error->error_code, error->error_message, error->error_details); + } else { + result = codec->EncodeSuccessEnvelope(); + } reply(result->data(), result->size()); } else if (method.compare(kOnCancelMethod) == 0) { std::unique_ptr> result; - if (is_listend) { - handler->onCancel(method_call->arguments()); - result = codec->EncodeSuccessEnvelope(); - is_listend = false; + if (is_listening_) { + auto error = handler->onCancel(method_call->arguments()); + if (error) { + result = codec->EncodeErrorEnvelope( + error->error_code, error->error_message, error->error_details); + } else { + result = codec->EncodeSuccessEnvelope(); + } + is_listening_ = false; } else { result = codec->EncodeErrorEnvelope( "error", "No active stream to cancel", nullptr); @@ -140,7 +157,7 @@ class EventChannel { BinaryMessenger* messenger_; const std::string name_; const MethodCodec* codec_; - bool is_listened_; + bool is_listening_; }; } // namespace flutter diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h index c744f0ffb7cea..08e9c99e8afea 100755 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h @@ -9,6 +9,19 @@ namespace flutter { +template +struct 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, + const T* error_details) + : error_code(error_code), error_message(error_message), + error_details(error_details) {} +}; + // Handler of stream setup and tear-down requests. // Implementations must be prepared to accept sequences of alternating calls to // onListen() and onCancel(). Implementations should ideally consume no @@ -17,15 +30,20 @@ namespace flutter { // platform-specific event sources onListen() and deregister again onCancel(). template struct StreamHandler { - // Handles a request to set up an event stream. + // Handles a request to set up an event stream. Returns error if representing + // an unsuccessful outcome of invoking the method, possibly nullptr. // |arguments| is stream configuration arguments and // |events| is an EventSink for emitting events to the Flutter receiver. - using OnListen = std::function>&& events)>; + using OnListen = + std::function> + (const T* arguments, std::unique_ptr>&& events)>; // Handles a request to tear down the most recently created event stream. + // Returns error if representing an unsuccessful outcome of invoking the method, + // possibly nullptr. // |arguments| is stream configuration arguments. - using OnCancel = std::function; + using OnCancel = + std::function>(const T* arguments)>; OnListen onListen; OnCancel onCancel; From 3befe52fe4e20eec986b3c198e1d8b68cf6896d2 Mon Sep 17 00:00:00 2001 From: Kurun <38722979+Kurun-pan@users.noreply.github.com> Date: Sat, 18 Apr 2020 18:12:02 +0900 Subject: [PATCH 35/46] Fix code formatting. --- .../include/flutter/event_channel.h | 6 +++--- .../include/flutter/event_stream_handler.h | 15 ++++++++------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h index 0f721defa5b69..70eb5b2092344 100755 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h @@ -78,8 +78,7 @@ class EventChannel { auto error = handler->onCancel(nullptr); if (error) { std::cerr << "Failed to cancel existing stream: " - << (error->error_code) - << ", " << (error->error_message) + << (error->error_code) << ", " << (error->error_message) << ", " << (error->error_details); } } @@ -88,7 +87,8 @@ class EventChannel { std::unique_ptr> result; auto sink = std::make_unique( messenger, channel_name, codec); - auto error = handler->onListen(method_call->arguments(), std::move(sink)); + auto error = + handler->onListen(method_call->arguments(), std::move(sink)); if (error) { result = codec->EncodeErrorEnvelope( error->error_code, error->error_message, error->error_details); diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h index 08e9c99e8afea..e4bc4304171ec 100755 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h @@ -18,7 +18,8 @@ struct StreamHandlerError { StreamHandlerError(const std::string& error_code, const std::string& error_message, const T* error_details) - : error_code(error_code), error_message(error_message), + : error_code(error_code), + error_message(error_message), error_details(error_details) {} }; @@ -34,16 +35,16 @@ struct StreamHandler { // an unsuccessful outcome of invoking the method, possibly nullptr. // |arguments| is stream configuration arguments and // |events| is an EventSink for emitting events to the Flutter receiver. - using OnListen = - std::function> - (const T* arguments, std::unique_ptr>&& events)>; + using OnListen = std::function>( + const T* arguments, + std::unique_ptr>&& events)>; // Handles a request to tear down the most recently created event stream. - // Returns error if representing an unsuccessful outcome of invoking the method, - // possibly nullptr. + // Returns error if representing an unsuccessful outcome of invoking the + // method, possibly nullptr. // |arguments| is stream configuration arguments. using OnCancel = - std::function>(const T* arguments)>; + std::function>(const T* arguments)>; OnListen onListen; OnCancel onCancel; From 8a76fba16bc0cce7a7ca7a7a99ffb3a2684925eb Mon Sep 17 00:00:00 2001 From: Kurun <38722979+Kurun-pan@users.noreply.github.com> Date: Sat, 18 Apr 2020 18:36:47 +0900 Subject: [PATCH 36/46] Fix code formatting. --- .../cpp/client_wrapper/include/flutter/event_channel.h | 10 +++++----- .../include/flutter/event_stream_handler.h | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h index 70eb5b2092344..b2a8c70199193 100755 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h @@ -56,10 +56,10 @@ class EventChannel { const std::string channel_name = name_; const BinaryMessenger* messenger = messenger_; BinaryMessageHandler binary_handler = [handler, codec, channel_name, - messenger, this]( - const uint8_t* message, - const size_t message_size, - BinaryReply reply) { + messenger, + this](const uint8_t* message, + const size_t message_size, + BinaryReply reply) { constexpr char kOnListenMethod[] = "listen"; constexpr char kOnCancelMethod[] = "cancel"; @@ -87,7 +87,7 @@ class EventChannel { std::unique_ptr> result; auto sink = std::make_unique( messenger, channel_name, codec); - auto error = + auto error = handler->onListen(method_call->arguments(), std::move(sink)); if (error) { result = codec->EncodeErrorEnvelope( diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h index e4bc4304171ec..3637e69aec1a0 100755 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h @@ -18,9 +18,9 @@ struct StreamHandlerError { StreamHandlerError(const std::string& error_code, const std::string& error_message, const T* error_details) - : error_code(error_code), - error_message(error_message), - error_details(error_details) {} + : error_code(error_code), + error_message(error_message), + error_details(error_details) {} }; // Handler of stream setup and tear-down requests. @@ -43,7 +43,7 @@ struct StreamHandler { // Returns error if representing an unsuccessful outcome of invoking the // method, possibly nullptr. // |arguments| is stream configuration arguments. - using OnCancel = + using OnCancel = std::function>(const T* arguments)>; OnListen onListen; From 5f09561da91faf513769a1eb9d631380f0a65412 Mon Sep 17 00:00:00 2001 From: Kurun <38722979+Kurun-pan@users.noreply.github.com> Date: Sat, 18 Apr 2020 21:35:14 +0900 Subject: [PATCH 37/46] Recreated StreamHandler class with reference to MethodResult. --- ci/licenses_golden/licenses_flutter | 1 + .../cpp/client_wrapper/core_wrapper_files.gni | 1 + .../client_wrapper/event_channel_unittests.cc | 21 ++++-- .../include/flutter/event_channel.h | 16 ++-- .../include/flutter/event_stream_handler.h | 34 ++++++--- .../flutter/event_stream_handler_functions.h | 74 +++++++++++++++++++ 6 files changed, 124 insertions(+), 23 deletions(-) mode change 100755 => 100644 shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc mode change 100755 => 100644 shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h mode change 100755 => 100644 shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h create mode 100644 shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler_functions.h diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index c095342e6ec4e..1ca03f53f506e 100644 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -784,6 +784,7 @@ FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/ FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/event_sink.h FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h +FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler_functions.h FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/message_codec.h FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/method_call.h FILE: ../../../flutter/shell/platform/common/cpp/client_wrapper/include/flutter/method_channel.h diff --git a/shell/platform/common/cpp/client_wrapper/core_wrapper_files.gni b/shell/platform/common/cpp/client_wrapper/core_wrapper_files.gni index 18996d0a5f93b..18b0897e2a987 100644 --- a/shell/platform/common/cpp/client_wrapper/core_wrapper_files.gni +++ b/shell/platform/common/cpp/client_wrapper/core_wrapper_files.gni @@ -10,6 +10,7 @@ core_cpp_client_wrapper_includes = "include/flutter/engine_method_result.h", "include/flutter/event_channel.h", "include/flutter/event_sink.h", + "include/flutter/event_stream_handler_functions.h", "include/flutter/event_stream_handler.h", "include/flutter/message_codec.h", "include/flutter/method_call.h", diff --git a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc old mode 100755 new mode 100644 index 7679608a49176..2a24cd9925d25 --- a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc +++ b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc @@ -8,6 +8,7 @@ #include #include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/binary_messenger.h" +#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler_functions.h" #include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/standard_method_codec.h" #include "gtest/gtest.h" @@ -50,7 +51,8 @@ TEST(EventChannelTest, Registration) { EventChannel channel(&messenger, channel_name, &codec); bool on_listen_called = false; - flutter::StreamHandler handler( + auto handler = + std::make_unique>( [&on_listen_called]( const flutter::EncodableValue* arguments, std::unique_ptr>&& @@ -62,7 +64,7 @@ TEST(EventChannelTest, Registration) { [](const flutter::EncodableValue* arguments) -> std::unique_ptr> { return nullptr; }); - channel.SetStreamHandler(&handler); + channel.SetStreamHandler(std::move(handler)); EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); EXPECT_NE(messenger.last_message_handler(), nullptr); @@ -84,7 +86,8 @@ TEST(EventChannelTest, Unregistration) { const StandardMethodCodec& codec = StandardMethodCodec::GetInstance(); EventChannel channel(&messenger, channel_name, &codec); - flutter::StreamHandler handler( + auto handler = + std::make_unique>( [](const flutter::EncodableValue* arguments, std::unique_ptr>&& events) -> @@ -93,7 +96,7 @@ TEST(EventChannelTest, Unregistration) { [](const flutter::EncodableValue* arguments) -> std::unique_ptr> { return nullptr; }); - channel.SetStreamHandler(&handler); + channel.SetStreamHandler(std::move(handler)); EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); EXPECT_NE(messenger.last_message_handler(), nullptr); @@ -111,7 +114,8 @@ TEST(EventChannelTest, Cancel) { bool on_listen_called = false; bool on_cancel_called = false; - flutter::StreamHandler handler( + auto handler = + std::make_unique>( [&on_listen_called]( const flutter::EncodableValue* arguments, std::unique_ptr>&& @@ -125,7 +129,7 @@ TEST(EventChannelTest, Cancel) { on_cancel_called = true; return nullptr; }); - channel.SetStreamHandler(&handler); + channel.SetStreamHandler(std::move(handler)); EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); EXPECT_NE(messenger.last_message_handler(), nullptr); @@ -157,7 +161,8 @@ TEST(EventChannelTest, ConsecutiveListen) { bool on_listen_called = false; bool on_cancel_called = false; - flutter::StreamHandler handler( + auto handler = + std::make_unique>( [&on_listen_called]( const flutter::EncodableValue* arguments, std::unique_ptr>&& @@ -171,7 +176,7 @@ TEST(EventChannelTest, ConsecutiveListen) { on_cancel_called = true; return nullptr; }); - channel.SetStreamHandler(&handler); + channel.SetStreamHandler(std::move(handler)); EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); EXPECT_NE(messenger.last_message_handler(), nullptr); diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h old mode 100755 new mode 100644 index b2a8c70199193..d23be883526df --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h @@ -45,21 +45,25 @@ class EventChannel { // Registers a stream handler on this channel. // If no handler has been registered, any incoming stream setup requests will // be handled silently by providing an empty stream. - void SetStreamHandler(StreamHandler* handler) { + void SetStreamHandler(std::unique_ptr> handler) { if (!handler) { messenger_->SetMessageHandler(name_, nullptr); is_listening_ = false; return; } + // std::function requires a copyable lambda, so convert to a shared pointer. + // This is safe since only one copy of the shared_pointer will ever be + // accessed. + std::shared_ptr> shared_handler(handler.release()); const MethodCodec* codec = codec_; const std::string channel_name = name_; const BinaryMessenger* messenger = messenger_; - BinaryMessageHandler binary_handler = [handler, codec, channel_name, + BinaryMessageHandler binary_handler = [shared_handler, codec, channel_name, messenger, this](const uint8_t* message, const size_t message_size, - BinaryReply reply) { + BinaryReply reply) { constexpr char kOnListenMethod[] = "listen"; constexpr char kOnCancelMethod[] = "cancel"; @@ -75,7 +79,7 @@ class EventChannel { const std::string& method = method_call->method_name(); if (method.compare(kOnListenMethod) == 0) { if (is_listening_) { - auto error = handler->onCancel(nullptr); + auto error = shared_handler->OnCancel(nullptr); if (error) { std::cerr << "Failed to cancel existing stream: " << (error->error_code) << ", " << (error->error_message) @@ -88,7 +92,7 @@ class EventChannel { auto sink = std::make_unique( messenger, channel_name, codec); auto error = - handler->onListen(method_call->arguments(), std::move(sink)); + shared_handler->OnListen(method_call->arguments(), std::move(sink)); if (error) { result = codec->EncodeErrorEnvelope( error->error_code, error->error_message, error->error_details); @@ -99,7 +103,7 @@ class EventChannel { } else if (method.compare(kOnCancelMethod) == 0) { std::unique_ptr> result; if (is_listening_) { - auto error = handler->onCancel(method_call->arguments()); + auto error = shared_handler->OnCancel(method_call->arguments()); if (error) { result = codec->EncodeErrorEnvelope( error->error_code, error->error_message, error->error_details); diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h old mode 100755 new mode 100644 index 3637e69aec1a0..3bb3f6ffdb87d --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h @@ -30,27 +30,43 @@ struct StreamHandlerError { // this means that the implementation should register itself with // platform-specific event sources onListen() and deregister again onCancel(). template -struct StreamHandler { +class StreamHandler { + public: + StreamHandler() = default; + virtual ~StreamHandler() = default; + + // Prevent copying. + StreamHandler(StreamHandler const&) = delete; + StreamHandler& operator=(StreamHandler const&) = delete; + // Handles a request to set up an event stream. Returns error if representing // an unsuccessful outcome of invoking the method, possibly nullptr. // |arguments| is stream configuration arguments and // |events| is an EventSink for emitting events to the Flutter receiver. - using OnListen = std::function>( + std::unique_ptr> OnListen( const T* arguments, - std::unique_ptr>&& events)>; + std::unique_ptr> error, + std::unique_ptr>&& events) { + return OnListenInternal(arguments, std::move(events)); + } // Handles a request to tear down the most recently created event stream. // Returns error if representing an unsuccessful outcome of invoking the // method, possibly nullptr. // |arguments| is stream configuration arguments. - using OnCancel = - std::function>(const T* arguments)>; + std::unique_ptr> OnCancel(const T* arguments) { + return OnCancelInternal(arguments); + } - OnListen onListen; - OnCancel onCancel; + protected: + // Implementation of the public interface, to be provided by subclasses. + virtual std::unique_ptr> + OnListenInternal(const T* arguments, + std::unique_ptr>&& events) = 0; - StreamHandler(OnListen const& onListen, OnCancel const& onCancel) - : onListen(onListen), onCancel(onCancel) {} + // Implementation of the public interface, to be provided by subclasses. + virtual std::unique_ptr> + OnCancelInternal(const T* arguments) = 0; }; } // namespace flutter diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler_functions.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler_functions.h new file mode 100644 index 0000000000000..6222e3e87d2f0 --- /dev/null +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler_functions.h @@ -0,0 +1,74 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_STREAM_HANDLER_FUNCTIONS_H_ +#define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_STREAM_HANDLER_FUNCTIONS_H_ + +#include + +#include "event_sink.h" +#include "event_stream_handler.h" + +namespace flutter { + +// Handler types for each of the StreamHandler outcomes. +template +using StreamHandlerListen = + std::function>( + const T* arguments, + std::unique_ptr>&& events)>; + +template +using StreamHandlerCancel = + std::function>(const T* arguments)>; + +template +class StreamHandlerFunctions : public StreamHandler { + public: + // Creates a result object that calls the provided functions for the + // corresponding StreamHandler outcomes. + StreamHandlerFunctions(StreamHandlerListen on_listen, + StreamHandlerCancel on_cancel) + : on_listen_(on_listen), + on_cancel_(on_cancel) {} + + virtual ~StreamHandlerFunctions() = default; + + // Prevent copying. + StreamHandlerFunctions(StreamHandlerFunctions const&) = delete; + StreamHandlerFunctions& operator=(StreamHandlerFunctions const&) = delete; + + protected: + // |flutter::StreamHandler| + std::unique_ptr> + OnListenInternal(const T* arguments, + std::unique_ptr>&& events) override { + if (on_listen_) { + return on_listen_(arguments, std::move(events)); + } + + auto error = std::make_unique> + ("error", "Not found StreamHandlerListen hander", nullptr); + return std::move(error); + } + + // |flutter::StreamHandler| + std::unique_ptr> + OnCancelInternal(const T* arguments) override { + if (on_cancel_) { + return on_cancel_(arguments); + } + + auto error = std::make_unique> + ("error", "Not found StreamHandlerCancel hander", nullptr); + return std::move(error); + } + + StreamHandlerListen on_listen_; + StreamHandlerCancel on_cancel_; +}; + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_EVENT_STREAM_HANDLER_FUNCTIONS_H_ From 4896184cce96f3bd194656a07d6e02b092c8c94c Mon Sep 17 00:00:00 2001 From: Kurun <38722979+Kurun-pan@users.noreply.github.com> Date: Sat, 18 Apr 2020 21:40:38 +0900 Subject: [PATCH 38/46] Fix build error. --- .../cpp/client_wrapper/include/flutter/event_stream_handler.h | 1 - 1 file changed, 1 deletion(-) diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h index 3bb3f6ffdb87d..f4a608ba5e5a9 100644 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h @@ -45,7 +45,6 @@ class StreamHandler { // |events| is an EventSink for emitting events to the Flutter receiver. std::unique_ptr> OnListen( const T* arguments, - std::unique_ptr> error, std::unique_ptr>&& events) { return OnListenInternal(arguments, std::move(events)); } From 41eb32fc9d52a9ece643ea7fff5a1ee4e686e40a Mon Sep 17 00:00:00 2001 From: Kurun <38722979+Kurun-pan@users.noreply.github.com> Date: Sat, 18 Apr 2020 21:54:48 +0900 Subject: [PATCH 39/46] Fix code formatting. --- .../flutter/event_stream_handler_functions.h | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler_functions.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler_functions.h index 6222e3e87d2f0..b69e29987b6a3 100644 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler_functions.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler_functions.h @@ -21,7 +21,7 @@ using StreamHandlerListen = template using StreamHandlerCancel = - std::function>(const T* arguments)>; + std::function>(const T* arguments)>; template class StreamHandlerFunctions : public StreamHandler { @@ -30,8 +30,7 @@ class StreamHandlerFunctions : public StreamHandler { // corresponding StreamHandler outcomes. StreamHandlerFunctions(StreamHandlerListen on_listen, StreamHandlerCancel on_cancel) - : on_listen_(on_listen), - on_cancel_(on_cancel) {} + : on_listen_(on_listen), on_cancel_(on_cancel) {} virtual ~StreamHandlerFunctions() = default; @@ -41,27 +40,27 @@ class StreamHandlerFunctions : public StreamHandler { protected: // |flutter::StreamHandler| - std::unique_ptr> - OnListenInternal(const T* arguments, - std::unique_ptr>&& events) override { + std::unique_ptr> OnListenInternal( + const T* arguments, + std::unique_ptr>&& events) override { if (on_listen_) { return on_listen_(arguments, std::move(events)); } - auto error = std::make_unique> - ("error", "Not found StreamHandlerListen hander", nullptr); + auto error = std::make_unique>( + "error", "Not found StreamHandlerListen hander", nullptr); return std::move(error); } // |flutter::StreamHandler| - std::unique_ptr> - OnCancelInternal(const T* arguments) override { + std::unique_ptr> OnCancelInternal( + const T* arguments) override { if (on_cancel_) { return on_cancel_(arguments); } - auto error = std::make_unique> - ("error", "Not found StreamHandlerCancel hander", nullptr); + auto error = std::make_unique>( + "error", "Not found StreamHandlerCancel hander", nullptr); return std::move(error); } From dc68fbdb9dff474d3ccf037e4db8c00f7111a991 Mon Sep 17 00:00:00 2001 From: Kurun <38722979+Kurun-pan@users.noreply.github.com> Date: Sat, 18 Apr 2020 22:09:12 +0900 Subject: [PATCH 40/46] Fix code formatting. --- .../include/flutter/event_stream_handler.h | 10 +++++----- .../include/flutter/event_stream_handler_functions.h | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h index f4a608ba5e5a9..3c9edaa1d1dbf 100644 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h @@ -59,13 +59,13 @@ class StreamHandler { protected: // Implementation of the public interface, to be provided by subclasses. - virtual std::unique_ptr> - OnListenInternal(const T* arguments, - std::unique_ptr>&& events) = 0; + virtual std::unique_ptr> OnListenInternal( + const T* arguments, + std::unique_ptr>&& events) = 0; // Implementation of the public interface, to be provided by subclasses. - virtual std::unique_ptr> - OnCancelInternal(const T* arguments) = 0; + virtual std::unique_ptr> OnCancelInternal( + const T* arguments) = 0; }; } // namespace flutter diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler_functions.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler_functions.h index b69e29987b6a3..235fc5982c574 100644 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler_functions.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler_functions.h @@ -16,8 +16,8 @@ namespace flutter { template using StreamHandlerListen = std::function>( - const T* arguments, - std::unique_ptr>&& events)>; + const T* arguments, + std::unique_ptr>&& events)>; template using StreamHandlerCancel = @@ -58,7 +58,7 @@ class StreamHandlerFunctions : public StreamHandler { if (on_cancel_) { return on_cancel_(arguments); } - + auto error = std::make_unique>( "error", "Not found StreamHandlerCancel hander", nullptr); return std::move(error); From 0416d5bd6b0b8b1a5eb25631afd3da675a82cb59 Mon Sep 17 00:00:00 2001 From: Kurun <38722979+Kurun-pan@users.noreply.github.com> Date: Sat, 18 Apr 2020 22:58:00 +0900 Subject: [PATCH 41/46] Fix code formatting. --- .../client_wrapper/event_channel_unittests.cc | 60 +++++++++---------- 1 file changed, 28 insertions(+), 32 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc index 2a24cd9925d25..63ce0a69eefb2 100644 --- a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc +++ b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc @@ -55,12 +55,11 @@ TEST(EventChannelTest, Registration) { std::make_unique>( [&on_listen_called]( const flutter::EncodableValue* arguments, - std::unique_ptr>&& - events) -> - std::unique_ptr> { - on_listen_called = true; - return nullptr; - }, + std::unique_ptr>&& events) + -> std::unique_ptr> { + on_listen_called = true; + return nullptr; + }, [](const flutter::EncodableValue* arguments) -> std::unique_ptr> { return nullptr; }); @@ -89,10 +88,9 @@ TEST(EventChannelTest, Unregistration) { auto handler = std::make_unique>( [](const flutter::EncodableValue* arguments, - std::unique_ptr>&& - events) -> - std::unique_ptr> - { return nullptr; }, + std::unique_ptr>&& events) + -> std::unique_ptr> + { return nullptr; }, [](const flutter::EncodableValue* arguments) -> std::unique_ptr> { return nullptr; }); @@ -118,17 +116,16 @@ TEST(EventChannelTest, Cancel) { std::make_unique>( [&on_listen_called]( const flutter::EncodableValue* arguments, - std::unique_ptr>&& - events) -> - std::unique_ptr> { - on_listen_called = true; - return nullptr; - }, + std::unique_ptr>&& events) + -> std::unique_ptr> { + on_listen_called = true; + return nullptr; + }, [&on_cancel_called](const flutter::EncodableValue* arguments) - -> std::unique_ptr> { - on_cancel_called = true; - return nullptr; - }); + -> std::unique_ptr> { + on_cancel_called = true; + return nullptr; + }); channel.SetStreamHandler(std::move(handler)); EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); EXPECT_NE(messenger.last_message_handler(), nullptr); @@ -161,21 +158,20 @@ TEST(EventChannelTest, ConsecutiveListen) { bool on_listen_called = false; bool on_cancel_called = false; - auto handler = - std::make_unique>( + auto handler = std::make_unique< + flutter::StreamHandlerFunctions>( [&on_listen_called]( const flutter::EncodableValue* arguments, - std::unique_ptr>&& - events) -> - std::unique_ptr> { - on_listen_called = true; - return nullptr; - }, + std::unique_ptr>&& events) + -> std::unique_ptr> { + on_listen_called = true; + return nullptr; + }, [&on_cancel_called](const flutter::EncodableValue* arguments) - -> std::unique_ptr> { - on_cancel_called = true; - return nullptr; - }); + -> std::unique_ptr> { + on_cancel_called = true; + return nullptr; + }); channel.SetStreamHandler(std::move(handler)); EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); EXPECT_NE(messenger.last_message_handler(), nullptr); From b36ae30ddfee8db7b84f8c3ea1969bcb0971b490 Mon Sep 17 00:00:00 2001 From: Kurun <38722979+Kurun-pan@users.noreply.github.com> Date: Sat, 18 Apr 2020 23:52:02 +0900 Subject: [PATCH 42/46] Fix code formatting. --- .../client_wrapper/event_channel_unittests.cc | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc index 63ce0a69eefb2..8a5eb5850a541 100644 --- a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc +++ b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc @@ -60,9 +60,10 @@ TEST(EventChannelTest, Registration) { on_listen_called = true; return nullptr; }, - [](const flutter::EncodableValue* arguments) - -> std::unique_ptr> { - return nullptr; }); + [](const flutter::EncodableValue* arguments) + -> std::unique_ptr> { + return nullptr; + }); channel.SetStreamHandler(std::move(handler)); EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); EXPECT_NE(messenger.last_message_handler(), nullptr); @@ -85,15 +86,17 @@ TEST(EventChannelTest, Unregistration) { const StandardMethodCodec& codec = StandardMethodCodec::GetInstance(); EventChannel channel(&messenger, channel_name, &codec); - auto handler = - std::make_unique>( + auto handler = std::make_unique< + flutter::StreamHandlerFunctions>( [](const flutter::EncodableValue* arguments, std::unique_ptr>&& events) - -> std::unique_ptr> - { return nullptr; }, + -> std::unique_ptr> { + return nullptr; + }, [](const flutter::EncodableValue* arguments) - -> std::unique_ptr> { - return nullptr; }); + -> std::unique_ptr> { + return nullptr; + }); channel.SetStreamHandler(std::move(handler)); EXPECT_EQ(messenger.last_message_handler_channel(), channel_name); EXPECT_NE(messenger.last_message_handler(), nullptr); @@ -112,8 +115,8 @@ TEST(EventChannelTest, Cancel) { bool on_listen_called = false; bool on_cancel_called = false; - auto handler = - std::make_unique>( + auto handler = std::make_unique< + flutter::StreamHandlerFunctions>( [&on_listen_called]( const flutter::EncodableValue* arguments, std::unique_ptr>&& events) From dfb14512d037c6fb3bd6cc1623ec37e310a767f6 Mon Sep 17 00:00:00 2001 From: Kurun <38722979+Kurun-pan@users.noreply.github.com> Date: Sun, 19 Apr 2020 00:00:44 +0900 Subject: [PATCH 43/46] Fix code formatting. --- .../common/cpp/client_wrapper/event_channel_unittests.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc index 8a5eb5850a541..eb3fd59663a41 100644 --- a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc +++ b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc @@ -51,8 +51,8 @@ TEST(EventChannelTest, Registration) { EventChannel channel(&messenger, channel_name, &codec); bool on_listen_called = false; - auto handler = - std::make_unique>( + auto handler = std::make_unique< + flutter::StreamHandlerFunctions>( [&on_listen_called]( const flutter::EncodableValue* arguments, std::unique_ptr>&& events) @@ -93,7 +93,7 @@ TEST(EventChannelTest, Unregistration) { -> std::unique_ptr> { return nullptr; }, - [](const flutter::EncodableValue* arguments) + [](const flutter::EncodableValue* arguments) -> std::unique_ptr> { return nullptr; }); From 61df9badb31fe32a843a1074ecdf34059d4fd275 Mon Sep 17 00:00:00 2001 From: Kurun <38722979+Kurun-pan@users.noreply.github.com> Date: Sun, 19 Apr 2020 00:35:46 +0900 Subject: [PATCH 44/46] Modified method name and other. --- .../common/cpp/client_wrapper/event_channel_unittests.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc index eb3fd59663a41..e9292195a67c7 100644 --- a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc +++ b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc @@ -187,17 +187,17 @@ TEST(EventChannelTest, ConsecutiveListen) { [](const uint8_t* reply, const size_t reply_size) {}); EXPECT_EQ(on_listen_called, true); - // Send second dummy message to test StreamHandler's onCancel - // method is called before onListen method is called. + // Send second dummy message to test StreamHandler's OnCancel + // method is called before OnListen method is called. on_listen_called = false; message = codec.EncodeMethodCall(call); messenger.last_message_handler()( message->data(), message->size(), [](const uint8_t* reply, const size_t reply_size) {}); - EXPECT_EQ(on_listen_called, true); // Check results. EXPECT_EQ(on_cancel_called, true); + EXPECT_EQ(on_listen_called, true); } } // namespace flutter From 090f6bf7209925b899213ac10df3cbf5fe61641a Mon Sep 17 00:00:00 2001 From: Kurun <38722979+Kurun-pan@users.noreply.github.com> Date: Sun, 19 Apr 2020 08:38:23 +0900 Subject: [PATCH 45/46] Modify comments. --- .../include/flutter/event_stream_handler_functions.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler_functions.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler_functions.h index 235fc5982c574..1349764fcc27d 100644 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler_functions.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler_functions.h @@ -12,7 +12,8 @@ namespace flutter { -// Handler types for each of the StreamHandler outcomes. +// Handler types for each of the StreamHandler setup and tear-down +// requests. template using StreamHandlerListen = std::function>( @@ -23,11 +24,13 @@ template using StreamHandlerCancel = std::function>(const T* arguments)>; +// An implementation of StreamHandler that pass calls through to +// provided function objects. template class StreamHandlerFunctions : public StreamHandler { public: - // Creates a result object that calls the provided functions for the - // corresponding StreamHandler outcomes. + // Creates a handler object that calls the provided functions + // for the corresponding StreamHandler outcomes. StreamHandlerFunctions(StreamHandlerListen on_listen, StreamHandlerCancel on_cancel) : on_listen_(on_listen), on_cancel_(on_cancel) {} From 7905db118fb79b5fe8c5b25b1217801403f81134 Mon Sep 17 00:00:00 2001 From: Kurun <38722979+Kurun-pan@users.noreply.github.com> Date: Thu, 30 Apr 2020 11:51:18 +0900 Subject: [PATCH 46/46] Fixed some comments and unit test scenario name. Furthermore, don't use some auto variables. --- .../cpp/client_wrapper/event_channel_unittests.cc | 7 +++++-- .../client_wrapper/include/flutter/event_channel.h | 8 +++++--- .../include/flutter/event_stream_handler.h | 13 ++++++------- .../flutter/event_stream_handler_functions.h | 4 ++-- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc index e9292195a67c7..b06ff03f8dfdf 100644 --- a/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc +++ b/shell/platform/common/cpp/client_wrapper/event_channel_unittests.cc @@ -152,8 +152,11 @@ TEST(EventChannelTest, Cancel) { EXPECT_EQ(on_cancel_called, true); } -// Test that consecutive call of OnListen. -TEST(EventChannelTest, ConsecutiveListen) { +// Pseudo test when user re-registers or call OnListen to the same channel. +// Confirm that OnCancel is called and OnListen is called again +// when user re-registers the same channel that has already started +// communication. +TEST(EventChannelTest, ReRegistration) { TestBinaryMessenger messenger; const std::string channel_name("some_channel"); const StandardMethodCodec& codec = StandardMethodCodec::GetInstance(); diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h index d23be883526df..301c73a418195 100644 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_channel.h @@ -79,7 +79,8 @@ class EventChannel { const std::string& method = method_call->method_name(); if (method.compare(kOnListenMethod) == 0) { if (is_listening_) { - auto error = shared_handler->OnCancel(nullptr); + std::unique_ptr> error = + shared_handler->OnCancel(nullptr); if (error) { std::cerr << "Failed to cancel existing stream: " << (error->error_code) << ", " << (error->error_message) @@ -91,7 +92,7 @@ class EventChannel { std::unique_ptr> result; auto sink = std::make_unique( messenger, channel_name, codec); - auto error = + std::unique_ptr> error = shared_handler->OnListen(method_call->arguments(), std::move(sink)); if (error) { result = codec->EncodeErrorEnvelope( @@ -103,7 +104,8 @@ class EventChannel { } else if (method.compare(kOnCancelMethod) == 0) { std::unique_ptr> result; if (is_listening_) { - auto error = shared_handler->OnCancel(method_call->arguments()); + std::unique_ptr> error = + shared_handler->OnCancel(method_call->arguments()); if (error) { result = codec->EncodeErrorEnvelope( error->error_code, error->error_message, error->error_details); diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h index 3c9edaa1d1dbf..50d57b887f029 100644 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler.h @@ -25,10 +25,10 @@ struct StreamHandlerError { // Handler of stream setup and tear-down requests. // Implementations must be prepared to accept sequences of alternating calls to -// onListen() and onCancel(). Implementations should ideally consume no -// resources when the last such call is not onListen(). In typical situations, +// OnListen() and OnCancel(). Implementations should ideally consume no +// resources when the last such call is not OnListen(). In typical situations, // this means that the implementation should register itself with -// platform-specific event sources onListen() and deregister again onCancel(). +// platform-specific event sources OnListen() and deregister again OnCancel(). template class StreamHandler { public: @@ -39,8 +39,8 @@ class StreamHandler { StreamHandler(StreamHandler const&) = delete; StreamHandler& operator=(StreamHandler const&) = delete; - // Handles a request to set up an event stream. Returns error if representing - // an unsuccessful outcome of invoking the method, possibly nullptr. + // Handles a request to set up an event stream. Returns nullptr on success, + // or an error on failure. // |arguments| is stream configuration arguments and // |events| is an EventSink for emitting events to the Flutter receiver. std::unique_ptr> OnListen( @@ -50,8 +50,7 @@ class StreamHandler { } // Handles a request to tear down the most recently created event stream. - // Returns error if representing an unsuccessful outcome of invoking the - // method, possibly nullptr. + // Returns nullptr on success, or an error on failure. // |arguments| is stream configuration arguments. std::unique_ptr> OnCancel(const T* arguments) { return OnCancelInternal(arguments); diff --git a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler_functions.h b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler_functions.h index 1349764fcc27d..88fe8a1bc3639 100644 --- a/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler_functions.h +++ b/shell/platform/common/cpp/client_wrapper/include/flutter/event_stream_handler_functions.h @@ -51,7 +51,7 @@ class StreamHandlerFunctions : public StreamHandler { } auto error = std::make_unique>( - "error", "Not found StreamHandlerListen hander", nullptr); + "error", "No OnListen handler set", nullptr); return std::move(error); } @@ -63,7 +63,7 @@ class StreamHandlerFunctions : public StreamHandler { } auto error = std::make_unique>( - "error", "Not found StreamHandlerCancel hander", nullptr); + "error", "No OnCancel handler set", nullptr); return std::move(error); }