Flutter Engine
fl_platform_plugin.cc File Reference
#include "flutter/shell/platform/linux/fl_platform_plugin.h"
#include <gtk/gtk.h>
#include <cstring>
#include "flutter/shell/platform/linux/public/flutter_linux/fl_json_method_codec.h"
#include "flutter/shell/platform/linux/public/flutter_linux/fl_method_channel.h"

Go to the source code of this file.

Classes

struct  _FlPlatformPlugin
 

Functions

static void send_response (FlMethodCall *method_call, FlMethodResponse *response)
 
static void clipboard_text_cb (GtkClipboard *clipboard, const gchar *text, gpointer user_data)
 
static void clipboard_text_has_strings_cb (GtkClipboard *clipboard, const gchar *text, gpointer user_data)
 
static FlMethodResponse * clipboard_set_data (FlPlatformPlugin *self, FlValue *args)
 
static FlMethodResponse * clipboard_get_data_async (FlPlatformPlugin *self, FlMethodCall *method_call)
 
static FlMethodResponse * clipboard_has_strings_async (FlPlatformPlugin *self, FlMethodCall *method_call)
 
static FlMethodResponse * system_sound_play (FlPlatformPlugin *self, FlValue *args)
 
static FlMethodResponse * system_navigator_pop (FlPlatformPlugin *self)
 
static void method_call_cb (FlMethodChannel *channel, FlMethodCall *method_call, gpointer user_data)
 
static void fl_platform_plugin_dispose (GObject *object)
 
static void fl_platform_plugin_class_init (FlPlatformPluginClass *klass)
 
static void fl_platform_plugin_init (FlPlatformPlugin *self)
 
FlPlatformPlugin * fl_platform_plugin_new (FlBinaryMessenger *messenger)
 

Variables

static constexpr char kChannelName [] = "flutter/platform"
 
static constexpr char kBadArgumentsError [] = "Bad Arguments"
 
static constexpr char kUnknownClipboardFormatError []
 
static constexpr char kFailedError [] = "Failed"
 
static constexpr char kGetClipboardDataMethod [] = "Clipboard.getData"
 
static constexpr char kSetClipboardDataMethod [] = "Clipboard.setData"
 
static constexpr char kClipboardHasStringsMethod [] = "Clipboard.hasStrings"
 
static constexpr char kPlaySoundMethod [] = "SystemSound.play"
 
static constexpr char kSystemNavigatorPopMethod [] = "SystemNavigator.pop"
 
static constexpr char kTextKey [] = "text"
 
static constexpr char kValueKey [] = "value"
 
static constexpr char kTextPlainFormat [] = "text/plain"
 
static constexpr char kSoundTypeAlert [] = "SystemSoundType.alert"
 
static constexpr char kSoundTypeClick [] = "SystemSoundType.click"
 

Function Documentation

◆ clipboard_get_data_async()

static FlMethodResponse* clipboard_get_data_async ( FlPlatformPlugin *  self,
FlMethodCall *  method_call 
)
static

Definition at line 104 of file fl_platform_plugin.cc.

References args, clipboard_text_cb(), fl_method_call_get_args(), fl_method_error_response_new(), fl_value_get_string(), fl_value_get_type(), FL_VALUE_TYPE_STRING, format, kBadArgumentsError, kTextPlainFormat, and kUnknownClipboardFormatError.

Referenced by method_call_cb().

105  {
107 
109  return FL_METHOD_RESPONSE(fl_method_error_response_new(
110  kBadArgumentsError, "Expected string", nullptr));
111  }
112 
113  const gchar* format = fl_value_get_string(args);
114  if (strcmp(format, kTextPlainFormat) != 0) {
115  return FL_METHOD_RESPONSE(fl_method_error_response_new(
116  kUnknownClipboardFormatError, "GTK clipboard API only supports text",
117  nullptr));
118  }
119 
120  GtkClipboard* clipboard =
121  gtk_clipboard_get_default(gdk_display_get_default());
122  gtk_clipboard_request_text(clipboard, clipboard_text_cb,
123  g_object_ref(method_call));
124 
125  // Will respond later.
126  return nullptr;
127 }
G_BEGIN_DECLS FlValue * args
G_BEGIN_DECLS FlMethodCall * method_call
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:428
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:40
uint32_t uint32_t * format
static constexpr char kTextPlainFormat[]
static constexpr char kBadArgumentsError[]
G_MODULE_EXPORT FlValue * fl_method_call_get_args(FlMethodCall *self)
static constexpr char kUnknownClipboardFormatError[]
static void clipboard_text_cb(GtkClipboard *clipboard, const gchar *text, gpointer user_data)
G_MODULE_EXPORT const gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:642
G_MODULE_EXPORT FlMethodErrorResponse * fl_method_error_response_new(const gchar *code, const gchar *message, FlValue *details)

