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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
use super::*;
use std::{
error::Error,
ffi::CStr,
fmt,
mem::MaybeUninit,
os::raw::*
};
use browser_window_c::*;
use crate::window::WindowImpl;
#[derive(Clone,Copy)]
pub struct BrowserWindowImpl {
inner: *mut cbw_BrowserWindow
}
struct CreationCallbackData {
func: CreationCallbackFn,
data: *mut ()
}
struct EvalJsCallbackData {
callback: EvalJsCallbackFn,
data: *mut ()
}
#[derive(Debug)]
pub struct JsEvaluationError {
message: String
}
struct UserData {
func: ExternalInvocationHandlerFn,
data: *mut ()
}
#[cfg(target_pointer_width = "16")]
type UsizeFix = u16;
#[cfg(target_pointer_width = "32")]
type UsizeFix = u32;
#[cfg(target_pointer_width = "64")]
type UsizeFix = u64;
impl BrowserWindowExt for BrowserWindowImpl {
fn cookie_jar(&self) -> CookieJarImpl {
let inner = unsafe { cbw_CookieJar_newGlobal() };
CookieJarImpl {
inner
}
}
fn eval_js( &self, js: &str, callback: EvalJsCallbackFn, callback_data: *mut () ) {
let data = Box::new( EvalJsCallbackData {
callback,
data: callback_data
} );
let data_ptr = Box::into_raw( data );
unsafe { cbw_BrowserWindow_evalJs( self.inner, js.into(), Some( ffi_eval_js_callback_handler ), data_ptr as _ ) }
}
fn eval_js_threadsafe( &self, js: &str, callback: EvalJsCallbackFn, callback_data: *mut () ) {
let data = Box::new( EvalJsCallbackData {
callback,
data: callback_data
} );
let data_ptr = Box::into_raw( data );
unsafe { cbw_BrowserWindow_evalJsThreaded( self.inner, js.into(), Some( ffi_eval_js_callback_handler ), data_ptr as _ ) }
}
fn navigate( &self, uri: &str ) {
unsafe { cbw_BrowserWindow_navigate( self.inner, uri.into() ) };
}
fn new(
app: ApplicationImpl,
parent: WindowImpl,
source: Source,
title: &str,
width: Option<u32>,
height: Option<u32>,
window_options: &WindowOptions,
browser_window_options: &BrowserWindowOptions,
handler: ExternalInvocationHandlerFn,
_user_data: *mut (),
creation_callback: CreationCallbackFn,
_callback_data: *mut ()
) {
let w: c_int = match width {
None => -1,
Some(x) => x as _
};
let h: c_int = match height {
None => -1,
Some(x) => x as _
};
let user_data = Box::new( UserData {
func: handler,
data: _user_data
} );
let callback_data = Box::new( CreationCallbackData {
func: creation_callback,
data: _callback_data
} );
unsafe { cbw_BrowserWindow_new(
app.inner,
parent.inner,
source,
title.into(),
w, h,
window_options as _,
browser_window_options as _,
Some( ffi_handler ),
Box::into_raw( user_data ) as _,
Some( ffi_creation_callback_handler ),
Box::into_raw( callback_data ) as _
) };
}
fn user_data( &self ) -> *mut () {
let c_user_data_ptr: *mut UserData = unsafe { (*self.inner).user_data as _ };
unsafe { (*c_user_data_ptr).data }
}
fn url<'a>(&'a self) -> Cow<'a, str> {
let mut slice: cbw_StrSlice = unsafe { MaybeUninit::uninit().assume_init() };
let owned = unsafe { cbw_BrowserWindow_getUrl(self.inner, &mut slice) };
if owned > 0 {
let url: String = slice.into();
unsafe { cbw_string_free(slice) };
url.into()
}
else {
let url: &'a str = slice.into();
url.into()
}
}
fn window( &self ) -> WindowImpl {
WindowImpl {
inner: unsafe { cbw_BrowserWindow_getWindow( self.inner ) }
}
}
}
impl JsEvaluationError {
pub(in super) unsafe fn new( err: *const cbw_Err ) -> Self {
let msg_ptr = ((*err).alloc_message.unwrap())( (*err).code, (*err).data );
let cstr = CStr::from_ptr( msg_ptr );
let message: String = cstr.to_string_lossy().into();
Self {
message: message
}
}
}
impl Error for JsEvaluationError {
fn source(&self) -> Option<&(dyn Error + 'static)> { None }
}
impl fmt::Display for JsEvaluationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.message.as_str())
}
}
unsafe extern "C" fn ffi_creation_callback_handler( bw: *mut cbw_BrowserWindow, _data: *mut c_void ) {
let data_ptr = _data as *mut CreationCallbackData;
let data = Box::from_raw( data_ptr );
let handle = BrowserWindowImpl { inner: bw };
(data.func)( handle, data.data );
}
unsafe extern "C" fn ffi_eval_js_callback_handler( bw: *mut cbw_BrowserWindow, _data: *mut c_void, _result: *const c_char, error: *const cbw_Err ) {
let data_ptr = _data as *mut EvalJsCallbackData;
let data = Box::from_raw( data_ptr );
let (handle, result) = ffi_eval_js_callback_result( bw, _result, error );
(data.callback)( handle, data.data, result );
}
unsafe extern "C" fn ffi_handler( bw: *mut cbw_BrowserWindow, cmd: cbw_CStrSlice, args: *mut cbw_CStrSlice, arg_count: UsizeFix ) {
let handle = BrowserWindowImpl { inner: bw };
let data_ptr = (*bw).user_data as *mut UserData;
let data = &mut *data_ptr;
let cmd_string: &str = cmd.into();
let mut args_vec: Vec<String> = Vec::with_capacity( arg_count as usize );
for i in 0..arg_count {
args_vec.push( (*args.add( i as usize )).into() );
}
(data.func)( handle, cmd_string, args_vec );
}
unsafe fn ffi_eval_js_callback_result(
bw: *mut cbw_BrowserWindow,
result: *const c_char,
error: *const cbw_Err
) -> ( BrowserWindowImpl, Result<String, JsEvaluationError> ) {
let result_val: Result<String, JsEvaluationError> = if error.is_null() {
let result_str = CStr::from_ptr( result ).to_string_lossy().to_owned().to_string();
Ok( result_str )
}
else {
Err( JsEvaluationError::new( error ) )
};
let handle = BrowserWindowImpl { inner: bw };
( handle, result_val )
}