Flutter Engine
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 
5 #include "flutter/shell/platform/linux/public/flutter_linux/fl_method_call.h"
6 #include "flutter/shell/platform/linux/fl_method_call_private.h"
7 #include "flutter/shell/platform/linux/fl_method_channel_private.h"
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
126 fl_method_call_respond_not_implemented(FlMethodCall* self, GError** error) {
127  g_return_val_if_fail(FL_IS_METHOD_CALL(self), FALSE);
128 
129  g_autoptr(FlMethodResponse) response =
130  FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
131  return fl_method_channel_respond(self->channel, self->response_handle,
132  response, error);
133 }
gboolean fl_method_channel_respond(FlMethodChannel *self, FlBinaryMessengerResponseHandle *response_handle, FlMethodResponse *response, GError **error)
G_MODULE_EXPORT FlMethodSuccessResponse * fl_method_success_response_new(FlValue *result)
const uint8_t uint32_t uint32_t GError ** error
static void fl_method_call_init(FlMethodCall *self)
G_MODULE_EXPORT void fl_value_unref(FlValue *self)
Definition: fl_value.cc:369
G_MODULE_EXPORT gboolean fl_method_call_respond_not_implemented(FlMethodCall *self, GError **error)
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:40
G_MODULE_EXPORT gboolean fl_method_call_respond_success(FlMethodCall *self, FlValue *result, GError **error)
FlValue * args
GAsyncResult * result
FlMethodCall * fl_method_call_new(const gchar *name, FlValue *args, FlMethodChannel *channel, FlBinaryMessengerResponseHandle *response_handle)
G_MODULE_EXPORT FlValue * fl_method_call_get_args(FlMethodCall *self)
static void fl_method_call_dispose(GObject *object)
FlBinaryMessengerResponseHandle * response_handle
G_MODULE_EXPORT gboolean fl_method_call_respond(FlMethodCall *self, FlMethodResponse *response, GError **error)
G_MODULE_EXPORT const gchar * fl_method_call_get_name(FlMethodCall *self)
GObject parent_instance
G_MODULE_EXPORT gboolean fl_method_call_respond_error(FlMethodCall *self, const gchar *code, const gchar *message, FlValue *details, GError **error)
FlMethodChannel * channel
G_MODULE_EXPORT FlValue * fl_value_ref(FlValue *self)
Definition: fl_value.cc:363
G_DEFINE_TYPE(FlBasicMessageChannelResponseHandle, fl_basic_message_channel_response_handle, G_TYPE_OBJECT) static void fl_basic_message_channel_response_handle_dispose(GObject *object)
G_MODULE_EXPORT FlMethodNotImplementedResponse * fl_method_not_implemented_response_new()
static void fl_method_call_class_init(FlMethodCallClass *klass)
return FALSE
G_MODULE_EXPORT FlMethodErrorResponse * fl_method_error_response_new(const gchar *code, const gchar *message, FlValue *details)