mirror of
https://github.com/rustdesk/rustdesk.git
synced 2026-04-02 02:21:28 +03:00
remove redundent global variables
Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
@@ -61,6 +61,8 @@ pub trait PrivacyMode: Sync + Send {
|
||||
|
||||
fn pre_conn_id(&self) -> i32;
|
||||
|
||||
fn get_impl_key(&self) -> &str;
|
||||
|
||||
#[inline]
|
||||
fn check_on_conn_id(&self, conn_id: i32) -> ResultType<bool> {
|
||||
let pre_conn_id = self.pre_conn_id();
|
||||
@@ -117,24 +119,21 @@ lazy_static::lazy_static! {
|
||||
}
|
||||
};
|
||||
|
||||
static ref CUR_PRIVACY_MODE_IMPL: Arc<Mutex<String>> = {
|
||||
static ref PRIVACY_MODE: Arc<Mutex<Option<Box<dyn PrivacyMode>>>> = {
|
||||
let mut cur_impl = get_option("privacy-mode-impl-key".to_owned());
|
||||
if !get_supported_privacy_mode_impl().iter().any(|(k, _)| k == &cur_impl) {
|
||||
cur_impl = DEFAULT_PRIVACY_MODE_IMPL.to_owned();
|
||||
}
|
||||
Arc::new(Mutex::new(cur_impl))
|
||||
};
|
||||
static ref PRIVACY_MODE: Arc<Mutex<Option<Box<dyn PrivacyMode>>>> = {
|
||||
let cur_impl = (*CUR_PRIVACY_MODE_IMPL.lock().unwrap()).clone();
|
||||
|
||||
let privacy_mode = match PRIVACY_MODE_CREATOR.lock().unwrap().get(&(&cur_impl as &str)) {
|
||||
Some(creator) => Some(creator()),
|
||||
Some(creator) => Some(creator(&cur_impl)),
|
||||
None => None,
|
||||
};
|
||||
Arc::new(Mutex::new(privacy_mode))
|
||||
};
|
||||
}
|
||||
|
||||
pub type PrivacyModeCreator = fn() -> Box<dyn PrivacyMode>;
|
||||
pub type PrivacyModeCreator = fn(impl_key: &str) -> Box<dyn PrivacyMode>;
|
||||
lazy_static::lazy_static! {
|
||||
static ref PRIVACY_MODE_CREATOR: Arc<Mutex<HashMap<&'static str, PrivacyModeCreator>>> = {
|
||||
#[cfg(not(windows))]
|
||||
@@ -144,18 +143,18 @@ lazy_static::lazy_static! {
|
||||
#[cfg(windows)]
|
||||
{
|
||||
if win_exclude_from_capture::is_supported() {
|
||||
map.insert(win_exclude_from_capture::PRIVACY_MODE_IMPL, || {
|
||||
Box::new(win_exclude_from_capture::PrivacyModeImpl::default())
|
||||
map.insert(win_exclude_from_capture::PRIVACY_MODE_IMPL, |impl_key: &str| {
|
||||
Box::new(win_exclude_from_capture::PrivacyModeImpl::new(impl_key))
|
||||
});
|
||||
} else {
|
||||
map.insert(win_mag::PRIVACY_MODE_IMPL, || {
|
||||
Box::new(win_mag::PrivacyModeImpl::default())
|
||||
map.insert(win_mag::PRIVACY_MODE_IMPL, |impl_key: &str| {
|
||||
Box::new(win_mag::PrivacyModeImpl::new(impl_key))
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(feature = "virtual_display_driver")]
|
||||
map.insert(win_virtual_display::PRIVACY_MODE_IMPL, || {
|
||||
Box::new(win_virtual_display::PrivacyModeImpl::default())
|
||||
map.insert(win_virtual_display::PRIVACY_MODE_IMPL, |impl_key: &str| {
|
||||
Box::new(win_virtual_display::PrivacyModeImpl::new(impl_key))
|
||||
});
|
||||
}
|
||||
Arc::new(Mutex::new(map))
|
||||
@@ -174,13 +173,15 @@ pub fn clear() -> Option<()> {
|
||||
|
||||
#[inline]
|
||||
pub fn switch(impl_key: &str) {
|
||||
let mut cur_impl_lock = CUR_PRIVACY_MODE_IMPL.lock().unwrap();
|
||||
if *cur_impl_lock == impl_key {
|
||||
return;
|
||||
let mut privacy_mode_lock = PRIVACY_MODE.lock().unwrap();
|
||||
if let Some(privacy_mode) = privacy_mode_lock.as_ref() {
|
||||
if privacy_mode.get_impl_key() == impl_key {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(creator) = PRIVACY_MODE_CREATOR.lock().unwrap().get(impl_key) {
|
||||
*PRIVACY_MODE.lock().unwrap() = Some(creator());
|
||||
*cur_impl_lock = impl_key.to_owned();
|
||||
*privacy_mode_lock = Some(creator(impl_key));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -208,13 +209,15 @@ pub fn turn_on_privacy(impl_key: &str, conn_id: i32) -> Option<ResultType<bool>>
|
||||
|
||||
// Check or switch privacy mode implementation
|
||||
let impl_key = get_supported_impl(impl_key);
|
||||
let mut cur_impl_lock = CUR_PRIVACY_MODE_IMPL.lock().unwrap();
|
||||
|
||||
let mut cur_impl_key = "".to_string();
|
||||
if let Some(privacy_mode) = privacy_mode_lock.as_ref() {
|
||||
cur_impl_key = privacy_mode.get_impl_key().to_string();
|
||||
let check_on_conn_id = privacy_mode.check_on_conn_id(conn_id);
|
||||
match check_on_conn_id.as_ref() {
|
||||
Ok(true) => {
|
||||
if *cur_impl_lock == impl_key {
|
||||
if cur_impl_key == impl_key {
|
||||
// Same peer, same implementation.
|
||||
return Some(Ok(true));
|
||||
} else {
|
||||
// Same peer, switch to new implementation.
|
||||
@@ -225,7 +228,7 @@ pub fn turn_on_privacy(impl_key: &str, conn_id: i32) -> Option<ResultType<bool>>
|
||||
}
|
||||
}
|
||||
|
||||
if *cur_impl_lock != impl_key {
|
||||
if cur_impl_key != impl_key {
|
||||
if let Some(creator) = PRIVACY_MODE_CREATOR
|
||||
.lock()
|
||||
.unwrap()
|
||||
@@ -235,8 +238,7 @@ pub fn turn_on_privacy(impl_key: &str, conn_id: i32) -> Option<ResultType<bool>>
|
||||
privacy_mode.clear();
|
||||
}
|
||||
|
||||
*privacy_mode_lock = Some(creator());
|
||||
*cur_impl_lock = impl_key.to_owned();
|
||||
*privacy_mode_lock = Some(creator(&impl_key));
|
||||
} else {
|
||||
return Some(Err(anyhow!("Unsupported privacy mode: {}", impl_key)));
|
||||
}
|
||||
@@ -313,9 +315,23 @@ pub fn get_supported_privacy_mode_impl() -> Vec<(&'static str, &'static str)> {
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_cur_impl_key() -> Option<String> {
|
||||
PRIVACY_MODE
|
||||
.lock()
|
||||
.unwrap()
|
||||
.as_ref()
|
||||
.map(|pm| pm.get_impl_key().to_owned())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn is_current_privacy_mode_impl(impl_key: &str) -> bool {
|
||||
*CUR_PRIVACY_MODE_IMPL.lock().unwrap() == impl_key
|
||||
PRIVACY_MODE
|
||||
.lock()
|
||||
.unwrap()
|
||||
.as_ref()
|
||||
.map(|pm| pm.get_impl_key() == impl_key)
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@@ -347,3 +363,22 @@ pub fn check_privacy_mode_err(
|
||||
pub fn is_privacy_mode_supported() -> bool {
|
||||
!DEFAULT_PRIVACY_MODE_IMPL.is_empty()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_privacy_mode_conn_id() -> Option<i32> {
|
||||
PRIVACY_MODE
|
||||
.lock()
|
||||
.unwrap()
|
||||
.as_ref()
|
||||
.map(|pm| pm.pre_conn_id())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn is_in_privacy_mode() -> bool {
|
||||
PRIVACY_MODE
|
||||
.lock()
|
||||
.unwrap()
|
||||
.as_ref()
|
||||
.map(|pm| pm.pre_conn_id() != INVALID_PRIVACY_MODE_CONN_ID)
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user