Flutter Linux Embedder
fl_method_call.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 
8 
9 #include <gmodule.h>
10 
11 struct _FlMethodCall {
12  GObject parent_instance;
13 
14  // Name of method being called.
15  gchar* name;
16 
17  // Arguments provided to method call.
19 
20  // Channel to respond on.
21  FlMethodChannel* channel;
22  FlBinaryMessengerResponseHandle* response_handle;
23 };
24 
25 G_DEFINE_TYPE(FlMethodCall, fl_method_call, G_TYPE_OBJECT)
26 
27 static void fl_method_call_dispose(GObject* object) {
28  FlMethodCall* self = FL_METHOD_CALL(object);
29 
30  g_clear_pointer(&self->name, g_free);
31  g_clear_pointer(&self->args, fl_value_unref);
32  g_clear_object(&self->channel);
33  g_clear_object(&self->response_handle);
34 
35  G_OBJECT_CLASS(fl_method_call_parent_class)->dispose(object);
36 }
37 
38 static void fl_method_call_class_init(FlMethodCallClass* klass) {
39  G_OBJECT_CLASS(klass)->dispose = fl_method_call_dispose;
40 }
41 
42 static void fl_method_call_init(FlMethodCall* self) {}
43 
44 FlMethodCall* fl_method_call_new(
45  const gchar* name,
46  FlValue* args,
47  FlMethodChannel* channel,
48  FlBinaryMessengerResponseHandle* response_handle) {
49  g_return_val_if_fail(name != nullptr, nullptr);
50  g_return_val_if_fail(args != nullptr, nullptr);
51  g_return_val_if_fail(FL_IS_METHOD_CHANNEL(channel), nullptr);
52  g_return_val_if_fail(FL_IS_BINARY_MESSENGER_RESPONSE_HANDLE(response_handle),
53  nullptr);
54 
55  FlMethodCall* self =
56  FL_METHOD_CALL(g_object_new(fl_method_call_get_type(), nullptr));
57 
58  self->name = g_strdup(name);
59  self->args = fl_value_ref(args);
60  self->channel = FL_METHOD_CHANNEL(g_object_ref(channel));
61  self->response_handle =
62  FL_BINARY_MESSENGER_RESPONSE_HANDLE(g_object_ref(response_handle));
63 
64  return self;
65 }
66 
67 G_MODULE_EXPORT const gchar* fl_method_call_get_name(FlMethodCall* self) {
68  g_return_val_if_fail(FL_IS_METHOD_CALL(self), nullptr);
69  return self->name;
70 }
71 
72 G_MODULE_EXPORT FlValue* fl_method_call_get_args(FlMethodCall* self) {
73  g_return_val_if_fail(FL_IS_METHOD_CALL(self), nullptr);
74  return self->args;
75 }
76 
77 G_MODULE_EXPORT gboolean fl_method_call_respond(FlMethodCall* self,
78  FlMethodResponse* response,
79  GError** error) {
80  g_return_val_if_fail(FL_IS_METHOD_CALL(self), FALSE);
81  g_return_val_if_fail(FL_IS_METHOD_RESPONSE(response), FALSE);
82 
83  g_autoptr(GError) local_error = nullptr;
84  if (!fl_method_channel_respond(self->channel, self->response_handle, response,
85  &local_error)) {
86  // If the developer chose not to handle the error then log it so it's not
87  // missed.
88  if (error == nullptr) {
89  g_warning("Failed to send method call response: %s",
90  local_error->message);
91  }
92 
93  g_propagate_error(error, local_error);
94  return FALSE;
95  }
96 
97  return TRUE;
98 }
99 
100 G_MODULE_EXPORT gboolean fl_method_call_respond_success(FlMethodCall* self,
101  FlValue* result,
102  GError** error) {
103  g_return_val_if_fail(FL_IS_METHOD_CALL(self), FALSE);
104 
105  g_autoptr(FlMethodResponse) response =
106  FL_METHOD_RESPONSE(fl_method_success_response_new(result));
107  return fl_method_channel_respond(self->channel, self->response_handle,
108  response, error);
109 }
110 
111 G_MODULE_EXPORT gboolean fl_method_call_respond_error(FlMethodCall* self,
112  const gchar* code,
113  const gchar* message,
114  FlValue* details,
115  GError** error) {
116  g_return_val_if_fail(FL_IS_METHOD_CALL(self), FALSE);
117  g_return_val_if_fail(code != nullptr, FALSE);
118 
119  g_autoptr(FlMethodResponse) response =
120  FL_METHOD_RESPONSE(fl_method_error_response_new(code, message, details));
121  return fl_method_channel_respond(self->channel, self->response_handle,
122  response, error);
123 }
124 
125 G_MODULE_EXPORT gboolean fl_method_call_respond_not_implemented(
126  FlMethodCall* self,
127  GError** error) {
128  g_return_val_if_fail(FL_IS_METHOD_CALL(self), FALSE);
129 
130  g_autoptr(FlMethodResponse) response =
131  FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
132  return fl_method_channel_respond(self->channel, self->response_handle,
133  response, error);
134 }
fl_method_channel_private.h
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
_FlMethodCall::args
FlValue * args
Definition: fl_method_call.cc:18
_FlMethodCall::parent_instance
GObject parent_instance
Definition: fl_method_call.cc:12
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
fl_method_call_dispose
static void fl_method_call_dispose(GObject *object)
Definition: fl_method_call.cc:27
fl_method_call_init
static void fl_method_call_init(FlMethodCall *self)
Definition: fl_method_call.cc:42
fl_method_success_response_new
G_MODULE_EXPORT FlMethodSuccessResponse * fl_method_success_response_new(FlValue *result)
Definition: fl_method_response.cc:126
fl_value_unref
G_MODULE_EXPORT void fl_value_unref(FlValue *self)
Definition: fl_value.cc:400
fl_method_call_respond
G_MODULE_EXPORT gboolean fl_method_call_respond(FlMethodCall *self, FlMethodResponse *response, GError **error)
Definition: fl_method_call.cc:77
fl_value_ref
G_MODULE_EXPORT FlValue * fl_value_ref(FlValue *self)
Definition: fl_value.cc:394
fl_method_call_respond_success
G_MODULE_EXPORT gboolean fl_method_call_respond_success(FlMethodCall *self, FlValue *result, GError **error)
Definition: fl_method_call.cc:100
fl_method_call_respond_not_implemented
G_MODULE_EXPORT gboolean fl_method_call_respond_not_implemented(FlMethodCall *self, GError **error)
Definition: fl_method_call.cc:125
_FlMethodCall
Definition: fl_method_call.cc:11
fl_method_channel_respond
gboolean fl_method_channel_respond(FlMethodChannel *self, FlBinaryMessengerResponseHandle *response_handle, FlMethodResponse *response, GError **error)
Definition: fl_method_channel.cc:211
_FlMethodCall::channel
FlMethodChannel * channel
Definition: fl_method_call.cc:21
fl_method_call.h
fl_method_call_get_name
const G_MODULE_EXPORT gchar * fl_method_call_get_name(FlMethodCall *self)
Definition: fl_method_call.cc:67
G_DEFINE_TYPE
G_DEFINE_TYPE(FlBasicMessageChannelResponseHandle, fl_basic_message_channel_response_handle, G_TYPE_OBJECT) static void fl_basic_message_channel_response_handle_dispose(GObject *object)
Definition: fl_basic_message_channel.cc:37
TRUE
return TRUE
Definition: fl_pixel_buffer_texture_test.cc:53
fl_method_call_new
FlMethodCall * fl_method_call_new(const gchar *name, FlValue *args, FlMethodChannel *channel, FlBinaryMessengerResponseHandle *response_handle)
Definition: fl_method_call.cc:44
result
GAsyncResult * result
Definition: fl_text_input_plugin.cc:106
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_method_call_respond_error
G_MODULE_EXPORT gboolean fl_method_call_respond_error(FlMethodCall *self, const gchar *code, const gchar *message, FlValue *details, GError **error)
Definition: fl_method_call.cc:111
fl_method_call_private.h
_FlMethodCall::name
gchar * name
Definition: fl_method_call.cc:15
fl_method_call_class_init
static void fl_method_call_class_init(FlMethodCallClass *klass)
Definition: fl_method_call.cc:38
_FlMethodCall::response_handle
FlBinaryMessengerResponseHandle * response_handle
Definition: fl_method_call.cc:22
fl_method_call_get_args
G_MODULE_EXPORT FlValue * fl_method_call_get_args(FlMethodCall *self)
Definition: fl_method_call.cc:72