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

Commit 21a1be9

Browse files
committed
Add FlMethodChannel
1 parent 3b69942 commit 21a1be9

20 files changed

+4680
-0
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,18 +1173,35 @@ FILE: ../../../flutter/shell/platform/glfw/text_input_plugin.cc
11731173
FILE: ../../../flutter/shell/platform/glfw/text_input_plugin.h
11741174
FILE: ../../../flutter/shell/platform/linux/fl_binary_messenger.cc
11751175
FILE: ../../../flutter/shell/platform/linux/fl_binary_messenger_private.h
1176+
FILE: ../../../flutter/shell/platform/linux/fl_codec.cc
1177+
FILE: ../../../flutter/shell/platform/linux/fl_codec_test.cc
11761178
FILE: ../../../flutter/shell/platform/linux/fl_dart_project.cc
11771179
FILE: ../../../flutter/shell/platform/linux/fl_dart_project_test.cc
11781180
FILE: ../../../flutter/shell/platform/linux/fl_engine.cc
11791181
FILE: ../../../flutter/shell/platform/linux/fl_engine_private.h
1182+
FILE: ../../../flutter/shell/platform/linux/fl_method_channel.cc
1183+
FILE: ../../../flutter/shell/platform/linux/fl_method_codec.cc
1184+
FILE: ../../../flutter/shell/platform/linux/fl_method_codec_private.h
1185+
FILE: ../../../flutter/shell/platform/linux/fl_method_codec_test.cc
11801186
FILE: ../../../flutter/shell/platform/linux/fl_renderer.cc
11811187
FILE: ../../../flutter/shell/platform/linux/fl_renderer.h
11821188
FILE: ../../../flutter/shell/platform/linux/fl_renderer_x11.cc
11831189
FILE: ../../../flutter/shell/platform/linux/fl_renderer_x11.h
1190+
FILE: ../../../flutter/shell/platform/linux/fl_standard_codec.cc
1191+
FILE: ../../../flutter/shell/platform/linux/fl_standard_codec_test.cc
1192+
FILE: ../../../flutter/shell/platform/linux/fl_standard_method_codec.cc
1193+
FILE: ../../../flutter/shell/platform/linux/fl_standard_method_codec_test.cc
1194+
FILE: ../../../flutter/shell/platform/linux/fl_value.cc
11841195
FILE: ../../../flutter/shell/platform/linux/fl_view.cc
11851196
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_binary_messenger.h
1197+
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_codec.h
11861198
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_dart_project.h
11871199
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_engine.h
1200+
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_method_channel.h
1201+
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_method_codec.h
1202+
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_standard_codec.h
1203+
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_standard_method_codec.h
1204+
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_value.h
11881205
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/fl_view.h
11891206
FILE: ../../../flutter/shell/platform/linux/public/flutter_linux/flutter_linux.h
11901207
FILE: ../../../flutter/shell/platform/windows/angle_surface_manager.cc

