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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#![deny(missing_docs)]
use ffi_support::FfiStr;
use std::ffi::CString;
use std::sync::Mutex;
pub use glean_core::{Configuration, Error, Result};
pub mod metrics;
lazy_static::lazy_static! {
static ref GLEAN_HANDLE: Mutex<u64> = Mutex::new(0);
}
fn with_glean<F, R>(f: F) -> R
where
F: Fn(u64) -> R,
{
let lock = GLEAN_HANDLE.lock().unwrap();
debug_assert!(*lock != 0);
f(*lock)
}
pub fn initialize(cfg: Configuration) -> Result<()> {
unsafe {
let data_dir = CString::new(cfg.data_path).unwrap();
let package_name = CString::new(cfg.application_id).unwrap();
let upload_enabled = cfg.upload_enabled;
let max_events = cfg.max_events.map(|m| m as i32);
let ffi_cfg = glean_ffi::FfiConfiguration {
data_dir: FfiStr::from_cstr(&data_dir),
package_name: FfiStr::from_cstr(&package_name),
upload_enabled: upload_enabled as u8,
max_events: max_events.as_ref(),
delay_ping_lifetime_io: false as u8,
};
let handle = glean_ffi::glean_initialize(&ffi_cfg);
if handle == 0 {
return Err(glean_core::Error::utf8_error());
}
let mut lock = GLEAN_HANDLE.lock().unwrap();
*lock = handle;
Ok(())
}
}
pub fn set_upload_enabled(flag: bool) -> bool {
with_glean(|glean_handle| {
glean_ffi::glean_set_upload_enabled(glean_handle, flag as u8);
glean_ffi::glean_is_upload_enabled(glean_handle) != 0
})
}
pub fn is_upload_enabled() -> bool {
with_glean(|glean_handle| glean_ffi::glean_is_upload_enabled(glean_handle) != 0)
}
pub fn register_ping_type(ping: &metrics::PingType) {
with_glean(|glean_handle| {
glean_ffi::ping_type::glean_register_ping_type(glean_handle, ping.handle);
})
}
pub fn send_ping(ping: &metrics::PingType) -> bool {
send_ping_by_name(&ping.name)
}
pub fn send_ping_by_name(ping: &str) -> bool {
send_pings_by_name(&[ping])
}
pub fn send_pings_by_name(pings: &[&str]) -> bool {
let array: Vec<CString> = pings.iter().map(|s| CString::new(*s).unwrap()).collect();
let ptr_array: Vec<*const _> = array.iter().map(|s| s.as_ptr()).collect();
with_glean(|glean_handle| {
let res = glean_ffi::glean_send_pings_by_name(
glean_handle,
ptr_array.as_ptr(),
array.len() as i32,
);
res != 0
})
}