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_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 kSystemNavigatorPopMethod [] = "SystemNavigator.pop"
 
static constexpr char kTextKey [] = "text"
 
static constexpr char kValueKey [] = "value"
 
static constexpr char kTextPlainFormat [] = "text/plain"
 

Function Documentation

◆ clipboard_get_data_async()

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

Definition at line 100 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, kBadArgumentsError, kTextPlainFormat, and kUnknownClipboardFormatError.

Referenced by method_call_cb().

101  {
103 
105  return FL_METHOD_RESPONSE(fl_method_error_response_new(
106  kBadArgumentsError, "Expected string", nullptr));
107  }
108 
109  const gchar* format = fl_value_get_string(args);
110  if (strcmp(format, kTextPlainFormat) != 0) {
111  return FL_METHOD_RESPONSE(fl_method_error_response_new(
112  kUnknownClipboardFormatError, "GTK clipboard API only supports text",
113  nullptr));
114  }
115 
116  GtkClipboard* clipboard =
117  gtk_clipboard_get_default(gdk_display_get_default());
118  gtk_clipboard_request_text(clipboard, clipboard_text_cb,
119  g_object_ref(method_call));
120 
121  // Will respond later.
122  return nullptr;
123 }
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:395
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:39
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:596
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 127 of file fl_platform_plugin.cc.

References clipboard_text_has_strings_cb().

Referenced by method_call_cb().

129  {
130  GtkClipboard* clipboard =
131  gtk_clipboard_get_default(gdk_display_get_default());
132  gtk_clipboard_request_text(clipboard, clipboard_text_has_strings_cb,
133  g_object_ref(method_call));
134 
135  // Will respond later.
136  return nullptr;
137 }
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 78 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().

79  {
81  return FL_METHOD_RESPONSE(fl_method_error_response_new(
82  kBadArgumentsError, "Argument map missing or malformed", nullptr));
83  }
84 
86  if (text_value == nullptr ||
87  fl_value_get_type(text_value) != FL_VALUE_TYPE_STRING) {
88  return FL_METHOD_RESPONSE(fl_method_error_response_new(
89  kBadArgumentsError, "Missing clipboard text", nullptr));
90  }
91 
92  GtkClipboard* clipboard =
93  gtk_clipboard_get_default(gdk_display_get_default());
94  gtk_clipboard_set_text(clipboard, fl_value_get_string(text_value), -1);
95 
96  return FL_METHOD_RESPONSE(fl_method_success_response_new(nullptr));
97 }
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:395
typedefG_BEGIN_DECLS struct _FlValue FlValue
Definition: fl_value.h:39
static constexpr char kBadArgumentsError[]
G_MODULE_EXPORT FlValue * fl_value_lookup_string(FlValue *self, const gchar *key)
Definition: fl_value.cc:712
G_MODULE_EXPORT const gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:596
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 45 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, and send_response().

Referenced by clipboard_get_data_async().

47  {
48  g_autoptr(FlMethodCall) method_call = FL_METHOD_CALL(user_data);
49 
50  g_autoptr(FlValue) result = nullptr;
51  if (text != nullptr) {
52  result = fl_value_new_map();
54  }
55 
56  g_autoptr(FlMethodResponse) response =
57  FL_METHOD_RESPONSE(fl_method_success_response_new(result));
58  send_response(method_call, response);
59 }
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:39
G_MODULE_EXPORT FlValue * fl_value_new_map()
Definition: fl_value.cc:327
static void send_response(FlMethodCall *method_call, FlMethodResponse *response)
G_BEGIN_DECLS FlValue gpointer user_data
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:247
G_MODULE_EXPORT void fl_value_set_string_take(FlValue *self, const gchar *key, FlValue *value)
Definition: fl_value.cc:564
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 62 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, and send_response().

Referenced by clipboard_has_strings_async().

64  {
65  g_autoptr(FlMethodCall) method_call = FL_METHOD_CALL(user_data);
66 
67  g_autoptr(FlValue) result = fl_value_new_map();
69  result, kValueKey,
70  fl_value_new_bool(text != nullptr && strlen(text) > 0));
71 
72  g_autoptr(FlMethodResponse) response =
73  FL_METHOD_RESPONSE(fl_method_success_response_new(result));
74  send_response(method_call, response);
75 }
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:39
G_MODULE_EXPORT FlValue * fl_value_new_map()
Definition: fl_value.cc:327
G_MODULE_EXPORT FlValue * fl_value_new_bool(bool value)
Definition: fl_value.cc:226
static void send_response(FlMethodCall *method_call, FlMethodResponse *response)
G_BEGIN_DECLS FlValue gpointer user_data
static constexpr char kValueKey[]
G_MODULE_EXPORT void fl_value_set_string_take(FlValue *self, const gchar *key, FlValue *value)
Definition: fl_value.cc:564

