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
|
/* 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/. */
use crate::{
dispatch_background_task_runnable, dispatch_runnable, get_current_thread, DispatchOptions,
};
use nserror::{nsresult, NS_OK};
use nsstring::nsACString;
use std::sync::Mutex;
use xpcom::interfaces::{nsIEventTarget, nsIRunnablePriority};
use xpcom::xpcom;
/// Basic wrapper to convert a FnOnce callback into a `nsIRunnable` to be
/// dispatched using XPCOM.
#[xpcom(implement(nsIRunnable, nsINamed, nsIRunnablePriority), atomic)]
struct RunnableFunction<F: FnOnce() + 'static> {
name: &'static str,
priority: u32,
function: Mutex<Option<F>>,
}
impl<F: FnOnce() + 'static> RunnableFunction<F> {
#[allow(non_snake_case)]
fn Run(&self) -> nsresult {
let function = self.function.lock().unwrap().take();
debug_assert!(function.is_some(), "runnable invoked twice?");
if let Some(function) = function {
function();
}
NS_OK
}
#[allow(non_snake_case)]
unsafe fn GetName(&self, result: *mut nsACString) -> nsresult {
(*result).assign(self.name);
NS_OK
}
#[allow(non_snake_case)]
unsafe fn GetPriority(&self, result: *mut u32) -> nsresult {
*result = self.priority;
NS_OK
}
}
pub struct RunnableBuilder<F> {
name: &'static str,
function: F,
priority: u32,
options: DispatchOptions,
}
impl<F> RunnableBuilder<F> {
pub fn new(name: &'static str, function: F) -> Self {
RunnableBuilder {
name,
function,
priority: nsIRunnablePriority::PRIORITY_NORMAL,
options: DispatchOptions::default(),
}
}
pub fn priority(mut self, priority: u32) -> Self {
self.priority = priority;
self
}
pub fn options(mut self, options: DispatchOptions) -> Self {
self.options = options;
self
}
pub fn may_block(mut self, may_block: bool) -> Self {
self.options = self.options.may_block(may_block);
self
}
pub unsafe fn at_end(mut self, at_end: bool) -> Self {
self.options = self.options.at_end(at_end);
self
}
}
impl<F> RunnableBuilder<F>
where
F: FnOnce() + Send + 'static,
{
/// Dispatch this Runnable to the specified EventTarget. The runnable function must be `Send`.
pub fn dispatch(self, target: &nsIEventTarget) -> Result<(), nsresult> {
let runnable = RunnableFunction::allocate(InitRunnableFunction {
name: self.name,
priority: self.priority,
function: Mutex::new(Some(self.function)),
});
unsafe { dispatch_runnable(runnable.coerce(), target, self.options) }
}
/// Dispatch this Runnable to the specified EventTarget as a background
/// task. The runnable function must be `Send`.
pub fn dispatch_background_task(self) -> Result<(), nsresult> {
let runnable = RunnableFunction::allocate(InitRunnableFunction {
name: self.name,
priority: self.priority,
function: Mutex::new(Some(self.function)),
});
unsafe { dispatch_background_task_runnable(runnable.coerce(), self.options) }
}
}
impl<F> RunnableBuilder<F>
where
F: FnOnce() + 'static,
{
/// Dispatch this Runnable to the current thread.
///
/// Unlike `dispatch` and `dispatch_background_task`, the runnable does not
/// need to be `Send` to dispatch to the current thread.
pub fn dispatch_local(self) -> Result<(), nsresult> {
let target = get_current_thread()?;
let runnable = RunnableFunction::allocate(InitRunnableFunction {
name: self.name,
priority: self.priority,
function: Mutex::new(Some(self.function)),
});
unsafe { dispatch_runnable(runnable.coerce(), target.coerce(), self.options) }
}
}
pub fn dispatch_onto<F>(
name: &'static str,
target: &nsIEventTarget,
function: F,
) -> Result<(), nsresult>
where
F: FnOnce() + Send + 'static,
{
RunnableBuilder::new(name, function).dispatch(target)
}
pub fn dispatch_background_task<F>(name: &'static str, function: F) -> Result<(), nsresult>
where
F: FnOnce() + Send + 'static,
{
RunnableBuilder::new(name, function).dispatch_background_task()
}
pub fn dispatch_local<F>(name: &'static str, function: F) -> Result<(), nsresult>
where
F: FnOnce() + 'static,
{
RunnableBuilder::new(name, function).dispatch_local()
}
|