Flutter Engine
fl_json_method_codec.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "flutter/shell/platform/linux/public/flutter_linux/fl_json_method_codec.h"
6 
7 #include <gmodule.h>
8 
9 #include "flutter/shell/platform/linux/public/flutter_linux/fl_json_message_codec.h"
10 
11 static constexpr char kMethodKey[] = "method";
12 static constexpr char kArgsKey[] = "args";
13 
15  FlMethodCodec parent_instance;
16 
17  FlJsonMessageCodec* codec;
18 };
19 
20 G_DEFINE_TYPE(FlJsonMethodCodec,
21  fl_json_method_codec,
23 
24 static void fl_json_method_codec_dispose(GObject* object) {
25  FlJsonMethodCodec* self = FL_JSON_METHOD_CODEC(object);
26 
27  g_clear_object(&self->codec);
28 
29  G_OBJECT_CLASS(fl_json_method_codec_parent_class)->dispose(object);
30 }
31 
32 // Implements FlMethodCodec::encode_method_call.
33 static GBytes* fl_json_method_codec_encode_method_call(FlMethodCodec* codec,
34  const gchar* name,
35  FlValue* args,
36  GError** error) {
37  FlJsonMethodCodec* self = FL_JSON_METHOD_CODEC(codec);
38 
39  g_autoptr(FlValue) message = fl_value_new_map();
41  fl_value_new_string(name));
43  args != nullptr ? fl_value_ref(args) : fl_value_new_null());
44 
45  return fl_message_codec_encode_message(FL_MESSAGE_CODEC(self->codec), message,
46  error);
47 }
48 
49 // Implements FlMethodCodec::decode_method_call.
50 static gboolean fl_json_method_codec_decode_method_call(FlMethodCodec* codec,
51  GBytes* message,
52  gchar** name,
53  FlValue** args,
54  GError** error) {
55  FlJsonMethodCodec* self = FL_JSON_METHOD_CODEC(codec);
56 
58  FL_MESSAGE_CODEC(self->codec), message, error);
59  if (value == nullptr) {
60  return FALSE;
61  }
62 
65  "Expected JSON map in method resonse, got %d instead",
67  return FALSE;
68  }
69 
71  if (method_value == nullptr) {
73  "Missing JSON method field in method resonse");
74  return FALSE;
75  }
76  if (fl_value_get_type(method_value) != FL_VALUE_TYPE_STRING) {
78  "Expected JSON string for method name, got %d instead",
79  fl_value_get_type(method_value));
80  return FALSE;
81  }
83 
84  *name = g_strdup(fl_value_get_string(method_value));
85  *args = args_value != nullptr ? fl_value_ref(args_value) : nullptr;
86 
87  return TRUE;
88 }
89 
90 // Implements FlMethodCodec::encode_success_envelope.
92  FlMethodCodec* codec,
93  FlValue* result,
94  GError** error) {
95  FlJsonMethodCodec* self = FL_JSON_METHOD_CODEC(codec);
96 
97  g_autoptr(FlValue) message = fl_value_new_list();
99  message, result != nullptr ? fl_value_ref(result) : fl_value_new_null());
100 
101  return fl_message_codec_encode_message(FL_MESSAGE_CODEC(self->codec), message,
102  error);
103 }
104 
105 // Implements FlMethodCodec::encode_error_envelope.
107  FlMethodCodec* codec,
108  const gchar* code,
109  const gchar* error_message,
110  FlValue* details,
111  GError** error) {
112  FlJsonMethodCodec* self = FL_JSON_METHOD_CODEC(codec);
113 
114  g_autoptr(FlValue) message = fl_value_new_list();
116  fl_value_append_take(message, error_message != nullptr
117  ? fl_value_new_string(error_message)
118  : fl_value_new_null());
119  fl_value_append_take(message, details != nullptr ? fl_value_ref(details)
120  : fl_value_new_null());
121 
122  return fl_message_codec_encode_message(FL_MESSAGE_CODEC(self->codec), message,
123  error);
124 }
125 
126 // Implements FlMethodCodec::decode_response.
127 static FlMethodResponse* fl_json_method_codec_decode_response(
128  FlMethodCodec* codec,
129  GBytes* message,
130  GError** error) {
131  FlJsonMethodCodec* self = FL_JSON_METHOD_CODEC(codec);
132 
134  FL_MESSAGE_CODEC(self->codec), message, error);
135  if (value == nullptr) {
136  return nullptr;
137  }
138 
141  "Expected JSON list in method resonse, got %d instead",
143  return nullptr;
144  }
145 
146  size_t length = fl_value_get_length(value);
147  if (length == 1) {
148  return FL_METHOD_RESPONSE(
150  } else if (length == 3) {
151  FlValue* code_value = fl_value_get_list_value(value, 0);
152  if (fl_value_get_type(code_value) != FL_VALUE_TYPE_STRING) {
154  "Error code wrong type");
155  return nullptr;
156  }
157  const gchar* code = fl_value_get_string(code_value);
158 
159  FlValue* message_value = fl_value_get_list_value(value, 1);
160  if (fl_value_get_type(message_value) != FL_VALUE_TYPE_STRING &&
161  fl_value_get_type(message_value) != FL_VALUE_TYPE_NULL) {
163  "Error message wrong type");
164  return nullptr;
165  }
166  const gchar* message =
167  fl_value_get_type(message_value) == FL_VALUE_TYPE_STRING
168  ? fl_value_get_string(message_value)
169  : nullptr;
170 
172  if (fl_value_get_type(args) == FL_VALUE_TYPE_NULL) {
173  args = nullptr;
174  }
175 
176  return FL_METHOD_RESPONSE(
177  fl_method_error_response_new(code, message, args));
178  } else {
180  "Got response envelope of length %zi, expected 1 (success) or "
181  "3 (error)",
182  length);
183  return nullptr;
184  }
185 }
186 
187 static void fl_json_method_codec_class_init(FlJsonMethodCodecClass* klass) {
188  G_OBJECT_CLASS(klass)->dispose = fl_json_method_codec_dispose;
189  FL_METHOD_CODEC_CLASS(klass)->encode_method_call =
191  FL_METHOD_CODEC_CLASS(klass)->decode_method_call =
193  FL_METHOD_CODEC_CLASS(klass)->encode_success_envelope =
195  FL_METHOD_CODEC_CLASS(klass)->encode_error_envelope =
197  FL_METHOD_CODEC_CLASS(klass)->decode_response =
199 }
200 
201 static void fl_json_method_codec_init(FlJsonMethodCodec* self) {
202  self->codec = fl_json_message_codec_new();
203 }
204 
205 G_MODULE_EXPORT FlJsonMethodCodec* fl_json_method_codec_new() {
206  return static_cast<FlJsonMethodCodec*>(
207  g_object_new(fl_json_method_codec_get_type(), nullptr));
208 }
G_MODULE_EXPORT FlValue * fl_value_new_list()
Definition: fl_value.cc:310
G_BEGIN_DECLS FlValue * args
FlMethodCodec parent_instance
G_MODULE_EXPORT FlMethodSuccessResponse * fl_method_success_response_new(FlValue *result)
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:395
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:39
G_MODULE_EXPORT FlJsonMethodCodec * fl_json_method_codec_new()
FlMethodResponse GError ** error
static gboolean fl_json_method_codec_decode_method_call(FlMethodCodec *codec, GBytes *message, gchar **name, FlValue **args, GError **error)
G_MODULE_EXPORT FlValue * fl_value_new_map()
Definition: fl_value.cc:327
static void fl_json_method_codec_init(FlJsonMethodCodec *self)
#define FL_MESSAGE_CODEC_ERROR
G_MODULE_EXPORT FlJsonMessageCodec * fl_json_message_codec_new()
static constexpr char kArgsKey[]
G_DEFINE_TYPE(FlJsonMethodCodec, fl_json_method_codec, fl_method_codec_get_type()) static void fl_json_method_codec_dispose(GObject *object)
G_MODULE_EXPORT void fl_value_append_take(FlValue *self, FlValue *value)
Definition: fl_value.cc:514
uint8_t value
G_MODULE_EXPORT void fl_value_set_take(FlValue *self, FlValue *key, FlValue *value)
Definition: fl_value.cc:532
G_MODULE_EXPORT FlValue * fl_value_lookup_string(FlValue *self, const gchar *key)
Definition: fl_value.cc:712
static GBytes * fl_json_method_codec_encode_success_envelope(FlMethodCodec *codec, FlValue *result, GError **error)
size_t length
static void fl_json_method_codec_class_init(FlJsonMethodCodecClass *klass)
G_MODULE_EXPORT FlValue * fl_value_get_list_value(FlValue *self, size_t index)
Definition: fl_value.cc:677
static GBytes * fl_json_method_codec_encode_error_envelope(FlMethodCodec *codec, const gchar *code, const gchar *error_message, FlValue *details, GError **error)
FlJsonMessageCodec * codec
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:247
static constexpr char kMethodKey[]
G_MODULE_EXPORT FlValue * fl_value_ref(FlValue *self)
Definition: fl_value.cc:335
const char * name
Definition: fuchsia.cc:50
static GBytes * fl_json_method_codec_encode_method_call(FlMethodCodec *codec, const gchar *name, FlValue *args, GError **error)
G_MODULE_EXPORT GBytes * fl_message_codec_encode_message(FlMessageCodec *self, FlValue *message, GError **error)
return TRUE
Definition: fl_view.cc:107
G_MODULE_EXPORT const gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:596
G_MODULE_EXPORT FlValue * fl_message_codec_decode_message(FlMessageCodec *self, GBytes *message, GError **error)
G_MODULE_EXPORT GType fl_method_codec_get_type()
G_MODULE_EXPORT size_t fl_value_get_length(FlValue *self)
Definition: fl_value.cc:631
G_MODULE_EXPORT FlMethodErrorResponse * fl_method_error_response_new(const gchar *code, const gchar *message, FlValue *details)
G_MODULE_EXPORT FlValue * fl_value_new_null()
Definition: fl_value.cc:222
static FlMethodResponse * fl_json_method_codec_decode_response(FlMethodCodec *codec, GBytes *message, GError **error)