alcro 0.5.4

A library to create desktop apps using rust and modern web technologies
Documentation
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
use std::{
    fmt::Display,
    sync::{Arc, Mutex},
};

use crossbeam_channel::{bounded, Sender};
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::sync::atomic::{AtomicI32, Ordering};

mod devtools;
use devtools::{readloop, recv_msg, send, send_msg};
mod os;
#[cfg(target_family = "windows")]
use os::close_process_handle;
#[cfg(target_family = "unix")]
use os::kill_proc;
use os::{exited, new_process, wait_proc, PipeReader, PipeWriter, Process};

/// A JS object. It is an alias for `serde_json::Value`. See it's documentation for how to use it.
pub type JSObject = serde_json::Value;
/// The result of a JS function.
///
/// The Err variant will be returned if
/// * There is an exception
/// * An error type is returned
pub type JSResult = Result<JSObject, JSObject>;

/// An error from chrome in JSON format
#[derive(Debug)]
pub struct JSError(JSObject);
impl JSError {
    pub fn source(self) -> JSObject {
        self.0
    }
}
impl std::error::Error for JSError {}
impl Display for JSError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}
impl From<JSObject> for JSError {
    fn from(o: JSObject) -> Self {
        Self(o)
    }
}

trait ToResultOfJSError {
    fn to_result_of_jserror(self) -> Result<(), JSError>;
}
impl ToResultOfJSError for JSResult {
    fn to_result_of_jserror(self) -> Result<(), JSError> {
        match self {
            Ok(_) => Ok(()),
            Err(e) => Err(e.into()),
        }
    }
}

/// Context for an async binding function.
pub struct BindingContext {
    active: Option<ActiveBindingContext>,
}

impl BindingContext {
    fn new(active: ActiveBindingContext) -> Self {
        Self {
            active: Some(active),
        }
    }

    /// The arguments from JS.
    pub fn args(&self) -> &[JSObject] {
        match &self.active {
            None => &[],
            Some(active) => active.payload["args"].as_array().expect("Expected array"),
        }
    }

    /// Completes the JS function successfully. Equivalent to `complete(Ok(result))`
    pub fn done(self, result: JSObject) {
        self.complete(Ok(result))
    }

    /// Completes the JS function with an error. Equivalent to `complete(Err(error))`
    pub fn err(self, error: JSObject) {
        self.complete(Err(error))
    }

    /// Completes the JS function, either successfully or not. Takes the [`BindingContext`] by
    /// value as it releases the outstanding call on the Chrome(ium) side.
    pub fn complete(mut self, result: JSResult) {
        if let Some(incomplete) = self.active.take() {
            complete_binding(incomplete, result)
        }
    }
}

impl Drop for BindingContext {
    fn drop(&mut self) {
        if let Some(incomplete) = self.active.take() {
            complete_binding(incomplete, Ok(JSObject::Null))
        }
    }
}

struct ActiveBindingContext {
    chrome: Arc<Chrome>,
    payload: JSObject,
    context_id: i64,
}

type BindingFunc = Arc<dyn Fn(BindingContext) + Sync + Send>;

pub struct Chrome {
    id: AtomicI32,
    #[cfg(target_family = "unix")]
    pid: Process,
    #[cfg(target_family = "windows")]
    pid: usize,
    psend: Mutex<PipeWriter>,
    precv: Mutex<PipeReader>,
    target: String,
    session: String,
    kill_send: Sender<()>,
    pending: dashmap::DashMap<i32, Sender<JSResult>>,
    window: AtomicI32,
    bindings: dashmap::DashMap<String, BindingFunc>,
}

/// A struct that stores the size, position and window state of the browser window.

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
pub struct Bounds {
    /// x coordinate of the window
    pub left: i32,
    /// y coordinate of the window
    pub top: i32,
    /// width of the window
    pub width: i32,
    /// height of the window
    pub height: i32,
    pub window_state: WindowState,
}

/// The state of the window
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
pub enum WindowState {
    Normal,
    Maximized,
    Minimized,
    Fullscreen,
}

impl WindowState {
    /// Convert to Bounds struct
    pub fn to_bounds(self) -> Bounds {
        Bounds {
            height: 0,
            width: 0,
            top: 0,
            left: 0,
            window_state: self,
        }
    }
}

