Add custom FlValue types and support their en/decoding via FlStandardMessageCodec (flutter/engine#49162)

Add Linux support for custom data types in Flutter channels.

Fixes https://github.com/flutter/flutter/issues/140299
This commit is contained in:
Robert Ancell
2024-01-25 09:29:54 +13:00
committed by GitHub
parent 43599a528a
commit 7efa9951c2
11 changed files with 607 additions and 175 deletions

View File

@@ -7156,7 +7156,6 @@ ORIGIN: ../../../flutter/shell/platform/linux/fl_settings_portal.cc + ../../../f
ORIGIN: ../../../flutter/shell/platform/linux/fl_settings_portal.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/linux/fl_settings_portal_test.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/linux/fl_standard_message_codec.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/linux/fl_standard_message_codec_private.h + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/linux/fl_standard_message_codec_test.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/linux/fl_standard_method_codec.cc + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/shell/platform/linux/fl_standard_method_codec_test.cc + ../../../flutter/LICENSE
@@ -10030,7 +10029,6 @@ FILE: ../../../flutter/shell/platform/linux/fl_settings_portal.cc
FILE: ../../../flutter/shell/platform/linux/fl_settings_portal.h
FILE: ../../../flutter/shell/platform/linux/fl_settings_portal_test.cc
FILE: ../../../flutter/shell/platform/linux/fl_standard_message_codec.cc
FILE: ../../../flutter/shell/platform/linux/fl_standard_message_codec_private.h
FILE: ../../../flutter/shell/platform/linux/fl_standard_message_codec_test.cc
FILE: ../../../flutter/shell/platform/linux/fl_standard_method_codec.cc
FILE: ../../../flutter/shell/platform/linux/fl_standard_method_codec_test.cc

View File

@@ -90,7 +90,6 @@ source_set("flutter_linux_sources") {
"fl_method_channel_private.h",
"fl_method_codec_private.h",
"fl_plugin_registrar_private.h",
"fl_standard_message_codec_private.h",
"key_mapping.h",
]

View File

@@ -3,7 +3,6 @@
// found in the LICENSE file.
#include "flutter/shell/platform/linux/public/flutter_linux/fl_standard_message_codec.h"
#include "flutter/shell/platform/linux/fl_standard_message_codec_private.h"
#include <gmodule.h>
@@ -28,10 +27,6 @@ static constexpr int kValueList = 12;
static constexpr int kValueMap = 13;
static constexpr int kValueFloat32List = 14;
struct _FlStandardMessageCodec {
FlMessageCodec parent_instance;
};
G_DEFINE_TYPE(FlStandardMessageCodec,
fl_standard_message_codec,
fl_message_codec_get_type())
@@ -445,66 +440,12 @@ static FlValue* fl_standard_message_codec_decode_message(FlMessageCodec* codec,
return fl_value_ref(value);
}
static void fl_standard_message_codec_class_init(
FlStandardMessageCodecClass* klass) {
FL_MESSAGE_CODEC_CLASS(klass)->encode_message =
fl_standard_message_codec_encode_message;
FL_MESSAGE_CODEC_CLASS(klass)->decode_message =
fl_standard_message_codec_decode_message;
}
static void fl_standard_message_codec_init(FlStandardMessageCodec* self) {}
G_MODULE_EXPORT FlStandardMessageCodec* fl_standard_message_codec_new() {
return static_cast<FlStandardMessageCodec*>(
g_object_new(fl_standard_message_codec_get_type(), nullptr));
}
void fl_standard_message_codec_write_size(FlStandardMessageCodec* codec,
GByteArray* buffer,
uint32_t size) {
if (size < 254) {
write_uint8(buffer, size);
} else if (size <= 0xffff) {
write_uint8(buffer, 254);
write_uint16(buffer, size);
} else {
write_uint8(buffer, 255);
write_uint32(buffer, size);
}
}
gboolean fl_standard_message_codec_read_size(FlStandardMessageCodec* codec,
GBytes* buffer,
size_t* offset,
uint32_t* value,
GError** error) {
uint8_t value8;
if (!read_uint8(buffer, offset, &value8, error)) {
return FALSE;
}
if (value8 == 255) {
if (!read_uint32(buffer, offset, value, error)) {
return FALSE;
}
} else if (value8 == 254) {
uint16_t value16;
if (!read_uint16(buffer, offset, &value16, error)) {
return FALSE;
}
*value = value16;
} else {
*value = value8;
}
return TRUE;
}
gboolean fl_standard_message_codec_write_value(FlStandardMessageCodec* self,
GByteArray* buffer,
FlValue* value,
GError** error) {
// Implements FlStandardMessageCodec::write_value.
static gboolean fl_standard_message_codec_real_write_value(
FlStandardMessageCodec* self,
GByteArray* buffer,
FlValue* value,
GError** error) {
if (value == nullptr) {
write_uint8(buffer, kValueNull);
return TRUE;
@@ -622,6 +563,11 @@ gboolean fl_standard_message_codec_write_value(FlStandardMessageCodec* self,
}
}
return TRUE;
case FL_VALUE_TYPE_CUSTOM:
g_set_error(error, FL_MESSAGE_CODEC_ERROR,
FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE,
"Custom value not implemented");
return FALSE;
}
g_set_error(error, FL_MESSAGE_CODEC_ERROR,
@@ -630,15 +576,13 @@ gboolean fl_standard_message_codec_write_value(FlStandardMessageCodec* self,
return FALSE;
}
FlValue* fl_standard_message_codec_read_value(FlStandardMessageCodec* self,
GBytes* buffer,
size_t* offset,
GError** error) {
uint8_t type;
if (!read_uint8(buffer, offset, &type, error)) {
return nullptr;
}
// Implements FlStandardMessageCodec::read_value_of_type.
static FlValue* fl_standard_message_codec_read_value_of_type(
FlStandardMessageCodec* self,
GBytes* buffer,
size_t* offset,
int type,
GError** error) {
g_autoptr(FlValue) value = nullptr;
if (type == kValueNull) {
return fl_value_new_null();
@@ -677,3 +621,82 @@ FlValue* fl_standard_message_codec_read_value(FlStandardMessageCodec* self,
return value == nullptr ? nullptr : fl_value_ref(value);
}
static void fl_standard_message_codec_class_init(
FlStandardMessageCodecClass* klass) {
FL_MESSAGE_CODEC_CLASS(klass)->encode_message =
fl_standard_message_codec_encode_message;
FL_MESSAGE_CODEC_CLASS(klass)->decode_message =
fl_standard_message_codec_decode_message;
klass->write_value = fl_standard_message_codec_real_write_value;
klass->read_value_of_type = fl_standard_message_codec_read_value_of_type;
}
static void fl_standard_message_codec_init(FlStandardMessageCodec* self) {}
G_MODULE_EXPORT FlStandardMessageCodec* fl_standard_message_codec_new() {
return static_cast<FlStandardMessageCodec*>(
g_object_new(fl_standard_message_codec_get_type(), nullptr));
}
void fl_standard_message_codec_write_size(FlStandardMessageCodec* codec,
GByteArray* buffer,
uint32_t size) {
if (size < 254) {
write_uint8(buffer, size);
} else if (size <= 0xffff) {
write_uint8(buffer, 254);
write_uint16(buffer, size);
} else {
write_uint8(buffer, 255);
write_uint32(buffer, size);
}
}
gboolean fl_standard_message_codec_read_size(FlStandardMessageCodec* codec,
GBytes* buffer,
size_t* offset,
uint32_t* value,
GError** error) {
uint8_t value8;
if (!read_uint8(buffer, offset, &value8, error)) {
return FALSE;
}
if (value8 == 255) {
if (!read_uint32(buffer, offset, value, error)) {
return FALSE;
}
} else if (value8 == 254) {
uint16_t value16;
if (!read_uint16(buffer, offset, &value16, error)) {
return FALSE;
}
*value = value16;
} else {
*value = value8;
}
return TRUE;
}
gboolean fl_standard_message_codec_write_value(FlStandardMessageCodec* self,
GByteArray* buffer,
FlValue* value,
GError** error) {
return FL_STANDARD_MESSAGE_CODEC_GET_CLASS(self)->write_value(self, buffer,
value, error);
}
FlValue* fl_standard_message_codec_read_value(FlStandardMessageCodec* self,
GBytes* buffer,
size_t* offset,
GError** error) {
uint8_t type;
if (!read_uint8(buffer, offset, &type, error)) {
return nullptr;
}
return FL_STANDARD_MESSAGE_CODEC_GET_CLASS(self)->read_value_of_type(
self, buffer, offset, type, error);
}

View File

@@ -1,80 +0,0 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_SHELL_PLATFORM_LINUX_FL_STANDARD_MESSAGE_CODEC_PRIVATE_H_
#define FLUTTER_SHELL_PLATFORM_LINUX_FL_STANDARD_MESSAGE_CODEC_PRIVATE_H_
#include "flutter/shell/platform/linux/public/flutter_linux/fl_standard_message_codec.h"
G_BEGIN_DECLS
/**
* fl_standard_message_codec_write_size:
* @codec: an #FlStandardMessageCodec.
* @buffer: buffer to write into.
* @size: size value to write.
*
* Writes a size field in Flutter Standard encoding.
*/
void fl_standard_message_codec_write_size(FlStandardMessageCodec* codec,
GByteArray* buffer,
uint32_t size);
/**
* fl_standard_message_codec_read_size:
* @codec: an #FlStandardMessageCodec.
* @buffer: buffer to read from.
* @offset: (inout): read position in @buffer.
* @value: location to read size.
* @error: (allow-none): #GError location to store the error occurring, or
* %NULL.
*
* Reads a size field in Flutter Standard encoding.
*
* Returns: %TRUE on success.
*/
gboolean fl_standard_message_codec_read_size(FlStandardMessageCodec* codec,
GBytes* buffer,
size_t* offset,
uint32_t* value,
GError** error);
/**
* fl_standard_message_codec_write_value:
* @codec: an #FlStandardMessageCodec.
* @buffer: buffer to write into.
* @value: (allow-none): value to write.
* @error: (allow-none): #GError location to store the error occurring, or
* %NULL.
*
* Writes an #FlValue in Flutter Standard encoding.
*
* Returns: %TRUE on success.
*/
gboolean fl_standard_message_codec_write_value(FlStandardMessageCodec* codec,
GByteArray* buffer,
FlValue* value,
GError** error);
/**
* fl_standard_message_codec_read_value:
* @codec: an #FlStandardMessageCodec.
* @buffer: buffer to read from.
* @offset: (inout): read position in @buffer.
* @value: location to read size.
* @error: (allow-none): #GError location to store the error occurring, or
* %NULL.
*
* Reads an #FlValue in Flutter Standard encoding.
*
* Returns: a new #FlValue or %NULL on error.
*/
FlValue* fl_standard_message_codec_read_value(FlStandardMessageCodec* codec,
GBytes* buffer,
size_t* offset,
GError** error);
G_END_DECLS
#endif // FLUTTER_SHELL_PLATFORM_LINUX_FL_STANDARD_MESSAGE_CODEC_PRIVATE_H_

View File

@@ -9,30 +9,43 @@
// NOTE(robert-ancell) These test cases assumes a little-endian architecture.
// These tests will need to be updated if tested on a big endian architecture.
// Encodes a message using a FlStandardMessageCodec. Return a hex string with
// Encodes a message using the supplied codec. Return a hex string with
// the encoded binary output.
static gchar* encode_message(FlValue* value) {
g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
static gchar* encode_message_with_codec(FlValue* value, FlMessageCodec* codec) {
g_autoptr(GError) error = nullptr;
g_autoptr(GBytes) message =
fl_message_codec_encode_message(FL_MESSAGE_CODEC(codec), value, &error);
fl_message_codec_encode_message(codec, value, &error);
EXPECT_NE(message, nullptr);
EXPECT_EQ(error, nullptr);
return bytes_to_hex_string(message);
}
// Encodes a message using a FlStandardMessageCodec. Return a hex string with
// the encoded binary output.
static gchar* encode_message(FlValue* value) {
g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
return encode_message_with_codec(value, FL_MESSAGE_CODEC(codec));
}
// Decodes a message using the supplied codec. The binary data is given in
// the form of a hex string.
static FlValue* decode_message_with_codec(const char* hex_string,
FlMessageCodec* codec) {
g_autoptr(GBytes) data = hex_string_to_bytes(hex_string);
g_autoptr(GError) error = nullptr;
g_autoptr(FlValue) value =
fl_message_codec_decode_message(codec, data, &error);
EXPECT_EQ(error, nullptr);
EXPECT_NE(value, nullptr);
return fl_value_ref(value);
}
// Decodes a message using a FlStandardMessageCodec. The binary data is given in
// the form of a hex string.
static FlValue* decode_message(const char* hex_string) {
g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();
g_autoptr(GBytes) data = hex_string_to_bytes(hex_string);
g_autoptr(GError) error = nullptr;
g_autoptr(FlValue) value =
fl_message_codec_decode_message(FL_MESSAGE_CODEC(codec), data, &error);
EXPECT_EQ(error, nullptr);
EXPECT_NE(value, nullptr);
return fl_value_ref(value);
return decode_message_with_codec(hex_string, FL_MESSAGE_CODEC(codec));
}
// Decodes a message using a FlStandardMessageCodec. The binary data is given in
@@ -1077,6 +1090,178 @@ TEST(FlStandardMessageCodecTest, DecodeUnknownType) {
FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE);
}
G_DECLARE_FINAL_TYPE(FlTestStandardMessageCodec,
fl_test_standard_message_codec,
FL,
TEST_STANDARD_MESSAGE_CODEC,
FlStandardMessageCodec)
struct _FlTestStandardMessageCodec {
FlStandardMessageCodec parent_instance;
};
G_DEFINE_TYPE(FlTestStandardMessageCodec,
fl_test_standard_message_codec,
fl_standard_message_codec_get_type())
static gboolean write_custom_value1(FlStandardMessageCodec* codec,
GByteArray* buffer,
FlValue* value,
GError** error) {
const gchar* text =
static_cast<const gchar*>(fl_value_get_custom_value(value));
size_t length = strlen(text);
uint8_t type = 128;
g_byte_array_append(buffer, &type, sizeof(uint8_t));
fl_standard_message_codec_write_size(codec, buffer, length);
g_byte_array_append(buffer, reinterpret_cast<const uint8_t*>(text), length);
return TRUE;
}
static gboolean write_custom_value2(FlStandardMessageCodec* codec,
GByteArray* buffer,
FlValue* value,
GError** error) {
uint8_t type = 129;
g_byte_array_append(buffer, &type, sizeof(uint8_t));
return TRUE;
}
static gboolean fl_test_standard_message_codec_write_value(
FlStandardMessageCodec* codec,
GByteArray* buffer,
FlValue* value,
GError** error) {
if (fl_value_get_type(value) == FL_VALUE_TYPE_CUSTOM &&
fl_value_get_custom_type(value) == 128) {
return write_custom_value1(codec, buffer, value, error);
} else if (fl_value_get_type(value) == FL_VALUE_TYPE_CUSTOM &&
fl_value_get_custom_type(value) == 129) {
return write_custom_value2(codec, buffer, value, error);
} else {
return FL_STANDARD_MESSAGE_CODEC_CLASS(
fl_test_standard_message_codec_parent_class)
->write_value(codec, buffer, value, error);
}
}
static FlValue* read_custom_value1(FlStandardMessageCodec* codec,
GBytes* buffer,
size_t* offset,
GError** error) {
uint32_t length;
if (!fl_standard_message_codec_read_size(codec, buffer, offset, &length,
error)) {
return nullptr;
}
if (*offset + length > g_bytes_get_size(buffer)) {
g_set_error(error, FL_MESSAGE_CODEC_ERROR,
FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA, "Unexpected end of data");
return nullptr;
}
FlValue* value = fl_value_new_custom(
128,
g_strndup(static_cast<const gchar*>(g_bytes_get_data(buffer, nullptr)) +
*offset,
length),
g_free);
*offset += length;
return value;
}
static FlValue* read_custom_value2(FlStandardMessageCodec* codec,
GBytes* buffer,
size_t* offset,
GError** error) {
return fl_value_new_custom(129, nullptr, nullptr);
}
static FlValue* fl_test_standard_message_codec_read_value_of_type(
FlStandardMessageCodec* codec,
GBytes* buffer,
size_t* offset,
int type,
GError** error) {
if (type == 128) {
return read_custom_value1(codec, buffer, offset, error);
} else if (type == 129) {
return read_custom_value2(codec, buffer, offset, error);
} else {
return FL_STANDARD_MESSAGE_CODEC_CLASS(
fl_test_standard_message_codec_parent_class)
->read_value_of_type(codec, buffer, offset, type, error);
}
}
static void fl_test_standard_message_codec_class_init(
FlTestStandardMessageCodecClass* klass) {
FL_STANDARD_MESSAGE_CODEC_CLASS(klass)->write_value =
fl_test_standard_message_codec_write_value;
FL_STANDARD_MESSAGE_CODEC_CLASS(klass)->read_value_of_type =
fl_test_standard_message_codec_read_value_of_type;
}
static void fl_test_standard_message_codec_init(
FlTestStandardMessageCodec* self) {
// The following line suppresses a warning for unused function
FL_IS_TEST_STANDARD_MESSAGE_CODEC(self);
}
static FlTestStandardMessageCodec* fl_test_standard_message_codec_new() {
return FL_TEST_STANDARD_MESSAGE_CODEC(
g_object_new(fl_test_standard_message_codec_get_type(), nullptr));
}
TEST(FlStandardMessageCodecTest, DecodeCustomType) {
g_autoptr(FlTestStandardMessageCodec) codec =
fl_test_standard_message_codec_new();
g_autoptr(FlValue) value =
decode_message_with_codec("800568656c6c6f", FL_MESSAGE_CODEC(codec));
ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_CUSTOM);
ASSERT_EQ(fl_value_get_custom_type(value), 128);
EXPECT_STREQ(static_cast<const gchar*>(fl_value_get_custom_value(value)),
"hello");
}
TEST(FlStandardMessageCodecTest, DecodeCustomTypes) {
g_autoptr(FlTestStandardMessageCodec) codec =
fl_test_standard_message_codec_new();
g_autoptr(FlValue) value = decode_message_with_codec("0c02800568656c6c6f81",
FL_MESSAGE_CODEC(codec));
ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_LIST);
EXPECT_EQ(fl_value_get_length(value), static_cast<size_t>(2));
FlValue* value1 = fl_value_get_list_value(value, 0);
ASSERT_EQ(fl_value_get_type(value1), FL_VALUE_TYPE_CUSTOM);
ASSERT_EQ(fl_value_get_custom_type(value1), 128);
EXPECT_STREQ(static_cast<const gchar*>(fl_value_get_custom_value(value1)),
"hello");
FlValue* value2 = fl_value_get_list_value(value, 1);
ASSERT_EQ(fl_value_get_type(value2), FL_VALUE_TYPE_CUSTOM);
ASSERT_EQ(fl_value_get_custom_type(value2), 129);
}
TEST(FlStandardMessageCodecTest, EncodeCustomType) {
g_autoptr(FlValue) value = fl_value_new_custom(128, "hello", nullptr);
g_autoptr(FlTestStandardMessageCodec) codec =
fl_test_standard_message_codec_new();
g_autofree gchar* hex_string =
encode_message_with_codec(value, FL_MESSAGE_CODEC(codec));
EXPECT_STREQ(hex_string, "800568656c6c6f");
}
TEST(FlStandardMessageCodecTest, EncodeCustomTypes) {
g_autoptr(FlValue) value = fl_value_new_list();
fl_value_append_take(value, fl_value_new_custom(128, "hello", nullptr));
fl_value_append_take(value, fl_value_new_custom(129, nullptr, nullptr));
g_autoptr(FlTestStandardMessageCodec) codec =
fl_test_standard_message_codec_new();
g_autofree gchar* hex_string =
encode_message_with_codec(value, FL_MESSAGE_CODEC(codec));
EXPECT_STREQ(hex_string, "0c02800568656c6c6f81");
}
TEST(FlStandardMessageCodecTest, EncodeDecode) {
g_autoptr(FlStandardMessageCodec) codec = fl_standard_message_codec_new();

View File

@@ -6,7 +6,6 @@
#include <gmodule.h>
#include "flutter/shell/platform/linux/fl_standard_message_codec_private.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_standard_message_codec.h"
// See lib/src/services/message_codecs.dart in Flutter source for description of

View File

@@ -74,6 +74,13 @@ typedef struct {
GPtrArray* values;
} FlValueMap;
typedef struct {
FlValue parent;
int type;
gconstpointer value;
GDestroyNotify destroy_notify;
} FlValueCustom;
static FlValue* fl_value_new(FlValueType type, size_t size) {
FlValue* self = static_cast<FlValue*>(g_malloc0(size));
self->type = type;
@@ -232,6 +239,10 @@ static void value_to_string(FlValue* value, GString* buffer) {
g_string_append(buffer, "}");
return;
}
case FL_VALUE_TYPE_CUSTOM:
g_string_append_printf(buffer, "(custom %d)",
fl_value_get_custom_type(value));
return;
default:
g_string_append_printf(buffer, "<unknown type %d>", value->type);
}
@@ -360,6 +371,25 @@ G_MODULE_EXPORT FlValue* fl_value_new_map() {
return reinterpret_cast<FlValue*>(self);
}
FlValue* fl_value_new_custom(int type,
gconstpointer value,
GDestroyNotify destroy_notify) {
FlValueCustom* self = reinterpret_cast<FlValueCustom*>(
fl_value_new(FL_VALUE_TYPE_CUSTOM, sizeof(FlValueCustom)));
self->type = type;
self->value = value;
self->destroy_notify = destroy_notify;
return reinterpret_cast<FlValue*>(self);
}
FlValue* fl_value_new_custom_object(int type, GObject* object) {
return fl_value_new_custom(type, g_object_ref(object), g_object_unref);
}
FlValue* fl_value_new_custom_object_take(int type, GObject* object) {
return fl_value_new_custom(type, object, g_object_unref);
}
G_MODULE_EXPORT FlValue* fl_value_ref(FlValue* self) {
g_return_val_if_fail(self != nullptr, nullptr);
self->ref_count++;
@@ -416,6 +446,13 @@ G_MODULE_EXPORT void fl_value_unref(FlValue* self) {
g_ptr_array_unref(v->values);
break;
}
case FL_VALUE_TYPE_CUSTOM: {
FlValueCustom* v = reinterpret_cast<FlValueCustom*>(self);
if (v->destroy_notify != nullptr) {
v->destroy_notify((gpointer)v->value);
}
break;
}
case FL_VALUE_TYPE_NULL:
case FL_VALUE_TYPE_BOOL:
case FL_VALUE_TYPE_INT:
@@ -546,6 +583,8 @@ G_MODULE_EXPORT bool fl_value_equal(FlValue* a, FlValue* b) {
}
return true;
}
case FL_VALUE_TYPE_CUSTOM:
return false;
}
}
@@ -726,6 +765,7 @@ G_MODULE_EXPORT size_t fl_value_get_length(FlValue* self) {
case FL_VALUE_TYPE_INT:
case FL_VALUE_TYPE_FLOAT:
case FL_VALUE_TYPE_STRING:
case FL_VALUE_TYPE_CUSTOM:
return 0;
}
@@ -778,6 +818,30 @@ G_MODULE_EXPORT FlValue* fl_value_lookup_string(FlValue* self,
return value;
}
G_MODULE_EXPORT int fl_value_get_custom_type(FlValue* self) {
g_return_val_if_fail(self != nullptr, -1);
g_return_val_if_fail(self->type == FL_VALUE_TYPE_CUSTOM, -1);
FlValueCustom* v = reinterpret_cast<FlValueCustom*>(self);
return v->type;
}
G_MODULE_EXPORT gconstpointer fl_value_get_custom_value(FlValue* self) {
g_return_val_if_fail(self != nullptr, nullptr);
g_return_val_if_fail(self->type == FL_VALUE_TYPE_CUSTOM, nullptr);
FlValueCustom* v = reinterpret_cast<FlValueCustom*>(self);
return v->value;
}
G_MODULE_EXPORT GObject* fl_value_get_custom_value_object(FlValue* self) {
g_return_val_if_fail(self != nullptr, nullptr);
g_return_val_if_fail(self->type == FL_VALUE_TYPE_CUSTOM, nullptr);
FlValueCustom* v = reinterpret_cast<FlValueCustom*>(self);
return G_OBJECT(v->value);
}
G_MODULE_EXPORT gchar* fl_value_to_string(FlValue* value) {
GString* buffer = g_string_new("");
value_to_string(value, buffer);

View File

@@ -885,6 +885,51 @@ TEST(FlValueTest, MapToString) {
"int64_list, []: float_list, []: list, {}: map}");
}
TEST(FlDartProjectTest, Custom) {
g_autoptr(FlValue) value =
fl_value_new_custom(128, g_strdup("Hello World"), g_free);
ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_CUSTOM);
ASSERT_EQ(fl_value_get_custom_type(value), 128);
ASSERT_STREQ(reinterpret_cast<const gchar*>(fl_value_get_custom_value(value)),
"Hello World");
}
TEST(FlDartProjectTest, CustomNoDestroyNotify) {
g_autoptr(FlValue) value = fl_value_new_custom(128, "Hello World", nullptr);
ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_CUSTOM);
ASSERT_EQ(fl_value_get_custom_type(value), 128);
ASSERT_STREQ(reinterpret_cast<const gchar*>(fl_value_get_custom_value(value)),
"Hello World");
}
TEST(FlDartProjectTest, CustomObject) {
g_autoptr(GObject) v = G_OBJECT(g_object_new(G_TYPE_OBJECT, nullptr));
g_autoptr(FlValue) value = fl_value_new_custom_object(128, v);
ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_CUSTOM);
ASSERT_EQ(fl_value_get_custom_type(value), 128);
ASSERT_TRUE(G_IS_OBJECT(fl_value_get_custom_value_object(value)));
}
TEST(FlDartProjectTest, CustomObjectTake) {
g_autoptr(FlValue) value = fl_value_new_custom_object_take(
128, G_OBJECT(g_object_new(G_TYPE_OBJECT, nullptr)));
ASSERT_EQ(fl_value_get_type(value), FL_VALUE_TYPE_CUSTOM);
ASSERT_EQ(fl_value_get_custom_type(value), 128);
ASSERT_TRUE(G_IS_OBJECT(fl_value_get_custom_value_object(value)));
}
TEST(FlValueTest, CustomEqual) {
g_autoptr(FlValue) value1 = fl_value_new_custom(128, "Hello World", nullptr);
g_autoptr(FlValue) value2 = fl_value_new_custom(128, "Hello World", nullptr);
EXPECT_FALSE(fl_value_equal(value1, value2));
}
TEST(FlValueTest, CustomToString) {
g_autoptr(FlValue) value = fl_value_new_custom(128, nullptr, nullptr);
g_autofree gchar* text = fl_value_to_string(value);
EXPECT_STREQ(text, "(custom 128)");
}
TEST(FlValueTest, EqualSameObject) {
g_autoptr(FlValue) value = fl_value_new_null();
EXPECT_TRUE(fl_value_equal(value, value));

View File

@@ -63,7 +63,7 @@ struct _FlMessageCodecClass {
/**
* FlMessageCodec::encode_message:
* @codec: A #FlMessageCodec.
* @codec: an #FlMessageCodec.
* @message: message to encode or %NULL to encode the null value.
* @error: (allow-none): #GError location to store the error occurring, or
* %NULL.

View File

@@ -16,11 +16,11 @@
G_BEGIN_DECLS
G_MODULE_EXPORT
G_DECLARE_FINAL_TYPE(FlStandardMessageCodec,
fl_standard_message_codec,
FL,
STANDARD_CODEC,
FlMessageCodec)
G_DECLARE_DERIVABLE_TYPE(FlStandardMessageCodec,
fl_standard_message_codec,
FL,
STANDARD_MESSAGE_CODEC,
FlMessageCodec)
/**
* FlStandardMessageCodec:
@@ -32,10 +32,61 @@ G_DECLARE_FINAL_TYPE(FlStandardMessageCodec,
* #FL_VALUE_TYPE_INT32_LIST, #FL_VALUE_TYPE_INT64_LIST,
* #FL_VALUE_TYPE_FLOAT_LIST, #FL_VALUE_TYPE_LIST, and #FL_VALUE_TYPE_MAP.
*
* If other values types are required to be supported create a new subclass that
* overrides write_value and read_value_of_type.
*
* #FlStandardMessageCodec matches the StandardCodec class in the Flutter
* services library.
*/
struct _FlStandardMessageCodecClass {
FlMessageCodecClass parent_class;
/**
* FlStandardMessageCodec::write_value:
* @codec: an #FlStandardMessageCodec.
* @buffer: a buffer to write into.
* @value: (allow-none): value to write.
* @error: (allow-none): #GError location to store the error occurring, or
* %NULL.
*
* Virtual method to write an #FlValue in Flutter Standard encoding.
*
* If a codec needs to support custom #FlValue objects it must override this
* method to encode those values. For non-custom values the parent method
* should be called.
*
* Returns: %TRUE on success.
*/
gboolean (*write_value)(FlStandardMessageCodec* codec,
GByteArray* buffer,
FlValue* value,
GError** error);
/**
* FlStandardMessageCodec::read_value_of_type:
* @codec: an #FlStandardMessageCodec.
* @buffer: buffer to read from.
* @offset: (inout): read position in @buffer.
* @type: the type of the value.
* @error: (allow-none): #GError location to store the error occurring, or
* %NULL.
*
* Virtual method to read an #FlValue in Flutter Standard encoding.
*
* If a codec needs to support custom #FlValue objects it must override this
* method to decode those values. For non-custom values the parent method
* should be called.
*
* Returns: an #FlValue or %NULL on error.
*/
FlValue* (*read_value_of_type)(FlStandardMessageCodec* codec,
GBytes* buffer,
size_t* offset,
int type,
GError** error);
};
/*
* fl_standard_message_codec_new:
*
@@ -45,6 +96,81 @@ G_DECLARE_FINAL_TYPE(FlStandardMessageCodec,
*/
FlStandardMessageCodec* fl_standard_message_codec_new();
/**
* fl_standard_message_codec_write_size:
* @codec: an #FlStandardMessageCodec.
* @buffer: buffer to write into.
* @size: size value to write.
*
* Writes a size field in Flutter Standard encoding.
*/
void fl_standard_message_codec_write_size(FlStandardMessageCodec* codec,
GByteArray* buffer,
uint32_t size);
/**
* fl_standard_message_codec_read_size:
* @codec: an #FlStandardMessageCodec.
* @buffer: buffer to read from.
* @offset: (inout): read position in @buffer.
* @value: location to read size.
* @error: (allow-none): #GError location to store the error occurring, or
* %NULL.
*
* Reads a size field in Flutter Standard encoding.
*
* This method is intended for use by subclasses overriding
* FlStandardMessageCodec::read_value_of_type.
*
* Returns: %TRUE on success.
*/
gboolean fl_standard_message_codec_read_size(FlStandardMessageCodec* codec,
GBytes* buffer,
size_t* offset,
uint32_t* value,
GError** error);
/**
* fl_standard_message_codec_write_value:
* @codec: an #FlStandardMessageCodec.
* @buffer: buffer to write into.
* @value: (allow-none): value to write.
* @error: (allow-none): #GError location to store the error occurring, or
* %NULL.
*
* Writes an #FlValue in Flutter Standard encoding.
*
* This method is intended for use by subclasses overriding
* FlStandardMessageCodec::write_value.
*
* Returns: %TRUE on success.
*/
gboolean fl_standard_message_codec_write_value(FlStandardMessageCodec* codec,
GByteArray* buffer,
FlValue* value,
GError** error);
/**
* fl_standard_message_codec_read_value:
* @codec: an #FlStandardMessageCodec.
* @buffer: buffer to read from.
* @offset: (inout): read position in @buffer.
* @value: location to read size.
* @error: (allow-none): #GError location to store the error occurring, or
* %NULL.
*
* Reads an #FlValue in Flutter Standard encoding.
*
* This method is intended for use by subclasses overriding
* FlStandardMessageCodec::read_value_of_type.
*
* Returns: a new #FlValue or %NULL on error.
*/
FlValue* fl_standard_message_codec_read_value(FlStandardMessageCodec* codec,
GBytes* buffer,
size_t* offset,
GError** error);
G_END_DECLS
#endif // FLUTTER_SHELL_PLATFORM_LINUX_PUBLIC_FLUTTER_LINUX_FL_STANDARD_MESSAGE_CODEC_H_

View File

@@ -5,6 +5,7 @@
#ifndef FLUTTER_SHELL_PLATFORM_LINUX_PUBLIC_FLUTTER_LINUX_FL_VALUE_H_
#define FLUTTER_SHELL_PLATFORM_LINUX_PUBLIC_FLUTTER_LINUX_FL_VALUE_H_
#include <glib-object.h>
#include <glib.h>
#include <stdbool.h>
#include <stdint.h>
@@ -34,6 +35,7 @@ G_BEGIN_DECLS
* - #FL_VALUE_TYPE_FLOAT_LIST: Float64List
* - #FL_VALUE_TYPE_LIST: List<dynamic>
* - #FL_VALUE_TYPE_MAP: Map<dynamic>
* - #FL_VALUE_TYPE_CUSTOM: (custom type)
*
* See #FlMessageCodec to encode and decode these values.
*/
@@ -53,6 +55,7 @@ typedef struct _FlValue FlValue;
* @FL_VALUE_TYPE_LIST: An ordered list of #FlValue objects.
* @FL_VALUE_TYPE_MAP: A map of #FlValue objects keyed by #FlValue object.
* @FL_VALUE_TYPE_FLOAT32_LIST: An ordered list of 32bit floating point numbers.
* @FL_VALUE_TYPE_CUSTOM: A custom value.
*
* Types of #FlValue.
*/
@@ -71,6 +74,7 @@ typedef enum {
FL_VALUE_TYPE_LIST,
FL_VALUE_TYPE_MAP,
FL_VALUE_TYPE_FLOAT32_LIST,
FL_VALUE_TYPE_CUSTOM,
// NOLINTEND(readability-identifier-naming)
} FlValueType;
@@ -287,6 +291,45 @@ FlValue* fl_value_new_list_from_strv(const gchar* const* value);
*/
FlValue* fl_value_new_map();
/**
* fl_value_new_custom:
* @type: an ID for this type.
* @value: pointer to the custom value.
* @destroy_notify: function to call when @value is no longer required.
*
* Creates a new custom data type. The Dart side of the channel must have
* equivalent custom code to access this object.
*
* Returns: a new #FlValue.
*/
FlValue* fl_value_new_custom(int type,
gconstpointer value,
GDestroyNotify destroy_notify);
/**
* fl_value_new_custom_object:
* @type: an ID for this type.
* @object: the custom object.
*
* Creates a new custom data type. The Dart side of the channel must have
* equivalent custom code to access this object.
*
* Returns: a new #FlValue.
*/
FlValue* fl_value_new_custom_object(int type, GObject* object);
/**
* fl_value_new_custom_object_take:
* @type: an ID for this type.
* @object: (transfer full): the custom object.
*
* Creates a new custom data type. The Dart side of the channel must have
* equivalent custom code to access this object. Ownership of @object is taken.
*
* Returns: a new #FlValue.
*/
FlValue* fl_value_new_custom_object_take(int type, GObject* object);
/**
* fl_value_ref:
* @value: an #FlValue.
@@ -599,6 +642,36 @@ FlValue* fl_value_lookup(FlValue* value, FlValue* key);
*/
FlValue* fl_value_lookup_string(FlValue* value, const gchar* key);
/**
* fl_value_get_custom_type:
* @value: an #FlValue of type #FL_VALUE_TYPE_CUSTOM.
*
* Gets the type ID for this custom type.
*
* Returns: a type ID.
*/
int fl_value_get_custom_type(FlValue* value);
/**
* fl_value_get_custom_value:
* @value: an #FlValue of type #FL_VALUE_TYPE_CUSTOM.
*
* Gets the address of the custom value.
*
* Returns: a pointer to the custom value.
*/
gconstpointer fl_value_get_custom_value(FlValue* value);
/**
* fl_value_get_custom_value_object:
* @value: an #FlValue of type #FL_VALUE_TYPE_CUSTOM.
*
* Gets the custom value as an object.
*
* Returns: an object.
*/
GObject* fl_value_get_custom_value_object(FlValue* value);
/**
* fl_value_to_string:
* @value: an #FlValue.