◆ clipboard_has_strings_async()

static FlMethodResponse* clipboard_has_strings_async ( FlPlatformPlugin *  self,
FlMethodCall *  method_call 
)
static

Definition at line 131 of file fl_platform_plugin.cc.

References clipboard_text_has_strings_cb().

Referenced by method_call_cb().

133  {
134  GtkClipboard* clipboard =
135  gtk_clipboard_get_default(gdk_display_get_default());
136  gtk_clipboard_request_text(clipboard, clipboard_text_has_strings_cb,
137  g_object_ref(method_call));
138 
139  // Will respond later.
140  return nullptr;
141 }
G_BEGIN_DECLS FlMethodCall * method_call
static void clipboard_text_has_strings_cb(GtkClipboard *clipboard, const gchar *text, gpointer user_data)

◆ clipboard_set_data()

static FlMethodResponse* clipboard_set_data ( FlPlatformPlugin *  self,
FlValue args 
)
static

Definition at line 82 of file fl_platform_plugin.cc.

References fl_method_error_response_new(), fl_method_success_response_new(), fl_value_get_string(), fl_value_get_type(), fl_value_lookup_string(), FL_VALUE_TYPE_MAP, FL_VALUE_TYPE_STRING, kBadArgumentsError, and kTextKey.

Referenced by method_call_cb().

83  {
85  return FL_METHOD_RESPONSE(fl_method_error_response_new(
86  kBadArgumentsError, "Argument map missing or malformed", nullptr));
87  }
88 
90  if (text_value == nullptr ||
91  fl_value_get_type(text_value) != FL_VALUE_TYPE_STRING) {
92  return FL_METHOD_RESPONSE(fl_method_error_response_new(
93  kBadArgumentsError, "Missing clipboard text", nullptr));
94  }
95 
96  GtkClipboard* clipboard =
97  gtk_clipboard_get_default(gdk_display_get_default());
98  gtk_clipboard_set_text(clipboard, fl_value_get_string(text_value), -1);
99 
100  return FL_METHOD_RESPONSE(fl_method_success_response_new(nullptr));
101 }
G_BEGIN_DECLS FlValue * args
G_MODULE_EXPORT FlMethodSuccessResponse * fl_method_success_response_new(FlValue *result)
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:428
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:40
static constexpr char kBadArgumentsError[]
G_MODULE_EXPORT FlValue * fl_value_lookup_string(FlValue *self, const gchar *key)
Definition: fl_value.cc:770
G_MODULE_EXPORT const gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:642
G_MODULE_EXPORT FlMethodErrorResponse * fl_method_error_response_new(const gchar *code, const gchar *message, FlValue *details)
static constexpr char kTextKey[]

◆ clipboard_text_cb()

static void clipboard_text_cb ( GtkClipboard *  clipboard,
const gchar *  text,
gpointer  user_data 
)
static

Definition at line 49 of file fl_platform_plugin.cc.

References fl_method_success_response_new(), fl_value_new_map(), fl_value_new_string(), fl_value_set_string_take(), kTextKey, method_call, result, and send_response().

Referenced by clipboard_get_data_async().

51  {
52  g_autoptr(FlMethodCall) method_call = FL_METHOD_CALL(user_data);
53 
54  g_autoptr(FlValue) result = nullptr;
55  if (text != nullptr) {
58  }
59 
60  g_autoptr(FlMethodResponse) response =
61  FL_METHOD_RESPONSE(fl_method_success_response_new(result));
62  send_response(method_call, response);
63 }
G_MODULE_EXPORT FlMethodSuccessResponse * fl_method_success_response_new(FlValue *result)
G_BEGIN_DECLS FlMethodCall * method_call
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:40
G_MODULE_EXPORT FlValue * fl_value_new_map()
Definition: fl_value.cc:355
void * user_data
GAsyncResult * result
static void send_response(FlMethodCall *method_call, FlMethodResponse *response)
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:265
std::u16string text
G_MODULE_EXPORT void fl_value_set_string_take(FlValue *self, const gchar *key, FlValue *value)
Definition: fl_value.cc:610
static constexpr char kTextKey[]

◆ clipboard_text_has_strings_cb()

static void clipboard_text_has_strings_cb ( GtkClipboard *  clipboard,
const gchar *  text,
gpointer  user_data 
)
static

