summaryrefslogtreecommitdiffstats
path: root/xpcom/rust/xpcom/src/promise.rs
blob: 0fdab9b6aa9a56d6b691735ea87001129ace0886 (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
/* 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 https://mozilla.org/MPL/2.0/. */

use crate::{
    create_instance,
    interfaces::{nsIVariant, nsIWritableVariant},
    RefCounted,
};

use cstr::*;

mod ffi {
    use super::*;

    extern "C" {
        // These are implemented in dom/promise/Promise.cpp
        pub fn DomPromise_AddRef(promise: *const Promise);
        pub fn DomPromise_Release(promise: *const Promise);
        pub fn DomPromise_RejectWithVariant(promise: *const Promise, variant: *const nsIVariant);
        pub fn DomPromise_ResolveWithVariant(promise: *const Promise, variant: *const nsIVariant);
    }
}

#[repr(C)]
pub struct Promise {
    private: [u8; 0],

    /// This field is a phantomdata to ensure that the Promise type and any
    /// struct containing it is not safe to send across threads, as DOM is
    /// generally not threadsafe.
    __nosync: ::std::marker::PhantomData<::std::rc::Rc<u8>>,
}

impl Promise {
    pub fn reject_with_undefined(&self) {
        let variant = create_instance::<nsIWritableVariant>(cstr!("@mozilla.org/variant;1"))
            .expect("Failed to create writable variant");
        unsafe {
            variant.SetAsVoid();
        }
        self.reject_with_variant(&variant);
    }

    pub fn reject_with_variant(&self, variant: &nsIVariant) {
        unsafe { ffi::DomPromise_RejectWithVariant(self, variant) }
    }

    pub fn resolve_with_variant(&self, variant: &nsIVariant) {
        unsafe { ffi::DomPromise_ResolveWithVariant(self, variant) }
    }
}

unsafe impl RefCounted for Promise {
    unsafe fn addref(&self) {
        ffi::DomPromise_AddRef(self)
    }

    unsafe fn release(&self) {
        ffi::DomPromise_Release(self)
    }
}