Flutter Linux Embedder
fl_event_channel.cc File Reference

Go to the source code of this file.

Classes

struct  _FlEventChannel
 
struct  _FlEventChannelResponseHandle
 

Functions

static FlMethodErrorResponse * handle_method_call (FlEventChannel *self, const gchar *name, FlValue *args)
 
static void message_cb (FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)
 
static void remove_handlers (FlEventChannel *self)
 
static void channel_closed_cb (gpointer user_data)
 
static void fl_event_channel_dispose (GObject *object)
 
static void fl_event_channel_class_init (FlEventChannelClass *klass)
 
static void fl_event_channel_init (FlEventChannel *self)
 
G_MODULE_EXPORT FlEventChannel * fl_event_channel_new (FlBinaryMessenger *messenger, const gchar *name, FlMethodCodec *codec)
 
G_MODULE_EXPORT void fl_event_channel_set_stream_handlers (FlEventChannel *self, FlEventChannelHandler listen_handler, FlEventChannelHandler cancel_handler, gpointer user_data, GDestroyNotify destroy_notify)
 
G_MODULE_EXPORT gboolean fl_event_channel_send (FlEventChannel *self, FlValue *event, GCancellable *cancellable, GError **error)
 
G_MODULE_EXPORT gboolean fl_event_channel_send_error (FlEventChannel *self, const gchar *code, const gchar *message, FlValue *details, GCancellable *cancellable, GError **error)
 
G_MODULE_EXPORT gboolean fl_event_channel_send_end_of_stream (FlEventChannel *self, GCancellable *cancellable, GError **error)
 

Variables

static constexpr char kListenMethod [] = "listen"
 
static constexpr char kCancelMethod [] = "cancel"
 
static constexpr char kEventRequestError [] = "error"
 

Function Documentation

◆ channel_closed_cb()

static void channel_closed_cb ( gpointer  user_data)
static

Definition at line 130 of file fl_event_channel.cc.

130  {
131  g_autoptr(FlEventChannel) self = FL_EVENT_CHANNEL(user_data);
132  self->channel_closed = TRUE;
133  remove_handlers(self);
134 }

References remove_handlers(), TRUE, and user_data.

Referenced by fl_event_channel_new().

◆ fl_event_channel_class_init()

static void fl_event_channel_class_init ( FlEventChannelClass *  klass)
static

Definition at line 153 of file fl_event_channel.cc.

153  {
154  G_OBJECT_CLASS(klass)->dispose = fl_event_channel_dispose;
155 }

References fl_event_channel_dispose().

◆ fl_event_channel_dispose()

static void fl_event_channel_dispose ( GObject *  object)
static

Definition at line 136 of file fl_event_channel.cc.

136  {
137  FlEventChannel* self = FL_EVENT_CHANNEL(object);
138 
139  if (!self->channel_closed) {
141  self->messenger, self->name, nullptr, nullptr, nullptr);
142  }
143 
144  g_clear_object(&self->messenger);
145  g_clear_pointer(&self->name, g_free);
146  g_clear_object(&self->codec);
147 
148  remove_handlers(self);
149 
150  G_OBJECT_CLASS(fl_event_channel_parent_class)->dispose(object);
151 }

References fl_binary_messenger_set_message_handler_on_channel(), and remove_handlers().

Referenced by fl_event_channel_class_init().

◆ fl_event_channel_init()

static void fl_event_channel_init ( FlEventChannel *  self)
static

Definition at line 157 of file fl_event_channel.cc.

157 {}

◆ fl_event_channel_new()

G_MODULE_EXPORT FlEventChannel* fl_event_channel_new ( FlBinaryMessenger *  messenger,
const gchar *  name,
FlMethodCodec *  codec 
)

Definition at line 159 of file fl_event_channel.cc.

