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

Commit b39f606

Browse files
committed
Add FlBinaryCodec
1 parent 6f214c7 commit b39f606

File tree

6 files changed

+209
-0
lines changed

6 files changed

+209
-0
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,8 @@ FILE: ../../../flutter/shell/platform/glfw/platform_handler.h
11721172
FILE: ../../../flutter/shell/platform/glfw/public/flutter_glfw.h
11731173
FILE: ../../../flutter/shell/platform/glfw/text_input_plugin.cc
11741174
FILE: ../../../flutter/shell/platform/glfw/text_input_plugin.h
1175+
FILE: ../../../flutter/shell/platform/linux/fl_binary_codec.cc
1176+
FILE: ../../../flutter/shell/platform/linux/fl_binary_codec_test.cc
11751177
FILE: ../../../flutter/shell/platform/linux/fl_binary_messenger.cc
11761178
FILE: ../../../flutter/shell/platform/linux/fl_binary_messenger_private.h
11771179
FILE: ../../../flutter/shell/platform/linux/fl_codec.cc
@@ -1187,6 +1189,7 @@ FILE: ../../../flutter/shell/platform/linux/fl_renderer_x11.h
11871189
FILE: ../../../flutter/shell/platform/linux/fl_value.cc
11881190
FILE: ../../../flutter/shell/platform/linux/fl_value_test.cc
11891191
FILE: ../../../flutter/shell/platform/linux/fl_view.cc
1192+
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_binary_codec.h
11901193
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_binary_messenger.h
11911194
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_codec.h
11921195
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_dart_project.h

shell/platform/linux/BUILD.gn

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ if (build_glfw_shell) {
4444
}
4545

