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_linux/flutter_linux.h> |
||||||
#include <flutter/plugin_registrar_glfw.h> |
#include <gtk/gtk.h> |
||||||
#include <flutter/standard_method_codec.h> |
|
||||||
#include <sys/utsname.h> |
#include <sys/utsname.h> |
||||||
|
|
||||||
#include <map> |
#define ARGON2_FFI_PLUGIN(obj) \ |
||||||
#include <memory> |
(G_TYPE_CHECK_INSTANCE_CAST((obj), argon2_ffi_plugin_get_type(), \
|
||||||
#include <sstream> |
Argon2FfiPlugin)) |
||||||
|
|
||||||
namespace { |
struct _Argon2FfiPlugin { |
||||||
|
GObject parent_instance; |
||||||
class Argon2FfiPlugin : public flutter::Plugin { |
}; |
||||||
public: |
|
||||||
static void RegisterWithRegistrar(flutter::PluginRegistrarGlfw *registrar); |
|
||||||
|
|
||||||
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: |
const gchar* method = fl_method_call_get_name(method_call); |
||||||
// 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); |
|
||||||
}; |
|
||||||
|
|
||||||
// static
|
if (strcmp(method, "getPlatformVersion") == 0) { |
||||||
void Argon2FfiPlugin::RegisterWithRegistrar( |
struct utsname uname_data = {}; |
||||||
flutter::PluginRegistrarGlfw *registrar) { |
uname(&uname_data); |
||||||
auto channel = |
g_autofree gchar *version = g_strdup_printf("Linux %s", uname_data.version); |
||||||
std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>( |
g_autoptr(FlValue) result = fl_value_new_string(version); |
||||||
registrar->messenger(), "argon2_ffi", |
response = FL_METHOD_RESPONSE(fl_method_success_response_new(result)); |
||||||
&flutter::StandardMethodCodec::GetInstance()); |
} else { |
||||||
auto plugin = std::make_unique<Argon2FfiPlugin>(); |
response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new()); |
||||||
|
} |
||||||
|
|
||||||
channel->SetMethodCallHandler( |
fl_method_call_respond(method_call, response, nullptr); |
||||||
[plugin_pointer = plugin.get()](const auto &call, auto result) { |
} |
||||||
plugin_pointer->HandleMethodCall(call, std::move(result)); |
|
||||||
}); |
|
||||||
|
|
||||||
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( |
static void method_call_cb(FlMethodChannel* channel, FlMethodCall* method_call, |
||||||
const flutter::MethodCall<flutter::EncodableValue> &method_call, |
gpointer user_data) { |
||||||
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) { |
Argon2FfiPlugin* plugin = ARGON2_FFI_PLUGIN(user_data); |
||||||
// Replace "getPlatformVersion" check with your plugin's method.
|
argon2_ffi_plugin_handle_method_call(plugin, method_call); |
||||||
// 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(); |
|
||||||
} |
|
||||||
} |
} |
||||||
|
|
||||||
} // 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( |
g_object_unref(plugin); |
||||||
FlutterDesktopPluginRegistrarRef registrar) { |
|
||||||
Argon2FfiPlugin::RegisterWithRegistrar( |
|
||||||
flutter::PluginRegistrarManager::GetInstance() |
|
||||||
->GetRegistrar<flutter::PluginRegistrarGlfw>(registrar)); |
|
||||||
} |
} |
||||||
|
@ -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