Flutter Linux Embedder
fl_standard_method_codec.cc File Reference

Go to the source code of this file.

Classes

struct  _FlStandardMethodCodec
 

Enumerations

enum  {
  kPropMessageCodec = 1,
  kPropLast
}
 

Functions

 G_DEFINE_TYPE (FlStandardMethodCodec, fl_standard_method_codec, fl_method_codec_get_type()) static void fl_standard_method_codec_set_property(GObject *object
 
 switch (prop_id)
 
static void fl_standard_method_codec_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
 
static void fl_standard_method_codec_dispose (GObject *object)
 
static GBytes * fl_standard_method_codec_encode_method_call (FlMethodCodec *codec, const gchar *name, FlValue *args, GError **error)
 
static gboolean fl_standard_method_codec_decode_method_call (FlMethodCodec *codec, GBytes *message, gchar **name, FlValue **args, GError **error)
 
static GBytes * fl_standard_method_codec_encode_success_envelope (FlMethodCodec *codec, FlValue *result, GError **error)
 
static GBytes * fl_standard_method_codec_encode_error_envelope (FlMethodCodec *codec, const gchar *code, const gchar *message, FlValue *details, GError **error)
 
static FlMethodResponse * fl_standard_method_codec_decode_response (FlMethodCodec *codec, GBytes *message, GError **error)
 
static void fl_standard_method_codec_class_init (FlStandardMethodCodecClass *klass)
 
static void fl_standard_method_codec_init (FlStandardMethodCodec *self)
 
G_MODULE_EXPORT FlStandardMethodCodec * fl_standard_method_codec_new ()
 
G_MODULE_EXPORT FlStandardMethodCodec * fl_standard_method_codec_new_with_message_codec (FlStandardMessageCodec *message_codec)
 

Variables

static constexpr guint8 kEnvelopeTypeSuccess = 0
 
static constexpr guint8 kEnvelopeTypeError = 1
 
guint prop_id
 
guint const GValue * value
 
guint const GValue GParamSpec * pspec
 

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
kPropMessageCodec 
kPropLast 

Definition at line 24 of file fl_standard_method_codec.cc.

Function Documentation

◆ fl_standard_method_codec_class_init()

static void fl_standard_method_codec_class_init ( FlStandardMethodCodecClass *  klass)
static

Definition at line 263 of file fl_standard_method_codec.cc.

264  {
265  G_OBJECT_CLASS(klass)->set_property = fl_standard_method_codec_set_property;
266  G_OBJECT_CLASS(klass)->get_property = fl_standard_method_codec_get_property;
267  G_OBJECT_CLASS(klass)->dispose = fl_standard_method_codec_dispose;
268 
269  FL_METHOD_CODEC_CLASS(klass)->encode_method_call =
271  FL_METHOD_CODEC_CLASS(klass)->decode_method_call =
273  FL_METHOD_CODEC_CLASS(klass)->encode_success_envelope =
275  FL_METHOD_CODEC_CLASS(klass)->encode_error_envelope =
277  FL_METHOD_CODEC_CLASS(klass)->decode_response =
279 
280  g_object_class_install_property(
281  G_OBJECT_CLASS(klass), kPropMessageCodec,
282  g_param_spec_object(
283  "message-codec", "message-codec", "Message codec to use",
284  fl_message_codec_get_type(),
285  static_cast<GParamFlags>(G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
286  G_PARAM_STATIC_STRINGS)));
287 }

References fl_standard_method_codec_decode_method_call(), fl_standard_method_codec_decode_response(), fl_standard_method_codec_dispose(), fl_standard_method_codec_encode_error_envelope(), fl_standard_method_codec_encode_method_call(), fl_standard_method_codec_encode_success_envelope(), fl_standard_method_codec_get_property(), and kPropMessageCodec.

◆ fl_standard_method_codec_decode_method_call()

static gboolean fl_standard_method_codec_decode_method_call ( FlMethodCodec *  codec,
GBytes *  message,
gchar **  name,
FlValue **  args,
GError **  error 
)
static

Definition at line 94 of file fl_standard_method_codec.cc.

99  {
100  FlStandardMethodCodec* self = FL_STANDARD_METHOD_CODEC(codec);
101 
102  size_t offset = 0;
103  g_autoptr(FlValue) name_value = fl_standard_message_codec_read_value(
104  self->message_codec, message, &offset, error);
105  if (name_value == nullptr) {
106  return FALSE;
107  }
108  if (fl_value_get_type(name_value) != FL_VALUE_TYPE_STRING) {
110  "Method call name wrong type");
111  return FALSE;
112  }
113 
114  g_autoptr(FlValue) args_value = fl_standard_message_codec_read_value(
115  self->message_codec, message, &offset, error);
116  if (args_value == nullptr) {
117  return FALSE;
118  }
119 
120  if (offset != g_bytes_get_size(message)) {
122  "Unexpected extra data");
123  return FALSE;
124  }
125 
126  *name = g_strdup(fl_value_get_string(name_value));
127  *args = fl_value_ref(args_value);
128 
129  return TRUE;
130 }