Definition at line 66 of file fl_platform_plugin.cc.

References fl_method_success_response_new(), fl_value_new_bool(), fl_value_new_map(), fl_value_set_string_take(), kValueKey, method_call, result, and send_response().

Referenced by clipboard_has_strings_async().

68  {
69  g_autoptr(FlMethodCall) method_call = FL_METHOD_CALL(user_data);
70 
71  g_autoptr(FlValue) result = fl_value_new_map();
74  fl_value_new_bool(text != nullptr && strlen(text) > 0));
75 
76  g_autoptr(FlMethodResponse) response =
77  FL_METHOD_RESPONSE(fl_method_success_response_new(result));
78  send_response(method_call, response);
79 }
G_MODULE_EXPORT FlMethodSuccessResponse * fl_method_success_response_new(FlValue *result)
G_BEGIN_DECLS FlMethodCall * method_call
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:40
G_MODULE_EXPORT FlValue * fl_value_new_map()
Definition: fl_value.cc:355
void * user_data
GAsyncResult * result
G_MODULE_EXPORT FlValue * fl_value_new_bool(bool value)
Definition: fl_value.cc:244
static void send_response(FlMethodCall *method_call, FlMethodResponse *response)
std::u16string text
static constexpr char kValueKey[]
G_MODULE_EXPORT void fl_value_set_string_take(FlValue *self, const gchar *key, FlValue *value)
Definition: fl_value.cc:610

◆ fl_platform_plugin_class_init()

static void fl_platform_plugin_class_init ( FlPlatformPluginClass *  klass)
static

Definition at line 216 of file fl_platform_plugin.cc.

References fl_platform_plugin_dispose().

216  {
217  G_OBJECT_CLASS(klass)->dispose = fl_platform_plugin_dispose;
218 }
static void fl_platform_plugin_dispose(GObject *object)

◆ fl_platform_plugin_dispose()

static void fl_platform_plugin_dispose ( GObject *  object)
static

Definition at line 208 of file fl_platform_plugin.cc.

Referenced by fl_platform_plugin_class_init().

208  {
209  FlPlatformPlugin* self = FL_PLATFORM_PLUGIN(object);
210 
211  g_clear_object(&self->channel);
212 
213  G_OBJECT_CLASS(fl_platform_plugin_parent_class)->dispose(object);
214 }

◆ fl_platform_plugin_init()

static void fl_platform_plugin_init ( FlPlatformPlugin *  self)
static

Definition at line 220 of file fl_platform_plugin.cc.

220 {}

◆ fl_platform_plugin_new()

FlPlatformPlugin* fl_platform_plugin_new ( FlBinaryMessenger *  messenger)

FlPlatformPlugin:

#FlPlatformPlugin is a plugin that implements the shell side of SystemChannels.platform from the Flutter services library. fl_platform_plugin_new: : an #FlBinaryMessenger

Creates a new plugin that implements SystemChannels.platform from the Flutter services library.

Returns: a new #FlPlatformPlugin

Definition at line 222 of file fl_platform_plugin.cc.

References fl_json_method_codec_new(), fl_method_channel_new(), fl_method_channel_set_method_call_handler(), kChannelName, and method_call_cb().

Referenced by fl_view_constructed().

