Flutter Engine
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 
5 #include "flutter/shell/platform/linux/public/flutter_linux/fl_method_response.h"
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 // Added here to stop the compiler from optimising these functions away.
30 G_MODULE_EXPORT GType fl_method_response_get_type();
31 G_MODULE_EXPORT GType fl_method_success_response_get_type();
32 G_MODULE_EXPORT GType fl_method_error_response_get_type();
33 G_MODULE_EXPORT GType fl_method_not_implemented_response_get_type();
34 
35 G_DEFINE_TYPE(FlMethodResponse, fl_method_response, G_TYPE_OBJECT)
36 G_DEFINE_TYPE(FlMethodSuccessResponse,
37  fl_method_success_response,
39 G_DEFINE_TYPE(FlMethodErrorResponse,
42 G_DEFINE_TYPE(FlMethodNotImplementedResponse,
45 
46 static void fl_method_response_class_init(FlMethodResponseClass* klass) {}
47 
48 static void fl_method_response_init(FlMethodResponse* self) {}
49 
50 static void fl_method_success_response_dispose(GObject* object) {
51  FlMethodSuccessResponse* self = FL_METHOD_SUCCESS_RESPONSE(object);
52 
53  g_clear_pointer(&self->result, fl_value_unref);
54 
55  G_OBJECT_CLASS(fl_method_success_response_parent_class)->dispose(object);
56 }
57 
59  FlMethodSuccessResponseClass* klass) {
60  G_OBJECT_CLASS(klass)->dispose = fl_method_success_response_dispose;
61 }
62 
63 static void fl_method_success_response_init(FlMethodSuccessResponse* self) {}
64 
65 static void fl_method_error_response_dispose(GObject* object) {
66  FlMethodErrorResponse* self = FL_METHOD_ERROR_RESPONSE(object);
67 
68  g_clear_pointer(&self->code, g_free);
69  g_clear_pointer(&self->message, g_free);
70  g_clear_pointer(&self->details, fl_value_unref);
71 
72  G_OBJECT_CLASS(fl_method_error_response_parent_class)->dispose(object);
73 }
74 
76  FlMethodErrorResponseClass* klass) {
77  G_OBJECT_CLASS(klass)->dispose = fl_method_error_response_dispose;
78 }
79 
80 static void fl_method_error_response_init(FlMethodErrorResponse* self) {}
81 
83  FlMethodNotImplementedResponseClass* klass) {}
84 
86  FlMethodNotImplementedResponse* self) {}
87 
88 G_MODULE_EXPORT FlValue* fl_method_response_get_result(FlMethodResponse* self,
89  GError** error) {
90  if (FL_IS_METHOD_SUCCESS_RESPONSE(self)) {
92  FL_METHOD_SUCCESS_RESPONSE(self));
93  }
94 
95  if (FL_IS_METHOD_ERROR_RESPONSE(self)) {
96  const gchar* code =
97  fl_method_error_response_get_code(FL_METHOD_ERROR_RESPONSE(self));
98  const gchar* message =
99  fl_method_error_response_get_message(FL_METHOD_ERROR_RESPONSE(self));
100  FlValue* details =
101  fl_method_error_response_get_details(FL_METHOD_ERROR_RESPONSE(self));
102  g_autofree gchar* details_text = nullptr;
103  if (details != nullptr) {
104  details_text = fl_value_to_string(details);
105  }
106 
107  g_autoptr(GString) error_message = g_string_new("");
108  g_string_append_printf(error_message, "Remote code returned error %s",
109  code);
110  if (message != nullptr) {
111  g_string_append_printf(error_message, ": %s", message);
112  }
113  if (details_text != nullptr) {
114  g_string_append_printf(error_message, " %s", details_text);
115  }
116  g_set_error_literal(error, FL_METHOD_RESPONSE_ERROR,
118  error_message->str);
119  return nullptr;
120  } else if (FL_IS_METHOD_NOT_IMPLEMENTED_RESPONSE(self)) {
121  g_set_error(error, FL_METHOD_RESPONSE_ERROR,
123  "Requested method is not implemented");
124  return nullptr;
125  } else {
126  g_set_error(error, FL_METHOD_RESPONSE_ERROR,
127  FL_METHOD_RESPONSE_ERROR_FAILED, "Unknown response type");
128  return nullptr;
129  }
130 }
131 
132 G_MODULE_EXPORT FlMethodSuccessResponse* fl_method_success_response_new(
133  FlValue* result) {
134  FlMethodSuccessResponse* self = FL_METHOD_SUCCESS_RESPONSE(
135  g_object_new(fl_method_success_response_get_type(), nullptr));
136 
137  if (result != nullptr) {
138  self->result = fl_value_ref(result);
139  }
140 
141  return self;
142 }
143 
145  FlMethodSuccessResponse* self) {
146  g_return_val_if_fail(FL_IS_METHOD_SUCCESS_RESPONSE(self), nullptr);
147  return self->result;
148 }
149 
150 G_MODULE_EXPORT FlMethodErrorResponse* fl_method_error_response_new(
151  const gchar* code,
152  const gchar* message,
153  FlValue* details) {
154  g_return_val_if_fail(code != nullptr, nullptr);
155 
156  FlMethodErrorResponse* self = FL_METHOD_ERROR_RESPONSE(
157  g_object_new(fl_method_error_response_get_type(), nullptr));
158 
159  self->code = g_strdup(code);
160  self->message = g_strdup(message);
161  self->details = details != nullptr ? fl_value_ref(details) : nullptr;
162 
163  return self;
164 }
165 
166 G_MODULE_EXPORT const gchar* fl_method_error_response_get_code(
167  FlMethodErrorResponse* self) {
168  g_return_val_if_fail(FL_IS_METHOD_ERROR_RESPONSE(self), nullptr);
169  return self->code;
170 }
171 
172 G_MODULE_EXPORT const gchar* fl_method_error_response_get_message(
173  FlMethodErrorResponse* self) {
174  g_return_val_if_fail(FL_IS_METHOD_ERROR_RESPONSE(self), nullptr);
175  return self->message;
176 }
177 
179  FlMethodErrorResponse* self) {
180  g_return_val_if_fail(FL_IS_METHOD_ERROR_RESPONSE(self), nullptr);
181  return self->details;
182 }
183 
184 G_MODULE_EXPORT FlMethodNotImplementedResponse*
186  return FL_METHOD_NOT_IMPLEMENTED_RESPONSE(
187  g_object_new(fl_method_not_implemented_response_get_type(), nullptr));
188 }
fl_method_not_implemented_response
G_MODULE_EXPORT FlMethodSuccessResponse * fl_method_success_response_new(FlValue *result)
static void fl_method_error_response_init(FlMethodErrorResponse *self)
static void fl_method_error_response_dispose(GObject *object)
G_MODULE_EXPORT void fl_value_unref(FlValue *self)
Definition: fl_value.cc:341
fl_method_error_response
static void fl_method_success_response_dispose(GObject *object)
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:39
FlMethodResponse GError ** error
G_MODULE_EXPORT GType fl_method_not_implemented_response_get_type()
static void fl_method_not_implemented_response_init(FlMethodNotImplementedResponse *self)
GQuark fl_method_response_error_quark(void) G_GNUC_CONST
static void fl_method_response_init(FlMethodResponse *self)
G_DEFINE_QUARK(fl_binary_messenger_codec_error_quark, fl_binary_messenger_codec_error) struct _FlBinaryMessenger
G_MODULE_EXPORT FlValue * fl_method_success_response_get_result(FlMethodSuccessResponse *self)
G_DEFINE_TYPE(FlMethodSuccessResponse, fl_method_success_response, fl_method_response_get_type()) G_DEFINE_TYPE(FlMethodErrorResponse
G_MODULE_EXPORT const gchar * fl_method_error_response_get_message(FlMethodErrorResponse *self)
G_MODULE_EXPORT GType fl_method_error_response_get_type()
G_MODULE_EXPORT GType fl_method_success_response_get_type()
static void fl_method_success_response_class_init(FlMethodSuccessResponseClass *klass)
static void fl_method_not_implemented_response_class_init(FlMethodNotImplementedResponseClass *klass)
G_MODULE_EXPORT GType fl_method_response_get_type()
static void fl_method_error_response_class_init(FlMethodErrorResponseClass *klass)
FlMethodResponse parent_instance
G_MODULE_EXPORT gchar * fl_value_to_string(FlValue *value)
Definition: fl_value.cc:723
G_MODULE_EXPORT FlValue * fl_value_ref(FlValue *self)
Definition: fl_value.cc:335
#define FL_METHOD_RESPONSE_ERROR
G_MODULE_EXPORT const gchar * fl_method_error_response_get_code(FlMethodErrorResponse *self)
G_MODULE_EXPORT FlValue * fl_method_response_get_result(FlMethodResponse *self, GError **error)
G_MODULE_EXPORT FlMethodNotImplementedResponse * fl_method_not_implemented_response_new()
static void fl_method_success_response_init(FlMethodSuccessResponse *self)
FlMethodResponse parent_instance
G_MODULE_EXPORT FlValue * fl_method_error_response_get_details(FlMethodErrorResponse *self)
G_MODULE_EXPORT FlMethodErrorResponse * fl_method_error_response_new(const gchar *code, const gchar *message, FlValue *details)