impl Chrome {
    pub fn new_with_args(chrome_binary: &str, args: &[&str]) -> Result<Arc<Chrome>, JSError> {
        let (pid, precv, psend) =
            new_process(chrome_binary, &args).expect("Unable to launch chrome");
        let (kill_send, kill_recv) = bounded(1);

        let mut c = Chrome {
            id: AtomicI32::new(2),
            precv: Mutex::new(precv),
            psend: Mutex::new(psend),
            target: String::new(),
            session: String::new(),
            window: AtomicI32::new(0),
            pending: dashmap::DashMap::new(),
            bindings: dashmap::DashMap::new(),
            kill_send,
            #[cfg(target_family = "windows")]
            pid: pid as usize,
            #[cfg(target_family = "unix")]
            pid: pid,
        };

        c.target = c.find_target();
        c.session = c.start_session()?;

        let c_arc = Arc::new(c);

        #[cfg(target_family = "unix")]
        std::thread::spawn(move || {
            kill_recv.recv().unwrap();
            kill_proc(pid).expect("Unable to kill process");
        });

        let c_arc_clone = c_arc.clone();
        std::thread::spawn(move || readloop(c_arc_clone));

        for (method, args) in [
            ("Page.enable", JSObject::Null),
            (
                "Target.setAutoAttach",
                json!({"autoAttach": true, "waitForDebuggerOnStart": false}),
            ),
            ("Network.enable", JSObject::Null),
            ("Runtime.enable", JSObject::Null),
            ("Security.enable", JSObject::Null),
            ("Performance.enable", JSObject::Null),
            ("Log.enable", JSObject::Null),
            ("DOM.enable", JSObject::Null),
            ("CSS.enable", JSObject::Null),
        ]
        .iter()
        {
            send(Arc::clone(&c_arc), method, args)?;
        }

        if !args.contains(&"--headless") {
            let win_id = get_window_for_target(Arc::clone(&c_arc))?;
            Arc::clone(&c_arc).window.store(win_id, Ordering::Relaxed);
        }
        Ok(c_arc)
    }

    fn find_target(&self) -> String {
        send_msg(
            &self.psend,
            json!(
            {
            "id": 0,
            "method": "Target.setDiscoverTargets",
            "params": { "discover": true }
            }
            )
            .to_string(),
        );

        loop {
            let pmsg: JSObject =
                serde_json::from_str(&recv_msg(&self.precv)).expect("Invalid JSON");
            if pmsg["method"] == "Target.targetCreated" {
                let params = &pmsg["params"];
                if params["targetInfo"]["type"] == "page" {
                    return params["targetInfo"]["targetId"]
                        .as_str()
                        .expect("Value not of string datatype")
                        .to_string();
                }
            }
        }
    }

    fn start_session(&self) -> Result<String, JSError> {
        send_msg(
            &self.psend,
            json!(
            {
            "id": 1,
            "method": "Target.attachToTarget",
            "params": {"targetId": self.target}
            }
            )
            .to_string(),
        );

        loop {
            let pmsg: JSObject =
                serde_json::from_str(&recv_msg(&self.precv)).expect("Invalid JSON");
            if pmsg["id"] == 1 {
                if pmsg["error"] != JSObject::Null {
                    return Err(pmsg["error"].clone().into());
                }
                let session = &pmsg["result"];
                return Ok(session["sessionId"]
                    .as_str()
                    .expect("Value not of string datatype")
                    .to_string());
            }
        }
    }

    pub fn done(&self) -> bool {
        exited(self.pid as Process).expect("Error in getting process state")
    }

    pub fn wait_finish(&self) {
        wait_proc(self.pid as Process).expect("Error in waiting for process")
    }
}

fn get_window_for_target(c: Arc<Chrome>) -> Result<i32, JSObject> {
    match send(
        Arc::clone(&c),
        "Browser.getWindowForTarget",
        &json!({
            "targetId": c.target
        }),
    ) {
        Ok(v) => Ok(v["windowId"].as_i64().expect("Value not i64") as i32),
        Err(e) => Err(e),
    }
}

pub fn load(c: Arc<Chrome>, url: &str) -> Result<(), JSError> {
    send(Arc::clone(&c), "Page.navigate", &json!({ "url": url })).to_result_of_jserror()
}

pub fn eval(c: Arc<Chrome>, expr: &str) -> JSResult {
    send(
        c,
        "Runtime.evaluate",
        &json!({
            "expression": expr, "awaitPromise": true, "returnByValue": true
        }),
    )
}

pub fn set_bounds(c: Arc<Chrome>, b: Bounds) -> Result<(), JSError> {
    let param = json!({
        "windowId": c.window,
        "bounds": if b.window_state != WindowState::Normal {
            json!({
                "windowState":b.window_state
            })
        }else {
            serde_json::to_value(b).unwrap()
        }
    });
    send(c, "Browser.setWindowBounds", &param).to_result_of_jserror()
}

