Skip to content

Commit

Permalink
[autofix.ci] apply automated fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
autofix-ci[bot] authored Nov 2, 2023
1 parent c15a97c commit 4945e93
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 28 deletions.
4 changes: 2 additions & 2 deletions mitmproxy-rs/src/process_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use std::path::{Path, PathBuf};
use anyhow::Result;
use pyo3::prelude::*;

#[cfg(target_os = "macos")]
use mitmproxy::macos::{icons, processes};
#[allow(unused_imports)]
use mitmproxy::processes::image;
#[cfg(windows)]
use mitmproxy::windows::{icons, processes};
#[cfg(target_os = "macos")]
use mitmproxy::macos::{processes, icons};

#[pyclass(module = "mitmproxy_rs", frozen)]
pub struct Process(mitmproxy::processes::ProcessInfo);
Expand Down
25 changes: 14 additions & 11 deletions src/macos/icons.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use once_cell::sync::Lazy;
use std::{sync::Mutex, collections::hash_map::Entry};
use anyhow::{bail, Result};
use cocoa::base::id;
use sysinfo::{PidExt, ProcessExt, ProcessRefreshKind, System, SystemExt};
use objc::{class, msg_send, sel, sel_impl};
use std::path::PathBuf;
use std::path::Path; use std::collections::HashMap;
use once_cell::sync::Lazy;
use std::collections::hash_map::DefaultHasher;
use anyhow::{bail, Result};
use std::hash::{Hasher, Hash};

use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::path::Path;
use std::path::PathBuf;
use std::{collections::hash_map::Entry, sync::Mutex};
use sysinfo::{PidExt, ProcessExt, ProcessRefreshKind, System, SystemExt};

pub static ICON_CACHE: Lazy<Mutex<IconCache>> = Lazy::new(|| Mutex::new(IconCache::default()));

Expand Down Expand Up @@ -45,8 +45,11 @@ unsafe fn png_data_for_executable(executable: &Path) -> Result<Vec<u8>> {
sys.refresh_processes_specifics(ProcessRefreshKind::new());
for (pid, process) in sys.processes() {
let pid = pid.as_u32();
if executable == process.exe().to_path_buf(){
let app: id = msg_send![class!(NSRunningApplication), runningApplicationWithProcessIdentifier: pid];
if executable == process.exe().to_path_buf() {
let app: id = msg_send![
class!(NSRunningApplication),
runningApplicationWithProcessIdentifier: pid
];
if !app.is_null() {
let img: id = msg_send![app, icon];
let tif: id = msg_send![img, TIFFRepresentation];
Expand All @@ -55,7 +58,7 @@ unsafe fn png_data_for_executable(executable: &Path) -> Result<Vec<u8>> {
let length: usize = msg_send![png, length];
let bytes: *const u8 = msg_send![png, bytes];
let data = std::slice::from_raw_parts(bytes, length).to_vec();
return Ok(data)
return Ok(data);
}
}
}
Expand Down
44 changes: 29 additions & 15 deletions src/macos/processes.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
use std::collections::{HashMap, HashSet};
use std::collections::hash_map::Entry;
use crate::intercept_conf::PID;
use crate::processes::{ProcessInfo, ProcessList};
use anyhow::Result;
use cocoa::base::nil;
use cocoa::foundation::NSString;
use core_foundation::number::kCFNumberSInt32Type;
use core_foundation::number::CFNumberGetValue;
use core_foundation::number::CFNumberRef;
use core_foundation::number::kCFNumberSInt32Type;
use core_graphics::display::{CFDictionaryGetValueIfPresent, CGWindowListCopyWindowInfo, CFArrayGetCount, CFArrayGetValueAtIndex, CFDictionaryRef, kCGNullWindowID, kCGWindowListOptionOnScreenOnly, kCGWindowListExcludeDesktopElements};
use core_graphics::display::{
kCGNullWindowID, kCGWindowListExcludeDesktopElements, kCGWindowListOptionOnScreenOnly,
CFArrayGetCount, CFArrayGetValueAtIndex, CFDictionaryGetValueIfPresent, CFDictionaryRef,
CGWindowListCopyWindowInfo,
};
use std::collections::hash_map::Entry;
use std::collections::{HashMap, HashSet};
use std::ffi::c_void;
use sysinfo::{PidExt, ProcessExt, ProcessRefreshKind, System, SystemExt};
use std::path::PathBuf;
use crate::intercept_conf::PID;

use sysinfo::{PidExt, ProcessExt, ProcessRefreshKind, System, SystemExt};

pub fn active_executables() -> Result<ProcessList> {
let mut executables: HashMap<PathBuf, ProcessInfo> = HashMap::new();
Expand All @@ -28,7 +31,7 @@ pub fn active_executables() -> Result<ProcessList> {
match executables.entry(executable) {
Entry::Occupied(mut e) => {
let process_info = e.get();
if !process_info.is_visible && visible.contains(&pid){
if !process_info.is_visible && visible.contains(&pid) {
e.get_mut().is_visible = true;
}
}
Expand All @@ -47,22 +50,33 @@ pub fn active_executables() -> Result<ProcessList> {
Ok(executables.into_values().collect())
}


pub fn visible_windows() -> Result<HashSet<PID>> {
let mut pids: HashSet<PID> = HashSet::new();
unsafe{
let windows_info_list = CGWindowListCopyWindowInfo( kCGWindowListOptionOnScreenOnly + kCGWindowListExcludeDesktopElements, kCGNullWindowID);
unsafe {
let windows_info_list = CGWindowListCopyWindowInfo(
kCGWindowListOptionOnScreenOnly + kCGWindowListExcludeDesktopElements,
kCGNullWindowID,
);
let count = CFArrayGetCount(windows_info_list);

for i in 0..count-1 {
for i in 0..count - 1 {
let dic_ref = CFArrayGetValueAtIndex(windows_info_list, i);
let key = NSString::alloc(nil).init_str("kCGWindowOwnerPID");
let mut pid: *const c_void = std::ptr::null_mut();

if CFDictionaryGetValueIfPresent(dic_ref as CFDictionaryRef, key as *const c_void, &mut pid) != 0{
if CFDictionaryGetValueIfPresent(
dic_ref as CFDictionaryRef,
key as *const c_void,
&mut pid,
) != 0
{
let pid_cf_ref = pid as CFNumberRef;
let mut pid:i32 = 0;
if CFNumberGetValue(pid_cf_ref, kCFNumberSInt32Type, &mut pid as *mut i32 as *mut c_void) {
let mut pid: i32 = 0;
if CFNumberGetValue(
pid_cf_ref,
kCFNumberSInt32Type,
&mut pid as *mut i32 as *mut c_void,
) {
pids.insert(pid as u32);
}
}
Expand Down

0 comments on commit 4945e93

Please sign in to comment.