Flutter Linux Embedder
fl_json_message_codec.cc File Reference
#include "flutter/shell/platform/linux/public/flutter_linux/fl_json_message_codec.h"
#include <gmodule.h>
#include <cstring>
#include "rapidjson/reader.h"
#include "rapidjson/writer.h"

Go to the source code of this file.

Classes

struct  _FlJsonMessageCodec
 
struct  FlValueHandler
 

Functions

 G_DEFINE_TYPE (FlJsonMessageCodec, fl_json_message_codec, fl_message_codec_get_type()) static gboolean write_value(rapidjson
 
static GBytes * fl_json_message_codec_encode_message (FlMessageCodec *codec, FlValue *message, GError **error)
 
static FlValuefl_json_message_codec_decode_message (FlMessageCodec *codec, GBytes *message, GError **error)
 
static void fl_json_message_codec_class_init (FlJsonMessageCodecClass *klass)
 
static void fl_json_message_codec_init (FlJsonMessageCodec *self)
 
G_MODULE_EXPORT FlJsonMessageCodec * fl_json_message_codec_new ()
 
G_MODULE_EXPORT gchar * fl_json_message_codec_encode (FlJsonMessageCodec *codec, FlValue *value, GError **error)
 
G_MODULE_EXPORT FlValuefl_json_message_codec_decode (FlJsonMessageCodec *codec, const gchar *text, GError **error)
 

Function Documentation

◆ fl_json_message_codec_class_init()

static void fl_json_message_codec_class_init ( FlJsonMessageCodecClass *  klass)
static

Definition at line 297 of file fl_json_message_codec.cc.

297  {
298  FL_MESSAGE_CODEC_CLASS(klass)->encode_message =
300  FL_MESSAGE_CODEC_CLASS(klass)->decode_message =
302 }

References fl_json_message_codec_decode_message(), and fl_json_message_codec_encode_message().

◆ fl_json_message_codec_decode()

G_MODULE_EXPORT FlValue* fl_json_message_codec_decode ( FlJsonMessageCodec *  codec,
const gchar *  text,
GError **  error 
)

fl_json_message_codec_decode: @codec: an #FlJsonMessageCodec. @text: UTF-8 text in JSON format. @error: (allow-none): #GError location to store the error occurring, or NULL.

Decodes a value from a JSON string.

Returns: an FlValue or NULL on error.

Definition at line 326 of file fl_json_message_codec.cc.

328  {
329  g_return_val_if_fail(FL_IS_JSON_CODEC(codec), nullptr);
330 
331  g_autoptr(GBytes) data = g_bytes_new_static(text, strlen(text));
333  FL_MESSAGE_CODEC(codec), data, error);
334  if (value == nullptr) {
335  return nullptr;
336  }
337 
338  return fl_value_ref(value);
339 }

References error, fl_json_message_codec_decode_message(), fl_value_ref(), and value.

Referenced by decode_error_message(), decode_message(), and TEST().

◆ fl_json_message_codec_decode_message()

static FlValue* fl_json_message_codec_decode_message ( FlMessageCodec *  codec,
GBytes *  message,
GError **  error 
)
static

Definition at line 258 of file fl_json_message_codec.cc.

260  {
261  gsize data_length;
262  const gchar* data =
263  static_cast<const char*>(g_bytes_get_data(message, &data_length));
264  if (!g_utf8_validate(data, data_length, nullptr)) {
265  g_set_error(error, FL_JSON_MESSAGE_CODEC_ERROR,
267  "Message is not valid UTF8");
268  return nullptr;
269  }
270 
271  FlValueHandler handler;
272  rapidjson::Reader reader;
273  rapidjson::MemoryStream ss(data, data_length);
274  if (!reader.Parse(ss, handler)) {
275  if (handler.error != nullptr) {
276  g_propagate_error(error, handler.error);
277  handler.error = nullptr;
278  } else {
279  g_set_error(error, FL_JSON_MESSAGE_CODEC_ERROR,
281  "Message is not valid JSON");
282  }
283  return nullptr;
284  }
285 
286  FlValue* value = handler.get_head();
287  if (value == nullptr) {
288  g_set_error(error, FL_JSON_MESSAGE_CODEC_ERROR,
290  "Message is not valid JSON");
291  return nullptr;
292  }
293 
294  return fl_value_ref(value);
295 }

