Flutter Linux Embedder
fl_standard_method_codec.cc File Reference

Go to the source code of this file.

Classes

struct  _FlStandardMethodCodec
 

Functions

 G_DEFINE_TYPE (FlStandardMethodCodec, fl_standard_method_codec, fl_method_codec_get_type()) 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 ()
 

Variables

static constexpr guint8 kEnvelopeTypeSuccess = 0
 
static constexpr guint8 kEnvelopeTypeError = 1
 

Function Documentation

◆ fl_standard_method_codec_class_init()

static void fl_standard_method_codec_class_init ( FlStandardMethodCodecClass *  klass)
static

Definition at line 229 of file fl_standard_method_codec.cc.

230  {
231  G_OBJECT_CLASS(klass)->dispose = fl_standard_method_codec_dispose;
232  FL_METHOD_CODEC_CLASS(klass)->encode_method_call =
234  FL_METHOD_CODEC_CLASS(klass)->decode_method_call =
236  FL_METHOD_CODEC_CLASS(klass)->encode_success_envelope =
238  FL_METHOD_CODEC_CLASS(klass)->encode_error_envelope =
240  FL_METHOD_CODEC_CLASS(klass)->decode_response =
242 }

References fl_standard_method_codec_decode_method_call(), fl_standard_method_codec_decode_response(), fl_standard_method_codec_encode_error_envelope(), fl_standard_method_codec_encode_method_call(), and fl_standard_method_codec_encode_success_envelope().

◆ 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 60 of file fl_standard_method_codec.cc.

65  {
66  FlStandardMethodCodec* self = FL_STANDARD_METHOD_CODEC(codec);
67 
68  size_t offset = 0;
69  g_autoptr(FlValue) name_value = fl_standard_message_codec_read_value(
70  self->codec, message, &offset, error);
71  if (name_value == nullptr) {
72  return FALSE;
73  }
74  if (fl_value_get_type(name_value) != FL_VALUE_TYPE_STRING) {
76  "Method call name wrong type");
77  return FALSE;
78  }
79 
80  g_autoptr(FlValue) args_value = fl_standard_message_codec_read_value(
81  self->codec, message, &offset, error);
82  if (args_value == nullptr) {
83  return FALSE;
84  }
85 
86  if (offset != g_bytes_get_size(message)) {
88  "Unexpected extra data");
89  return FALSE;
90  }
91 
92  *name = g_strdup(fl_value_get_string(name_value));
93  *args = fl_value_ref(args_value);
94 
95  return TRUE;
96 }

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, self, 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 150 of file fl_standard_method_codec.cc.

153  {
154  FlStandardMethodCodec* self = FL_STANDARD_METHOD_CODEC(codec);
155 
156  if (g_bytes_get_size(message) == 0) {
157  g_set_error(error, FL_MESSAGE_CODEC_ERROR,
158  FL_MESSAGE_CODEC_ERROR_OUT_OF_DATA, "Empty response");
159  return nullptr;
160  }
161 
162  // First byte is response type.
163  const guint8* data =
164  static_cast<const guint8*>(g_bytes_get_data(message, nullptr));
165  guint8 type = data[0];
166  size_t offset = 1;
167 
168  g_autoptr(FlMethodResponse) response = nullptr;
169  if (type == kEnvelopeTypeError) {
171  self->codec, message, &offset, error);
172  if (code == nullptr) {
173  return nullptr;
174  }
177  "Error code wrong type");
178  return nullptr;
179  }
180 
181  g_autoptr(FlValue) error_message = fl_standard_message_codec_read_value(
182  self->codec, message, &offset, error);
183  if (error_message == nullptr) {
184  return nullptr;
185  }
186  if (fl_value_get_type(error_message) != FL_VALUE_TYPE_STRING &&
187  fl_value_get_type(error_message) != FL_VALUE_TYPE_NULL) {
189  "Error message wrong type");
190  return nullptr;
191  }
192 
193  g_autoptr(FlValue) details = fl_standard_message_codec_read_value(
194  self->codec, message, &offset, error);
195  if (details == nullptr) {
196  return nullptr;
197  }
198 
199  response = FL_METHOD_RESPONSE(fl_method_error_response_new(
200  fl_value_get_string(code),
201  fl_value_get_type(error_message) == FL_VALUE_TYPE_STRING
202  ? fl_value_get_string(error_message)
203  : nullptr,
204  fl_value_get_type(details) != FL_VALUE_TYPE_NULL ? details : nullptr));
205  } else if (type == kEnvelopeTypeSuccess) {
207  self->codec, message, &offset, error);
208 
209  if (result == nullptr) {
210  return nullptr;
211  }
212 
213  response = FL_METHOD_RESPONSE(fl_method_success_response_new(result));
214  } else {
216  "Unknown envelope type %02x", type);
217  return nullptr;
218  }
219 
220  if (offset != g_bytes_get_size(message)) {
222  "Unexpected extra data");
223  return nullptr;
224  }
225 
226  return FL_METHOD_RESPONSE(g_object_ref(response));
227 }

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 self.

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 118 of file fl_standard_method_codec.cc.

