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
use std::ffi::CStr;
use std::ptr;
use nserror::NsresultExt;
use {
RefPtr,
GetterAddrefs,
XpCom,
};
use interfaces::{
nsIComponentManager,
nsIServiceManager,
nsIComponentRegistrar,
};
macro_rules! try_opt {
($e: expr) => {
match $e {
Some(x) => x,
None => return None,
}
}
}
#[inline]
pub fn component_manager() -> Option<RefPtr<nsIComponentManager>> {
unsafe {
RefPtr::from_raw(Gecko_GetComponentManager())
}
}
#[inline]
pub fn service_manager() -> Option<RefPtr<nsIServiceManager>> {
unsafe {
RefPtr::from_raw(Gecko_GetServiceManager())
}
}
#[inline]
pub fn component_registrar() -> Option<RefPtr<nsIComponentRegistrar>> {
unsafe {
RefPtr::from_raw(Gecko_GetComponentRegistrar())
}
}
#[inline]
pub fn create_instance<T: XpCom>(id: &CStr) -> Option<RefPtr<T>> {
unsafe {
let mut ga = GetterAddrefs::<T>::new();
if try_opt!(component_manager()).CreateInstanceByContractID(
id.as_ptr(),
ptr::null(),
&T::IID,
ga.void_ptr(),
).succeeded() {
ga.refptr()
} else {
None
}
}
}
#[inline]
pub fn get_service<T: XpCom>(id: &CStr) -> Option<RefPtr<T>> {
unsafe {
let mut ga = GetterAddrefs::<T>::new();
if try_opt!(service_manager()).GetServiceByContractID(
id.as_ptr(),
&T::IID,
ga.void_ptr()
).succeeded() {
ga.refptr()
} else {
None
}
}
}
extern "C" {
fn Gecko_GetComponentManager() -> *const nsIComponentManager;
fn Gecko_GetServiceManager() -> *const nsIServiceManager;
fn Gecko_GetComponentRegistrar() -> *const nsIComponentRegistrar;
}