1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//! [`EventBtmLaunchItemAdd`]

use std::ffi::OsStr;

use endpoint_sec_sys::{
    es_btm_item_type_t, es_btm_launch_item_t, es_event_btm_launch_item_add_t, uid_t,
};

use crate::Process;

/// A launch item being made known to background task management.
#[doc(alias = "es_event_btm_launch_item_add_t")]
pub struct EventBtmLaunchItemAdd<'a> {
    /// Raw event
    pub(crate) raw: &'a es_event_btm_launch_item_add_t,
    /// Message version
    pub(crate) version: u32,
}

impl<'a> EventBtmLaunchItemAdd<'a> {
    /// Optional. Process that instigated the BTM operation (XPC caller that asked for the item to
    /// be added).
    #[inline(always)]
    pub fn instigator(&self) -> Option<Process<'a>> {
        // Safety: 'a tied to self, object obtained through ES
        let process = unsafe { self.raw.instigator()? };
        Some(Process::new(process, self.version))
    }

    /// Optional. App process that registered the item.
    #[inline(always)]
    pub fn app(&self) -> Option<Process<'a>> {
        // Safety: 'a tied to self, object obtained through ES
        let process = unsafe { self.raw.app()? };
        Some(Process::new(process, self.version))
    }

    /// BTM launch item.
    #[inline(always)]
    pub fn item(&self) -> BtmLaunchItem<'a> {
        // Safety: 'a tied to self, object obtained through ES
        BtmLaunchItem::new(unsafe { self.raw.item() })
    }

    /// Optional. If available and applicable, the POSIX executable path from the launchd plist. If
    /// the path is relative, it is relative to `item.app_url`.
    #[inline(always)]
    pub fn executable_path(&self) -> &'a OsStr {
        // Safety: 'a tied to self, object obtained through ES
        unsafe { self.raw.executable_path.as_os_str() }
    }
}

// Safety: safe to send across threads: does not contain any interior mutability nor depend on current thread state
unsafe impl Send for EventBtmLaunchItemAdd<'_> {}

impl_debug_eq_hash_with_functions!(
    EventBtmLaunchItemAdd<'a>;
    instigator, app, item, executable_path,
);

/// A BTM launch item
#[doc(alias = "es_btm_launch_item_t")]
pub struct BtmLaunchItem<'a> {
    /// Raw data
    pub(crate) raw: &'a es_btm_launch_item_t,
}

impl<'a> BtmLaunchItem<'a> {
    /// New launch item
    #[inline(always)]
    pub(crate) fn new(raw: &'a es_btm_launch_item_t) -> Self {
        Self { raw }
    }

    /// Type of launch item.
    #[inline(always)]
    pub fn item_type(&self) -> es_btm_item_type_t {
        self.raw.item_type
    }

    /// True only if item is a legacy plist.
    #[inline(always)]
    pub fn legacy(&self) -> bool {
        self.raw.legacy
    }

    /// True only if item is managed by MDM.
    #[inline(always)]
    pub fn managed(&self) -> bool {
        self.raw.managed
    }

    /// User ID for the item (may be user `nobody` (`-2`)).
    #[inline(always)]
    pub fn uid(&self) -> uid_t {
        self.raw.uid
    }

    /// URL for item.
    ///
    /// If a file URL describing a relative path, it is relative to `app_url`.
    #[inline(always)]
    pub fn item_url(&self) -> &'a OsStr {
        // Safety: 'a tied to self, object obtained through ES
        unsafe { self.raw.item_url.as_os_str() }
    }

    /// Optional. URL for app the item is attributed to.
    #[inline(always)]
    pub fn app_url(&self) -> &'a OsStr {
        // Safety: 'a tied to self, object obtained through ES
        unsafe { self.raw.app_url.as_os_str() }
    }
}

// Safety: safe to send across threads: does not contain any interior mutability nor depend on current thread state
unsafe impl Send for BtmLaunchItem<'_> {}

impl_debug_eq_hash_with_functions!(
    BtmLaunchItem<'a>;
    item_type, legacy, managed, uid, item_url, app_url,
);