123  {
124  FlStandardMethodCodec* self = FL_STANDARD_METHOD_CODEC(codec);
125 
126  g_autoptr(GByteArray) buffer = g_byte_array_new();
127  guint8 type = kEnvelopeTypeError;
128  g_byte_array_append(buffer, &type, 1);
129  g_autoptr(FlValue) code_value = fl_value_new_string(code);
130  if (!fl_standard_message_codec_write_value(self->codec, buffer, code_value,
131  error)) {
132  return nullptr;
133  }
134  g_autoptr(FlValue) message_value =
135  message != nullptr ? fl_value_new_string(message) : nullptr;
136  if (!fl_standard_message_codec_write_value(self->codec, buffer, message_value,
137  error)) {
138  return nullptr;
139  }
140  if (!fl_standard_message_codec_write_value(self->codec, buffer, details,
141  error)) {
142  return nullptr;
143  }
144 
145  return g_byte_array_free_to_bytes(
146  static_cast<GByteArray*>(g_steal_pointer(&buffer)));
147 }

References buffer, error, fl_standard_message_codec_write_value(), fl_value_new_string(), kEnvelopeTypeError, and self.

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 38 of file fl_standard_method_codec.cc.

41  {
42  FlStandardMethodCodec* self = FL_STANDARD_METHOD_CODEC(codec);
43 
44  g_autoptr(GByteArray) buffer = g_byte_array_new();
45  g_autoptr(FlValue) name_value = fl_value_new_string(name);
46  if (!fl_standard_message_codec_write_value(self->codec, buffer, name_value,
47  error)) {
48  return nullptr;
49  }
51  error)) {
52  return nullptr;
53  }
54 
55  return g_byte_array_free_to_bytes(
56  static_cast<GByteArray*>(g_steal_pointer(&buffer)));
57 }

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

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 99 of file fl_standard_method_codec.cc.

102  {
103  FlStandardMethodCodec* self = FL_STANDARD_METHOD_CODEC(codec);
104 
105  g_autoptr(GByteArray) buffer = g_byte_array_new();
106  guint8 type = kEnvelopeTypeSuccess;
107  g_byte_array_append(buffer, &type, 1);
109  error)) {
110  return nullptr;
111  }
112 
113  return g_byte_array_free_to_bytes(
114  static_cast<GByteArray*>(g_steal_pointer(&buffer)));
115 }

References buffer, error, fl_standard_message_codec_write_value(), kEnvelopeTypeSuccess, result, and self.

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 244 of file fl_standard_method_codec.cc.

244  {
245  self->codec = fl_standard_message_codec_new();
246 }

References fl_standard_message_codec_new().

◆ fl_standard_method_codec_new()

◆ G_DEFINE_TYPE()

G_DEFINE_TYPE ( FlStandardMethodCodec  ,
fl_standard_method_codec  ,
fl_method_codec_get_type()   
)

Definition at line 25 of file fl_standard_method_codec.cc.

29  {
30  FlStandardMethodCodec* self = FL_STANDARD_METHOD_CODEC(object);
31 
32  g_clear_object(&self->codec);
33 
34  G_OBJECT_CLASS(fl_standard_method_codec_parent_class)->dispose(object);
35 }

References self.

Variable Documentation

◆ kEnvelopeTypeError

constexpr guint8 kEnvelopeTypeError = 1
staticconstexpr

◆ kEnvelopeTypeSuccess

constexpr guint8 kEnvelopeTypeSuccess = 0
staticconstexpr
fl_standard_message_codec_read_value
FlValue * fl_standard_message_codec_read_value(FlStandardMessageCodec *self, GBytes *buffer, size_t *offset, GError **error)
Definition: fl_standard_message_codec.cc:633
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:458
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:118
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:40
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:60
fl_value_get_string
const G_MODULE_EXPORT gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:642
fl_method_success_response_new
G_MODULE_EXPORT FlMethodSuccessResponse * fl_method_success_response_new(FlValue *result)
Definition: fl_method_response.cc:126
_FlValue::type
FlValueType type
Definition: fl_value.cc:12
FL_VALUE_TYPE_NULL
@ FL_VALUE_TYPE_NULL
Definition: fl_value.h:60
fl_value_ref
G_MODULE_EXPORT FlValue * fl_value_ref(FlValue *self)
Definition: fl_value.cc:363
fl_value_get_type
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:428
FL_VALUE_TYPE_STRING
@ FL_VALUE_TYPE_STRING
Definition: fl_value.h:64
kEnvelopeTypeError
static constexpr guint8 kEnvelopeTypeError
Definition: fl_standard_method_codec.cc:17
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:33
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:38
self
GdkEvent FlView * self
Definition: fl_view.cc:100
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:34
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:150
buffer
static const uint8_t buffer[]
Definition: fl_pixel_buffer_texture_test.cc:44
kEnvelopeTypeSuccess
static constexpr guint8 kEnvelopeTypeSuccess
Definition: fl_standard_method_codec.cc:16
fl_standard_message_codec_write_value
gboolean fl_standard_message_codec_write_value(FlStandardMessageCodec *self, GByteArray *buffer, FlValue *value, GError **error)
Definition: fl_standard_message_codec.cc:504
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:99
fl_value_new_string
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:265
FL_MESSAGE_CODEC_ERROR
#define FL_MESSAGE_CODEC_ERROR
Definition: fl_message_codec.h:30