Skip to main content

endpoint_sec/event/
event_btm_launch_item_add.rs

1//! [`EventBtmLaunchItemAdd`]
2
3use std::ffi::OsStr;
4
5use endpoint_sec_sys::{es_btm_item_type_t, es_btm_launch_item_t, es_event_btm_launch_item_add_t, uid_t};
6
7use crate::Process;
8
9/// A launch item being made known to background task management.
10#[doc(alias = "es_event_btm_launch_item_add_t")]
11pub struct EventBtmLaunchItemAdd<'a> {
12    /// Raw event
13    pub(crate) raw: &'a es_event_btm_launch_item_add_t,
14    /// Message version
15    pub(crate) version: u32,
16}
17
18impl<'a> EventBtmLaunchItemAdd<'a> {
19    /// Optional. Process that instigated the BTM operation (XPC caller that asked for the item to
20    /// be added).
21    #[inline(always)]
22    pub fn instigator(&self) -> Option<Process<'a>> {
23        // Safety: 'a tied to self, object obtained through ES
24        let process = unsafe { self.raw.instigator()? };
25        Some(Process::new(process, self.version))
26    }
27
28    /// Optional. App process that registered the item.
29    #[inline(always)]
30    pub fn app(&self) -> Option<Process<'a>> {
31        // Safety: 'a tied to self, object obtained through ES
32        let process = unsafe { self.raw.app()? };
33        Some(Process::new(process, self.version))
34    }
35
36    /// BTM launch item.
37    #[inline(always)]
38    pub fn item(&self) -> BtmLaunchItem<'a> {
39        // Safety: 'a tied to self, object obtained through ES
40        BtmLaunchItem::new(unsafe { self.raw.item() })
41    }
42
43    /// Optional. If available and applicable, the POSIX executable path from the launchd plist. If
44    /// the path is relative, it is relative to `item.app_url`.
45    #[inline(always)]
46    pub fn executable_path(&self) -> &'a OsStr {
47        // Safety: 'a tied to self, object obtained through ES
48        unsafe { self.raw.executable_path.as_os_str() }
49    }
50}
51
52// Safety: safe to send across threads: does not contain any interior mutability nor depend on current thread state
53unsafe impl Send for EventBtmLaunchItemAdd<'_> {}
54// Safety: safe to share across threads: does not contain any interior mutability nor depend on current thread state
55unsafe impl Sync for EventBtmLaunchItemAdd<'_> {}
56
57impl_debug_eq_hash_with_functions!(
58    EventBtmLaunchItemAdd<'a>;
59    instigator, app, item, executable_path,
60);
61
62/// A BTM launch item
63#[doc(alias = "es_btm_launch_item_t")]
64pub struct BtmLaunchItem<'a> {
65    /// Raw data
66    pub(crate) raw: &'a es_btm_launch_item_t,
67}
68
69impl<'a> BtmLaunchItem<'a> {
70    /// New launch item
71    #[inline(always)]
72    pub(crate) fn new(raw: &'a es_btm_launch_item_t) -> Self {
73        Self { raw }
74    }
75
76    /// Type of launch item.
77    #[inline(always)]
78    pub fn item_type(&self) -> es_btm_item_type_t {
79        self.raw.item_type
80    }
81
82    /// True only if item is a legacy plist.
83    #[inline(always)]
84    pub fn legacy(&self) -> bool {
85        self.raw.legacy
86    }
87
88    /// True only if item is managed by MDM.
89    #[inline(always)]
90    pub fn managed(&self) -> bool {
91        self.raw.managed
92    }
93
94    /// User ID for the item (may be user `nobody` (`-2`)).
95    #[inline(always)]
96    pub fn uid(&self) -> uid_t {
97        self.raw.uid
98    }
99
100    /// URL for item.
101    ///
102    /// If a file URL describing a relative path, it is relative to `app_url`.
103    #[inline(always)]
104    pub fn item_url(&self) -> &'a OsStr {
105        // Safety: 'a tied to self, object obtained through ES
106        unsafe { self.raw.item_url.as_os_str() }
107    }
108
109    /// Optional. URL for app the item is attributed to.
110    #[inline(always)]
111    pub fn app_url(&self) -> &'a OsStr {
112        // Safety: 'a tied to self, object obtained through ES
113        unsafe { self.raw.app_url.as_os_str() }
114    }
115}
116
117// Safety: safe to send across threads: does not contain any interior mutability nor depend on current thread state
118unsafe impl Send for BtmLaunchItem<'_> {}
119
120impl_debug_eq_hash_with_functions!(
121    BtmLaunchItem<'a>;
122    item_type, legacy, managed, uid, item_url, app_url,
123);