222  {
223  g_return_val_if_fail(FL_IS_BINARY_MESSENGER(messenger), nullptr);
224 
225  FlPlatformPlugin* self =
226  FL_PLATFORM_PLUGIN(g_object_new(fl_platform_plugin_get_type(), nullptr));
227 
228  g_autoptr(FlJsonMethodCodec) codec = fl_json_method_codec_new();
229  self->channel =
230  fl_method_channel_new(messenger, kChannelName, FL_METHOD_CODEC(codec));
232  nullptr);
233 
234  return self;
235 }
G_MODULE_EXPORT FlJsonMethodCodec * fl_json_method_codec_new()
static constexpr char kChannelName[]
G_MODULE_EXPORT void fl_method_channel_set_method_call_handler(FlMethodChannel *self, FlMethodChannelMethodCallHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
G_MODULE_EXPORT FlMethodChannel * fl_method_channel_new(FlBinaryMessenger *messenger, const gchar *name, FlMethodCodec *codec)
static void method_call_cb(FlMethodChannel *channel, FlMethodCall *method_call, gpointer user_data)

◆ method_call_cb()

static void method_call_cb ( FlMethodChannel *  channel,
FlMethodCall *  method_call,
gpointer  user_data 
)
static

Definition at line 180 of file fl_platform_plugin.cc.

References args, clipboard_get_data_async(), clipboard_has_strings_async(), clipboard_set_data(), fl_method_call_get_args(), fl_method_call_get_name(), fl_method_not_implemented_response_new(), kClipboardHasStringsMethod, kGetClipboardDataMethod, kPlaySoundMethod, kSetClipboardDataMethod, kSystemNavigatorPopMethod, send_response(), system_navigator_pop(), and system_sound_play().

Referenced by fl_platform_plugin_new().

182  {
183  FlPlatformPlugin* self = FL_PLATFORM_PLUGIN(user_data);
184 
185  const gchar* method = fl_method_call_get_name(method_call);
187 
188  g_autoptr(FlMethodResponse) response = nullptr;
189  if (strcmp(method, kSetClipboardDataMethod) == 0) {
190  response = clipboard_set_data(self, args);
191  } else if (strcmp(method, kGetClipboardDataMethod) == 0) {
192  response = clipboard_get_data_async(self, method_call);
193  } else if (strcmp(method, kClipboardHasStringsMethod) == 0) {
194  response = clipboard_has_strings_async(self, method_call);
195  } else if (strcmp(method, kPlaySoundMethod) == 0) {
196  response = system_sound_play(self, args);
197  } else if (strcmp(method, kSystemNavigatorPopMethod) == 0) {
198  response = system_navigator_pop(self);
199  } else {
200  response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
201  }
202 
203  if (response != nullptr) {
204  send_response(method_call, response);
205  }
206 }
G_BEGIN_DECLS FlValue * args
static constexpr char kClipboardHasStringsMethod[]
G_BEGIN_DECLS FlMethodCall * method_call
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:40
static constexpr char kPlaySoundMethod[]
static FlMethodResponse * system_sound_play(FlPlatformPlugin *self, FlValue *args)
void * user_data
static constexpr char kGetClipboardDataMethod[]
static FlMethodResponse * clipboard_has_strings_async(FlPlatformPlugin *self, FlMethodCall *method_call)
G_MODULE_EXPORT FlValue * fl_method_call_get_args(FlMethodCall *self)
static void send_response(FlMethodCall *method_call, FlMethodResponse *response)
static constexpr char kSetClipboardDataMethod[]
static constexpr char kSystemNavigatorPopMethod[]
G_MODULE_EXPORT const gchar * fl_method_call_get_name(FlMethodCall *self)
static FlMethodResponse * clipboard_get_data_async(FlPlatformPlugin *self, FlMethodCall *method_call)
G_MODULE_EXPORT FlMethodNotImplementedResponse * fl_method_not_implemented_response_new()
static FlMethodResponse * clipboard_set_data(FlPlatformPlugin *self, FlValue *args)
static FlMethodResponse * system_navigator_pop(FlPlatformPlugin *self)

◆ send_response()

static void send_response ( FlMethodCall *  method_call,
FlMethodResponse *  response 
)
static

Definition at line 40 of file fl_platform_plugin.cc.

References error, and fl_method_call_respond().

Referenced by clipboard_text_cb(), clipboard_text_has_strings_cb(), method_call_cb(), and _FlutterTaskRunner::~_FlutterTaskRunner().

41  {
42  g_autoptr(GError) error = nullptr;
43  if (!fl_method_call_respond(method_call, response, &error)) {
44  g_warning("Failed to send method call response: %s", error->message);
45  }
46 }
G_BEGIN_DECLS FlMethodCall * method_call
const uint8_t uint32_t uint32_t GError ** error
G_MODULE_EXPORT gboolean fl_method_call_respond(FlMethodCall *self, FlMethodResponse *response, GError **error)

◆ system_navigator_pop()

static FlMethodResponse* system_navigator_pop ( FlPlatformPlugin *  self)
static

Definition at line 167 of file fl_platform_plugin.cc.

References fl_method_error_response_new(), fl_method_success_response_new(), and kFailedError.

Referenced by method_call_cb().

167  {
168  GApplication* app = g_application_get_default();
169  if (app == nullptr) {
170  return FL_METHOD_RESPONSE(fl_method_error_response_new(
171  kFailedError, "Unable to get GApplication", nullptr));
172  }
173 
174  g_application_quit(app);
175 
176  return FL_METHOD_RESPONSE(fl_method_success_response_new(nullptr));
177 }
G_MODULE_EXPORT FlMethodSuccessResponse * fl_method_success_response_new(FlValue *result)
static constexpr char kFailedError[]
G_MODULE_EXPORT FlMethodErrorResponse * fl_method_error_response_new(const gchar *code, const gchar *message, FlValue *details)

◆ system_sound_play()

static FlMethodResponse* system_sound_play ( FlPlatformPlugin *  self,
FlValue args 
)
static

Definition at line 144 of file fl_platform_plugin.cc.

References fl_method_error_response_new(), fl_method_not_implemented_response_new(), fl_value_get_string(), fl_value_get_type(), FL_VALUE_TYPE_STRING, kBadArgumentsError, kSoundTypeAlert, kSoundTypeClick, and type.

Referenced by method_call_cb().

145  {
147  return FL_METHOD_RESPONSE(fl_method_error_response_new(
148  kBadArgumentsError, "Expected string", nullptr));
149  }
150 
151  const gchar* type = fl_value_get_string(args);
152  if (strcmp(type, kSoundTypeAlert) == 0) {
153  GdkDisplay* display = gdk_display_get_default();
154  if (display != nullptr) {
155  gdk_display_beep(display);
156  }
157  } else if (strcmp(type, kSoundTypeClick) == 0) {
158  // We don't make sounds for keyboard on desktops.
159  } else {
160  g_warning("Ignoring unknown sound type %s in SystemSound.play.\n", type);
161  }
162 
163  return FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
164 }
G_BEGIN_DECLS FlValue * args
KeyCallType type
static constexpr char kSoundTypeAlert[]
G_MODULE_EXPORT FlValueType fl_value_get_type(FlValue *self)
Definition: fl_value.cc:428
static constexpr char kSoundTypeClick[]
static constexpr char kBadArgumentsError[]
G_MODULE_EXPORT const gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:642
G_MODULE_EXPORT FlMethodNotImplementedResponse * fl_method_not_implemented_response_new()
G_MODULE_EXPORT FlMethodErrorResponse * fl_method_error_response_new(const gchar *code, const gchar *message, FlValue *details)