References args, error, FL_MESSAGE_CODEC_ERROR, FL_MESSAGE_CODEC_ERROR_FAILED, fl_standard_message_codec_read_value(), fl_value_get_string(), fl_value_get_type(), fl_value_ref(), FL_VALUE_TYPE_STRING, and TRUE.

Referenced by fl_standard_method_codec_class_init().

◆ fl_standard_method_codec_decode_response()

static FlMethodResponse* fl_standard_method_codec_decode_response ( FlMethodCodec *  codec,
GBytes *  message,
GError **  error 
)
static

Definition at line 184 of file fl_standard_method_codec.cc.

187  {
188  FlStandardMethodCodec* self = FL_STANDARD_METHOD_CODEC(codec);
189 
190  if (g_bytes_get_size(message) == 0) {
191  g_set_error(error, FL_MESSAGE_CODEC_ERROR,
192  FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA, "Empty response");
193  return nullptr;
194  }
195 
196  // First byte is response type.
197  const guint8* data =
198  static_cast<const guint8*>(g_bytes_get_data(message, nullptr));
199  guint8 type = data[0];
200  size_t offset = 1;
201 
202  g_autoptr(FlMethodResponse) response = nullptr;
203  if (type == kEnvelopeTypeError) {
205  self->message_codec, message, &offset, error);
206  if (code == nullptr) {
207  return nullptr;
208  }
211  "Error code wrong type");
212  return nullptr;
213  }
214 
215  g_autoptr(FlValue) error_message = fl_standard_message_codec_read_value(
216  self->message_codec, message, &offset, error);
217  if (error_message == nullptr) {
218  return nullptr;
219  }
220  if (fl_value_get_type(error_message) != FL_VALUE_TYPE_STRING &&
221  fl_value_get_type(error_message) != FL_VALUE_TYPE_NULL) {
223  "Error message wrong type");
224  return nullptr;
225  }
226 
227  g_autoptr(FlValue) details = fl_standard_message_codec_read_value(
228  self->message_codec, message, &offset, error);
229  if (details == nullptr) {
230  return nullptr;
231  }
232 
233  response = FL_METHOD_RESPONSE(fl_method_error_response_new(
234  fl_value_get_string(code),
235  fl_value_get_type(error_message) == FL_VALUE_TYPE_STRING
236  ? fl_value_get_string(error_message)
237  : nullptr,
238  fl_value_get_type(details) != FL_VALUE_TYPE_NULL ? details : nullptr));
239  } else if (type == kEnvelopeTypeSuccess) {
241  self->message_codec, message, &offset, error);
242 
243  if (result == nullptr) {
244  return nullptr;
245  }
246 
247  response = FL_METHOD_RESPONSE(fl_method_success_response_new(result));
248  } else {
250  "Unknown envelope type %02x", type);
251  return nullptr;
252  }
253 
254  if (offset != g_bytes_get_size(message)) {
256  "Unexpected extra data");
257  return nullptr;
258  }
259 
260  return FL_METHOD_RESPONSE(g_object_ref(response));
261 }

