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

Commit d4c0649

Browse files
committed
Add FlStringCodec
1 parent b39f606 commit d4c0649

File tree

6 files changed

+198
-0
lines changed

6 files changed

+198
-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_value.cc
11901192
FILE: ../../../flutter/shell/platform/linux/fl_value_test.cc
11911193
FILE: ../../../flutter/shell/platform/linux/fl_view.cc
@@ -1194,6 +1196,7 @@ FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_binary_messe
11941196
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_codec.h
11951197
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_dart_project.h
11961198
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_engine.h
1199+
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_string_codec.h
11971200
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_value.h
11981201
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_view.h
11991202
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_codec.h",
5151
"public/flutter_linux/fl_engine.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_engine.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_codec_test.cc",
100102
"fl_dart_project_test.cc",
103+
"fl_string_codec_test.cc",
101104
"fl_value_test.cc",
102105
]
103106

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
FlCodec parent_instance;
13+
};
14+
15+
G_DEFINE_TYPE(FlStringCodec, fl_string_codec, fl_codec_get_type())
16+
17+
static gboolean fl_string_codec_write_value(FlCodec* codec,
18+
GByteArray* buffer,
19+
FlValue* value,
20+
GError** error) {
21+
if (fl_value_get_type(value) != FL_VALUE_TYPE_STRING) {
22+
g_set_error(error, FL_CODEC_ERROR, FL_CODEC_ERROR_UNSUPPORTED_TYPE,
23+
"Only string values supported");
24+
return FALSE;
25+
}
26+
27+
const gchar* text = fl_value_get_string(value);
28+
g_byte_array_append(buffer, reinterpret_cast<const guint8*>(text),
29+
strlen(text));
30+
31+
return TRUE;
32+
}
33+
34+
static FlValue* fl_string_codec_read_value(FlCodec* codec,
35+
GBytes* message,
36+
size_t* offset,
37+
GError** error) {
38+
gsize data_length;
39+
const gchar* data =
40+
static_cast<const gchar*>(g_bytes_get_data(message, &data_length));
41+
g_autoptr(FlValue) value =
42+
fl_value_string_new_sized(data + *offset, data_length - *offset);
43+
*offset = data_length;
44+
45+
return fl_value_ref(value);
46+
}
47+
48+
static void fl_string_codec_class_init(FlStringCodecClass* klass) {
49+
FL_CODEC_CLASS(klass)->write_value = fl_string_codec_write_value;
50+
FL_CODEC_CLASS(klass)->read_value = fl_string_codec_read_value;
51+
}
52+
53+
static void fl_string_codec_init(FlStringCodec* self) {}
54+
55+
G_MODULE_EXPORT FlStringCodec* fl_string_codec_new() {
56+
return static_cast<FlStringCodec*>(
57+
g_object_new(fl_string_codec_get_type(), nullptr));
58+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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 "gtest/gtest.h"
7+
8+
static uint8_t hex_digit_to_int(char value) {
9+
if (value >= '0' && value <= '9')
10+
return value - '0';
11+
else if (value >= 'a' && value <= 'f')
12+
return value - 'a' + 10;
13+
else if (value >= 'F' && value <= 'F')
14+
return value - 'A' + 10;
15+
else
16+
return 0;
17+
}
18+
19+
static uint8_t parse_hex8(const gchar* hex_string) {
20+
if (hex_string[0] == '\0')
21+
return 0x00;
22+
return hex_digit_to_int(hex_string[0]) << 4 | hex_digit_to_int(hex_string[1]);
23+
}
24+
25+
static GBytes* hex_string_to_bytes(const gchar* hex_string) {
26+
GByteArray* buffer = g_byte_array_new();
27+
for (int i = 0; hex_string[i] != '\0' && hex_string[i + 1] != '\0'; i += 2) {
28+
uint8_t value = parse_hex8(hex_string + i);
29+
g_byte_array_append(buffer, &value, 1);
30+
}
31+
return g_byte_array_free_to_bytes(buffer);
32+
}
33+
34+
static gchar* encode_value(FlValue* value) {
35+
g_autoptr(FlStringCodec) codec = fl_string_codec_new();
36+
g_autoptr(GByteArray) buffer = g_byte_array_new();
37+
g_autoptr(GError) error = nullptr;
38+
gboolean result =
39+
fl_codec_write_value(FL_CODEC(codec), buffer, value, &error);
40+
EXPECT_TRUE(result);
41+
EXPECT_EQ(error, nullptr);
42+
43+
GString* hex_string = g_string_new("");
44+
for (guint i = 0; i < buffer->len; i++)
45+
g_string_append_printf(hex_string, "%02x", buffer->data[i]);
46+
return g_string_free(hex_string, FALSE);
47+
}
48+
49+
static void encode_value_error(FlValue* value, GQuark domain, int code) {
50+
g_autoptr(FlStringCodec) codec = fl_string_codec_new();
51+
g_autoptr(GByteArray) buffer = g_byte_array_new();
52+
g_autoptr(GError) error = nullptr;
53+
gboolean result =
54+
fl_codec_write_value(FL_CODEC(codec), buffer, value, &error);
55+
EXPECT_FALSE(result);
56+
EXPECT_TRUE(g_error_matches(error, domain, code));
57+
EXPECT_EQ(buffer->len, static_cast<guint>(0));
58+
}
59+
60+
static FlValue* decode_value(const char* hex_string) {
61+
g_autoptr(FlStringCodec) codec = fl_string_codec_new();
62+
g_autoptr(GBytes) data = hex_string_to_bytes(hex_string);
63+
size_t offset = 0;
64+
g_autoptr(GError) error = nullptr;
65+
g_autoptr(FlValue) value =
66+
fl_codec_read_value(FL_CODEC(codec), data, &offset, &error);
67+
EXPECT_EQ(offset, g_bytes_get_size(data));
68+
EXPECT_EQ(error, nullptr);
69+
EXPECT_NE(value, nullptr);
70+
return fl_value_ref(value);
71+
}
72+
73+
TEST(FlStringCodecTest, EncodeData) {
74+
g_autoptr(FlValue) value = fl_value_string_new("hello");
75+
g_autofree gchar* hex_string = encode_value(value);
76+
EXPECT_STREQ(hex_string, "68656c6c6f");
77+
}
78+
79+
TEST(FlStringCodecTest, EncodeEmpty) {
80+
g_autoptr(FlValue) value = fl_value_string_new("");
81+
g_autofree gchar* hex_string = encode_value(value);
82+
EXPECT_STREQ(hex_string, "");
83+
}
84+
85+
TEST(FlStringCodecTest, EncodeNULL) {
86+
encode_value_error(nullptr, FL_CODEC_ERROR, FL_CODEC_ERROR_UNSUPPORTED_TYPE);
87+
}
88+
89+
TEST(FlStringCodecTest, EncodeUnknownType) {
90+
g_autoptr(FlValue) value = fl_value_null_new();
91+
encode_value_error(value, FL_CODEC_ERROR, FL_CODEC_ERROR_UNSUPPORTED_TYPE);
92+
}
93+
94+
TEST(FlStringCodecTest, DecodeData) {
95+
g_autoptr(FlValue) value = decode_value("68656c6c6f");
96+
ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_STRING);
97+
ASSERT_STREQ(fl_value_get_string(value), "hello");
98+
}
99+
100+
TEST(FlStringCodecTest, DecodeEmpty) {
101+
g_autoptr(FlValue) value = decode_value("");
102+
ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_STRING);
103+
ASSERT_STREQ(fl_value_get_string(value), "");
104+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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_codec.h"
13+
14+
G_BEGIN_DECLS
15+
16+
G_DECLARE_FINAL_TYPE(FlStringCodec, fl_string_codec, FL, STRING_CODEC, FlCodec)
17+
18+
/**
19+
* FlStringCodec:
20+
*
21+
* #FlStringCodec is a #FlCodec that implements the Flutter string message
22+
* encoding.
23+
*/
24+
25+
FlStringCodec* fl_string_codec_new();
26+
27+
G_END_DECLS
28+
29+
#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_codec.h>
1313
#include <flutter_linux/fl_dart_project.h>
1414
#include <flutter_linux/fl_engine.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)