From 8523980d2c8928633c0ef157fa58ef3f33b36e63 Mon Sep 17 00:00:00 2001 From: Swift Kim Date: Fri, 25 Jun 2021 13:04:34 +0900 Subject: [PATCH 1/3] Remove engine dependency of LifecycleChannel --- .../tizen/channels/key_event_channel.cc | 1 + .../tizen/channels/key_event_channel.h | 1 - .../tizen/channels/lifecycle_channel.cc | 30 +++++++++---------- .../tizen/channels/lifecycle_channel.h | 12 ++++---- shell/platform/tizen/flutter_tizen_engine.cc | 3 +- 5 files changed, 24 insertions(+), 23 deletions(-) diff --git a/shell/platform/tizen/channels/key_event_channel.cc b/shell/platform/tizen/channels/key_event_channel.cc index 6582d4bddf975..859883e18e8d1 100644 --- a/shell/platform/tizen/channels/key_event_channel.cc +++ b/shell/platform/tizen/channels/key_event_channel.cc @@ -6,6 +6,7 @@ #include +#include "flutter/shell/platform/common/json_message_codec.h" #include "flutter/shell/platform/tizen/tizen_log.h" namespace flutter { diff --git a/shell/platform/tizen/channels/key_event_channel.h b/shell/platform/tizen/channels/key_event_channel.h index 833bc10646e0e..74708af766d32 100644 --- a/shell/platform/tizen/channels/key_event_channel.h +++ b/shell/platform/tizen/channels/key_event_channel.h @@ -11,7 +11,6 @@ #include "flutter/shell/platform/common/client_wrapper/include/flutter/basic_message_channel.h" #include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h" -#include "flutter/shell/platform/common/json_message_codec.h" #include "rapidjson/document.h" namespace flutter { diff --git a/shell/platform/tizen/channels/lifecycle_channel.cc b/shell/platform/tizen/channels/lifecycle_channel.cc index f3419be6362cd..793af02d51ca4 100644 --- a/shell/platform/tizen/channels/lifecycle_channel.cc +++ b/shell/platform/tizen/channels/lifecycle_channel.cc @@ -4,6 +4,7 @@ #include "lifecycle_channel.h" +#include "flutter/shell/platform/common/client_wrapper/include/flutter/standard_message_codec.h" #include "flutter/shell/platform/tizen/flutter_tizen_engine.h" #include "flutter/shell/platform/tizen/tizen_log.h" @@ -20,35 +21,32 @@ constexpr char kDetached[] = "AppLifecycleState.detached"; } // namespace -LifecycleChannel::LifecycleChannel(FlutterTizenEngine* engine) - : engine_(engine) {} +LifecycleChannel::LifecycleChannel(BinaryMessenger* messenger) + : channel_(std::make_unique>( + messenger, + kChannelName, + &StandardMessageCodec::GetInstance())) {} LifecycleChannel::~LifecycleChannel() {} -void LifecycleChannel::SendLifecycleMessage(const char message[]) { - engine_->SendPlatformMessage(kChannelName, - reinterpret_cast(message), - strlen(message), nullptr, nullptr); -} - void LifecycleChannel::AppIsInactive() { - FT_LOGI("send app lifecycle state inactive."); - SendLifecycleMessage(kInactive); + FT_LOGI("Sending %s message.", kInactive); + channel_->Send(EncodableValue(kInactive)); } void LifecycleChannel::AppIsResumed() { - FT_LOGI("send app lifecycle state resumed."); - SendLifecycleMessage(kResumed); + FT_LOGI("Sending %s message.", kResumed); + channel_->Send(EncodableValue(kResumed)); } void LifecycleChannel::AppIsPaused() { - FT_LOGI("send app lifecycle state paused."); - SendLifecycleMessage(kPaused); + FT_LOGI("Sending %s message.", kPaused); + channel_->Send(EncodableValue(kPaused)); } void LifecycleChannel::AppIsDetached() { - FT_LOGI("send app lifecycle state detached."); - SendLifecycleMessage(kDetached); + FT_LOGI("Sending %s message.", kDetached); + channel_->Send(EncodableValue(kDetached)); } } // namespace flutter diff --git a/shell/platform/tizen/channels/lifecycle_channel.h b/shell/platform/tizen/channels/lifecycle_channel.h index 98301688e08e8..1c134eaf5a650 100644 --- a/shell/platform/tizen/channels/lifecycle_channel.h +++ b/shell/platform/tizen/channels/lifecycle_channel.h @@ -5,23 +5,25 @@ #ifndef EMBEDDER_LIFECYCLE_CHANNEL_H_ #define EMBEDDER_LIFECYCLE_CHANNEL_H_ -namespace flutter { +#include + +#include "flutter/shell/platform/common/client_wrapper/include/flutter/basic_message_channel.h" +#include "flutter/shell/platform/common/client_wrapper/include/flutter/binary_messenger.h" -class FlutterTizenEngine; +namespace flutter { class LifecycleChannel { public: - explicit LifecycleChannel(FlutterTizenEngine* engine); + explicit LifecycleChannel(BinaryMessenger* messenger); virtual ~LifecycleChannel(); void AppIsInactive(); void AppIsResumed(); void AppIsPaused(); void AppIsDetached(); - void SendLifecycleMessage(const char message[]); private: - FlutterTizenEngine* engine_{nullptr}; + std::unique_ptr> channel_; }; } // namespace flutter diff --git a/shell/platform/tizen/flutter_tizen_engine.cc b/shell/platform/tizen/flutter_tizen_engine.cc index 309182e1461ab..20839242a971a 100644 --- a/shell/platform/tizen/flutter_tizen_engine.cc +++ b/shell/platform/tizen/flutter_tizen_engine.cc @@ -236,7 +236,8 @@ bool FlutterTizenEngine::RunEngine( internal_plugin_registrar_->messenger()); localization_channel = std::make_unique(this); localization_channel->SendLocales(); - lifecycle_channel = std::make_unique(this); + lifecycle_channel = std::make_unique( + internal_plugin_registrar_->messenger()); if (IsHeaded()) { texture_registrar_ = std::make_unique(this); From bfc4d3034b760046f527df067dfe8d6c2d84c016 Mon Sep 17 00:00:00 2001 From: Swift Kim Date: Fri, 25 Jun 2021 15:14:31 +0900 Subject: [PATCH 2/3] Implement StringSerializer --- .../tizen/channels/lifecycle_channel.cc | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/shell/platform/tizen/channels/lifecycle_channel.cc b/shell/platform/tizen/channels/lifecycle_channel.cc index 793af02d51ca4..65ad089667daf 100644 --- a/shell/platform/tizen/channels/lifecycle_channel.cc +++ b/shell/platform/tizen/channels/lifecycle_channel.cc @@ -4,8 +4,9 @@ #include "lifecycle_channel.h" +#include + #include "flutter/shell/platform/common/client_wrapper/include/flutter/standard_message_codec.h" -#include "flutter/shell/platform/tizen/flutter_tizen_engine.h" #include "flutter/shell/platform/tizen/tizen_log.h" namespace flutter { @@ -19,13 +20,38 @@ constexpr char kResumed[] = "AppLifecycleState.resumed"; constexpr char kPaused[] = "AppLifecycleState.paused"; constexpr char kDetached[] = "AppLifecycleState.detached"; +// Codec extension for UTF-8 strings. +class StringSerializer : public StandardCodecSerializer { + public: + StringSerializer() = default; + virtual ~StringSerializer() = default; + + // Returns the shared serializer instance. + static const StringSerializer& GetInstance() { + static StringSerializer sInstance; + return sInstance; + } + + virtual void WriteValue(const EncodableValue& value, + ByteStreamWriter* stream) const override { + if (auto string_value = std::get_if(&value)) { + size_t size = string_value->size(); + if (size > 0) { + stream->WriteBytes( + reinterpret_cast(string_value->data()), size); + } + } + } +}; + } // namespace LifecycleChannel::LifecycleChannel(BinaryMessenger* messenger) : channel_(std::make_unique>( messenger, kChannelName, - &StandardMessageCodec::GetInstance())) {} + &StandardMessageCodec::GetInstance( + &StringSerializer::GetInstance()))) {} LifecycleChannel::~LifecycleChannel() {} From ad787111f2031f56711f888a6a6e0e5e56df2f58 Mon Sep 17 00:00:00 2001 From: Swift Kim Date: Fri, 25 Jun 2021 16:16:07 +0900 Subject: [PATCH 3/3] Remove StringSerializer and implement StringCodec --- .../tizen/channels/lifecycle_channel.cc | 31 +--------- shell/platform/tizen/channels/string_codec.h | 58 +++++++++++++++++++ 2 files changed, 60 insertions(+), 29 deletions(-) create mode 100644 shell/platform/tizen/channels/string_codec.h diff --git a/shell/platform/tizen/channels/lifecycle_channel.cc b/shell/platform/tizen/channels/lifecycle_channel.cc index 65ad089667daf..95aca8582746d 100644 --- a/shell/platform/tizen/channels/lifecycle_channel.cc +++ b/shell/platform/tizen/channels/lifecycle_channel.cc @@ -4,9 +4,7 @@ #include "lifecycle_channel.h" -#include - -#include "flutter/shell/platform/common/client_wrapper/include/flutter/standard_message_codec.h" +#include "flutter/shell/platform/tizen/channels/string_codec.h" #include "flutter/shell/platform/tizen/tizen_log.h" namespace flutter { @@ -20,38 +18,13 @@ constexpr char kResumed[] = "AppLifecycleState.resumed"; constexpr char kPaused[] = "AppLifecycleState.paused"; constexpr char kDetached[] = "AppLifecycleState.detached"; -// Codec extension for UTF-8 strings. -class StringSerializer : public StandardCodecSerializer { - public: - StringSerializer() = default; - virtual ~StringSerializer() = default; - - // Returns the shared serializer instance. - static const StringSerializer& GetInstance() { - static StringSerializer sInstance; - return sInstance; - } - - virtual void WriteValue(const EncodableValue& value, - ByteStreamWriter* stream) const override { - if (auto string_value = std::get_if(&value)) { - size_t size = string_value->size(); - if (size > 0) { - stream->WriteBytes( - reinterpret_cast(string_value->data()), size); - } - } - } -}; - } // namespace LifecycleChannel::LifecycleChannel(BinaryMessenger* messenger) : channel_(std::make_unique>( messenger, kChannelName, - &StandardMessageCodec::GetInstance( - &StringSerializer::GetInstance()))) {} + &StringCodec::GetInstance())) {} LifecycleChannel::~LifecycleChannel() {} diff --git a/shell/platform/tizen/channels/string_codec.h b/shell/platform/tizen/channels/string_codec.h new file mode 100644 index 0000000000000..2132f26f3f631 --- /dev/null +++ b/shell/platform/tizen/channels/string_codec.h @@ -0,0 +1,58 @@ +// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved. +// 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 EMBEDDER_STRING_CODEC_H_ +#define EMBEDDER_STRING_CODEC_H_ + +#include +#include +#include + +#include "flutter/shell/platform/common/client_wrapper/include/flutter/encodable_value.h" +#include "flutter/shell/platform/common/client_wrapper/include/flutter/message_codec.h" + +namespace flutter { + +// A string message encoding/decoding mechanism for communications to/from the +// Flutter engine via message channels. +class StringCodec : public MessageCodec { + public: + ~StringCodec() = default; + + // Returns an instance of the codec. + static const StringCodec& GetInstance() { + static StringCodec sInstance; + return sInstance; + } + + // Prevent copying. + StringCodec(StringCodec const&) = delete; + StringCodec& operator=(StringCodec const&) = delete; + + protected: + // |flutter::MessageCodec| + std::unique_ptr DecodeMessageInternal( + const uint8_t* binary_message, + const size_t message_size) const override { + return std::make_unique(std::string( + reinterpret_cast(binary_message), message_size)); + } + + // |flutter::MessageCodec| + std::unique_ptr> EncodeMessageInternal( + const EncodableValue& message) const override { + auto string_value = std::get(message); + return std::make_unique>(string_value.begin(), + string_value.end()); + } + + private: + // Instances should be obtained via GetInstance. + explicit StringCodec() = default; +}; + +} // namespace flutter + +#endif // EMBEDDER_STRING_CODEC_H_