References error, FL_MESSAGE_CODEC_ERROR, FL_MESSAGE_CODEC_ERROR_FAILED, FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA, fl_method_error_response_new(), fl_method_success_response_new(), fl_standard_message_codec_read_value(), fl_value_get_string(), fl_value_get_type(), FL_VALUE_TYPE_NULL, FL_VALUE_TYPE_STRING, kEnvelopeTypeError, kEnvelopeTypeSuccess, result, and type.

Referenced by fl_standard_method_codec_class_init().

◆ fl_standard_method_codec_dispose()

static void fl_standard_method_codec_dispose ( GObject *  object)
static

Definition at line 63 of file fl_standard_method_codec.cc.

63  {
64  FlStandardMethodCodec* self = FL_STANDARD_METHOD_CODEC(object);
65 
66  g_clear_object(&self->message_codec);
67 
68  G_OBJECT_CLASS(fl_standard_method_codec_parent_class)->dispose(object);
69 }

Referenced by fl_standard_method_codec_class_init().

◆ fl_standard_method_codec_encode_error_envelope()

static GBytes* fl_standard_method_codec_encode_error_envelope ( FlMethodCodec *  codec,
const gchar *  code,
const gchar *  message,
FlValue details,
GError **  error 
)
static

Definition at line 152 of file fl_standard_method_codec.cc.

157  {
158  FlStandardMethodCodec* self = FL_STANDARD_METHOD_CODEC(codec);
159 
160  g_autoptr(GByteArray) buffer = g_byte_array_new();
161  guint8 type = kEnvelopeTypeError;
163  g_autoptr(FlValue) code_value = fl_value_new_string(code);
164  if (!fl_standard_message_codec_write_value(self->message_codec, buffer,
165  code_value, error)) {
166  return nullptr;
167  }
168  g_autoptr(FlValue) message_value =
169  message != nullptr ? fl_value_new_string(message) : nullptr;
170  if (!fl_standard_message_codec_write_value(self->message_codec, buffer,
171  message_value, error)) {
172  return nullptr;
173  }
174  if (!fl_standard_message_codec_write_value(self->message_codec, buffer,
175  details, error)) {
176  return nullptr;
177  }
178 
179  return g_byte_array_free_to_bytes(
180  static_cast<GByteArray*>(g_steal_pointer(&buffer)));
181 }

References buffer, error, fl_standard_message_codec_write_value(), fl_value_new_string(), g_byte_array_append(), kEnvelopeTypeError, and type.

Referenced by fl_standard_method_codec_class_init().

◆ fl_standard_method_codec_encode_method_call()

static GBytes* fl_standard_method_codec_encode_method_call ( FlMethodCodec *  codec,
const gchar *  name,
FlValue args,
GError **  error 
)
static

Definition at line 72 of file fl_standard_method_codec.cc.

75  {
76  FlStandardMethodCodec* self = FL_STANDARD_METHOD_CODEC(codec);
77 
78  g_autoptr(GByteArray) buffer = g_byte_array_new();
79  g_autoptr(FlValue) name_value = fl_value_new_string(name);
80  if (!fl_standard_message_codec_write_value(self->message_codec, buffer,
81  name_value, error)) {
82  return nullptr;
83  }
84  if (!fl_standard_message_codec_write_value(self->message_codec, buffer, args,
85  error)) {
86  return nullptr;
87  }
88 
89  return g_byte_array_free_to_bytes(
90  static_cast<GByteArray*>(g_steal_pointer(&buffer)));
91 }

References args, buffer, error, fl_standard_message_codec_write_value(), and fl_value_new_string().

Referenced by fl_standard_method_codec_class_init().

◆ fl_standard_method_codec_encode_success_envelope()

static GBytes* fl_standard_method_codec_encode_success_envelope ( FlMethodCodec *  codec,
FlValue result,
GError **  error 
)
static

Definition at line 133 of file fl_standard_method_codec.cc.

136  {
137  FlStandardMethodCodec* self = FL_STANDARD_METHOD_CODEC(codec);
138 
139  g_autoptr(GByteArray) buffer = g_byte_array_new();
140  guint8 type = kEnvelopeTypeSuccess;
142  if (!fl_standard_message_codec_write_value(self->message_codec, buffer,
143  result, error)) {
144  return nullptr;
145  }
146 
147  return g_byte_array_free_to_bytes(
148  static_cast<GByteArray*>(g_steal_pointer(&buffer)));
149 }

