Herbert Poul
4 years ago
4 changed files with 86 additions and 96 deletions
@ -1,78 +1,68 @@
|
||||
#include "include/argon2_ffi_plugin.h" |
||||
#include "include/argon2_ffi/argon2_ffi_plugin.h" |
||||
|
||||
#include <flutter/method_channel.h> |
||||
#include <flutter/plugin_registrar_glfw.h> |
||||
#include <flutter/standard_method_codec.h> |
||||
#include <flutter_linux/flutter_linux.h> |
||||
#include <gtk/gtk.h> |
||||
#include <sys/utsname.h> |
||||
|
||||
#include <map> |
||||
#include <memory> |
||||
#include <sstream> |
||||
#define ARGON2_FFI_PLUGIN(obj) \ |
||||
(G_TYPE_CHECK_INSTANCE_CAST((obj), argon2_ffi_plugin_get_type(), \
|
||||
Argon2FfiPlugin)) |
||||
|
||||
namespace { |
||||
|
||||
class Argon2FfiPlugin : public flutter::Plugin { |
||||
public: |
||||
static void RegisterWithRegistrar(flutter::PluginRegistrarGlfw *registrar); |
||||
struct _Argon2FfiPlugin { |
||||
GObject parent_instance; |
||||
}; |
||||
|
||||
Argon2FfiPlugin(); |
||||
G_DEFINE_TYPE(Argon2FfiPlugin, argon2_ffi_plugin, g_object_get_type()) |
||||
|
||||
virtual ~Argon2FfiPlugin(); |
||||
// Called when a method call is received from Flutter.
|
||||
static void argon2_ffi_plugin_handle_method_call( |
||||
Argon2FfiPlugin* self, |
||||
FlMethodCall* method_call) { |
||||
g_autoptr(FlMethodResponse) response = nullptr; |
||||
|
||||
private: |
||||
// Called when a method is called on this plugin's channel from Dart.
|
||||
void HandleMethodCall( |
||||
const flutter::MethodCall<flutter::EncodableValue> &method_call, |
||||
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result); |
||||
}; |
||||
const gchar* method = fl_method_call_get_name(method_call); |
||||
|
||||
// static
|
||||
void Argon2FfiPlugin::RegisterWithRegistrar( |
||||
flutter::PluginRegistrarGlfw *registrar) { |
||||
auto channel = |
||||
std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>( |
||||
registrar->messenger(), "argon2_ffi", |
||||
&flutter::StandardMethodCodec::GetInstance()); |
||||
auto plugin = std::make_unique<Argon2FfiPlugin>(); |
||||
if (strcmp(method, "getPlatformVersion") == 0) { |
||||
struct utsname uname_data = {}; |
||||
uname(&uname_data); |
||||
g_autofree gchar *version = g_strdup_printf("Linux %s", uname_data.version); |
||||
g_autoptr(FlValue) result = fl_value_new_string(version); |
||||
response = FL_METHOD_RESPONSE(fl_method_success_response_new(result)); |
||||
} else { |
||||
response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new()); |
||||
} |
||||
|
||||
channel->SetMethodCallHandler( |
||||
[plugin_pointer = plugin.get()](const auto &call, auto result) { |
||||
plugin_pointer->HandleMethodCall(call, std::move(result)); |
||||
}); |
||||
fl_method_call_respond(method_call, response, nullptr); |
||||
} |
||||
|
||||
registrar->AddPlugin(std::move(plugin)); |
||||
static void argon2_ffi_plugin_dispose(GObject* object) { |
||||
G_OBJECT_CLASS(argon2_ffi_plugin_parent_class)->dispose(object); |
||||
} |
||||
|
||||
Argon2FfiPlugin::Argon2FfiPlugin() {} |
||||
static void argon2_ffi_plugin_class_init(Argon2FfiPluginClass* klass) { |
||||
G_OBJECT_CLASS(klass)->dispose = argon2_ffi_plugin_dispose; |
||||
} |
||||
|
||||
Argon2FfiPlugin::~Argon2FfiPlugin() {} |
||||
static void argon2_ffi_plugin_init(Argon2FfiPlugin* self) {} |
||||
|
||||
void Argon2FfiPlugin::HandleMethodCall( |
||||
const flutter::MethodCall<flutter::EncodableValue> &method_call, |
||||
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) { |
||||
// Replace "getPlatformVersion" check with your plugin's method.
|
||||
// See:
|
||||
// https://github.com/flutter/engine/tree/master/shell/platform/common/cpp/client_wrapper/include/flutter
|
||||
// and
|
||||
// https://github.com/flutter/engine/tree/master/shell/platform/glfw/client_wrapper/include/flutter
|
||||
// for the relevant Flutter APIs.
|
||||
if (method_call.method_name().compare("getPlatformVersion") == 0) { |
||||
struct utsname uname_data = {}; |
||||
uname(&uname_data); |
||||
std::ostringstream version_stream; |
||||
version_stream << "Linux " << uname_data.version; |
||||
flutter::EncodableValue response(version_stream.str()); |
||||
result->Success(&response); |
||||
} else { |
||||
result->NotImplemented(); |
||||
} |
||||
static void method_call_cb(FlMethodChannel* channel, FlMethodCall* method_call, |
||||
gpointer user_data) { |
||||
Argon2FfiPlugin* plugin = ARGON2_FFI_PLUGIN(user_data); |
||||
argon2_ffi_plugin_handle_method_call(plugin, method_call); |
||||
} |
||||
|
||||
} // namespace
|
||||
void argon2_ffi_plugin_register_with_registrar(FlPluginRegistrar* registrar) { |
||||
Argon2FfiPlugin* plugin = ARGON2_FFI_PLUGIN( |
||||
g_object_new(argon2_ffi_plugin_get_type(), nullptr)); |
||||
|
||||
g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new(); |
||||
g_autoptr(FlMethodChannel) channel = |
||||
fl_method_channel_new(fl_plugin_registrar_get_messenger(registrar), |
||||
"argon2_ffi", |
||||
FL_METHOD_CODEC(codec)); |
||||
fl_method_channel_set_method_call_handler(channel, method_call_cb, |
||||
g_object_ref(plugin), |
||||
g_object_unref); |
||||
|
||||
void Argon2FfiPluginRegisterWithRegistrar( |
||||
FlutterDesktopPluginRegistrarRef registrar) { |
||||
Argon2FfiPlugin::RegisterWithRegistrar( |
||||
flutter::PluginRegistrarManager::GetInstance() |
||||
->GetRegistrar<flutter::PluginRegistrarGlfw>(registrar)); |
||||
g_object_unref(plugin); |
||||
} |
||||
|
@ -0,0 +1,26 @@
|
||||
#ifndef FLUTTER_PLUGIN_ARGON2_FFI_PLUGIN_H_ |
||||
#define FLUTTER_PLUGIN_ARGON2_FFI_PLUGIN_H_ |
||||
|
||||
#include <flutter_linux/flutter_linux.h> |
||||
|
||||
G_BEGIN_DECLS |
||||
|
||||
#ifdef FLUTTER_PLUGIN_IMPL |
||||
#define FLUTTER_PLUGIN_EXPORT __attribute__((visibility("default"))) |
||||
#else |
||||
#define FLUTTER_PLUGIN_EXPORT |
||||
#endif |
||||
|
||||
typedef struct _Argon2FfiPlugin Argon2FfiPlugin; |
||||
typedef struct { |
||||
GObjectClass parent_class; |
||||
} Argon2FfiPluginClass; |
||||
|
||||
FLUTTER_PLUGIN_EXPORT GType argon2_ffi_plugin_get_type(); |
||||
|
||||
FLUTTER_PLUGIN_EXPORT void argon2_ffi_plugin_register_with_registrar( |
||||
FlPluginRegistrar* registrar); |
||||
|
||||
G_END_DECLS |
||||
|
||||
#endif // FLUTTER_PLUGIN_ARGON2_FFI_PLUGIN_H_
|
@ -1,23 +0,0 @@
|
||||
#ifndef FLUTTER_PLUGIN_ARGON2_FFI_PLUGIN_H_ |
||||
#define FLUTTER_PLUGIN_ARGON2_FFI_PLUGIN_H_ |
||||
|
||||
#include <flutter_plugin_registrar.h> |
||||
|
||||
#ifdef FLUTTER_PLUGIN_IMPL |
||||
#define FLUTTER_PLUGIN_EXPORT __attribute__((visibility("default"))) |
||||
#else |
||||
#define FLUTTER_PLUGIN_EXPORT |
||||
#endif |
||||
|
||||
#if defined(__cplusplus) |
||||
extern "C" { |
||||
#endif |
||||
|
||||
FLUTTER_PLUGIN_EXPORT void Argon2FfiPluginRegisterWithRegistrar( |
||||
FlutterDesktopPluginRegistrarRef registrar); |
||||
|
||||
#if defined(__cplusplus) |
||||
} // extern "C"
|
||||
#endif |
||||
|
||||
#endif // FLUTTER_PLUGIN_ARGON2_FFI_PLUGIN_H_
|
Loading…
Reference in new issue