summaryrefslogtreecommitdiffstats
path: root/xpcom/rust/gtest/xpcom/test.rs
blob: f26a0140f33ea29a979e7ae2e47cf787883a8bff (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#![allow(non_snake_case)]

#[macro_use]
extern crate xpcom;

extern crate nserror;

use nserror::{nsresult, NS_OK};
use std::ffi::{CStr, CString};
use std::os::raw::c_char;
use std::ptr;
use xpcom::{interfaces, RefPtr};

#[no_mangle]
pub unsafe extern "C" fn Rust_ObserveFromRust() -> *const interfaces::nsIObserverService {
    let obssvc: RefPtr<interfaces::nsIObserverService> =
        xpcom::components::Observer::service().unwrap();

    // Define an observer
    #[xpcom(implement(nsIObserver), nonatomic)]
    struct Observer {
        run: *mut bool,
    }
    impl Observer {
        unsafe fn Observe(
            &self,
            _subject: *const interfaces::nsISupports,
            topic: *const c_char,
            _data: *const u16,
        ) -> nsresult {
            *self.run = true;
            assert!(CStr::from_ptr(topic).to_str() == Ok("test-rust-observe"));
            NS_OK
        }
    }

    let topic = CString::new("test-rust-observe").unwrap();

    let mut run = false;
    let observer = Observer::allocate(InitObserver { run: &mut run });
    let rv = obssvc.AddObserver(
        observer.coerce::<interfaces::nsIObserver>(),
        topic.as_ptr(),
        false,
    );
    assert!(rv.succeeded());

    let rv = obssvc.NotifyObservers(ptr::null(), topic.as_ptr(), ptr::null());
    assert!(rv.succeeded());
    assert!(run, "The observer should have been run!");

    let rv = obssvc.RemoveObserver(observer.coerce::<interfaces::nsIObserver>(), topic.as_ptr());
    assert!(rv.succeeded());

    assert!(
        observer.coerce::<interfaces::nsISupports>() as *const _
            == &*observer
                .query_interface::<interfaces::nsISupports>()
                .unwrap() as *const _
    );

    &*obssvc
}

#[no_mangle]
pub unsafe extern "C" fn Rust_ImplementRunnableInRust(
    it_worked: *mut bool,
    runnable: *mut *const interfaces::nsIRunnable,
) {
    // Define a type which implements nsIRunnable in rust.
    #[xpcom(implement(nsIRunnable), atomic)]
    struct RunnableFn<F: Fn() + 'static> {
        run: F,
    }

    impl<F: Fn() + 'static> RunnableFn<F> {
        unsafe fn Run(&self) -> nsresult {
            (self.run)();
            NS_OK
        }
    }

    let my_runnable = RunnableFn::allocate(InitRunnableFn {
        run: move || {
            *it_worked = true;
        },
    });
    my_runnable
        .query_interface::<interfaces::nsIRunnable>()
        .unwrap()
        .forget(&mut *runnable);
}

#[no_mangle]
pub unsafe extern "C" fn Rust_GetMultipleInterfaces(
    runnable: *mut *const interfaces::nsIRunnable,
    observer: *mut *const interfaces::nsIObserver,
) {
    // Define a type which implements nsIRunnable and nsIObserver in rust, and
    // hand both references back to c++
    #[xpcom(implement(nsIRunnable, nsIObserver), atomic)]
    struct MultipleInterfaces {}

    impl MultipleInterfaces {
        unsafe fn Run(&self) -> nsresult {
            NS_OK
        }
        unsafe fn Observe(
            &self,
            _subject: *const interfaces::nsISupports,
            _topic: *const c_char,
            _data: *const u16,
        ) -> nsresult {
            NS_OK
        }
    }

    let instance = MultipleInterfaces::allocate(InitMultipleInterfaces {});
    instance
        .query_interface::<interfaces::nsIRunnable>()
        .unwrap()
        .forget(&mut *runnable);
    instance
        .query_interface::<interfaces::nsIObserver>()
        .unwrap()
        .forget(&mut *observer);
}