Flutter Engine
fl_platform_plugin.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/fl_platform_plugin.h"
6 
7 #include <gtk/gtk.h>
8 #include <cstring>
9 
10 #include "flutter/shell/platform/linux/public/flutter_linux/fl_json_method_codec.h"
11 #include "flutter/shell/platform/linux/public/flutter_linux/fl_method_channel.h"
12 
13 static constexpr char kChannelName[] = "flutter/platform";
14 static constexpr char kBadArgumentsError[] = "Bad Arguments";
15 static constexpr char kUnknownClipboardFormatError[] =
16  "Unknown Clipboard Format";
17 static constexpr char kFailedError[] = "Failed";
18 static constexpr char kGetClipboardDataMethod[] = "Clipboard.getData";
19 static constexpr char kSetClipboardDataMethod[] = "Clipboard.setData";
20 static constexpr char kClipboardHasStringsMethod[] = "Clipboard.hasStrings";
21 static constexpr char kPlaySoundMethod[] = "SystemSound.play";
22 static constexpr char kSystemNavigatorPopMethod[] = "SystemNavigator.pop";
23 static constexpr char kTextKey[] = "text";
24 static constexpr char kValueKey[] = "value";
25 
26 static constexpr char kTextPlainFormat[] = "text/plain";
27 
28 static constexpr char kSoundTypeAlert[] = "SystemSoundType.alert";
29 static constexpr char kSoundTypeClick[] = "SystemSoundType.click";
30 
32  GObject parent_instance;
33 
34  FlMethodChannel* channel;
35 };
36 
37 G_DEFINE_TYPE(FlPlatformPlugin, fl_platform_plugin, G_TYPE_OBJECT)
38 
39 // Sends the method call response to Flutter.
40 static void send_response(FlMethodCall* method_call,
41  FlMethodResponse* response) {
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 }
47 
48 // Called when clipboard text received.
49 static void clipboard_text_cb(GtkClipboard* clipboard,
50  const gchar* text,
51  gpointer user_data) {
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 }
64 
65 // Called when clipboard text received during has_strings.
66 static void clipboard_text_has_strings_cb(GtkClipboard* clipboard,
67  const gchar* text,
68  gpointer user_data) {
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 }
80 
81 // Called when Flutter wants to copy to the clipboard.
82 static FlMethodResponse* clipboard_set_data(FlPlatformPlugin* self,
83  FlValue* args) {
84  if (fl_value_get_type(args) != FL_VALUE_TYPE_MAP) {
85  return FL_METHOD_RESPONSE(fl_method_error_response_new(
86  kBadArgumentsError, "Argument map missing or malformed", nullptr));
87  }
88 
89  FlValue* text_value = fl_value_lookup_string(args, kTextKey);
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 }
102 
103 // Called when Flutter wants to paste from the clipboard.
104 static FlMethodResponse* clipboard_get_data_async(FlPlatformPlugin* self,
105  FlMethodCall* method_call) {
106  FlValue* args = fl_method_call_get_args(method_call);
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 }
128 
129 // Called when Flutter wants to know if the content of the clipboard is able to
130 // be pasted, without actually accessing the clipboard content itself.
131 static FlMethodResponse* clipboard_has_strings_async(
132  FlPlatformPlugin* self,
133  FlMethodCall* method_call) {
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 }
142 
143 // Called when Flutter wants to play a sound.
144 static FlMethodResponse* system_sound_play(FlPlatformPlugin* self,
145  FlValue* args) {
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 }
165 
166 // Called when Flutter wants to quit the application.
167 static FlMethodResponse* system_navigator_pop(FlPlatformPlugin* self) {
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 }
178 
179 // Called when a method call is received from Flutter.
180 static void method_call_cb(FlMethodChannel* channel,
181  FlMethodCall* method_call,
182  gpointer user_data) {
183  FlPlatformPlugin* self = FL_PLATFORM_PLUGIN(user_data);
184 
185  const gchar* method = fl_method_call_get_name(method_call);
186  FlValue* args = fl_method_call_get_args(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 }
207 
208 static void fl_platform_plugin_dispose(GObject* object) {
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 }
215 
216 static void fl_platform_plugin_class_init(FlPlatformPluginClass* klass) {
217  G_OBJECT_CLASS(klass)->dispose = fl_platform_plugin_dispose;
218 }
219 
220 static void fl_platform_plugin_init(FlPlatformPlugin* self) {}
221 
222 FlPlatformPlugin* fl_platform_plugin_new(FlBinaryMessenger* messenger) {
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_BEGIN_DECLS FlValue * args
static constexpr char kClipboardHasStringsMethod[]
KeyCallType type
G_MODULE_EXPORT FlMethodSuccessResponse * fl_method_success_response_new(FlValue *result)
static constexpr char kSoundTypeAlert[]
G_BEGIN_DECLS FlMethodCall * method_call
const uint8_t uint32_t uint32_t GError ** error
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 kPlaySoundMethod[]
G_MODULE_EXPORT FlJsonMethodCodec * fl_json_method_codec_new()
G_MODULE_EXPORT FlValue * fl_value_new_map()
Definition: fl_value.cc:355
static constexpr char kFailedError[]
static FlMethodResponse * system_sound_play(FlPlatformPlugin *self, FlValue *args)
static constexpr char kChannelName[]
void * user_data
GAsyncResult * result
uint32_t uint32_t * format
static constexpr char kSoundTypeClick[]
static constexpr char kGetClipboardDataMethod[]
static constexpr char kTextPlainFormat[]
static FlMethodResponse * clipboard_has_strings_async(FlPlatformPlugin *self, FlMethodCall *method_call)
G_MODULE_EXPORT FlValue * fl_value_new_bool(bool value)
Definition: fl_value.cc:244
static constexpr char kBadArgumentsError[]
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 kUnknownClipboardFormatError[]
static void clipboard_text_has_strings_cb(GtkClipboard *clipboard, const gchar *text, gpointer user_data)
static constexpr char kSystemNavigatorPopMethod[]
static void clipboard_text_cb(GtkClipboard *clipboard, const gchar *text, gpointer user_data)
static void fl_platform_plugin_init(FlPlatformPlugin *self)
G_MODULE_EXPORT FlValue * fl_value_lookup_string(FlValue *self, const gchar *key)
Definition: fl_value.cc:770
G_MODULE_EXPORT gboolean fl_method_call_respond(FlMethodCall *self, FlMethodResponse *response, GError **error)
G_MODULE_EXPORT void fl_method_channel_set_method_call_handler(FlMethodChannel *self, FlMethodChannelMethodCallHandler handler, gpointer user_data, GDestroyNotify destroy_notify)
G_MODULE_EXPORT const gchar * fl_method_call_get_name(FlMethodCall *self)
G_MODULE_EXPORT FlMethodChannel * fl_method_channel_new(FlBinaryMessenger *messenger, const gchar *name, FlMethodCodec *codec)
static void fl_platform_plugin_class_init(FlPlatformPluginClass *klass)
static void method_call_cb(FlMethodChannel *channel, FlMethodCall *method_call, gpointer user_data)
FlMethodChannel * channel
G_MODULE_EXPORT FlValue * fl_value_new_string(const gchar *value)
Definition: fl_value.cc:265
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 const gchar * fl_value_get_string(FlValue *self)
Definition: fl_value.cc:642
std::u16string text
FlPlatformPlugin * fl_platform_plugin_new(FlBinaryMessenger *messenger)
static constexpr char kValueKey[]
static FlMethodResponse * clipboard_get_data_async(FlPlatformPlugin *self, FlMethodCall *method_call)
G_MODULE_EXPORT FlMethodNotImplementedResponse * fl_method_not_implemented_response_new()
G_MODULE_EXPORT void fl_value_set_string_take(FlValue *self, const gchar *key, FlValue *value)
Definition: fl_value.cc:610
static FlMethodResponse * clipboard_set_data(FlPlatformPlugin *self, FlValue *args)
static FlMethodResponse * system_navigator_pop(FlPlatformPlugin *self)
static void fl_platform_plugin_dispose(GObject *object)
G_MODULE_EXPORT FlMethodErrorResponse * fl_method_error_response_new(const gchar *code, const gchar *message, FlValue *details)
static constexpr char kTextKey[]