Skip to content

Commit

Permalink
Remove engine dependency of LifecycleChannel (#127)
Browse files Browse the repository at this point in the history
* Remove engine dependency of LifecycleChannel

* Implement StringSerializer

* Remove StringSerializer and implement StringCodec
  • Loading branch information
swift-kim authored Jun 29, 2021
1 parent 8f3fbb5 commit cafd059
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 24 deletions.
1 change: 1 addition & 0 deletions shell/platform/tizen/channels/key_event_channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <map>

#include "flutter/shell/platform/common/json_message_codec.h"
#include "flutter/shell/platform/tizen/tizen_log.h"

namespace flutter {
Expand Down
1 change: 0 additions & 1 deletion shell/platform/tizen/channels/key_event_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
31 changes: 14 additions & 17 deletions shell/platform/tizen/channels/lifecycle_channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "lifecycle_channel.h"

#include "flutter/shell/platform/tizen/flutter_tizen_engine.h"
#include "flutter/shell/platform/tizen/channels/string_codec.h"
#include "flutter/shell/platform/tizen/tizen_log.h"

namespace flutter {
Expand All @@ -20,35 +20,32 @@ constexpr char kDetached[] = "AppLifecycleState.detached";

} // namespace

LifecycleChannel::LifecycleChannel(FlutterTizenEngine* engine)
: engine_(engine) {}
LifecycleChannel::LifecycleChannel(BinaryMessenger* messenger)
: channel_(std::make_unique<BasicMessageChannel<EncodableValue>>(
messenger,
kChannelName,
&StringCodec::GetInstance())) {}

LifecycleChannel::~LifecycleChannel() {}

void LifecycleChannel::SendLifecycleMessage(const char message[]) {
engine_->SendPlatformMessage(kChannelName,
reinterpret_cast<const uint8_t*>(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
12 changes: 7 additions & 5 deletions shell/platform/tizen/channels/lifecycle_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,25 @@
#ifndef EMBEDDER_LIFECYCLE_CHANNEL_H_
#define EMBEDDER_LIFECYCLE_CHANNEL_H_

namespace flutter {
#include <memory>

#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<BasicMessageChannel<EncodableValue>> channel_;
};

} // namespace flutter
Expand Down
58 changes: 58 additions & 0 deletions shell/platform/tizen/channels/string_codec.h
Original file line number Diff line number Diff line change
@@ -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 <memory>
#include <string>
#include <variant>

#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<EncodableValue> {
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<EncodableValue> DecodeMessageInternal(
const uint8_t* binary_message,
const size_t message_size) const override {
return std::make_unique<EncodableValue>(std::string(
reinterpret_cast<const char*>(binary_message), message_size));
}

// |flutter::MessageCodec|
std::unique_ptr<std::vector<uint8_t>> EncodeMessageInternal(
const EncodableValue& message) const override {
auto string_value = std::get<std::string>(message);
return std::make_unique<std::vector<uint8_t>>(string_value.begin(),
string_value.end());
}

private:
// Instances should be obtained via GetInstance.
explicit StringCodec() = default;
};

} // namespace flutter

#endif // EMBEDDER_STRING_CODEC_H_
3 changes: 2 additions & 1 deletion shell/platform/tizen/flutter_tizen_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ bool FlutterTizenEngine::RunEngine(
internal_plugin_registrar_->messenger());
localization_channel = std::make_unique<LocalizationChannel>(this);
localization_channel->SendLocales();
lifecycle_channel = std::make_unique<LifecycleChannel>(this);
lifecycle_channel = std::make_unique<LifecycleChannel>(
internal_plugin_registrar_->messenger());

if (IsHeaded()) {
texture_registrar_ = std::make_unique<FlutterTizenTextureRegistrar>(this);
Expand Down

0 comments on commit cafd059

Please sign in to comment.