Skip to content

Commit ad78711

Browse files
committed
Remove StringSerializer and implement StringCodec
1 parent bfc4d30 commit ad78711

File tree

2 files changed

+60
-29
lines changed

2 files changed

+60
-29
lines changed

shell/platform/tizen/channels/lifecycle_channel.cc

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
#include "lifecycle_channel.h"
66

7-
#include <variant>
8-
9-
#include "flutter/shell/platform/common/client_wrapper/include/flutter/standard_message_codec.h"
7+
#include "flutter/shell/platform/tizen/channels/string_codec.h"
108
#include "flutter/shell/platform/tizen/tizen_log.h"
119

1210
namespace flutter {
@@ -20,38 +18,13 @@ constexpr char kResumed[] = "AppLifecycleState.resumed";
2018
constexpr char kPaused[] = "AppLifecycleState.paused";
2119
constexpr char kDetached[] = "AppLifecycleState.detached";
2220

23-
// Codec extension for UTF-8 strings.
24-
class StringSerializer : public StandardCodecSerializer {
25-
public:
26-
StringSerializer() = default;
27-
virtual ~StringSerializer() = default;
28-
29-
// Returns the shared serializer instance.
30-
static const StringSerializer& GetInstance() {
31-
static StringSerializer sInstance;
32-
return sInstance;
33-
}
34-
35-
virtual void WriteValue(const EncodableValue& value,
36-
ByteStreamWriter* stream) const override {
37-
if (auto string_value = std::get_if<std::string>(&value)) {
38-
size_t size = string_value->size();
39-
if (size > 0) {
40-
stream->WriteBytes(
41-
reinterpret_cast<const uint8_t*>(string_value->data()), size);
42-
}
43-
}
44-
}
45-
};
46-
4721
} // namespace
4822

4923
LifecycleChannel::LifecycleChannel(BinaryMessenger* messenger)
5024
: channel_(std::make_unique<BasicMessageChannel<EncodableValue>>(
5125
messenger,
5226
kChannelName,
53-
&StandardMessageCodec::GetInstance(
54-
&StringSerializer::GetInstance()))) {}
27+
&StringCodec::GetInstance())) {}
5528

5629
LifecycleChannel::~LifecycleChannel() {}
5730

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved.
2+
// Copyright 2013 The Flutter Authors. All rights reserved.
3+
// Use of this source code is governed by a BSD-style license that can be
4+
// found in the LICENSE file.
5+
6+
#ifndef EMBEDDER_STRING_CODEC_H_
7+
#define EMBEDDER_STRING_CODEC_H_
8+
9+
#include <memory>
10+
#include <string>
11+
#include <variant>
12+
13+
#include "flutter/shell/platform/common/client_wrapper/include/flutter/encodable_value.h"
14+
#include "flutter/shell/platform/common/client_wrapper/include/flutter/message_codec.h"
15+
16+
namespace flutter {
17+
18+
// A string message encoding/decoding mechanism for communications to/from the
19+
// Flutter engine via message channels.
20+
class StringCodec : public MessageCodec<EncodableValue> {
21+
public:
22+
~StringCodec() = default;
23+
24+
// Returns an instance of the codec.
25+
static const StringCodec& GetInstance() {
26+
static StringCodec sInstance;
27+
return sInstance;
28+
}
29+
30+
// Prevent copying.
31+
StringCodec(StringCodec const&) = delete;
32+
StringCodec& operator=(StringCodec const&) = delete;
33+
34+
protected:
35+
// |flutter::MessageCodec|
36+
std::unique_ptr<EncodableValue> DecodeMessageInternal(
37+
const uint8_t* binary_message,
38+
const size_t message_size) const override {
39+
return std::make_unique<EncodableValue>(std::string(
40+
reinterpret_cast<const char*>(binary_message), message_size));
41+
}
42+
43+
// |flutter::MessageCodec|
44+
std::unique_ptr<std::vector<uint8_t>> EncodeMessageInternal(
45+
const EncodableValue& message) const override {
46+
auto string_value = std::get<std::string>(message);
47+
return std::make_unique<std::vector<uint8_t>>(string_value.begin(),
48+
string_value.end());
49+
}
50+
51+
private:
52+
// Instances should be obtained via GetInstance.
53+
explicit StringCodec() = default;
54+
};
55+
56+
} // namespace flutter
57+
58+
#endif // EMBEDDER_STRING_CODEC_H_

0 commit comments

Comments
 (0)