162  {
163  g_return_val_if_fail(FL_IS_BINARY_MESSENGER(messenger), nullptr);
164  g_return_val_if_fail(name != nullptr, nullptr);
165  g_return_val_if_fail(FL_IS_METHOD_CODEC(codec), nullptr);
166 
167  FlEventChannel* self =
168  FL_EVENT_CHANNEL(g_object_new(fl_event_channel_get_type(), nullptr));
169 
170  self->messenger = FL_BINARY_MESSENGER(g_object_ref(messenger));
171  self->name = g_strdup(name);
172  self->codec = FL_METHOD_CODEC(g_object_ref(codec));
173 
175  self->messenger, self->name, message_cb, g_object_ref(self),
177 
178  return self;
179 }

References channel_closed_cb(), fl_binary_messenger_set_message_handler_on_channel(), and message_cb().

Referenced by TEST().

◆ fl_event_channel_send()

G_MODULE_EXPORT gboolean fl_event_channel_send ( FlEventChannel *  channel,
FlValue event,
GCancellable *  cancellable,
GError **  error 
)

fl_event_channel_send: @channel: an #FlEventChannel. @event: event to send, must match what the #FlMethodCodec supports. @cancellable: (allow-none): a #GCancellable or NULL. @error: (allow-none): #GError location to store the error occurring, or NULL to ignore.

Sends an event on the channel. Events should only be sent once the channel is being listened to.

Returns: TRUE if successful.

Definition at line 196 of file fl_event_channel.cc.

199  {
200  g_return_val_if_fail(FL_IS_EVENT_CHANNEL(self), FALSE);
201  g_return_val_if_fail(event != nullptr, FALSE);
202 
203  g_autoptr(GBytes) data =
205  if (data == nullptr) {
206  return FALSE;
207  }
208 
209  fl_binary_messenger_send_on_channel(self->messenger, self->name, data,
210  cancellable, nullptr, nullptr);
211 
212  return TRUE;
213 }

References error, event, fl_binary_messenger_send_on_channel(), fl_method_codec_encode_success_envelope(), and TRUE.

Referenced by send_events_listen_cb().

◆ fl_event_channel_send_end_of_stream()

G_MODULE_EXPORT gboolean fl_event_channel_send_end_of_stream ( FlEventChannel *  self,
GCancellable *  cancellable,
GError **  error 
)

Definition at line 237 of file fl_event_channel.cc.

240  {
241  g_return_val_if_fail(FL_IS_EVENT_CHANNEL(self), FALSE);
242  fl_binary_messenger_send_on_channel(self->messenger, self->name, nullptr,
243  cancellable, nullptr, nullptr);
244  return TRUE;
245 }

References fl_binary_messenger_send_on_channel(), and TRUE.

◆ fl_event_channel_send_error()

G_MODULE_EXPORT gboolean fl_event_channel_send_error ( FlEventChannel *  self,
const gchar *  code,
const gchar *  message,
FlValue details,
GCancellable *  cancellable,
GError **  error 
)

Definition at line 215 of file fl_event_channel.cc.

220  {
221  g_return_val_if_fail(FL_IS_EVENT_CHANNEL(self), FALSE);
222  g_return_val_if_fail(code != nullptr, FALSE);
223  g_return_val_if_fail(message != nullptr, FALSE);
224 
225  g_autoptr(GBytes) data = fl_method_codec_encode_error_envelope(
226  self->codec, code, message, details, error);
227  if (data == nullptr) {
228  return FALSE;
229  }
230 
231  fl_binary_messenger_send_on_channel(self->messenger, self->name, data,
232  cancellable, nullptr, nullptr);
233 
234  return TRUE;
235 }

References error, fl_binary_messenger_send_on_channel(), fl_method_codec_encode_error_envelope(), and TRUE.

◆ fl_event_channel_set_stream_handlers()

G_MODULE_EXPORT void fl_event_channel_set_stream_handlers ( FlEventChannel *  channel,
FlEventChannelHandler  listen_handler,
FlEventChannelHandler  cancel_handler,
gpointer  user_data,
GDestroyNotify  destroy_notify 
)

fl_event_channel_set_stream_handlers: @channel: an #FlEventChannel. @listen_handler: (allow-none): function to call when the Dart side of the channel starts listening to the stream. @cancel_handler: (allow-none): function to call when the Dart side of the channel cancels their subscription to the stream. @user_data: (closure): user data to pass to @listen_handler and @cancel_handler. @destroy_notify: (allow-none): a function which gets called to free @user_data, or NULL.