Variable Documentation

◆ kBadArgumentsError

constexpr char kBadArgumentsError[] = "Bad Arguments"
static

◆ kChannelName

constexpr char kChannelName[] = "flutter/platform"
static

Definition at line 13 of file fl_platform_plugin.cc.

Referenced by fl_platform_plugin_new().

◆ kClipboardHasStringsMethod

constexpr char kClipboardHasStringsMethod[] = "Clipboard.hasStrings"
static

Definition at line 20 of file fl_platform_plugin.cc.

Referenced by method_call_cb().

◆ kFailedError

constexpr char kFailedError[] = "Failed"
static

Definition at line 17 of file fl_platform_plugin.cc.

Referenced by system_navigator_pop().

◆ kGetClipboardDataMethod

constexpr char kGetClipboardDataMethod[] = "Clipboard.getData"
static

Definition at line 18 of file fl_platform_plugin.cc.

Referenced by method_call_cb().

◆ kPlaySoundMethod

constexpr char kPlaySoundMethod[] = "SystemSound.play"
static

Definition at line 21 of file fl_platform_plugin.cc.

Referenced by method_call_cb().

◆ kSetClipboardDataMethod

constexpr char kSetClipboardDataMethod[] = "Clipboard.setData"
static

Definition at line 19 of file fl_platform_plugin.cc.

Referenced by method_call_cb().

◆ kSoundTypeAlert

constexpr char kSoundTypeAlert[] = "SystemSoundType.alert"
static

Definition at line 28 of file fl_platform_plugin.cc.

Referenced by system_sound_play().

◆ kSoundTypeClick

constexpr char kSoundTypeClick[] = "SystemSoundType.click"
static

Definition at line 29 of file fl_platform_plugin.cc.

Referenced by system_sound_play().

◆ kSystemNavigatorPopMethod

constexpr char kSystemNavigatorPopMethod[] = "SystemNavigator.pop"
static

Definition at line 22 of file fl_platform_plugin.cc.

Referenced by method_call_cb().

◆ kTextKey

constexpr char kTextKey[] = "text"
static

Definition at line 23 of file fl_platform_plugin.cc.

Referenced by clipboard_set_data(), and clipboard_text_cb().

◆ kTextPlainFormat

constexpr char kTextPlainFormat[] = "text/plain"
static

Definition at line 26 of file fl_platform_plugin.cc.

Referenced by clipboard_get_data_async().

◆ kUnknownClipboardFormatError

constexpr char kUnknownClipboardFormatError[]
static
Initial value:
=
"Unknown Clipboard Format"

Definition at line 15 of file fl_platform_plugin.cc.

Referenced by clipboard_get_data_async().

◆ kValueKey

constexpr char kValueKey[] = "value"
static

Definition at line 24 of file fl_platform_plugin.cc.

Referenced by clipboard_text_has_strings_cb().