shell/platform/linux/BUILD.gn

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,13 @@ if (build_glfw_shell) {
4646
_public_headers = [
4747
"public/flutter_linux/fl_binary_messenger.h",
4848
"public/flutter_linux/fl_dart_project.h",
49+
"public/flutter_linux/fl_codec.h",
4950
"public/flutter_linux/fl_engine.h",
51+
"public/flutter_linux/fl_method_channel.h",
52+
"public/flutter_linux/fl_method_codec.h",
53+
"public/flutter_linux/fl_standard_codec.h",
54+
"public/flutter_linux/fl_standard_method_codec.h",
55+
"public/flutter_linux/fl_value.h",
5056
"public/flutter_linux/fl_view.h",
5157
"public/flutter_linux/flutter_linux.h",
5258
]
@@ -60,10 +66,16 @@ source_set("flutter_linux") {
6066

6167
sources = [
6268
"fl_binary_messenger.cc",
69+
"fl_codec.cc",
6370
"fl_dart_project.cc",
6471
"fl_engine.cc",
72+
"fl_method_channel.cc",
73+
"fl_method_codec.cc",
6574
"fl_renderer.cc",
6675
"fl_renderer_x11.cc",
76+
"fl_standard_codec.cc",
77+
"fl_standard_method_codec.cc",
78+
"fl_value.cc",
6779
"fl_view.cc",
6880
]
6981

@@ -89,7 +101,11 @@ executable("flutter_linux_unittests") {
89101
testonly = true
90102

91103
sources = [
104+
"fl_codec_test.cc",
92105
"fl_dart_project_test.cc",
106+
"fl_method_codec_test.cc",
107+
"fl_standard_codec_test.cc",
108+
"fl_standard_method_codec_test.cc",
93109
]
94110

95111
public_configs = [ "//flutter:config" ]

shell/platform/linux/fl_codec.cc

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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_codec.h"
6+
7+
#include <gmodule.h>
8+
9+
G_DEFINE_QUARK(fl_codec_error_quark, fl_codec_error)
10+
11+
// Added here to stop the compiler from optimising this function away
12+
G_MODULE_EXPORT GType fl_codec_get_type();
13+
14+
G_DEFINE_TYPE(FlCodec, fl_codec, G_TYPE_OBJECT)
15+
16+
static void fl_codec_class_init(FlCodecClass* klass) {}
17+
18+
static void fl_codec_init(FlCodec* self) {}
19+
20+
G_MODULE_EXPORT gboolean fl_codec_write_value(FlCodec* self,
21+
GByteArray* buffer,
22+
FlValue* value,
23+
GError** error) {
24+
g_return_val_if_fail(FL_IS_CODEC(self), FALSE);
25+
g_return_val_if_fail(buffer != nullptr, FALSE);
26+
27+
g_autoptr(FlValue) null_value = NULL;
28+
if (value == nullptr)
29+
value = null_value = fl_value_null_new();
30+
31+
return FL_CODEC_GET_CLASS(self)->write_value(self, buffer, value, error);
32+
}
33+
34+
G_MODULE_EXPORT FlValue* fl_codec_read_value(FlCodec* self,
35+
GBytes* buffer,
36+
size_t* offset,
37+
GError** error) {
38+
g_return_val_if_fail(FL_IS_CODEC(self), nullptr);
39+
g_return_val_if_fail(buffer != nullptr, nullptr);
40+
41+
size_t o = offset != nullptr ? *offset : 0;
42+
if (o >= g_bytes_get_size(buffer)) {
43+
g_set_error(error, FL_CODEC_ERROR, FL_CODEC_ERROR_OUT_OF_DATA,
44+
"Out of data");
45+
return NULL;
46+
}
47+
48+
FlValue* value =
49+
FL_CODEC_GET_CLASS(self)->read_value(self, buffer, &o, error);
50+
if (value != nullptr && offset != nullptr)
51+
*offset = o;
52+
return value;
53+
}
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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_codec.h"
6+
#include "gtest/gtest.h"
7+
8+
G_DECLARE_FINAL_TYPE(FlTestCodec, fl_test_codec, FL, TEST_CODEC, FlCodec)
9+
10+
struct _FlTestCodec {
11+
FlCodec parent_instance;
12+
};
13+
14+
G_DEFINE_TYPE(FlTestCodec, fl_test_codec, fl_codec_get_type())
15+
16+
static gboolean fl_test_codec_write_value(FlCodec* codec,
17+
GByteArray* buffer,
18+
FlValue* value,
19+
GError** error) {
20+
EXPECT_TRUE(FL_IS_TEST_CODEC(codec));
21+
22+
if (fl_value_get_type(value) == FL_VALUE_TYPE_INT) {
23+
char c = '0' + fl_value_get_int(value);
24+
g_byte_array_append(buffer, reinterpret_cast<const guint8*>(&c), 1);
25+
return TRUE;
26+
} else {
27+
g_set_error(error, FL_CODEC_ERROR, FL_CODEC_ERROR_FAILED, "ERROR");
28+
return FALSE;
29+
}
30+
}
31+
32+
static FlValue* fl_test_codec_read_value(FlCodec* codec,
33+
GBytes* message,
34+
size_t* offset,
35+
GError** error) {
36+
EXPECT_TRUE(FL_IS_TEST_CODEC(codec));
37+
EXPECT_TRUE(*offset < g_bytes_get_size(message));
38+
39+
size_t data_length;
40+
const uint8_t* data =
41+
static_cast<const uint8_t*>(g_bytes_get_data(message, &data_length));
42+
if (data_length == 0) {
43+
g_set_error(error, FL_CODEC_ERROR, FL_CODEC_ERROR_FAILED, "ERROR");
44+
return FALSE;
45+
}
46+
47+
g_autoptr(FlValue) value = fl_value_int_new(data[*offset] - '0');
48+
(*offset)++;
49+
return fl_value_ref(value);
50+
}
51+
52+
static void fl_test_codec_class_init(FlTestCodecClass* klass) {
53+
FL_CODEC_CLASS(klass)->write_value = fl_test_codec_write_value;
54+
FL_CODEC_CLASS(klass)->read_value = fl_test_codec_read_value;
55+
}
56+
57+
static void fl_test_codec_init(FlTestCodec* self) {}
58+
59+
static FlTestCodec* fl_test_codec_new() {
60+
return FL_TEST_CODEC(g_object_new(fl_test_codec_get_type(), nullptr));
61+
}
62+
63+
TEST(FlCodecTest, WriteValue) {
64+
g_autoptr(FlTestCodec) codec = fl_test_codec_new();
65+
g_autoptr(GByteArray) buffer = g_byte_array_new();
66+
67+
g_autoptr(FlValue) value = fl_value_int_new(1);
68+
g_autoptr(GError) error = nullptr;
69+
gboolean result =
70+
fl_codec_write_value(FL_CODEC(codec), buffer, value, &error);
71+
EXPECT_TRUE(result);
72+
EXPECT_EQ(error, nullptr);
73+
EXPECT_EQ(buffer->len, static_cast<unsigned int>(1));
74+
EXPECT_EQ(buffer->data[0], '1');
75+
}
76+
77+
TEST(FlCodecTest, WriteValues) {
78+
g_autoptr(FlTestCodec) codec = fl_test_codec_new();
79+
g_autoptr(GByteArray) buffer = g_byte_array_new();
80+
81+
for (int i = 1; i <= 5; i++) {
82+
g_autoptr(FlValue) value = fl_value_int_new(i);
83+
g_autoptr(GError) error = nullptr;
84+
gboolean result =
85+
fl_codec_write_value(FL_CODEC(codec), buffer, value, &error);
86+
EXPECT_TRUE(result);
87+
EXPECT_EQ(error, nullptr);
88+
}
89+
90+
EXPECT_EQ(buffer->len, static_cast<unsigned int>(5));
91+
for (int i = 1; i <= 5; i++)
92+
EXPECT_EQ(buffer->data[i - 1], '0' + i);
93+
}
94+
95+
TEST(FlCodecTest, WriteValueError) {
96+
g_autoptr(FlTestCodec) codec = fl_test_codec_new();
97+
g_autoptr(GByteArray) buffer = g_byte_array_new();
98+
99+
g_autoptr(FlValue) value = fl_value_null_new();
100+
g_autoptr(GError) error = nullptr;
101+
gboolean result =
102+
fl_codec_write_value(FL_CODEC(codec), buffer, value, &error);
103+
EXPECT_FALSE(result);
104+
EXPECT_TRUE(g_error_matches(error, FL_CODEC_ERROR, FL_CODEC_ERROR_FAILED));
105+
EXPECT_EQ(buffer->len, static_cast<unsigned int>(0));
106+
}
107+
108+
TEST(FlCodecTest, ReadValueEmpty) {
109+
g_autoptr(FlTestCodec) codec = fl_test_codec_new();
110+
g_autoptr(GBytes) message = g_bytes_new(nullptr, 0);
111+
112+
size_t offset = 0;
113+
g_autoptr(GError) error = nullptr;
114+
g_autoptr(FlValue) value =
115+
fl_codec_read_value(FL_CODEC(codec), message, &offset, &error);
116+
EXPECT_EQ(value, nullptr);
117+
EXPECT_TRUE(
118+
g_error_matches(error, FL_CODEC_ERROR, FL_CODEC_ERROR_OUT_OF_DATA));
119+
EXPECT_EQ(offset, static_cast<size_t>(0));
120+
}
121+
122+
TEST(FlCodecTest, ReadValue) {
123+
g_autoptr(FlTestCodec) codec = fl_test_codec_new();
124+
uint8_t data[] = {'1'};
125+
g_autoptr(GBytes) message = g_bytes_new(data, 1);
126+
127+
size_t offset = 0;
128+
g_autoptr(GError) error = nullptr;
129+
g_autoptr(FlValue) value =
130+
fl_codec_read_value(FL_CODEC(codec), message, &offset, &error);
131+
EXPECT_NE(value, nullptr);
132+
EXPECT_EQ(error, nullptr);
133+
EXPECT_EQ(offset, static_cast<size_t>(1));
134+
135+
ASSERT_TRUE(fl_value_get_type(value) == FL_VALUE_TYPE_INT);
136+
EXPECT_EQ(fl_value_get_int(value), 1);
137+
}
138+
139+
TEST(FlCodecTest, ReadValues) {
140+
g_autoptr(FlTestCodec) codec = fl_test_codec_new();
141+
uint8_t data[] = {'1', '2', '3', '4', '5'};
142+
g_autoptr(GBytes) message = g_bytes_new(data, 5);
143+
144+
size_t offset = 0;
145+
for (int i = 1; i <= 5; i++) {
146+
g_autoptr(GError) error = nullptr;
147+
g_autoptr(FlValue) value =
148+
fl_codec_read_value(FL_CODEC(codec), message, &offset, &error);
149+
EXPECT_NE(value, nullptr);
150+
EXPECT_EQ(error, nullptr);
151+
ASSERT_TRUE(fl_value_get_type(value) == FL_VALUE_TYPE_INT);
152+
EXPECT_EQ(fl_value_get_int(value), i);
153+
}
154+
155+
EXPECT_EQ(offset, static_cast<size_t>(5));
156+
}
157+
158+
TEST(FlCodecTest, ReadValueNullOffset) {
159+
g_autoptr(FlTestCodec) codec = fl_test_codec_new();
160+
uint8_t data[] = {'1'};
161+
g_autoptr(GBytes) message = g_bytes_new(data, 1);
162+
163+
g_autoptr(GError) error = nullptr;
164+
g_autoptr(FlValue) value =
165+
fl_codec_read_value(FL_CODEC(codec), message, NULL, &error);
166+
EXPECT_NE(value, nullptr);
167+
EXPECT_EQ(error, nullptr);
168+
169+
ASSERT_TRUE(fl_value_get_type(value) == FL_VALUE_TYPE_INT);
170+
EXPECT_EQ(fl_value_get_int(value), 1);
171+
}
172+
173+
TEST(FlCodecTest, ReadValueInvalidOffset) {
174+
g_autoptr(FlTestCodec) codec = fl_test_codec_new();
175+
uint8_t data[] = {'1', '2', '3', '4', '5'};
176+
g_autoptr(GBytes) message = g_bytes_new(data, 5);
177+
178+
size_t offset = 9999;
179+
g_autoptr(GError) error = nullptr;
180+
g_autoptr(FlValue) value =
181+
fl_codec_read_value(FL_CODEC(codec), message, &offset, &error);
182+
EXPECT_EQ(value, nullptr);
183+
EXPECT_TRUE(
184+
g_error_matches(error, FL_CODEC_ERROR, FL_CODEC_ERROR_OUT_OF_DATA));
185+
EXPECT_EQ(offset, static_cast<size_t>(9999));
186+
}

0 commit comments

Comments
 (0)