Sets the functions called when the Dart side requests the stream to start and finish.

The handlers are removed if the channel is closed or is replaced by another handler, set @destroy_notify if you want to detect this.

Definition at line 181 of file fl_event_channel.cc.

186  {
187  g_return_if_fail(FL_IS_EVENT_CHANNEL(self));
188 
189  remove_handlers(self);
190  self->listen_handler = listen_handler;
191  self->cancel_handler = cancel_handler;
192  self->handler_data = user_data;
193  self->handler_data_destroy_notify = destroy_notify;
194 }

References remove_handlers(), and user_data.

Referenced by TEST().

◆ handle_method_call()

static FlMethodErrorResponse* handle_method_call ( FlEventChannel *  self,
const gchar *  name,
FlValue args 
)
static

Definition at line 46 of file fl_event_channel.cc.

48  {
49  FlEventChannelHandler handler;
50  if (g_strcmp0(name, kListenMethod) == 0) {
51  handler = self->listen_handler;
52  } else if (g_strcmp0(name, kCancelMethod) == 0) {
53  handler = self->cancel_handler;
54  } else {
55  g_autofree gchar* message =
56  g_strdup_printf("Unknown event channel request '%s'", name);
57  return fl_method_error_response_new(kEventRequestError, message, nullptr);
58  }
59 
60  // If not handled, just accept requests.
61  if (handler == nullptr) {
62  return nullptr;
63  }
64 
65  return handler(self, args, self->handler_data);
66 }

References args, fl_method_error_response_new(), kCancelMethod, kEventRequestError, and kListenMethod.

Referenced by message_cb().

◆ message_cb()

static void message_cb ( FlBinaryMessenger *  messenger,
const gchar *  channel,
GBytes *  message,
FlBinaryMessengerResponseHandle *  response_handle,
gpointer  user_data 
)
static

Definition at line 69 of file fl_event_channel.cc.

73  {
74  FlEventChannel* self = FL_EVENT_CHANNEL(user_data);
75 
76  g_autofree gchar* name = nullptr;
77  g_autoptr(GError) error = nullptr;
78  g_autoptr(FlValue) args = nullptr;
79  if (!fl_method_codec_decode_method_call(self->codec, message, &name, &args,
80  &error)) {
81  g_warning("Failed to decode message on event channel %s: %s", self->name,
82  error->message);
83  fl_binary_messenger_send_response(messenger, response_handle, nullptr,
84  nullptr);
85  return;
86  }
87 
88  g_autoptr(FlMethodErrorResponse) response =
89  handle_method_call(self, name, args);
90 
91  g_autoptr(GBytes) data = nullptr;
92  if (response == nullptr) {
93  g_autoptr(GError) codec_error = nullptr;
94  data = fl_method_codec_encode_success_envelope(self->codec, nullptr,
95  &codec_error);
96  if (data == nullptr) {
97  g_warning("Failed to encode event channel %s success response: %s",
98  self->name, codec_error->message);
99  }
100  } else {
101  g_autoptr(GError) codec_error = nullptr;
103  self->codec, fl_method_error_response_get_code(response),
105  fl_method_error_response_get_details(response), &codec_error);
106  if (data == nullptr) {
107  g_warning("Failed to encode event channel %s error response: %s",
108  self->name, codec_error->message);
109  }
110  }
111 
112  if (!fl_binary_messenger_send_response(messenger, response_handle, data,
113  &error)) {
114  g_warning("Failed to send event channel response: %s", error->message);
115  }
116 }

References args, error, fl_binary_messenger_send_response(), fl_method_codec_decode_method_call(), fl_method_codec_encode_error_envelope(), fl_method_codec_encode_success_envelope(), fl_method_error_response_get_code(), fl_method_error_response_get_details(), fl_method_error_response_get_message(), handle_method_call(), and user_data.

Referenced by fl_event_channel_new().

◆ remove_handlers()

static void remove_handlers ( FlEventChannel *  self)
static

Definition at line 119 of file fl_event_channel.cc.

