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

Commit f477ea2

Browse files
committed
Add FlBinaryCodec
1 parent 7fef87f commit f477ea2

File tree

8 files changed

+275
-0
lines changed

8 files changed

+275
-0
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,8 @@ FILE: ../../../flutter/shell/platform/glfw/platform_handler.h
11841184
FILE: ../../../flutter/shell/platform/glfw/public/flutter_glfw.h
11851185
FILE: ../../../flutter/shell/platform/glfw/text_input_plugin.cc
11861186
FILE: ../../../flutter/shell/platform/glfw/text_input_plugin.h
1187+
FILE: ../../../flutter/shell/platform/linux/fl_binary_codec.cc
1188+
FILE: ../../../flutter/shell/platform/linux/fl_binary_codec_test.cc
11871189
FILE: ../../../flutter/shell/platform/linux/fl_binary_messenger.cc
11881190
FILE: ../../../flutter/shell/platform/linux/fl_binary_messenger_private.h
11891191
FILE: ../../../flutter/shell/platform/linux/fl_dart_project.cc
@@ -1199,13 +1201,16 @@ FILE: ../../../flutter/shell/platform/linux/fl_renderer_x11.h
11991201
FILE: ../../../flutter/shell/platform/linux/fl_value.cc
12001202
FILE: ../../../flutter/shell/platform/linux/fl_value_test.cc
12011203
FILE: ../../../flutter/shell/platform/linux/fl_view.cc
1204+
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_binary_codec.h
12021205
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_binary_messenger.h
12031206
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_dart_project.h
12041207
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_engine.h
12051208
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_message_codec.h
12061209
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_value.h
12071210
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_view.h
12081211
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/flutter_linux.h
1212+
FILE: ../../../flutter/shell/platform/linux/testing/fl_test.cc
1213+
FILE: ../../../flutter/shell/platform/linux/testing/fl_test.h
12091214
FILE: ../../../flutter/shell/platform/windows/angle_surface_manager.cc
12101215
FILE: ../../../flutter/shell/platform/windows/angle_surface_manager.h
12111216
FILE: ../../../flutter/shell/platform/windows/client_wrapper/dart_project_unittests.cc

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,9 +95,11 @@ 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",
98101
"fl_value_test.cc",
102+
"testing/fl_test.cc",
99103
]
100104

101105
public_configs = [ "//flutter:config" ]
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_new_uint8_list(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: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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/testing/fl_test.h"
7+
#include "gtest/gtest.h"
8+
9+
// Encode a message using a FlBinaryCodec. Return a hex string with the encoded
10+
// binary output.
11+
static gchar* encode_message(FlValue* value) {
12+
g_autoptr(FlBinaryCodec) codec = fl_binary_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 FlBinaryCodec. Expect the given error.
23+
static void encode_message_error(FlValue* value, GQuark domain, int code) {
24+
g_autoptr(FlBinaryCodec) codec = fl_binary_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 FlBinaryCodec. 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(FlBinaryCodec) codec = fl_binary_codec_new();
36+
g_autoptr(GBytes) data = 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), data, &error);
40+
EXPECT_EQ(error, nullptr);
41+
EXPECT_NE(value, nullptr);
42+
return fl_value_ref(value);
43+
}
44+
45+
TEST(FlBinaryCodecTest, EncodeData) {
46+
uint8_t data[] = {0x00, 0x01, 0x02, 0xFD, 0xFE, 0xFF};
47+
g_autoptr(FlValue) value = fl_value_new_uint8_list(data, 6);
48+
g_autofree gchar* hex_string = encode_message(value);
49+
EXPECT_STREQ(hex_string, "000102fdfeff");
50+
}
51+
52+
TEST(FlBinaryCodecTest, EncodeEmpty) {
53+
g_autoptr(FlValue) value = fl_value_new_uint8_list(nullptr, 0);
54+
g_autofree gchar* hex_string = encode_message(value);
55+
EXPECT_STREQ(hex_string, "");
56+
}
57+
58+
TEST(FlBinaryCodecTest, EncodeNULL) {
59+
encode_message_error(nullptr, FL_MESSAGE_CODEC_ERROR,
60+
FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE);
61+
}
62+
63+
TEST(FlBinaryCodecTest, EncodeUnknownType) {
64+
g_autoptr(FlValue) value = fl_value_new_null();
65+
encode_message_error(value, FL_MESSAGE_CODEC_ERROR,
66+
FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE);
67+
}
68+
69+
TEST(FlBinaryCodecTest, DecodeData) {
70+
g_autoptr(FlValue) value = decode_message("000102fdfeff");
71+
ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_UINT8_LIST);
72+
ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(6));
73+
EXPECT_EQ(fl_value_get_uint8_list(value)[0], 0x00);
74+
EXPECT_EQ(fl_value_get_uint8_list(value)[1], 0x01);
75+
EXPECT_EQ(fl_value_get_uint8_list(value)[2], 0x02);
76+
EXPECT_EQ(fl_value_get_uint8_list(value)[3], 0xFD);
77+
EXPECT_EQ(fl_value_get_uint8_list(value)[4], 0xFE);
78+
EXPECT_EQ(fl_value_get_uint8_list(value)[5], 0xFF);
79+
}
80+
81+
TEST(FlBinaryCodecTest, DecodeEmpty) {
82+
g_autoptr(FlValue) value = decode_message("");
83+
ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_UINT8_LIST);
84+
ASSERT_EQ(fl_value_get_length(value), static_cast<size_t>(0));
85+
}
86+
87+
TEST(FlBinaryCodecTest, EncodeDecode) {
88+
g_autoptr(FlBinaryCodec) codec = fl_binary_codec_new();
89+
90+
uint8_t data[] = {0x00, 0x01, 0x02, 0xFD, 0xFE, 0xFF};
91+
g_autoptr(FlValue) input = fl_value_new_uint8_list(data, 6);
92+
93+
g_autoptr(GError) error = nullptr;
94+
g_autoptr(GBytes) message =
95+
fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), input, &error);
96+
EXPECT_NE(message, nullptr);
97+
EXPECT_EQ(error, nullptr);
98+
99+
g_autoptr(FlValue) output =
100+
fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), message, &error);
101+
EXPECT_EQ(error, nullptr);
102+
EXPECT_NE(output, nullptr);
103+
104+
ASSERT_TRUE(fl_value_equal(input, output));
105+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
* #FlBinaryCodec matches the BinaryCodec class in the Flutter services
30+
* library.
31+
*/
32+
33+
/**
34+
* fl_binary_codec_new:
35+
*
36+
* Creates a #FlBinaryCodec.
37+
*
38+
* Returns: a new #FlBinaryCodec.
39+
*/
40+
FlBinaryCodec* fl_binary_codec_new();
41+
42+
G_END_DECLS
43+
44+
#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>
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/testing/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+
}
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_

0 commit comments

Comments
 (0)