References error, FlValueHandler::error, FL_JSON_MESSAGE_CODEC_ERROR, FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON, FL_JSON_MESSAGE_CODEC_ERROR_INVALID_UTF8, fl_value_ref(), FlValueHandler::get_head(), and value.

Referenced by fl_json_message_codec_class_init(), and fl_json_message_codec_decode().

◆ fl_json_message_codec_encode()

G_MODULE_EXPORT gchar* fl_json_message_codec_encode ( FlJsonMessageCodec *  codec,
FlValue value,
GError **  error 
)

fl_json_message_codec_encode: @codec: an #FlJsonMessageCodec. @value: value to encode. @error: (allow-none): #GError location to store the error occurring, or NULL.

Encodes a value to a JSON string.

Returns: a JSON representation of this value or NULL on error.

Definition at line 311 of file fl_json_message_codec.cc.

313  {
314  g_return_val_if_fail(FL_IS_JSON_CODEC(codec), nullptr);
315 
316  rapidjson::StringBuffer buffer;
317  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
318 
319  if (!write_value(writer, value, error)) {
320  return nullptr;
321  }
322 
323  return g_strdup(buffer.GetString());
324 }

References buffer, error, and value.

Referenced by encode_error_message(), encode_message(), and TEST().

◆ fl_json_message_codec_encode_message()

static GBytes* fl_json_message_codec_encode_message ( FlMessageCodec *  codec,
FlValue message,
GError **  error 
)
static

Definition at line 243 of file fl_json_message_codec.cc.

245  {
246  rapidjson::StringBuffer buffer;
247  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
248 
249  if (!write_value(writer, message, error)) {
250  return nullptr;
251  }
252 
253  const gchar* text = buffer.GetString();
254  return g_bytes_new(text, strlen(text));
255 }

References buffer, and error.

Referenced by fl_json_message_codec_class_init().

◆ fl_json_message_codec_init()

static void fl_json_message_codec_init ( FlJsonMessageCodec *  self)
static

Definition at line 304 of file fl_json_message_codec.cc.

304 {}

◆ fl_json_message_codec_new()

G_MODULE_EXPORT FlJsonMessageCodec* fl_json_message_codec_new ( )

Definition at line 306 of file fl_json_message_codec.cc.

306  {
307  return static_cast<FlJsonMessageCodec*>(
308  g_object_new(fl_json_message_codec_get_type(), nullptr));
309 }

Referenced by decode_error_message(), decode_message(), encode_error_message(), encode_message(), fl_json_method_codec_init(), fl_key_channel_responder_new(), fl_settings_plugin_new(), MATCHER_P2(), and TEST().

◆ G_DEFINE_TYPE()

G_DEFINE_TYPE ( FlJsonMessageCodec  ,
fl_json_message_codec  ,
fl_message_codec_get_type()   
)

Definition at line 20 of file fl_json_message_codec.cc.