119  {
120  if (self->handler_data_destroy_notify != nullptr) {
121  self->handler_data_destroy_notify(self->handler_data);
122  }
123  self->listen_handler = nullptr;
124  self->cancel_handler = nullptr;
125  self->handler_data = nullptr;
126  self->handler_data_destroy_notify = nullptr;
127 }

Referenced by channel_closed_cb(), fl_event_channel_dispose(), and fl_event_channel_set_stream_handlers().

Variable Documentation

◆ kCancelMethod

constexpr char kCancelMethod[] = "cancel"
staticconstexpr

Definition at line 12 of file fl_event_channel.cc.

Referenced by handle_method_call().

◆ kEventRequestError

constexpr char kEventRequestError[] = "error"
staticconstexpr

Definition at line 13 of file fl_event_channel.cc.

Referenced by handle_method_call().

◆ kListenMethod

constexpr char kListenMethod[] = "listen"
staticconstexpr

Definition at line 11 of file fl_event_channel.cc.

Referenced by handle_method_call().

event
FlKeyEvent * event
Definition: fl_key_channel_responder.cc:118
kEventRequestError
static constexpr char kEventRequestError[]
Definition: fl_event_channel.cc:13
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
FlValue
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:42
remove_handlers
static void remove_handlers(FlEventChannel *self)
Definition: fl_event_channel.cc:119
user_data
FlKeyEvent uint64_t FlKeyResponderAsyncCallback gpointer user_data
Definition: fl_key_channel_responder.cc:121
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
channel_closed_cb
static void channel_closed_cb(gpointer user_data)
Definition: fl_event_channel.cc:130
fl_method_error_response_get_details
G_MODULE_EXPORT FlValue * fl_method_error_response_get_details(FlMethodErrorResponse *self)
Definition: fl_method_response.cc:172
fl_event_channel_dispose
static void fl_event_channel_dispose(GObject *object)
Definition: fl_event_channel.cc:136
fl_method_codec_decode_method_call
gboolean fl_method_codec_decode_method_call(FlMethodCodec *self, GBytes *message, gchar **name, FlValue **args, GError **error)
Definition: fl_method_codec.cc:27
fl_binary_messenger_set_message_handler_on_channel
G_MODULE_EXPORT void fl_binary_messenger_set_message_handler_on_channel(FlBinaryMessenger *self, const gchar *channel, FlBinaryMessengerMessageHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
Definition: fl_binary_messenger.cc:424
TRUE
return TRUE
Definition: fl_pixel_buffer_texture_test.cc:53
fl_method_codec_encode_success_envelope
GBytes * fl_method_codec_encode_success_envelope(FlMethodCodec *self, FlValue *result, GError **error)
Definition: fl_method_codec.cc:41
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
message_cb
static void message_cb(FlBinaryMessenger *messenger, const gchar *channel, GBytes *message, FlBinaryMessengerResponseHandle *response_handle, gpointer user_data)
Definition: fl_event_channel.cc:69
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_codec_encode_error_envelope
GBytes * fl_method_codec_encode_error_envelope(FlMethodCodec *self, const gchar *code, const gchar *message, FlValue *details, GError **error)
Definition: fl_method_codec.cc:50
fl_binary_messenger_send_response
G_MODULE_EXPORT gboolean fl_binary_messenger_send_response(FlBinaryMessenger *self, FlBinaryMessengerResponseHandle *response_handle, GBytes *response, GError **error)
Definition: fl_binary_messenger.cc:438
kCancelMethod
static constexpr char kCancelMethod[]
Definition: fl_event_channel.cc:12
handle_method_call
static FlMethodErrorResponse * handle_method_call(FlEventChannel *self, const gchar *name, FlValue *args)
Definition: fl_event_channel.cc:46
fl_binary_messenger_send_on_channel
G_MODULE_EXPORT void fl_binary_messenger_send_on_channel(FlBinaryMessenger *self, const gchar *channel, GBytes *message, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
Definition: fl_binary_messenger.cc:451
kListenMethod
static constexpr char kListenMethod[]
Definition: fl_event_channel.cc:11