4646
_public_headers = [
47+
"public/flutter_linux/fl_binary_codec.h",
4748
"public/flutter_linux/fl_binary_messenger.h",
4849
"public/flutter_linux/fl_dart_project.h",
4950
"public/flutter_linux/fl_codec.h",
@@ -61,6 +62,7 @@ source_set("flutter_linux") {
6162
public = _public_headers
6263

6364
sources = [
65+
"fl_binary_codec.cc",
6466
"fl_binary_messenger.cc",
6567
"fl_codec.cc",
6668
"fl_dart_project.cc",
@@ -93,6 +95,7 @@ executable("flutter_linux_unittests") {
9395
testonly = true
9496

9597
sources = [
98+
"fl_binary_codec_test.cc",
9699
"fl_codec_test.cc",
97100
"fl_dart_project_test.cc",
98101
"fl_value_test.cc",
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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_binary_codec.h"
6+
7+
#include <gmodule.h>
8+
9+
G_DEFINE_QUARK(fl_binary_codec_error_quark, fl_binary_codec_error)
10+
11+
struct _FlBinaryCodec {
12+
FlCodec parent_instance;
13+
};
14+
15+
G_DEFINE_TYPE(FlBinaryCodec, fl_binary_codec, fl_codec_get_type())
16+
17+
static gboolean fl_binary_codec_write_value(FlCodec* codec,
18+
GByteArray* buffer,
19+
FlValue* value,
20+
GError** error) {
21+
if (fl_value_get_type(value) != FL_VALUE_TYPE_UINT8_LIST) {
22+
g_set_error(error, FL_CODEC_ERROR, FL_CODEC_ERROR_UNSUPPORTED_TYPE,
23+
"Only uint8[] values supported");
24+
return FALSE;
25+
}
26+
27+
g_byte_array_append(buffer, fl_value_get_uint8_list(value),
28+
fl_value_get_length(value));
29+
30+
return TRUE;
31+
}
32+
33+
static FlValue* fl_binary_codec_read_value(FlCodec* codec,
34+
GBytes* message,
35+
size_t* offset,
36+
GError** error) {
37+
gsize data_length;
38+
const uint8_t* data =
39+
static_cast<const uint8_t*>(g_bytes_get_data(message, &data_length));
40+
g_autoptr(FlValue) value =
41+
fl_value_uint8_list_new(data + *offset, data_length - *offset);
42+
*offset = data_length;
43+
44+
return fl_value_ref(value);
45+
}
46+
47+
static void fl_binary_codec_class_init(FlBinaryCodecClass* klass) {
48+
FL_CODEC_CLASS(klass)->write_value = fl_binary_codec_write_value;
49+
FL_CODEC_CLASS(klass)->read_value = fl_binary_codec_read_value;
50+
}
51+
52+
static void fl_binary_codec_init(FlBinaryCodec* self) {}
53+
54+
G_MODULE_EXPORT FlBinaryCodec* fl_binary_codec_new() {
55+
return static_cast<FlBinaryCodec*>(
56+
g_object_new(fl_binary_codec_get_type(), nullptr));
57+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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_binary_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(FlBinaryCodec) codec = fl_binary_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(FlBinaryCodec) codec = fl_binary_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(FlBinaryCodec) codec = fl_binary_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(FlBinaryCodecTest, EncodeData) {
74+
uint8_t data[] = {0x00, 0x01, 0x02, 0xFD, 0xFE, 0xFF};
75+
g_autoptr(FlValue) value = fl_value_uint8_list_new(data, 6);
76+
g_autofree gchar* hex_string = encode_value(value);
77+
EXPECT_STREQ(hex_string, "000102fdfeff");
78+
}
79+
80+
TEST(FlBinaryCodecTest, EncodeEmpty) {
81+
g_autoptr(FlValue) value = fl_value_uint8_list_new(nullptr, 0);
82+
g_autofree gchar* hex_string = encode_value(value);
83+
EXPECT_STREQ(hex_string, "");
84+
}
85+
86+
TEST(FlBinaryCodecTest, EncodeNULL) {
87+
encode_value_error(nullptr, FL_CODEC_ERROR, FL_CODEC_ERROR_UNSUPPORTED_TYPE);
88+
}
89+
90+
TEST(FlBinaryCodecTest, EncodeUnknownType) {
91+
g_autoptr(FlValue) value = fl_value_null_new();
92+
encode_value_error(value, FL_CODEC_ERROR, FL_CODEC_ERROR_UNSUPPORTED_TYPE);
93+
}
94+
95+
TEST(FlBinaryCodecTest, DecodeData) {
96+
g_autoptr(FlValue) value = decode_value("000102fdfeff");
97+
ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_UINT8_LIST);
98+
ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(6));
99+
EXPECT_EQ(fl_value_get_uint8_list(value)[0], 0x00);
100+
EXPECT_EQ(fl_value_get_uint8_list(value)[1], 0x01);
101+
EXPECT_EQ(fl_value_get_uint8_list(value)[2], 0x02);
102+
EXPECT_EQ(fl_value_get_uint8_list(value)[3], 0xFD);
103+
EXPECT_EQ(fl_value_get_uint8_list(value)[4], 0xFE);
104+
EXPECT_EQ(fl_value_get_uint8_list(value)[5], 0xFF);
105+
}
106+
107+
TEST(FlBinaryCodecTest, DecodeEmpty) {
108+
g_autoptr(FlValue) value = decode_value("");
109+
ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_UINT8_LIST);
110+
ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
111+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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_BINARY_CODEC_H_
6+
#define FLUTTER_SHELL_PLATFORM_LINUX_FL_BINARY_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(FlBinaryCodec,
17+
fl_standard_codec,
18+
FL,
19+
BINARY_CODEC,
20+
FlCodec)
21+
22+
/**
23+
* FlBinaryCodec:
24+
*
25+
* #FlBinaryCodec is a #FlCodec that implements the Flutter binary message
26+
* encoding. This encodes and decodes #FlValue of type
27+
* #FL_VALUE_TYPE_UINT8_LIST.
28+
*/
29+
30+
FlBinaryCodec* fl_binary_codec_new();
31+
32+
G_END_DECLS
33+
34+
#endif // FLUTTER_SHELL_PLATFORM_LINUX_FL_BINARY_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
@@ -7,6 +7,7 @@
77

88
#define __FLUTTER_LINUX_INSIDE__
99

10+
#include <flutter_linux/fl_binary_codec.h>
1011
#include <flutter_linux/fl_binary_messenger.h>
1112
#include <flutter_linux/fl_codec.h>
1213
#include <flutter_linux/fl_dart_project.h>

0 commit comments

Comments
 (0)