Skip to content

Commit

Permalink
Remove StringSerializer and implement StringCodec
Browse files Browse the repository at this point in the history
  • Loading branch information
swift-kim committed Jun 28, 2021
1 parent bfc4d30 commit ad78711
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 29 deletions.
31 changes: 2 additions & 29 deletions shell/platform/tizen/channels/lifecycle_channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

#include "lifecycle_channel.h"

#include <variant>

#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 {
Expand All @@ -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<std::string>(&value)) {
size_t size = string_value->size();
if (size > 0) {
stream->WriteBytes(
reinterpret_cast<const uint8_t*>(string_value->data()), size);
}
}
}
};

} // namespace

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

LifecycleChannel::~LifecycleChannel() {}

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_

0 comments on commit ad78711

Please sign in to comment.