Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit a60e62b

Browse files
committed
Add FlPlatformPlugin
1 parent c7f9725 commit a60e62b

5 files changed

Lines changed: 155 additions & 0 deletions

File tree

ci/licenses_golden/licenses_flutter

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,8 @@ FILE: ../../../flutter/shell/platform/linux/fl_method_codec_private.h
12261226
FILE: ../../../flutter/shell/platform/linux/fl_method_codec_test.cc
12271227
FILE: ../../../flutter/shell/platform/linux/fl_method_response.cc
12281228
FILE: ../../../flutter/shell/platform/linux/fl_method_response_test.cc
1229+
FILE: ../../../flutter/shell/platform/linux/fl_platform_plugin.cc
1230+
FILE: ../../../flutter/shell/platform/linux/fl_platform_plugin.h
12291231
FILE: ../../../flutter/shell/platform/linux/fl_plugin_registrar.cc
12301232
FILE: ../../../flutter/shell/platform/linux/fl_plugin_registrar_private.h
12311233
FILE: ../../../flutter/shell/platform/linux/fl_plugin_registry.cc

shell/platform/linux/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ source_set("flutter_linux_sources") {
8989
"fl_method_channel.cc",
9090
"fl_method_codec.cc",
9191
"fl_method_response.cc",
92+
"fl_platform_plugin.cc",
9293
"fl_plugin_registrar.cc",
9394
"fl_plugin_registry.cc",
9495
"fl_renderer.cc",
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
#include "flutter/shell/platform/linux/public/flutter_linux/fl_json_method_codec.h"
7+
#include "flutter/shell/platform/linux/public/flutter_linux/fl_method_channel.h"
8+
9+
static constexpr char kChannelName[] = "flutter/platform";
10+
static constexpr char kBadArgumentError[] = "Bad Arguments";
11+
static constexpr char kGetClipboardDataMethod[] = "Clipboard.getData";
12+
static constexpr char kSetClipboardDataMethod[] = "Clipboard.setData";
13+
static constexpr char kPlaySoundMethod[] = "SystemSound.play";
14+
static constexpr char kSystemNavigatorPopMethod[] = "SystemNavigator.pop";
15+
16+
struct _FlPlatformPlugin {
17+
GObject parent_instance;
18+
19+
FlMethodChannel* channel;
20+
};
21+
22+
G_DEFINE_TYPE(FlPlatformPlugin, fl_platform_plugin, G_TYPE_OBJECT)
23+
24+
// Called when Flutter wants to copy to the clipboard.
25+
static FlMethodResponse* clipboard_set_data(FlPlatformPlugin* self,
26+
FlValue* args) {
27+
// FIXME
28+
return FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
29+
}
30+
31+
// Called when Flutter wants to paste from the clipboard.
32+
static FlMethodResponse* clipboard_get_data(FlPlatformPlugin* self) {
33+
// FIXME
34+
return FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
35+
}
36+
37+
// Called when Flutter wants to play a sound.
38+
static FlMethodResponse* system_sound_play(FlPlatformPlugin* self,
39+
FlValue* args) {
40+
if (fl_value_get_type(args) != FL_VALUE_TYPE_STRING) {
41+
return FL_METHOD_RESPONSE(fl_method_error_response_new(
42+
kBadArgumentError, "Expected string", nullptr));
43+
}
44+
45+
// const gchar* type = fl_value_get_string(args);
46+
// FIXME(robert-ancell): Play sound
47+
48+
return FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
49+
}
50+
51+
// Called when Flutter wants to quit the application.
52+
static FlMethodResponse* system_navigator_pop(FlPlatformPlugin* self) {
53+
// FIXME(robert-ancell): Quit the application
54+
55+
return FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
56+
}
57+
58+
// Called when a method call is received from Flutter.
59+
static void method_call_cb(FlMethodChannel* channel,
60+
FlMethodCall* method_call,
61+
gpointer user_data) {
62+
FlPlatformPlugin* self = FL_PLATFORM_PLUGIN(user_data);
63+
64+
const gchar* method = fl_method_call_get_name(method_call);
65+
FlValue* args = fl_method_call_get_args(method_call);
66+
67+
g_autoptr(FlMethodResponse) response = nullptr;
68+
if (strcmp(method, kSetClipboardDataMethod) == 0)
69+
response = clipboard_set_data(self, args);
70+
else if (strcmp(method, kGetClipboardDataMethod) == 0)
71+
response = clipboard_get_data(self, args);
72+
else if (strcmp(method, kPlaySoundMethod) == 0)
73+
response = system_sunnd_play(self, args);
74+
else if (strcmp(method, kSystemNavigatorPopMethod) == 0)
75+
response = system_navigator_pop(self);
76+
else
77+
response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
78+
79+
g_autoptr(GError) error = nullptr;
80+
if (!fl_method_call_respond(method_call, response, &error))
81+
g_warning("Failed to send method call response: %s", error->message);
82+
}
83+
84+
static void fl_platform_plugin_dispose(GObject* object) {
85+
FlPlatformPlugin* self = FL_PLATFORM_PLUGIN(object);
86+
87+
g_clear_object(&self->channel);
88+
89+
G_OBJECT_CLASS(fl_platform_plugin_parent_class)->dispose(object);
90+
}
91+
92+
static void fl_platform_plugin_class_init(FlPlatformPluginClass* klass) {
93+
G_OBJECT_CLASS(klass)->dispose = fl_platform_plugin_dispose;
94+
}
95+
96+
static void fl_platform_plugin_init(FlPlatformPlugin* self) {}
97+
98+
FlPlatformPlugin* fl_platform_plugin_new(FlBinaryMessenger* messenger) {
99+
g_return_val_if_fail(FL_IS_BINARY_MESSENGER(messenger), nullptr);
100+
101+
FlPlatformPlugin* self =
102+
FL_PLATFORM_PLUGIN(g_object_new(fl_platform_plugin_get_type(), nullptr));
103+
104+
g_autoptr(FlJsonMethodCodec) codec = fl_json_method_codec_new();
105+
self->channel =
106+
fl_method_channel_new(messenger, kChannelName, FL_METHOD_CODEC(codec));
107+
fl_method_channel_set_method_call_handler(self->channel, method_call_cb, self,
108+
nullptr);
109+
110+
return self;
111+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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_PLATFORM_PLUGIN_H_
6+
#define FLUTTER_SHELL_PLATFORM_LINUX_FL_PLATFORM_PLUGIN_H_
7+
8+
#include "flutter/shell/platform/linux/public/flutter_linux/fl_binary_messenger.h"
9+
10+
G_BEGIN_DECLS
11+
12+
G_DECLARE_FINAL_TYPE(FlPlatformPlugin,
13+
fl_platform_plugin,
14+
FL,
15+
PLATFORM_PLUGIN,
16+
GObject);
17+
18+
/**
19+
* FlPlatformPlugin:
20+
*
21+
* #FlPlatformPlugin is a platform channel that implements the shell side
22+
* of PlatformPlugins.platform from the Flutter services library.
23+
*/
24+
25+
/**
26+
* fl_platform_plugin_new:
27+
* @messenger: an #FlBinaryMessenger
28+
*
29+
* Creates a new platform channel.
30+
*
31+
* Returns: a new #FlPlatformPlugin
32+
*/
33+
FlPlatformPlugin* fl_platform_plugin_new(FlBinaryMessenger* messenger);
34+
35+
G_END_DECLS
36+
37+
#endif // FLUTTER_SHELL_PLATFORM_LINUX_FL_PLATFORM_PLUGIN_H_

shell/platform/linux/fl_view.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "flutter/shell/platform/linux/fl_engine_private.h"
88
#include "flutter/shell/platform/linux/fl_key_event_plugin.h"
9+
#include "flutter/shell/platform/linux/fl_platform_plugin.h"
910
#include "flutter/shell/platform/linux/fl_plugin_registrar_private.h"
1011
#include "flutter/shell/platform/linux/fl_renderer_x11.h"
1112
#include "flutter/shell/platform/linux/fl_text_input_plugin.h"
@@ -33,6 +34,7 @@ struct _FlView {
3334

3435
// Flutter system channel handlers.
3536
FlKeyEventPlugin* key_event_plugin;
37+
FlPlatformPlugin* platform_plugin;
3638
FlTextInputPlugin* text_input_plugin;
3739
};
3840

@@ -116,6 +118,7 @@ static void fl_view_constructed(GObject* object) {
116118
// Create system channel handlers.
117119
FlBinaryMessenger* messenger = fl_engine_get_binary_messenger(self->engine);
118120
self->key_event_plugin = fl_key_event_plugin_new(messenger);
121+
self->platform_plugin = fl_platform_plugin_new(messenger);
119122
self->text_input_plugin = fl_text_input_plugin_new(messenger);
120123
}
121124

@@ -159,6 +162,7 @@ static void fl_view_dispose(GObject* object) {
159162
g_clear_object(&self->renderer);
160163
g_clear_object(&self->engine);
161164
g_clear_object(&self->key_event_plugin);
165+
g_clear_object(&self->platform_plugin);
162166
g_clear_object(&self->text_input_plugin);
163167

164168
G_OBJECT_CLASS(fl_view_parent_class)->dispose(object);

0 commit comments

Comments
 (0)