Skip to content

Commit 4f0df04

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

File tree

2 files changed

+61
-29
lines changed

2 files changed

+61
-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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
#include "flutter/shell/platform/tizen/tizen_log.h"
16+
17+
namespace flutter {
18+
19+
// A string message encoding/decoding mechanism for communications to/from the
20+
// Flutter engine via message channels.
21+
class StringCodec : public MessageCodec<EncodableValue> {
22+
public:
23+
~StringCodec() = default;
24+
25+
// Returns an instance of the codec.
26+
static const StringCodec& GetInstance() {
27+
static StringCodec sInstance;
28+
return sInstance;
29+
}
30+
31+
// Prevent copying.
32+
StringCodec(StringCodec const&) = delete;
33+
StringCodec& operator=(StringCodec const&) = delete;
34+
35+
protected:
36+
// |flutter::MessageCodec|
37+
std::unique_ptr<EncodableValue> DecodeMessageInternal(
38+
const uint8_t* binary_message,
39+
const size_t message_size) const override {
40+
return std::make_unique<EncodableValue>(std::string(
41+
reinterpret_cast<const char*>(binary_message), message_size));
42+
}
43+
44+
// |flutter::MessageCodec|
45+
std::unique_ptr<std::vector<uint8_t>> EncodeMessageInternal(
46+
const EncodableValue& message) const override {
47+
auto string_value = std::get<std::string>(message);
48+
return std::make_unique<std::vector<uint8_t>>(string_value.begin(),
49+
string_value.end());
50+
}
51+
52+
private:
53+
// Instances should be obtained via GetInstance.
54+
explicit StringCodec() = default;
55+
};
56+
57+
} // namespace flutter
58+
59+
#endif // EMBEDDER_STRING_CODEC_H_

0 commit comments

Comments
 (0)