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

Commit 4a4a7af

Browse files
committed
Add FlBinaryCodec
1 parent fc2ae8f commit 4a4a7af

File tree

8 files changed

+240
-0
lines changed

8 files changed

+240
-0
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 5 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_dart_project.cc
@@ -1184,9 +1186,12 @@ FILE: ../../../flutter/shell/platform/linux/fl_renderer.cc
11841186
FILE: ../../../flutter/shell/platform/linux/fl_renderer.h
11851187
FILE: ../../../flutter/shell/platform/linux/fl_renderer_x11.cc
11861188
FILE: ../../../flutter/shell/platform/linux/fl_renderer_x11.h
1189+
FILE: ../../../flutter/shell/platform/linux/fl_test.cc
1190+
FILE: ../../../flutter/shell/platform/linux/fl_test.h
11871191
FILE: ../../../flutter/shell/platform/linux/fl_value.cc
11881192
FILE: ../../../flutter/shell/platform/linux/fl_value_test.cc
11891193
FILE: ../../../flutter/shell/platform/linux/fl_view.cc
1194+
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_binary_codec.h
11901195
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_binary_messenger.h
11911196
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_dart_project.h
11921197
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_engine.h

shell/platform/linux/BUILD.gn

Lines changed: 4 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_engine.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_dart_project.cc",
6668
"fl_engine.cc",
@@ -93,8 +95,10 @@ executable("flutter_linux_unittests") {
9395
testonly = true
9496

9597
sources = [
98+
"fl_binary_codec_test.cc",
9699
"fl_dart_project_test.cc",
97100
"fl_message_codec_test.cc",
101+
"fl_test.cc",
98102
"fl_value_test.cc",
99103
]
100104

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_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+
FlMessageCodec parent_instance;
13+
};
14+
15+
G_DEFINE_TYPE(FlBinaryCodec, fl_binary_codec, fl_message_codec_get_type())
16+
17+
static GBytes* fl_binary_codec_encode_message(FlMessageCodec* codec,
18+
FlValue* value,
19+
GError** error) {
20+
if (fl_value_get_type(value) != FL_VALUE_TYPE_UINT8_LIST) {
21+
g_set_error(error, FL_MESSAGE_CODEC_ERROR,
22+
FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE,
23+
"Only uint8[] values supported");
24+
return nullptr;
25+
}
26+
27+
return g_bytes_new(fl_value_get_uint8_list(value),
28+
fl_value_get_length(value));
29+
}
30+
31+
static FlValue* fl_binary_codec_decode_message(FlMessageCodec* codec,
32+
GBytes* message,
33+
GError** error) {
34+
gsize data_length;
35+
const uint8_t* data =
36+
static_cast<const uint8_t*>(g_bytes_get_data(message, &data_length));
37+
return fl_value_uint8_list_new(data, data_length);
38+
}
39+
40+
static void fl_binary_codec_class_init(FlBinaryCodecClass* klass) {
41+
FL_MESSAGE_CODEC_CLASS(klass)->encode_message =
42+
fl_binary_codec_encode_message;
43+
FL_MESSAGE_CODEC_CLASS(klass)->decode_message =
44+
fl_binary_codec_decode_message;
45+
}
46+
47+
static void fl_binary_codec_init(FlBinaryCodec* self) {}
48+
49+
G_MODULE_EXPORT FlBinaryCodec* fl_binary_codec_new() {
50+
return static_cast<FlBinaryCodec*>(
51+
g_object_new(fl_binary_codec_get_type(), nullptr));
52+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 "flutter/shell/platform/linux/fl_test.h"
7+
#include "gtest/gtest.h"
8+
9+
static gchar* encode_message(FlValue* value) {
10+
g_autoptr(FlBinaryCodec) codec = fl_binary_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_message_error(FlValue* value, GQuark domain, int code) {
21+
g_autoptr(FlBinaryCodec) codec = fl_binary_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_message(const char* hex_string) {
30+
g_autoptr(FlBinaryCodec) codec = fl_binary_codec_new();
31+
g_autoptr(GBytes) data = 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), data, &error);
35+
EXPECT_EQ(error, nullptr);
36+
EXPECT_NE(value, nullptr);
37+
return fl_value_ref(value);
38+
}
39+
40+
TEST(FlBinaryCodecTest, EncodeData) {
41+
uint8_t data[] = {0x00, 0x01, 0x02, 0xFD, 0xFE, 0xFF};
42+
g_autoptr(FlValue) value = fl_value_uint8_list_new(data, 6);
43+
g_autofree gchar* hex_string = encode_message(value);
44+
EXPECT_STREQ(hex_string, "000102fdfeff");
45+
}
46+
47+
TEST(FlBinaryCodecTest, EncodeEmpty) {
48+
g_autoptr(FlValue) value = fl_value_uint8_list_new(nullptr, 0);
49+
g_autofree gchar* hex_string = encode_message(value);
50+
EXPECT_STREQ(hex_string, "");
51+
}
52+
53+
TEST(FlBinaryCodecTest, EncodeNULL) {
54+
encode_message_error(nullptr, FL_MESSAGE_CODEC_ERROR,
55+
FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE);
56+
}
57+
58+
TEST(FlBinaryCodecTest, EncodeUnknownType) {
59+
g_autoptr(FlValue) value = fl_value_null_new();
60+
encode_message_error(value, FL_MESSAGE_CODEC_ERROR,
61+
FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE);
62+
}
63+
64+
TEST(FlBinaryCodecTest, DecodeData) {
65+
g_autoptr(FlValue) value = decode_message("000102fdfeff");
66+
ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_UINT8_LIST);
67+
ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(6));
68+
EXPECT_EQ(fl_value_get_uint8_list(value)[0], 0x00);
69+
EXPECT_EQ(fl_value_get_uint8_list(value)[1], 0x01);
70+
EXPECT_EQ(fl_value_get_uint8_list(value)[2], 0x02);
71+
EXPECT_EQ(fl_value_get_uint8_list(value)[3], 0xFD);
72+
EXPECT_EQ(fl_value_get_uint8_list(value)[4], 0xFE);
73+
EXPECT_EQ(fl_value_get_uint8_list(value)[5], 0xFF);
74+
}
75+
76+
TEST(FlBinaryCodecTest, DecodeEmpty) {
77+
g_autoptr(FlValue) value = decode_message("");
78+
ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_UINT8_LIST);
79+
ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
80+
}