References buffer, error, fl_standard_message_codec_write_value(), g_byte_array_append(), kEnvelopeTypeSuccess, result, and type.

Referenced by fl_standard_method_codec_class_init().

◆ fl_standard_method_codec_get_property()

static void fl_standard_method_codec_get_property ( GObject *  object,
guint  prop_id,
GValue *  value,
GParamSpec *  pspec 
)
static

Definition at line 47 of file fl_standard_method_codec.cc.

50  {
51  FlStandardMethodCodec* self = FL_STANDARD_METHOD_CODEC(object);
52 
53  switch (prop_id) {
54  case kPropMessageCodec:
55  g_value_set_object(value, self->message_codec);
56  break;
57  default:
58  G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
59  break;
60  }
61 }

References kPropMessageCodec, prop_id, pspec, and value.

Referenced by fl_standard_method_codec_class_init().

◆ fl_standard_method_codec_init()

static void fl_standard_method_codec_init ( FlStandardMethodCodec *  self)
static

Definition at line 289 of file fl_standard_method_codec.cc.

289 {}

◆ fl_standard_method_codec_new()

◆ fl_standard_method_codec_new_with_message_codec()

G_MODULE_EXPORT FlStandardMethodCodec* fl_standard_method_codec_new_with_message_codec ( FlStandardMessageCodec *  message_codec)

fl_standard_method_codec_new: @message_codec: A #FlMessageCodec.

Creates an #FlStandardMethodCodec with a custom message codec.

Returns: a new #FlStandardMethodCodec.

Definition at line 298 of file fl_standard_method_codec.cc.

299  {
300  return FL_STANDARD_METHOD_CODEC(
301  g_object_new(fl_standard_method_codec_get_type(), "message-codec",
302  message_codec, nullptr));
303 }

Referenced by fl_standard_method_codec_new(), and TEST().

◆ G_DEFINE_TYPE()

G_DEFINE_TYPE ( FlStandardMethodCodec  ,
fl_standard_method_codec  ,
fl_method_codec_get_type()   
)

◆ switch()

switch ( prop_id  )

Definition at line 36 of file fl_standard_method_codec.cc.

36  {
37  case kPropMessageCodec:
38  g_set_object(&self->message_codec,
39  FL_STANDARD_MESSAGE_CODEC(g_value_get_object(value)));
40  break;
41  default:
42  G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
43  break;
44  }

References kPropMessageCodec, prop_id, pspec, and value.

Variable Documentation

◆ kEnvelopeTypeError

constexpr guint8 kEnvelopeTypeError = 1
staticconstexpr

◆ kEnvelopeTypeSuccess

constexpr guint8 kEnvelopeTypeSuccess = 0
staticconstexpr

◆ prop_id

◆ pspec

guint const GValue GParamSpec* pspec

◆ value

guint const GValue* value

Definition at line 32 of file fl_standard_method_codec.cc.

Referenced by fl_standard_method_codec_get_property(), and switch().