pub fn bounds(c: Arc<Chrome>) -> Result<Bounds, JSObject> {
    match send(
        Arc::clone(&c),
        "Browser.getWindowBounds",
        &json!({
            "windowId": c.window.load(Ordering::Relaxed)
        }),
    ) {
        Err(e) => Err(e),
        Ok(result) => {
            let ret: Bounds = serde_json::from_value(result["bounds"].clone())
                .expect("Value not of bounds datatype");
            Ok(ret)
        }
    }
}

pub fn load_js(c: Arc<Chrome>, script: &str) -> Result<(), JSError> {
    if let Err(e) = send(
        Arc::clone(&c),
        "Page.addScriptToEvaluateOnNewDocument",
        &json!({ "source": script }),
    ) {
        return Err(e.into());
    }
    eval(Arc::clone(&c), &script).to_result_of_jserror()
}

pub fn load_css(c: Arc<Chrome>, css: &str) -> Result<(), JSError> {
    let frame_tree = match send(
        Arc::clone(&c),
        "Page.getFrameTree",
        &json!({ "targetId": c.target }),
    ) {
        Ok(ft) => ft,
        Err(e) => return Err(e.into()),
    };
    let frame_id = frame_tree["frameTree"]["frame"]["id"].as_str().unwrap();
    let style_sheet = match send(
        Arc::clone(&c),
        "CSS.createStyleSheet",
        &json!({ "frameId": frame_id }),
    ) {
        Ok(ss) => ss,
        Err(e) => return Err(e.into()),
    };
    let style_sheet_id = style_sheet["styleSheetId"].as_str().unwrap();
    send(
        Arc::clone(&c),
        "CSS.setStyleSheetText",
        &json!({ "styleSheetId": style_sheet_id, "text": css }),
    )
    .to_result_of_jserror()
}

pub fn bind(c: Arc<Chrome>, name: &str, f: BindingFunc) -> Result<(), JSError> {
    c.bindings.insert(name.to_string(), f);

    if let Err(e) = send(
        Arc::clone(&c),
        "Runtime.addBinding",
        &json!({ "name": name }),
    ) {
        return Err(e.into());
    }

    let script = format!(
        r"(()=>{{
        const bindingName = '{name}';
        const binding = window[bindingName];
        window[bindingName] = async (...args) => {{
            const me = window[bindingName];
            let errors = me['errors'];
            let callbacks = me['callbacks'];
            if (!callbacks) {{
                callbacks = new Map();
                me['callbacks'] = callbacks;
            }}
            if (!errors) {{
                errors = new Map();
                me['errors'] = errors;
            }}
            const seq = (me['lastSeq'] || 0) + 1;
            me['lastSeq'] = seq;
            const promise = new Promise((resolve, reject) => {{
                callbacks.set(seq, resolve);
                errors.set(seq, reject);
            }});
            binding(JSON.stringify({{name: bindingName, seq, args}}));
            return promise;
        }}}})();
   ",
        name = name
    );

    if let Err(e) = send(
        Arc::clone(&c),
        "Page.addScriptToEvaluateOnNewDocument",
        &json!({ "source": script }),
    ) {
        return Err(e.into());
    }
    eval(Arc::clone(&c), &script).to_result_of_jserror()
}

fn complete_binding(context: ActiveBindingContext, result: JSResult) {
    let (r, e) = match result {
        Ok(x) => (x.to_string(), r#""""#.to_string()),
        Err(e) => ("".to_string(), e.to_string()),
    };

    let expr = format!(
        r"
        if ({error}) {{
            window['{name}']['errors'].get({seq})({error});
        }} else {{
            window['{name}']['callbacks'].get({seq})({result});
        }}
        window['{name}']['callbacks'].delete({seq});
        window['{name}']['errors'].delete({seq});
        ",
        name = context.payload["name"].as_str().expect("Expected string"),
        seq = context.payload["seq"].as_i64().expect("Expected i64"),
        result = r,
        error = e
    );

    if let Err(e) = send(
        context.chrome,
        "Runtime.evaluate",
        &json!({
            "expression":expr,
            "contextId":context.context_id
        }),
    ) {
        eprintln!("{}", e);
    }
}

pub fn close(c: Arc<Chrome>) {
    std::thread::spawn(move || {
        if let Err(e) = send(c, "Browser.close", &json!({})) {
            eprintln!("{}", e);
        }
    });
}

#[cfg(target_family = "windows")]
pub fn close_handle(c: Arc<Chrome>) {
    close_process_handle(c.pid as Process).expect("Unable to close handle")
}