shell/platform/linux/fl_test.cc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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/fl_test.h"
6+
7+
static uint8_t hex_digit_to_int(char value) {
8+
if (value >= '0' && value <= '9')
9+
return value - '0';
10+
else if (value >= 'a' && value <= 'f')
11+
return value - 'a' + 10;
12+
else if (value >= 'F' && value <= 'F')
13+
return value - 'A' + 10;
14+
else
15+
return 0;
16+
}
17+
18+
static uint8_t parse_hex8(const gchar* hex_string) {
19+
if (hex_string[0] == '\0')
20+
return 0x00;
21+
return hex_digit_to_int(hex_string[0]) << 4 | hex_digit_to_int(hex_string[1]);
22+
}
23+
24+
GBytes* hex_string_to_bytes(const gchar* hex_string) {
25+
GByteArray* buffer = g_byte_array_new();
26+
for (int i = 0; hex_string[i] != '\0' && hex_string[i + 1] != '\0'; i += 2) {
27+
uint8_t value = parse_hex8(hex_string + i);
28+
g_byte_array_append(buffer, &value, 1);
29+
}
30+
return g_byte_array_free_to_bytes(buffer);
31+
}
32+
33+
gchar* bytes_to_hex_string(GBytes* bytes) {
34+
GString* hex_string = g_string_new("");
35+
size_t data_length;
36+
const uint8_t* data =
37+
static_cast<const uint8_t*>(g_bytes_get_data(bytes, &data_length));
38+
for (size_t i = 0; i < data_length; i++)
39+
g_string_append_printf(hex_string, "%02x", data[i]);
40+
return g_string_free(hex_string, FALSE);
41+
}

shell/platform/linux/fl_test.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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_TEST_H_
6+
#define FLUTTER_SHELL_PLATFORM_LINUX_FL_TEST_H_
7+
8+
#include <glib.h>
9+
#include <stdint.h>
10+
11+
G_BEGIN_DECLS
12+
13+
// Helper functions for the tests. This is not included in the shell library.
14+
15+
// Helper function to convert a hexadecimal string (e.g. "01feab") into GBytes
16+
GBytes* hex_string_to_bytes(const gchar* hex_string);
17+
18+
// Helper function to convert GBytes into a hexadecimal string (e.g. "01feab")
19+
gchar* bytes_to_hex_string(GBytes* bytes);
20+
21+
G_END_DECLS
22+
23+
#endif // FLUTTER_SHELL_PLATFORM_LINUX_FL_TEST_H_
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_message_codec.h"
13+
14+
G_BEGIN_DECLS
15+
16+
G_DECLARE_FINAL_TYPE(FlBinaryCodec,
17+
fl_standard_codec,
18+
FL,
19+
BINARY_CODEC,
20+
FlMessageCodec)
21+
22+
/**
23+
* FlBinaryCodec:
24+
*
25+
* #FlBinaryCodec is a #FlMessageCodec that implements the Flutter binary
26+
* message 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_dart_project.h>
1213
#include <flutter_linux/fl_engine.h>

0 commit comments

Comments
 (0)