27  {
28  if (value == nullptr) {
29  writer.Null();
30  return TRUE;
31  }
32 
33  switch (fl_value_get_type(value)) {
34  case FL_VALUE_TYPE_NULL:
35  writer.Null();
36  break;
37  case FL_VALUE_TYPE_BOOL:
38  writer.Bool(fl_value_get_bool(value));
39  break;
40  case FL_VALUE_TYPE_INT:
41  writer.Int64(fl_value_get_int(value));
42  break;
44  writer.Double(fl_value_get_float(value));
45  break;
47  writer.String(fl_value_get_string(value));
48  break;
50  writer.StartArray();
51  const uint8_t* data = fl_value_get_uint8_list(value);
52  for (size_t i = 0; i < fl_value_get_length(value); i++) {
53  writer.Int(data[i]);
54  }
55  writer.EndArray();
56  break;
57  }
59  writer.StartArray();
60  const int32_t* data = fl_value_get_int32_list(value);
61  for (size_t i = 0; i < fl_value_get_length(value); i++) {
62  writer.Int(data[i]);
63  }
64  writer.EndArray();
65  break;
66  }
68  writer.StartArray();
69  const int64_t* data = fl_value_get_int64_list(value);
70  for (size_t i = 0; i < fl_value_get_length(value); i++) {
71  writer.Int64(data[i]);
72  }
73  writer.EndArray();
74  break;
75  }
77  writer.StartArray();
78  const double* data = fl_value_get_float_list(value);
79  for (size_t i = 0; i < fl_value_get_length(value); i++) {
80  writer.Double(data[i]);
81  }
82  writer.EndArray();
83  break;
84  }
85  case FL_VALUE_TYPE_LIST: {
86  writer.StartArray();
87  for (size_t i = 0; i < fl_value_get_length(value); i++) {
88  if (!write_value(writer, fl_value_get_list_value(value, i), error)) {
89  return FALSE;
90  }
91  }
92  writer.EndArray();
93  break;
94  }
95  case FL_VALUE_TYPE_MAP: {
96  writer.StartObject();
97  for (size_t i = 0; i < fl_value_get_length(value); i++) {
100  g_set_error(error, FL_JSON_MESSAGE_CODEC_ERROR,
102  "Invalid object key type");
103  return FALSE;
104  }
105  writer.Key(fl_value_get_string(key));
106  if (!write_value(writer, fl_value_get_map_value(value, i), error)) {
107  return FALSE;
108  }
109  }
110  writer.EndObject();
111  break;
112  }
113  default:
114  g_set_error(error, FL_MESSAGE_CODEC_ERROR,
116  "Unexpected FlValue type %d", fl_value_get_type(value));
117  return FALSE;
118  }
119 
120  return TRUE;
121 }

References error, FL_JSON_MESSAGE_CODEC_ERROR, FL_JSON_MESSAGE_CODEC_ERROR_INVALID_OBJECT_KEY_TYPE, FL_MESSAGE_CODEC_ERROR, FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE, fl_value_get_bool(), fl_value_get_float(), fl_value_get_float_list(), fl_value_get_int(), fl_value_get_int32_list(), fl_value_get_int64_list(), fl_value_get_length(), fl_value_get_list_value(), fl_value_get_map_key(), fl_value_get_map_value(), fl_value_get_string(), fl_value_get_type(), fl_value_get_uint8_list(), FL_VALUE_TYPE_BOOL, FL_VALUE_TYPE_FLOAT, FL_VALUE_TYPE_FLOAT_LIST, FL_VALUE_TYPE_INT, FL_VALUE_TYPE_INT32_LIST, FL_VALUE_TYPE_INT64_LIST, FL_VALUE_TYPE_LIST, FL_VALUE_TYPE_MAP, FL_VALUE_TYPE_NULL, FL_VALUE_TYPE_STRING, FL_VALUE_TYPE_UINT8_LIST, i, TRUE, and value.