◆ fl_platform_plugin_class_init()

static void fl_platform_plugin_class_init ( FlPlatformPluginClass *  klass)
static

Definition at line 187 of file fl_platform_plugin.cc.

References fl_platform_plugin_dispose().

187  {
188  G_OBJECT_CLASS(klass)->dispose = fl_platform_plugin_dispose;
189 }
static void fl_platform_plugin_dispose(GObject *object)

◆ fl_platform_plugin_dispose()

static void fl_platform_plugin_dispose ( GObject *  object)
static

Definition at line 179 of file fl_platform_plugin.cc.

Referenced by fl_platform_plugin_class_init().

179  {
180  FlPlatformPlugin* self = FL_PLATFORM_PLUGIN(object);
181 
182  g_clear_object(&self->channel);
183 
184  G_OBJECT_CLASS(fl_platform_plugin_parent_class)->dispose(object);
185 }

◆ fl_platform_plugin_init()

static void fl_platform_plugin_init ( FlPlatformPlugin *  self)
static

Definition at line 191 of file fl_platform_plugin.cc.

191 {}

◆ fl_platform_plugin_new()

FlPlatformPlugin* fl_platform_plugin_new ( FlBinaryMessenger *  messenger)

FlPlatformPlugin:

#FlPlatformPlugin is a plugin that implements the shell side of PlatformPlugins.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 193 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().

193  {
194  g_return_val_if_fail(FL_IS_BINARY_MESSENGER(messenger), nullptr);
195 
196  FlPlatformPlugin* self =
197  FL_PLATFORM_PLUGIN(g_object_new(fl_platform_plugin_get_type(), nullptr));
198 
199  g_autoptr(FlJsonMethodCodec) codec = fl_json_method_codec_new();
200  self->channel =
201  fl_method_channel_new(messenger, kChannelName, FL_METHOD_CODEC(codec));
203  nullptr);
204 
205  return self;
206 }
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 153 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, kSetClipboardDataMethod, kSystemNavigatorPopMethod, send_response(), and system_navigator_pop().

Referenced by fl_platform_plugin_new().

155  {
156  FlPlatformPlugin* self = FL_PLATFORM_PLUGIN(user_data);
157 
158  const gchar* method = fl_method_call_get_name(method_call);
160 
161  g_autoptr(FlMethodResponse) response = nullptr;
162  if (strcmp(method, kSetClipboardDataMethod) == 0) {
163  response = clipboard_set_data(self, args);
164  } else if (strcmp(method, kGetClipboardDataMethod) == 0) {
165  response = clipboard_get_data_async(self, method_call);
166  } else if (strcmp(method, kClipboardHasStringsMethod) == 0) {
167  response = clipboard_has_strings_async(self, method_call);
168  } else if (strcmp(method, kSystemNavigatorPopMethod) == 0) {
169  response = system_navigator_pop(self);
170  } else {
171  response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
172  }
173 
174  if (response != nullptr) {
175  send_response(method_call, response);
176  }
177 }
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:39
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[]
G_BEGIN_DECLS FlValue gpointer user_data
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 36 of file fl_platform_plugin.cc.

References error, and fl_method_call_respond().

Referenced by clipboard_text_cb(), clipboard_text_has_strings_cb(), and method_call_cb().

37  {
38  g_autoptr(GError) error = nullptr;
39  if (!fl_method_call_respond(method_call, response, &error)) {
40  g_warning("Failed to send method call response: %s", error->message);
41  }
42 }
G_BEGIN_DECLS FlMethodCall * method_call
FlMethodResponse 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 140 of file fl_platform_plugin.cc.

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

Referenced by method_call_cb().

140  {
141  GApplication* app = g_application_get_default();
142  if (app == nullptr) {
143  return FL_METHOD_RESPONSE(fl_method_error_response_new(
144  kFailedError, "Unable to get GApplication", nullptr));
145  }
146 
147  g_application_quit(app);
148 
149  return FL_METHOD_RESPONSE(fl_method_success_response_new(nullptr));
150 }
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)

Variable Documentation

◆ kBadArgumentsError

constexpr char kBadArgumentsError[] = "Bad Arguments"
static

Definition at line 14 of file fl_platform_plugin.cc.

Referenced by clipboard_get_data_async(), and clipboard_set_data().

◆ 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().

◆ kSetClipboardDataMethod

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

Definition at line 19 of file fl_platform_plugin.cc.

Referenced by method_call_cb().

◆ kSystemNavigatorPopMethod

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

Definition at line 21 of file fl_platform_plugin.cc.

Referenced by method_call_cb().

◆ kTextKey

constexpr char kTextKey[] = "text"
static

Definition at line 22 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 25 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 23 of file fl_platform_plugin.cc.

Referenced by clipboard_text_has_strings_cb().