Skip to content

Commit

Permalink
[pigeon] Make Linux type declarations public (#8040)
Browse files Browse the repository at this point in the history
Moves codec and HostAPI class declarations to the header to work around a glib bug. We could instead add an unnecessary call to the type check method, but we will need the codec to be public eventually anyway, and HostApi is conceptually a public class (it just isn't strictly necessary to declare it in the header due to the way abstract interfaces work in gobject) so may as well be declared publicly.

Part of flutter/flutter#153083
  • Loading branch information
stuartmorgan authored Nov 12, 2024
1 parent 3663103 commit 5e90ce2
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 41 deletions.
5 changes: 5 additions & 0 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 22.6.1

* [gobject] Moves class declarations to the header to work around a bug in some
versions of glib.

## 22.6.0

* [swift] Adds `includeErrorClass` to `SwiftOptions`.
Expand Down
9 changes: 0 additions & 9 deletions packages/pigeon/example/app/linux/messages.g.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,6 @@ pigeon_example_package_message_data_new_from_list(FlValue* values) {
return pigeon_example_package_message_data_new(name, description, code, data);
}

G_DECLARE_FINAL_TYPE(PigeonExamplePackageMessageCodec,
pigeon_example_package_message_codec,
PIGEON_EXAMPLE_PACKAGE, MESSAGE_CODEC,
FlStandardMessageCodec)

struct _PigeonExamplePackageMessageCodec {
FlStandardMessageCodec parent_instance;
};
Expand Down Expand Up @@ -468,10 +463,6 @@ pigeon_example_package_example_host_api_send_message_response_new_error(
return self;
}

G_DECLARE_FINAL_TYPE(PigeonExamplePackageExampleHostApi,
pigeon_example_package_example_host_api,
PIGEON_EXAMPLE_PACKAGE, EXAMPLE_HOST_API, GObject)

struct _PigeonExamplePackageExampleHostApi {
GObject parent_instance;

Expand Down
9 changes: 9 additions & 0 deletions packages/pigeon/example/app/linux/messages.g.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ PigeonExamplePackageCode pigeon_example_package_message_data_get_code(
FlValue* pigeon_example_package_message_data_get_data(
PigeonExamplePackageMessageData* object);

G_DECLARE_FINAL_TYPE(PigeonExamplePackageMessageCodec,
pigeon_example_package_message_codec,
PIGEON_EXAMPLE_PACKAGE, MESSAGE_CODEC,
FlStandardMessageCodec)

G_DECLARE_FINAL_TYPE(PigeonExamplePackageExampleHostApi,
pigeon_example_package_example_host_api,
PIGEON_EXAMPLE_PACKAGE, EXAMPLE_HOST_API, GObject)

G_DECLARE_FINAL_TYPE(PigeonExamplePackageExampleHostApiResponseHandle,
pigeon_example_package_example_host_api_response_handle,
PIGEON_EXAMPLE_PACKAGE, EXAMPLE_HOST_API_RESPONSE_HANDLE,
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/lib/generator_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import 'ast.dart';
/// The current version of pigeon.
///
/// This must match the version in pubspec.yaml.
const String pigeonVersion = '22.6.0';
const String pigeonVersion = '22.6.1';

/// Read all the content from [stdin] to a String.
String readStdin() {
Expand Down
30 changes: 17 additions & 13 deletions packages/pigeon/lib/gobject_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const DocumentCommentSpecification _docCommentSpec =
/// Name for codec class.
const String _codecBaseName = 'MessageCodec';

/// Name of the standard codec from the Flutter SDK.
const String _standardCodecName = 'FlStandardMessageCodec';

/// Options that control how GObject code will be generated.
class GObjectOptions {
/// Creates a [GObjectOptions] object
Expand Down Expand Up @@ -282,7 +285,12 @@ class GObjectHeaderGenerator extends StructuredGenerator<GObjectOptions> {
Root root,
Indent indent, {
required String dartPackageName,
}) {}
}) {
final String module = _getModule(generatorOptions, dartPackageName);
indent.newln();
_writeDeclareFinalType(indent, module, _codecBaseName,
parentClassName: _standardCodecName);
}

@override
void writeFlutterApi(
Expand Down Expand Up @@ -508,6 +516,9 @@ class GObjectHeaderGenerator extends StructuredGenerator<GObjectOptions> {
final String methodPrefix = _getMethodPrefix(module, api.name);
final String vtableName = _getVTableName(module, api.name);

indent.newln();
_writeDeclareFinalType(indent, module, api.name);

final bool hasAsyncMethod =
api.methods.any((Method method) => method.isAsynchronous);
if (hasAsyncMethod) {
Expand Down Expand Up @@ -950,13 +961,9 @@ class GObjectSourceGenerator extends StructuredGenerator<GObjectOptions> {

final Iterable<EnumeratedType> customTypes = getEnumeratedTypes(root);

indent.newln();
_writeDeclareFinalType(indent, module, _codecBaseName,
parentClassName: 'FlStandardMessageCodec');

indent.newln();
_writeObjectStruct(indent, module, _codecBaseName, () {},
parentClassName: 'FlStandardMessageCodec');
parentClassName: _standardCodecName);

indent.newln();
_writeDefineType(indent, module, _codecBaseName,
Expand All @@ -971,7 +978,7 @@ class GObjectSourceGenerator extends StructuredGenerator<GObjectOptions> {
? '$customTypeName*'
: 'FlValue*';
indent.writeScoped(
'static gboolean ${codecMethodPrefix}_write_$snakeCustomTypeName(FlStandardMessageCodec* codec, GByteArray* buffer, $valueType value, GError** error) {',
'static gboolean ${codecMethodPrefix}_write_$snakeCustomTypeName($_standardCodecName* codec, GByteArray* buffer, $valueType value, GError** error) {',
'}', () {
indent.writeln('uint8_t type = ${customType.enumeration};');
indent.writeln('g_byte_array_append(buffer, &type, sizeof(uint8_t));');
Expand All @@ -989,7 +996,7 @@ class GObjectSourceGenerator extends StructuredGenerator<GObjectOptions> {

indent.newln();
indent.writeScoped(
'static gboolean ${codecMethodPrefix}_write_value(FlStandardMessageCodec* codec, GByteArray* buffer, FlValue* value, GError** error) {',
'static gboolean ${codecMethodPrefix}_write_value($_standardCodecName* codec, GByteArray* buffer, FlValue* value, GError** error) {',
'}', () {
indent.writeScoped(
'if (fl_value_get_type(value) == FL_VALUE_TYPE_CUSTOM) {', '}', () {
Expand Down Expand Up @@ -1027,7 +1034,7 @@ class GObjectSourceGenerator extends StructuredGenerator<GObjectOptions> {
_snakeCaseFromCamelCase(customTypeName);
indent.newln();
indent.writeScoped(
'static FlValue* ${codecMethodPrefix}_read_$snakeCustomTypeName(FlStandardMessageCodec* codec, GBytes* buffer, size_t* offset, GError** error) {',
'static FlValue* ${codecMethodPrefix}_read_$snakeCustomTypeName($_standardCodecName* codec, GBytes* buffer, size_t* offset, GError** error) {',
'}', () {
if (customType.type == CustomTypes.customClass) {
indent.writeln(
Expand Down Expand Up @@ -1055,7 +1062,7 @@ class GObjectSourceGenerator extends StructuredGenerator<GObjectOptions> {

indent.newln();
indent.writeScoped(
'static FlValue* ${codecMethodPrefix}_read_value_of_type(FlStandardMessageCodec* codec, GBytes* buffer, size_t* offset, int type, GError** error) {',
'static FlValue* ${codecMethodPrefix}_read_value_of_type($_standardCodecName* codec, GBytes* buffer, size_t* offset, int type, GError** error) {',
'}', () {
indent.writeScoped('switch (type) {', '}', () {
for (final EnumeratedType customType in customTypes) {
Expand Down Expand Up @@ -1473,9 +1480,6 @@ class GObjectSourceGenerator extends StructuredGenerator<GObjectOptions> {
});
}

indent.newln();
_writeDeclareFinalType(indent, module, api.name);

indent.newln();
_writeObjectStruct(indent, module, api.name, () {
indent.writeln('const ${className}VTable* vtable;');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2397,11 +2397,6 @@ core_tests_pigeon_test_test_message_new_from_list(FlValue* values) {
return core_tests_pigeon_test_test_message_new(test_list);
}

G_DECLARE_FINAL_TYPE(CoreTestsPigeonTestMessageCodec,
core_tests_pigeon_test_message_codec,
CORE_TESTS_PIGEON_TEST, MESSAGE_CODEC,
FlStandardMessageCodec)

struct _CoreTestsPigeonTestMessageCodec {
FlStandardMessageCodec parent_instance;
};
Expand Down Expand Up @@ -13564,10 +13559,6 @@ core_tests_pigeon_test_host_integration_core_api_call_flutter_small_api_echo_str
return self;
}

G_DECLARE_FINAL_TYPE(CoreTestsPigeonTestHostIntegrationCoreApi,
core_tests_pigeon_test_host_integration_core_api,
CORE_TESTS_PIGEON_TEST, HOST_INTEGRATION_CORE_API, GObject)

struct _CoreTestsPigeonTestHostIntegrationCoreApi {
GObject parent_instance;

Expand Down Expand Up @@ -32742,10 +32733,6 @@ core_tests_pigeon_test_host_trivial_api_noop_response_new_error(
return self;
}

G_DECLARE_FINAL_TYPE(CoreTestsPigeonTestHostTrivialApi,
core_tests_pigeon_test_host_trivial_api,
CORE_TESTS_PIGEON_TEST, HOST_TRIVIAL_API, GObject)

struct _CoreTestsPigeonTestHostTrivialApi {
GObject parent_instance;

Expand Down Expand Up @@ -33021,10 +33008,6 @@ core_tests_pigeon_test_host_small_api_void_void_response_new_error(
return self;
}

G_DECLARE_FINAL_TYPE(CoreTestsPigeonTestHostSmallApi,
core_tests_pigeon_test_host_small_api,
CORE_TESTS_PIGEON_TEST, HOST_SMALL_API, GObject)

struct _CoreTestsPigeonTestHostSmallApi {
GObject parent_instance;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1428,6 +1428,15 @@ CoreTestsPigeonTestTestMessage* core_tests_pigeon_test_test_message_new(
FlValue* core_tests_pigeon_test_test_message_get_test_list(
CoreTestsPigeonTestTestMessage* object);

G_DECLARE_FINAL_TYPE(CoreTestsPigeonTestMessageCodec,
core_tests_pigeon_test_message_codec,
CORE_TESTS_PIGEON_TEST, MESSAGE_CODEC,
FlStandardMessageCodec)

G_DECLARE_FINAL_TYPE(CoreTestsPigeonTestHostIntegrationCoreApi,
core_tests_pigeon_test_host_integration_core_api,
CORE_TESTS_PIGEON_TEST, HOST_INTEGRATION_CORE_API, GObject)

G_DECLARE_FINAL_TYPE(
CoreTestsPigeonTestHostIntegrationCoreApiResponseHandle,
core_tests_pigeon_test_host_integration_core_api_response_handle,
Expand Down Expand Up @@ -11869,6 +11878,10 @@ core_tests_pigeon_test_flutter_integration_core_api_echo_async_string_finish(
CoreTestsPigeonTestFlutterIntegrationCoreApi* api, GAsyncResult* result,
GError** error);

G_DECLARE_FINAL_TYPE(CoreTestsPigeonTestHostTrivialApi,
core_tests_pigeon_test_host_trivial_api,
CORE_TESTS_PIGEON_TEST, HOST_TRIVIAL_API, GObject)

G_DECLARE_FINAL_TYPE(CoreTestsPigeonTestHostTrivialApiNoopResponse,
core_tests_pigeon_test_host_trivial_api_noop_response,
CORE_TESTS_PIGEON_TEST, HOST_TRIVIAL_API_NOOP_RESPONSE,
Expand Down Expand Up @@ -11936,6 +11949,10 @@ void core_tests_pigeon_test_host_trivial_api_set_method_handlers(
void core_tests_pigeon_test_host_trivial_api_clear_method_handlers(
FlBinaryMessenger* messenger, const gchar* suffix);

G_DECLARE_FINAL_TYPE(CoreTestsPigeonTestHostSmallApi,
core_tests_pigeon_test_host_small_api,
CORE_TESTS_PIGEON_TEST, HOST_SMALL_API, GObject)

G_DECLARE_FINAL_TYPE(CoreTestsPigeonTestHostSmallApiResponseHandle,
core_tests_pigeon_test_host_small_api_response_handle,
CORE_TESTS_PIGEON_TEST, HOST_SMALL_API_RESPONSE_HANDLE,
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: pigeon
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
repository: https://github.com/flutter/packages/tree/main/packages/pigeon
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+pigeon%22
version: 22.6.0 # This must match the version in lib/generator_tools.dart
version: 22.6.1 # This must match the version in lib/generator_tools.dart

environment:
sdk: ^3.3.0
Expand Down
4 changes: 4 additions & 0 deletions packages/pigeon/test/gobject_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ void main() {
code,
contains(
'static void test_package_api_init(TestPackageApi* self) {'));
// See https://github.com/flutter/flutter/issues/153083. If a private type
// is ever needed, this should be updated to ensure that any type declared
// in the implementation file has a corresponding _IS_ call in the file.
expect(code, isNot(contains('G_DECLARE_FINAL_TYPE(')));
}
});

Expand Down

0 comments on commit 5e90ce2

Please sign in to comment.