FL_JSON_MESSAGE_CODEC_ERROR_INVALID_UTF8
@ FL_JSON_MESSAGE_CODEC_ERROR_INVALID_UTF8
Definition: fl_json_message_codec.h:31
fl_value_get_int32_list
const G_MODULE_EXPORT int32_t * fl_value_get_int32_list(FlValue *self)
Definition: fl_value.cc:696
FL_VALUE_TYPE_UINT8_LIST
@ FL_VALUE_TYPE_UINT8_LIST
Definition: fl_value.h:70
FL_VALUE_TYPE_MAP
@ FL_VALUE_TYPE_MAP
Definition: fl_value.h:75
FL_JSON_MESSAGE_CODEC_ERROR_INVALID_OBJECT_KEY_TYPE
@ FL_JSON_MESSAGE_CODEC_ERROR_INVALID_OBJECT_KEY_TYPE
Definition: fl_json_message_codec.h:33
i
int i
Definition: fl_socket_accessible.cc:18
fl_value_get_float_list
const G_MODULE_EXPORT double * fl_value_get_float_list(FlValue *self)
Definition: fl_value.cc:717
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
FL_VALUE_TYPE_LIST
@ FL_VALUE_TYPE_LIST
Definition: fl_value.h:74
FlValueHandler::get_head
FlValue * get_head()
Definition: fl_json_message_codec.cc:147
fl_value_get_bool
G_MODULE_EXPORT bool fl_value_get_bool(FlValue *self)
Definition: fl_value.cc:661
fl_value_get_uint8_list
const G_MODULE_EXPORT uint8_t * fl_value_get_uint8_list(FlValue *self)
Definition: fl_value.cc:689
fl_value_get_string
const G_MODULE_EXPORT gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:682
FL_VALUE_TYPE_NULL
@ FL_VALUE_TYPE_NULL
Definition: fl_value.h:65
fl_value_get_int
G_MODULE_EXPORT int64_t fl_value_get_int(FlValue *self)
Definition: fl_value.cc:668
fl_value_ref
G_MODULE_EXPORT FlValue * fl_value_ref(FlValue *self)
Definition: fl_value.cc:394
fl_value_get_type
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:466
fl_value_get_list_value
G_MODULE_EXPORT FlValue * fl_value_get_list_value(FlValue *self, size_t index)
Definition: fl_value.cc:776
FL_VALUE_TYPE_STRING
@ FL_VALUE_TYPE_STRING
Definition: fl_value.h:69
TRUE
return TRUE
Definition: fl_pixel_buffer_texture_test.cc:53
fl_json_message_codec_encode_message
static GBytes * fl_json_message_codec_encode_message(FlMessageCodec *codec, FlValue *message, GError **error)
Definition: fl_json_message_codec.cc:243
fl_value_get_float
G_MODULE_EXPORT double fl_value_get_float(FlValue *self)
Definition: fl_value.cc:675
fl_value_get_length
G_MODULE_EXPORT size_t fl_value_get_length(FlValue *self)
Definition: fl_value.cc:724
FL_VALUE_TYPE_INT64_LIST
@ FL_VALUE_TYPE_INT64_LIST
Definition: fl_value.h:72
FL_VALUE_TYPE_INT
@ FL_VALUE_TYPE_INT
Definition: fl_value.h:67
fl_json_message_codec_decode_message
static FlValue * fl_json_message_codec_decode_message(FlMessageCodec *codec, GBytes *message, GError **error)
Definition: fl_json_message_codec.cc:258
error
const uint8_t uint32_t uint32_t GError ** error
Definition: fl_pixel_buffer_texture_test.cc:40
FlValueHandler
Definition: fl_json_message_codec.cc:124
FL_VALUE_TYPE_FLOAT_LIST
@ FL_VALUE_TYPE_FLOAT_LIST
Definition: fl_value.h:73
FL_JSON_MESSAGE_CODEC_ERROR
#define FL_JSON_MESSAGE_CODEC_ERROR
Definition: fl_json_message_codec.h:27
buffer
static const uint8_t buffer[]
Definition: fl_pixel_buffer_texture_test.cc:44
FL_VALUE_TYPE_FLOAT
@ FL_VALUE_TYPE_FLOAT
Definition: fl_value.h:68
FL_VALUE_TYPE_INT32_LIST
@ FL_VALUE_TYPE_INT32_LIST
Definition: fl_value.h:71
FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE
@ FL_MESSAGE_CODEC_ERROR_UNSUPPORTED_TYPE
Definition: fl_message_codec.h:37
FlValueHandler::error
GError * error
Definition: fl_json_message_codec.cc:127
FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON
@ FL_JSON_MESSAGE_CODEC_ERROR_INVALID_JSON
Definition: fl_json_message_codec.h:32
fl_value_get_map_key
G_MODULE_EXPORT FlValue * fl_value_get_map_key(FlValue *self, size_t index)
Definition: fl_value.cc:784
fl_value_get_int64_list
const G_MODULE_EXPORT int64_t * fl_value_get_int64_list(FlValue *self)
Definition: fl_value.cc:703
FL_VALUE_TYPE_BOOL
@ FL_VALUE_TYPE_BOOL
Definition: fl_value.h:66
value
uint8_t value
Definition: fl_standard_message_codec.cc:36
fl_value_get_map_value
G_MODULE_EXPORT FlValue * fl_value_get_map_value(FlValue *self, size_t index)
Definition: fl_value.cc:792
FL_MESSAGE_CODEC_ERROR
#define FL_MESSAGE_CODEC_ERROR
Definition: fl_message_codec.h:30