Flutter Linux Embedder
fl_method_response.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 
6 
7 #include <gmodule.h>
8 
9 G_DEFINE_QUARK(fl_method_response_error_quark, fl_method_response_error)
10 
12  FlMethodResponse parent_instance;
13 
15 };
16 
18  FlMethodResponse parent_instance;
19 
20  gchar* code;
21  gchar* message;
23 };
24 
26  FlMethodResponse parent_instance;
27 };
28 
29 G_DEFINE_TYPE(FlMethodResponse, fl_method_response, G_TYPE_OBJECT)
30 G_DEFINE_TYPE(FlMethodSuccessResponse,
31  fl_method_success_response,
33 G_DEFINE_TYPE(FlMethodErrorResponse,
36 G_DEFINE_TYPE(FlMethodNotImplementedResponse,
39 
40 static void fl_method_response_class_init(FlMethodResponseClass* klass) {}
41 
42 static void fl_method_response_init(FlMethodResponse* self) {}
43 
44 static void fl_method_success_response_dispose(GObject* object) {
45  FlMethodSuccessResponse* self = FL_METHOD_SUCCESS_RESPONSE(object);
46 
47  g_clear_pointer(&self->result, fl_value_unref);
48 
49  G_OBJECT_CLASS(fl_method_success_response_parent_class)->dispose(object);
50 }
51 
53  FlMethodSuccessResponseClass* klass) {
54  G_OBJECT_CLASS(klass)->dispose = fl_method_success_response_dispose;
55 }
56 
57 static void fl_method_success_response_init(FlMethodSuccessResponse* self) {}
58 
59 static void fl_method_error_response_dispose(GObject* object) {
60  FlMethodErrorResponse* self = FL_METHOD_ERROR_RESPONSE(object);
61 
62  g_clear_pointer(&self->code, g_free);
63  g_clear_pointer(&self->message, g_free);
64  g_clear_pointer(&self->details, fl_value_unref);
65 
66  G_OBJECT_CLASS(fl_method_error_response_parent_class)->dispose(object);
67 }
68 
70  FlMethodErrorResponseClass* klass) {
71  G_OBJECT_CLASS(klass)->dispose = fl_method_error_response_dispose;
72 }
73 
74 static void fl_method_error_response_init(FlMethodErrorResponse* self) {}
75 
77  FlMethodNotImplementedResponseClass* klass) {}
78 
80  FlMethodNotImplementedResponse* self) {}
81 
82 G_MODULE_EXPORT FlValue* fl_method_response_get_result(FlMethodResponse* self,
83  GError** error) {
84  if (FL_IS_METHOD_SUCCESS_RESPONSE(self)) {
86  FL_METHOD_SUCCESS_RESPONSE(self));
87  }
88 
89  if (FL_IS_METHOD_ERROR_RESPONSE(self)) {
90  const gchar* code =
91  fl_method_error_response_get_code(FL_METHOD_ERROR_RESPONSE(self));
92  const gchar* message =
93  fl_method_error_response_get_message(FL_METHOD_ERROR_RESPONSE(self));
94  FlValue* details =
95  fl_method_error_response_get_details(FL_METHOD_ERROR_RESPONSE(self));
96  g_autofree gchar* details_text = nullptr;
97  if (details != nullptr) {
98  details_text = fl_value_to_string(details);
99  }
100 
101  g_autoptr(GString) error_message = g_string_new("");
102  g_string_append_printf(error_message, "Remote code returned error %s",
103  code);
104  if (message != nullptr) {
105  g_string_append_printf(error_message, ": %s", message);
106  }
107  if (details_text != nullptr) {
108  g_string_append_printf(error_message, " %s", details_text);
109  }
110  g_set_error_literal(error, FL_METHOD_RESPONSE_ERROR,
112  error_message->str);
113  return nullptr;
114  } else if (FL_IS_METHOD_NOT_IMPLEMENTED_RESPONSE(self)) {
115  g_set_error(error, FL_METHOD_RESPONSE_ERROR,
117  "Requested method is not implemented");
118  return nullptr;
119  } else {
120  g_set_error(error, FL_METHOD_RESPONSE_ERROR,
121  FL_METHOD_RESPONSE_ERROR_FAILED, "Unknown response type");
122  return nullptr;
123  }
124 }
125 
126 G_MODULE_EXPORT FlMethodSuccessResponse* fl_method_success_response_new(
127  FlValue* result) {
128  FlMethodSuccessResponse* self = FL_METHOD_SUCCESS_RESPONSE(
129  g_object_new(fl_method_success_response_get_type(), nullptr));
130 
131  if (result != nullptr) {
132  self->result = fl_value_ref(result);
133  }
134 
135  return self;
136 }
137 
139  FlMethodSuccessResponse* self) {
140  g_return_val_if_fail(FL_IS_METHOD_SUCCESS_RESPONSE(self), nullptr);
141  return self->result;
142 }
143 
144 G_MODULE_EXPORT FlMethodErrorResponse* fl_method_error_response_new(
145  const gchar* code,
146  const gchar* message,
147  FlValue* details) {
148  g_return_val_if_fail(code != nullptr, nullptr);
149 
150  FlMethodErrorResponse* self = FL_METHOD_ERROR_RESPONSE(
151  g_object_new(fl_method_error_response_get_type(), nullptr));
152 
153  self->code = g_strdup(code);
154  self->message = g_strdup(message);
155  self->details = details != nullptr ? fl_value_ref(details) : nullptr;
156 
157  return self;
158 }
159 
160 G_MODULE_EXPORT const gchar* fl_method_error_response_get_code(
161  FlMethodErrorResponse* self) {
162  g_return_val_if_fail(FL_IS_METHOD_ERROR_RESPONSE(self), nullptr);
163  return self->code;
164 }
165 
166 G_MODULE_EXPORT const gchar* fl_method_error_response_get_message(
167  FlMethodErrorResponse* self) {
168  g_return_val_if_fail(FL_IS_METHOD_ERROR_RESPONSE(self), nullptr);
169  return self->message;
170 }
171 
173  FlMethodErrorResponse* self) {
174  g_return_val_if_fail(FL_IS_METHOD_ERROR_RESPONSE(self), nullptr);
175  return self->details;
176 }
177 
178 G_MODULE_EXPORT FlMethodNotImplementedResponse*
180  return FL_METHOD_NOT_IMPLEMENTED_RESPONSE(
181  g_object_new(fl_method_not_implemented_response_get_type(), nullptr));
182 }
_FlMethodNotImplementedResponse::parent_instance
FlMethodResponse parent_instance
Definition: fl_method_response.cc:26
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_method_not_implemented_response_new
G_MODULE_EXPORT FlMethodNotImplementedResponse * fl_method_not_implemented_response_new()
Definition: fl_method_response.cc:179
FL_METHOD_RESPONSE_ERROR_REMOTE_ERROR
@ FL_METHOD_RESPONSE_ERROR_REMOTE_ERROR
Definition: fl_method_response.h:35
_FlMethodErrorResponse
Definition: fl_method_response.cc:17
fl_method_success_response_dispose
static void fl_method_success_response_dispose(GObject *object)
Definition: fl_method_response.cc:44
FL_METHOD_RESPONSE_ERROR_NOT_IMPLEMENTED
@ FL_METHOD_RESPONSE_ERROR_NOT_IMPLEMENTED
Definition: fl_method_response.h:36
G_DEFINE_TYPE
G_DEFINE_TYPE(FlMethodSuccessResponse, fl_method_success_response, fl_method_response_get_type()) G_DEFINE_TYPE(FlMethodErrorResponse
_FlMethodNotImplementedResponse
Definition: fl_method_response.cc:25
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
fl_method_error_response_dispose
static void fl_method_error_response_dispose(GObject *object)
Definition: fl_method_response.cc:59
fl_method_response_get_result
G_MODULE_EXPORT FlValue * fl_method_response_get_result(FlMethodResponse *self, GError **error)
Definition: fl_method_response.cc:82
fl_method_response_get_type
fl_method_response_get_type()) G_DEFINE_TYPE(FlMethodNotImplementedResponse
Definition: fl_method_response.cc:38
fl_method_error_response_get_message
const G_MODULE_EXPORT gchar * fl_method_error_response_get_message(FlMethodErrorResponse *self)
Definition: fl_method_response.cc:166
fl_method_success_response_new
G_MODULE_EXPORT FlMethodSuccessResponse * fl_method_success_response_new(FlValue *result)
Definition: fl_method_response.cc:126
_FlMethodSuccessResponse::result
FlValue * result
Definition: fl_method_response.cc:14
fl_method_success_response_init
static void fl_method_success_response_init(FlMethodSuccessResponse *self)
Definition: fl_method_response.cc:57
FL_METHOD_RESPONSE_ERROR_FAILED
@ FL_METHOD_RESPONSE_ERROR_FAILED
Definition: fl_method_response.h:34
fl_method_response_init
static void fl_method_response_init(FlMethodResponse *self)
Definition: fl_method_response.cc:42
fl_method_error_response_init
static void fl_method_error_response_init(FlMethodErrorResponse *self)
Definition: fl_method_response.cc:74
fl_value_unref
G_MODULE_EXPORT void fl_value_unref(FlValue *self)
Definition: fl_value.cc:400
fl_method_success_response_class_init
static void fl_method_success_response_class_init(FlMethodSuccessResponseClass *klass)
Definition: fl_method_response.cc:52
fl_value_ref
G_MODULE_EXPORT FlValue * fl_value_ref(FlValue *self)
Definition: fl_value.cc:394
fl_method_error_response_get_details
G_MODULE_EXPORT FlValue * fl_method_error_response_get_details(FlMethodErrorResponse *self)
Definition: fl_method_response.cc:172
G_DEFINE_QUARK
G_DEFINE_QUARK(fl_binary_messenger_codec_error_quark, fl_binary_messenger_codec_error) G_DECLARE_FINAL_TYPE(FlBinaryMessengerImpl
_FlMethodErrorResponse::details
FlValue * details
Definition: fl_method_response.cc:22
fl_method_not_implemented_response_init
static void fl_method_not_implemented_response_init(FlMethodNotImplementedResponse *self)
Definition: fl_method_response.cc:79
FL_METHOD_RESPONSE_ERROR
#define FL_METHOD_RESPONSE_ERROR
Definition: fl_method_response.h:30
fl_method_success_response_get_result
G_MODULE_EXPORT FlValue * fl_method_success_response_get_result(FlMethodSuccessResponse *self)
Definition: fl_method_response.cc:138
fl_method_error_response
fl_method_error_response
Definition: fl_method_response.cc:34
fl_method_response.h
_FlMethodSuccessResponse::parent_instance
FlMethodResponse parent_instance
Definition: fl_method_response.cc:12
result
GAsyncResult * result
Definition: fl_text_input_plugin.cc:106
fl_method_error_response_get_code
const G_MODULE_EXPORT gchar * fl_method_error_response_get_code(FlMethodErrorResponse *self)
Definition: fl_method_response.cc:160
_FlMethodErrorResponse::code
gchar * code
Definition: fl_method_response.cc:20
error
const uint8_t uint32_t uint32_t GError ** error
Definition: fl_pixel_buffer_texture_test.cc:40
fl_method_error_response_class_init
static void fl_method_error_response_class_init(FlMethodErrorResponseClass *klass)
Definition: fl_method_response.cc:69
_FlMethodErrorResponse::parent_instance
FlMethodResponse parent_instance
Definition: fl_method_response.cc:18
_FlMethodSuccessResponse
Definition: fl_method_response.cc:11
fl_method_not_implemented_response_class_init
static void fl_method_not_implemented_response_class_init(FlMethodNotImplementedResponseClass *klass)
Definition: fl_method_response.cc:76
_FlMethodErrorResponse::message
gchar * message
Definition: fl_method_response.cc:21
fl_method_not_implemented_response
fl_method_not_implemented_response
Definition: fl_method_response.cc:37
fl_value_to_string
G_MODULE_EXPORT gchar * fl_value_to_string(FlValue *value)
Definition: fl_value.cc:846
fl_method_response_error_quark
G_MODULE_EXPORT GQuark fl_method_response_error_quark(void) G_GNUC_CONST