prop_id
guint prop_id
Definition: fl_standard_method_codec.cc:31
fl_method_error_response_new
G_MODULE_EXPORT FlMethodErrorResponse * fl_method_error_response_new(const gchar *code, const gchar *message, FlValue *details)
Definition: fl_method_response.cc:144
fl_standard_message_codec_new
G_MODULE_EXPORT FlStandardMessageCodec * fl_standard_message_codec_new()
Definition: fl_standard_message_codec.cc:637
type
uint8_t type
Definition: fl_standard_message_codec_test.cc:1115
fl_standard_message_codec_write_value
G_MODULE_EXPORT gboolean fl_standard_message_codec_write_value(FlStandardMessageCodec *self, GByteArray *buffer, FlValue *value, GError **error)
Definition: fl_standard_message_codec.cc:685
fl_standard_method_codec_encode_error_envelope
static GBytes * fl_standard_method_codec_encode_error_envelope(FlMethodCodec *codec, const gchar *code, const gchar *message, FlValue *details, GError **error)
Definition: fl_standard_method_codec.cc:152
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
fl_standard_method_codec_new_with_message_codec
G_MODULE_EXPORT FlStandardMethodCodec * fl_standard_method_codec_new_with_message_codec(FlStandardMessageCodec *message_codec)
Definition: fl_standard_method_codec.cc:298
fl_standard_method_codec_decode_method_call
static gboolean fl_standard_method_codec_decode_method_call(FlMethodCodec *codec, GBytes *message, gchar **name, FlValue **args, GError **error)
Definition: fl_standard_method_codec.cc:94
pspec
guint const GValue GParamSpec * pspec
Definition: fl_standard_method_codec.cc:33
fl_value_get_string
const G_MODULE_EXPORT gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:682
fl_method_success_response_new
G_MODULE_EXPORT FlMethodSuccessResponse * fl_method_success_response_new(FlValue *result)
Definition: fl_method_response.cc:126
FL_VALUE_TYPE_NULL
@ FL_VALUE_TYPE_NULL
Definition: fl_value.h:65
fl_value_ref
G_MODULE_EXPORT FlValue * fl_value_ref(FlValue *self)
Definition: fl_value.cc:394
g_byte_array_append
g_byte_array_append(buffer, &type, sizeof(uint8_t))
fl_value_get_type
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:466
FL_VALUE_TYPE_STRING
@ FL_VALUE_TYPE_STRING
Definition: fl_value.h:69
kEnvelopeTypeError
static constexpr guint8 kEnvelopeTypeError
Definition: fl_standard_method_codec.cc:16
fl_standard_method_codec_dispose
static void fl_standard_method_codec_dispose(GObject *object)
Definition: fl_standard_method_codec.cc:63
TRUE
return TRUE
Definition: fl_pixel_buffer_texture_test.cc:53
FL_MESSAGE_CODEC_ERROR_FAILED
@ FL_MESSAGE_CODEC_ERROR_FAILED
Definition: fl_message_codec.h:34
fl_standard_method_codec_get_property
static void fl_standard_method_codec_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
Definition: fl_standard_method_codec.cc:47
kPropMessageCodec
@ kPropMessageCodec
Definition: fl_standard_method_codec.cc:24
fl_standard_method_codec_encode_method_call
static GBytes * fl_standard_method_codec_encode_method_call(FlMethodCodec *codec, const gchar *name, FlValue *args, GError **error)
Definition: fl_standard_method_codec.cc:72
result
GAsyncResult * result
Definition: fl_text_input_plugin.cc:106
FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA
@ FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA
Definition: fl_message_codec.h:35
args
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
Definition: fl_event_channel.h:89
error
const uint8_t uint32_t uint32_t GError ** error
Definition: fl_pixel_buffer_texture_test.cc:40
fl_standard_method_codec_decode_response
static FlMethodResponse * fl_standard_method_codec_decode_response(FlMethodCodec *codec, GBytes *message, GError **error)
Definition: fl_standard_method_codec.cc:184
buffer
static const uint8_t buffer[]
Definition: fl_pixel_buffer_texture_test.cc:44
value
guint const GValue * value
Definition: fl_standard_method_codec.cc:32
kEnvelopeTypeSuccess
static constexpr guint8 kEnvelopeTypeSuccess
Definition: fl_standard_method_codec.cc:15
fl_standard_method_codec_encode_success_envelope
static GBytes * fl_standard_method_codec_encode_success_envelope(FlMethodCodec *codec, FlValue *result, GError **error)
Definition: fl_standard_method_codec.cc:133
fl_value_new_string
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:276
fl_standard_message_codec_read_value
G_MODULE_EXPORT FlValue * fl_standard_message_codec_read_value(FlStandardMessageCodec *self, GBytes *buffer, size_t *offset, GError **error)
Definition: fl_standard_message_codec.cc:694
kPropLast
@ kPropLast
Definition: fl_standard_method_codec.cc:24
FL_MESSAGE_CODEC_ERROR
#define FL_MESSAGE_CODEC_ERROR
Definition: fl_message_codec.h:30