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 | #ifndef FLUTTER_SHELL_PLATFORM_LINUX_FL_METHOD_CHANNEL_H_ |
6 | #define FLUTTER_SHELL_PLATFORM_LINUX_FL_METHOD_CHANNEL_H_ |
7 | |
8 | #if !defined(__FLUTTER_LINUX_INSIDE__) && !defined(FLUTTER_LINUX_COMPILATION) |
9 | #error "Only <flutter_linux/flutter_linux.h> can be included directly." |
10 | #endif |
11 | |
12 | #include <gio/gio.h> |
13 | #include <glib-object.h> |
14 | #include <gmodule.h> |
15 | |
16 | #include "fl_binary_messenger.h" |
17 | #include "fl_method_call.h" |
18 | #include "fl_method_codec.h" |
19 | #include "fl_method_response.h" |
20 | |
21 | G_BEGIN_DECLS |
22 | |
23 | G_MODULE_EXPORT |
24 | G_DECLARE_FINAL_TYPE(FlMethodChannel, |
25 | fl_method_channel, |
26 | FL, |
27 | METHOD_CHANNEL, |
28 | GObject) |
29 | |
30 | /** |
31 | * FlMethodChannel: |
32 | * |
33 | * #FlMethodChannel is an object that allows method calls to and from Dart code. |
34 | * |
35 | * The following example shows how to call and handle methods on a channel. |
36 | * See #FlMethodResponse for how to handle errors in more detail. |
37 | * |
38 | * |[<!-- language="C" --> |
39 | * static FlMethodChannel *channel = NULL; |
40 | * |
41 | * static void method_call_cb (FlMethodChannel* channel, |
42 | * FlMethodCall* method_call, |
43 | * gpointer user_data) { |
44 | * g_autoptr(FlMethodResponse) response = NULL; |
45 | * if (strcmp (fl_method_call_get_name (method_call), "Foo.bar") == 0) { |
46 | * g_autoptr(GError) bar_error = NULL; |
47 | * g_autoptr(FlValue) result = |
48 | * do_bar (fl_method_call_get_args (method_call), &bar_error); |
49 | * if (result == NULL) { |
50 | * response = |
51 | * FL_METHOD_RESPONSE (fl_method_error_response_new ("bar error", |
52 | * bar_error->message, |
53 | * nullptr); |
54 | * } else { |
55 | * response = |
56 | * FL_METHOD_RESPONSE (fl_method_success_response_new (result)); |
57 | * } |
58 | * } else { |
59 | * response = |
60 | * FL_METHOD_RESPONSE (fl_method_not_implemented_response_new ()); |
61 | * } |
62 | * |
63 | * g_autoptr(GError) error = NULL; |
64 | * if (!fl_method_call_respond(method_call, response, &error)) |
65 | * g_warning ("Failed to send response: %s", error->message); |
66 | * } |
67 | * |
68 | * static void method_response_cb(GObject *object, |
69 | * GAsyncResult *result, |
70 | * gpointer user_data) { |
71 | * g_autoptr(GError) error = NULL; |
72 | * g_autoptr(FlMethodResponse) response = |
73 | * fl_method_channel_invoke_method_finish (FL_METHOD_CODEC (object), result, |
74 | * &error); |
75 | * if (response == NULL) { |
76 | * g_warning ("Failed to call method: %s", error->message); |
77 | * return; |
78 | * } |
79 | * |
80 | * g_autoptr(FlValue) value = |
81 | * fl_method_response_get_result (response, &error); |
82 | * if (response == NULL) { |
83 | * g_warning ("Method returned error: %s", error->message); |
84 | * return; |
85 | * } |
86 | * |
87 | * use_result (value); |
88 | * } |
89 | * |
90 | * static void call_method () { |
91 | * g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new (); |
92 | * channel = |
93 | * fl_method_channel_new(messenger, "flutter/foo", FL_METHOD_CODEC (codec)); |
94 | * fl_method_channel_set_method_call_handler (channel, method_call_cb, NULL, |
95 | * NULL); |
96 | * |
97 | * g_autoptr(FlValue) args = fl_value_new_string ("Hello World"); |
98 | * fl_method_channel_invoke_method (channel, "Foo.foo", args, |
99 | * cancellable, method_response_cb, NULL); |
100 | * } |
101 | * ]| |
102 | * |
103 | * #FlMethodChannel matches the MethodChannel class in the Flutter services |
104 | * library. |
105 | */ |
106 | |
107 | /** |
108 | * FlMethodChannelMethodCallHandler: |
109 | * @channel: an #FlMethodChannel. |
110 | * @method_call: an #FlMethodCall. |
111 | * @user_data: (closure): data provided when registering this handler. |
112 | * |
113 | * Function called when a method call is received. Respond to the method call |
114 | * with fl_method_call_respond(). If the response is not occurring in this |
115 | * callback take a reference to @method_call and release that once it has been |
116 | * responded to. Failing to respond before the last reference to @method_call is |
117 | * dropped is a programming error. |
118 | */ |
119 | typedef void (*FlMethodChannelMethodCallHandler)(FlMethodChannel* channel, |
120 | FlMethodCall* method_call, |
121 | gpointer user_data); |
122 | |
123 | /** |
124 | * fl_method_channel_new: |
125 | * @messenger: an #FlBinaryMessenger. |
126 | * @name: a channel name. |
127 | * @codec: the method codec. |
128 | * |
129 | * Creates a new method channel. @codec must match the codec used on the Dart |
130 | * end of the channel. |
131 | * |
132 | * Returns: a new #FlMethodChannel. |
133 | */ |
134 | FlMethodChannel* fl_method_channel_new(FlBinaryMessenger* messenger, |
135 | const gchar* name, |
136 | FlMethodCodec* codec); |
137 | |
138 | /** |
139 | * fl_method_channel_set_method_call_handler: |
140 | * @channel: an #FlMethodChannel. |
141 | * @handler: function to call when a method call is received on this channel. |
142 | * @user_data: (closure): user data to pass to @handler. |
143 | * @destroy_notify: (allow-none): a function which gets called to free |
144 | * @user_data, or %NULL. |
145 | * |
146 | * Sets the function called when a method call is received from the Dart side of |
147 | * the channel. See #FlMethodChannelMethodCallHandler for details on how to |
148 | * respond to method calls. |
149 | * |
150 | * The handler is removed if the channel is closed or is replaced by another |
151 | * handler, set @destroy_notify if you want to detect this. |
152 | */ |
153 | void fl_method_channel_set_method_call_handler( |
154 | FlMethodChannel* channel, |
155 | FlMethodChannelMethodCallHandler handler, |
156 | gpointer user_data, |
157 | GDestroyNotify destroy_notify); |
158 | |
159 | /** |
160 | * fl_method_channel_invoke_method: |
161 | * @channel: an #FlMethodChannel. |
162 | * @method: the method to call. |
163 | * @args: (allow-none): arguments to the method, must match what the |
164 | * #FlMethodCodec supports. |
165 | * @cancellable: (allow-none): a #GCancellable or %NULL. |
166 | * @callback: (scope async): (allow-none): a #GAsyncReadyCallback to call when |
167 | * the request is satisfied or %NULL to ignore the response. |
168 | * @user_data: (closure): user data to pass to @callback. |
169 | * |
170 | * Calls a method on this channel. |
171 | */ |
172 | void fl_method_channel_invoke_method(FlMethodChannel* channel, |
173 | const gchar* method, |
174 | FlValue* args, |
175 | GCancellable* cancellable, |
176 | GAsyncReadyCallback callback, |
177 | gpointer user_data); |
178 | |
179 | /** |
180 | * fl_method_channel_invoke_method_finish: |
181 | * @channel: an #FlMethodChannel. |
182 | * @result: #GAsyncResult. |
183 | * @error: (allow-none): #GError location to store the error occurring, or %NULL |
184 | * to ignore. |
185 | * |
186 | * Completes request started with fl_method_channel_invoke_method(). |
187 | * |
188 | * Returns: (transfer full): an #FlMethodResponse or %NULL on error. |
189 | */ |
190 | FlMethodResponse* fl_method_channel_invoke_method_finish( |
191 | FlMethodChannel* channel, |
192 | GAsyncResult* result, |
193 | GError** error); |
194 | |
195 | G_END_DECLS |
196 | |
197 | #endif // FLUTTER_SHELL_PLATFORM_LINUX_FL_METHOD_CHANNEL_H_ |
198 | |