Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit ebcc83f

Browse files
committed
Add FlStringCodec
1 parent 4a4a7af commit ebcc83f

File tree

6 files changed

+165
-0
lines changed

6 files changed

+165
-0
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,8 @@ FILE: ../../../flutter/shell/platform/linux/fl_renderer.cc
11861186
FILE: ../../../flutter/shell/platform/linux/fl_renderer.h
11871187
FILE: ../../../flutter/shell/platform/linux/fl_renderer_x11.cc
11881188
FILE: ../../../flutter/shell/platform/linux/fl_renderer_x11.h
1189+
FILE: ../../../flutter/shell/platform/linux/fl_string_codec.cc
1190+
FILE: ../../../flutter/shell/platform/linux/fl_string_codec_test.cc
11891191
FILE: ../../../flutter/shell/platform/linux/fl_test.cc
11901192
FILE: ../../../flutter/shell/platform/linux/fl_test.h
11911193
FILE: ../../../flutter/shell/platform/linux/fl_value.cc
@@ -1196,6 +1198,7 @@ FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_binary_messe
11961198
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_dart_project.h
11971199
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_engine.h
11981200
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_message_codec.h
1201+
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_string_codec.h
11991202
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_value.h
12001203
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_view.h
12011204
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/flutter_linux.h

shell/platform/linux/BUILD.gn

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ _public_headers = [
4949
"public/flutter_linux/fl_dart_project.h",
5050
"public/flutter_linux/fl_engine.h",
5151
"public/flutter_linux/fl_message_codec.h",
52+
"public/flutter_linux/fl_string_codec.h",
5253
"public/flutter_linux/fl_value.h",
5354
"public/flutter_linux/fl_view.h",
5455
"public/flutter_linux/flutter_linux.h",
@@ -69,6 +70,7 @@ source_set("flutter_linux") {
6970
"fl_message_codec.cc",
7071
"fl_renderer.cc",
7172
"fl_renderer_x11.cc",
73+
"fl_string_codec.cc",
7274
"fl_value.cc",
7375
"fl_view.cc",
7476
]
@@ -98,6 +100,7 @@ executable("flutter_linux_unittests") {
98100
"fl_binary_codec_test.cc",
99101
"fl_dart_project_test.cc",
100102
"fl_message_codec_test.cc",
103+
"fl_string_codec_test.cc",
101104
"fl_test.cc",
102105
"fl_value_test.cc",
103106
]
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "flutter/shell/platform/linux/public/flutter_linux/fl_string_codec.h"
6+
7+
#include <gmodule.h>
8+
9+
G_DEFINE_QUARK(fl_string_codec_error_quark, fl_string_codec_error)
10+
11+
struct _FlStringCodec {
12+
FlMessageCodec parent_instance;
13+
};
14+
15+
G_DEFINE_TYPE(FlStringCodec, fl_string_codec, fl_message_codec_get_type())
16+
17+
static GBytes* fl_string_codec_encode_message(FlMessageCodec* codec,
18+
FlValue* value,
19+
GError** error) {
20+
if (fl_value_get_type(value) != FL_VALUE_TYPE_STRING) {
21+
g_set_error(error, FL_MESSAGE_CODEC_ERROR,
22+
FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE,
23+
"Only string values supported");
24+
return nullptr;
25+
}
26+
27+
const gchar* text = fl_value_get_string(value);
28+
return g_bytes_new(text, strlen(text));
29+
}
30+
31+
static FlValue* fl_string_codec_decode_message(FlMessageCodec* codec,
32+
GBytes* message,
33+
GError** error) {
34+
gsize data_length;
35+
const gchar* data =
36+
static_cast<const gchar*>(g_bytes_get_data(message, &data_length));
37+
return fl_value_string_new_sized(data, data_length);
38+
}
39+
40+
static void fl_string_codec_class_init(FlStringCodecClass* klass) {
41+
FL_MESSAGE_CODEC_CLASS(klass)->encode_message =
42+
fl_string_codec_encode_message;
43+
FL_MESSAGE_CODEC_CLASS(klass)->decode_message =
44+
fl_string_codec_decode_message;
45+
}
46+
47+
static void fl_string_codec_init(FlStringCodec* self) {}
48+
49+
G_MODULE_EXPORT FlStringCodec* fl_string_codec_new() {
50+
return static_cast<FlStringCodec*>(
51+
g_object_new(fl_string_codec_get_type(), nullptr));
52+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "flutter/shell/platform/linux/public/flutter_linux/fl_string_codec.h"
6+
#include "flutter/shell/platform/linux/fl_test.h"
7+
#include "gtest/gtest.h"
8+
9+
static gchar* encode_value(FlValue* value) {
10+
g_autoptr(FlStringCodec) codec = fl_string_codec_new();
11+
g_autoptr(GError) error = nullptr;
12+
g_autoptr(GBytes) message =
13+
fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), value, &error);
14+
EXPECT_NE(message, nullptr);
15+
EXPECT_EQ(error, nullptr);
16+
17+
return bytes_to_hex_string(message);
18+
}
19+
20+
static void encode_value_error(FlValue* value, GQuark domain, int code) {
21+
g_autoptr(FlStringCodec) codec = fl_string_codec_new();
22+
g_autoptr(GError) error = nullptr;
23+
g_autoptr(GBytes) message =
24+
fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), value, &error);
25+
EXPECT_EQ(message, nullptr);
26+
EXPECT_TRUE(g_error_matches(error, domain, code));
27+
}
28+
29+
static FlValue* decode_value(const char* hex_string) {
30+
g_autoptr(FlStringCodec) codec = fl_string_codec_new();
31+
g_autoptr(GBytes) message = hex_string_to_bytes(hex_string);
32+
g_autoptr(GError) error = nullptr;
33+
g_autoptr(FlValue) value =
34+
fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), message, &error);
35+
EXPECT_EQ(error, nullptr);
36+
EXPECT_NE(value, nullptr);
37+
return fl_value_ref(value);
38+
}
39+
40+
TEST(FlStringCodecTest, EncodeData) {
41+
g_autoptr(FlValue) value = fl_value_string_new("hello");
42+
g_autofree gchar* hex_string = encode_value(value);
43+
EXPECT_STREQ(hex_string, "68656c6c6f");
44+
}
45+
46+
TEST(FlStringCodecTest, EncodeEmpty) {
47+
g_autoptr(FlValue) value = fl_value_string_new("");
48+
g_autofree gchar* hex_string = encode_value(value);
49+
EXPECT_STREQ(hex_string, "");
50+
}
51+
52+
TEST(FlStringCodecTest, EncodeNULL) {
53+
encode_value_error(nullptr, FL_MESSAGE_CODEC_ERROR,
54+
FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE);
55+
}
56+
57+
TEST(FlStringCodecTest, EncodeUnknownType) {
58+
g_autoptr(FlValue) value = fl_value_null_new();
59+
encode_value_error(value, FL_MESSAGE_CODEC_ERROR,
60+
FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE);
61+
}
62+
63+
TEST(FlStringCodecTest, DecodeData) {
64+
g_autoptr(FlValue) value = decode_value("68656c6c6f");
65+
ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_STRING);
66+
ASSERT_STREQ(fl_value_get_string(value), "hello");
67+
}
68+
69+
TEST(FlStringCodecTest, DecodeEmpty) {
70+
g_autoptr(FlValue) value = decode_value("");
71+
ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_STRING);
72+
ASSERT_STREQ(fl_value_get_string(value), "");
73+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef FLUTTER_SHELL_PLATFORM_LINUX_FL_STRING_CODEC_H_
6+
#define FLUTTER_SHELL_PLATFORM_LINUX_FL_STRING_CODEC_H_
7+
8+
#if !defined(__FLUTTER_LINUX_INSIDE__) && !defined(FLUTTER_LINUX_COMPILATION)
9+
#error "Only <flutter_linux/flutter_linux.h> can be included directly."
10+
#endif
11+
12+
#include "fl_message_codec.h"
13+
14+
G_BEGIN_DECLS
15+
16+
G_DECLARE_FINAL_TYPE(FlStringCodec,
17+
fl_string_codec,
18+
FL,
19+
STRING_CODEC,
20+
FlMessageCodec)
21+
22+
/**
23+
* FlStringCodec:
24+
*
25+
* #FlStringCodec is a #FlMessageCodec that implements the Flutter string
26+
* message encoding.
27+
*/
28+
29+
FlStringCodec* fl_string_codec_new();
30+
31+
G_END_DECLS
32+
33+
#endif // FLUTTER_SHELL_PLATFORM_LINUX_FL_STRING_CODEC_H_

shell/platform/linux/public/flutter_linux/flutter_linux.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <flutter_linux/fl_dart_project.h>
1313
#include <flutter_linux/fl_engine.h>
1414
#include <flutter_linux/fl_message_codec.h>
15+
#include <flutter_linux/fl_string_codec.h>
1516
#include <flutter_linux/fl_value.h>
1617
#include <flutter_linux/fl_view.h>
1718

0 commit comments

Comments
 (0)