plugin_framework, flutter event handlers

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou
2023-04-21 21:40:34 +08:00
parent ae6d80cebe
commit 67413b7419
15 changed files with 301 additions and 192 deletions

View File

@@ -1,6 +1,7 @@
use super::cstr_to_string;
use super::*;
use crate::flutter::{self, APP_TYPE_CM, APP_TYPE_MAIN, SESSIONS};
use hbb_common::{lazy_static, log, message_proto::PluginRequest};
use serde_derive::Deserialize;
use serde_json;
use std::{
collections::HashMap,
@@ -10,6 +11,7 @@ use std::{
const MSG_TO_PEER_TARGET: &str = "peer";
const MSG_TO_UI_TARGET: &str = "ui";
const MSG_TO_CONFIG_TARGET: &str = "config";
#[allow(dead_code)]
const MSG_TO_UI_FLUTTER_CHANNEL_MAIN: u16 = 0x01 << 0;
@@ -34,6 +36,22 @@ lazy_static::lazy_static! {
};
}
#[derive(Deserialize)]
struct ConfigToUi {
channel: u16,
location: String,
}
#[derive(Deserialize)]
struct MsgToConfig {
id: String,
r#type: String,
key: String,
value: String,
#[serde(skip_serializing_if = "Option::is_none")]
ui: Option<ConfigToUi>, // If not None, send msg to ui.
}
/// Callback to send message to peer or ui.
/// peer, target, id are utf8 strings(null terminated).
///
@@ -83,25 +101,42 @@ pub fn callback_msg(
let channel = u16::from_be_bytes([content_slice[0], content_slice[1]]);
let content = std::string::String::from_utf8(content_slice[2..].to_vec())
.unwrap_or("".to_string());
let mut m = HashMap::new();
m.insert("name", "plugin_event");
m.insert("peer", &peer);
m.insert("content", &content);
let event = serde_json::to_string(&m).unwrap_or("".to_string());
for (k, v) in MSG_TO_UI_FLUTTER_CHANNELS.iter() {
if channel & k != 0 {
let _res = flutter::push_global_event(v as _, event.clone());
}
}
if channel & MSG_TO_UI_FLUTTER_CHANNEL_REMOTE != 0
|| channel & MSG_TO_UI_FLUTTER_CHANNEL_TRANSFER != 0
|| channel & MSG_TO_UI_FLUTTER_CHANNEL_FORWARD != 0
push_event_to_ui(channel, &peer, &content);
}
MSG_TO_CONFIG_TARGET => {
if let Ok(s) =
std::str::from_utf8(unsafe { std::slice::from_raw_parts(content as _, len) })
{
let _res = flutter::push_session_event(
&peer,
"plugin_event",
vec![("peer", &peer), ("content", &content)],
);
if let Ok(msg) = serde_json::from_str::<MsgToConfig>(s) {
match &msg.r#type as _ {
config::CONFIG_TYPE_LOCAL => {
match config::LocalConfig::set(&msg.id, &msg.key, &msg.value) {
Ok(_) => {
if let Some(ui) = &msg.ui {
// No need to set the peer id for location config.
push_option_to_ui(ui.channel, "", &msg, ui);
}
}
Err(e) => {
log::error!("Failed to set local config, {}", e);
}
}
}
config::CONFIG_TYPE_PEER => {
match config::PeerConfig::set(&msg.id, &peer, &msg.key, &msg.value) {
Ok(_) => {
if let Some(ui) = &msg.ui {
push_option_to_ui(ui.channel, &peer, &msg, ui);
}
}
Err(e) => {
log::error!("Failed to set peer config, {}", e);
}
}
}
_ => {}
}
}
}
}
_ => {
@@ -109,3 +144,51 @@ pub fn callback_msg(
}
}
}
#[inline]
fn is_peer_channel(channel: u16) -> bool {
channel & MSG_TO_UI_FLUTTER_CHANNEL_REMOTE != 0
|| channel & MSG_TO_UI_FLUTTER_CHANNEL_TRANSFER != 0
|| channel & MSG_TO_UI_FLUTTER_CHANNEL_FORWARD != 0
}
fn push_event_to_ui(channel: u16, peer: &str, content: &str) {
let mut m = HashMap::new();
m.insert("name", MSG_TO_UI_TYPE_PLUGIN_EVENT);
m.insert("peer", &peer);
m.insert("content", &content);
let event = serde_json::to_string(&m).unwrap_or("".to_string());
for (k, v) in MSG_TO_UI_FLUTTER_CHANNELS.iter() {
if channel & k != 0 {
let _res = flutter::push_global_event(v as _, event.to_string());
}
}
if is_peer_channel(channel) {
let _res = flutter::push_session_event(
&peer,
MSG_TO_UI_TYPE_PLUGIN_EVENT,
vec![("peer", &peer), ("content", &content)],
);
}
}
fn push_option_to_ui(channel: u16, peer: &str, msg: &MsgToConfig, ui: &ConfigToUi) {
let v = [
("name", MSG_TO_UI_TYPE_PLUGIN_OPTION),
("id", &msg.id),
("location", &ui.location),
("key", &msg.key),
("value", &msg.value),
];
let event = serde_json::to_string(&HashMap::from(v)).unwrap_or("".to_string());
for (k, v) in MSG_TO_UI_FLUTTER_CHANNELS.iter() {
if channel & k != 0 {
let _res = flutter::push_global_event(v as _, event.to_string());
}
}
let mut v = v.to_vec();
v.push(("peer", &peer));
if is_peer_channel(channel) {
let _res = flutter::push_session_event(&peer, MSG_TO_UI_TYPE_PLUGIN_OPTION, v.to_vec());
}
}