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

Commit eb22baf

Browse files
committed
Add FlStringCodec
1 parent f477ea2 commit eb22baf

File tree

6 files changed

+199
-0
lines changed

6 files changed

+199
-0
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,6 +1198,8 @@ FILE: ../../../flutter/shell/platform/linux/fl_renderer.cc
11981198
FILE: ../../../flutter/shell/platform/linux/fl_renderer.h
11991199
FILE: ../../../flutter/shell/platform/linux/fl_renderer_x11.cc
12001200
FILE: ../../../flutter/shell/platform/linux/fl_renderer_x11.h
1201+
FILE: ../../../flutter/shell/platform/linux/fl_string_codec.cc
1202+
FILE: ../../../flutter/shell/platform/linux/fl_string_codec_test.cc
12011203
FILE: ../../../flutter/shell/platform/linux/fl_value.cc
12021204
FILE: ../../../flutter/shell/platform/linux/fl_value_test.cc
12031205
FILE: ../../../flutter/shell/platform/linux/fl_view.cc
@@ -1206,6 +1208,7 @@ FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_binary_messe
12061208
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_dart_project.h
12071209
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_engine.h
12081210
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_message_codec.h
1211+
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_string_codec.h
12091212
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_value.h
12101213
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_view.h
12111214
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_value_test.cc",
102105
"testing/fl_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_new_string_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: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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/testing/fl_test.h"
7+
#include "gtest/gtest.h"
8+
9+
// Encode a message using a FlStringCodec. Return a hex string with the encoded
10+
// binary output.
11+
static gchar* encode_message(FlValue* value) {
12+
g_autoptr(FlStringCodec) codec = fl_string_codec_new();
13+
g_autoptr(GError) error = nullptr;
14+
g_autoptr(GBytes) message =
15+
fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), value, &error);
16+
EXPECT_NE(message, nullptr);
17+
EXPECT_EQ(error, nullptr);
18+
19+
return bytes_to_hex_string(message);
20+
}
21+
22+
// Encode a message using a FlStringCodec. Expect the given error.
23+
static void encode_message_error(FlValue* value, GQuark domain, int code) {
24+
g_autoptr(FlStringCodec) codec = fl_string_codec_new();
25+
g_autoptr(GError) error = nullptr;
26+
g_autoptr(GBytes) message =
27+
fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), value, &error);
28+
EXPECT_EQ(message, nullptr);
29+
EXPECT_TRUE(g_error_matches(error, domain, code));
30+
}
31+
32+
// Decode a message using a FlStringCodec. The binary data is given in the form
33+
// of a hex string.
34+
static FlValue* decode_message(const char* hex_string) {
35+
g_autoptr(FlStringCodec) codec = fl_string_codec_new();
36+
g_autoptr(GBytes) message = hex_string_to_bytes(hex_string);
37+
g_autoptr(GError) error = nullptr;
38+
g_autoptr(FlValue) value =
39+
fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), message, &error);
40+
EXPECT_EQ(error, nullptr);
41+
EXPECT_NE(value, nullptr);
42+
return fl_value_ref(value);
43+
}
44+
45+
TEST(FlStringCodecTest, EncodeData) {
46+
g_autoptr(FlValue) value = fl_value_new_string("hello");
47+
g_autofree gchar* hex_string = encode_message(value);
48+
EXPECT_STREQ(hex_string, "68656c6c6f");
49+
}
50+
51+
TEST(FlStringCodecTest, EncodeEmpty) {
52+
g_autoptr(FlValue) value = fl_value_new_string("");
53+
g_autofree gchar* hex_string = encode_message(value);
54+
EXPECT_STREQ(hex_string, "");
55+
}
56+
57+
TEST(FlStringCodecTest, EncodeNULL) {
58+
encode_message_error(nullptr, FL_MESSAGE_CODEC_ERROR,
59+
FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE);
60+
}
61+
62+
TEST(FlStringCodecTest, EncodeUnknownType) {
63+
g_autoptr(FlValue) value = fl_value_new_null();
64+
encode_message_error(value, FL_MESSAGE_CODEC_ERROR,
65+
FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE);
66+
}
67+
68+
TEST(FlStringCodecTest, DecodeData) {
69+
g_autoptr(FlValue) value = decode_message("68656c6c6f");
70+
ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_STRING);
71+
ASSERT_STREQ(fl_value_get_string(value), "hello");
72+
}
73+
74+
TEST(FlStringCodecTest, DecodeEmpty) {
75+
g_autoptr(FlValue) value = decode_message("");
76+
ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_STRING);
77+
ASSERT_STREQ(fl_value_get_string(value), "");
78+
}
79+
80+
TEST(FlStringCodecTest, EncodeDecode) {
81+
g_autoptr(FlStringCodec) codec = fl_string_codec_new();
82+
83+
g_autoptr(FlValue) input = fl_value_new_string("hello");
84+
85+
g_autoptr(GError) error = nullptr;
86+
g_autoptr(GBytes) message =
87+
fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), input, &error);
88+
EXPECT_NE(message, nullptr);
89+
EXPECT_EQ(error, nullptr);
90+
91+
g_autoptr(FlValue) output =
92+
fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), message, &error);
93+
EXPECT_EQ(error, nullptr);
94+
EXPECT_NE(output, nullptr);
95+
96+
ASSERT_TRUE(fl_value_equal(input, output));
97+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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. This encodes and decodes #FlValue of type
27+
* #FL_VALUE_TYPE_STRING.
28+
*
29+
* #FlStringCodec matches the StringCodec class in the Flutter services library.
30+
*/
31+
32+
/**
33+
* fl_string_codec_new:
34+
*
35+
* Creates a #FlStringCodec.
36+
*
37+
* Returns: a new #FlStringCodec.
38+
*/
39+
FlStringCodec* fl_string_codec_new();
40+
41+
G_END_DECLS
42+
43+
#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)