git.lucas.co / cce-browser
web browser (Servo)
git clone https://git.lucas.co/cce-browser.git

examples/wpe_host.rs (2.3K)

 1 //! Exercises `WebKitHost` — the real API surface `main.rs` will consume,
 2 //! not the raw FFI. Proves boot → pump → frames → page state.
 3 //!
 4 //! `cargo run --release -p cce-browser --features wpe --example wpe_host`
 5 
 6 #[cfg(not(feature = "wpe"))]
 7 fn main() {
 8     eprintln!("build with --features wpe");
 9 }
10 
11 #[cfg(feature = "wpe")]
12 #[derive(Debug, Clone, Copy)]
13 pub enum EditingCommand { Copy, Cut, Paste }
14 
15 #[cfg(feature = "wpe")]
16 #[path = "../src/pages.rs"]
17 mod pages;
18 #[cfg(feature = "wpe")]
19 #[path = "../src/downloads.rs"]
20 mod downloads;
21 #[cfg(feature = "wpe")]
22 // The host's vi channel and scripts.
23 #[path = "../src/vi.rs"]
24 #[allow(dead_code)]
25 mod vi;
26 
27 #[path = "../src/wpe/mod.rs"]
28 mod wpe;
29 
30 #[cfg(feature = "wpe")]
31 fn main() {
32     let url = std::env::args()
33         .nth(1)
34         .unwrap_or_else(|| "https://example.com".into());
35     let mut host = wpe::WebKitHost::new(url::Url::parse(&url).unwrap(), (1200, 800));
36 
37     let mut frames = 0;
38     let mut navigated = false;
39     for i in 0..60 {
40         // Once the first page settles, navigate and come back — exercises
41         // load / can_go_back / back on a live history.
42         if !navigated && !host.loading() && frames > 1 && i > 10 {
43             navigated = true;
44             println!("-- navigating to example.org --");
45             host.load(url::Url::parse("https://example.org").unwrap());
46         }
47         if navigated && i == 40 {
48             println!("-- back (can_go_back={}) --", host.can_go_back());
49             host.back();
50         }
51         let (new_frame, dirty) = host.pump();
52         host.frame_drawn();
53         if new_frame {
54             frames += 1;
55             println!(
56                 "t={:>4}ms frame#{frames} image={:?}",
57                 i * 100,
58                 host.image()
59             );
60         }
61         if dirty {
62             println!(
63                 "  state: title={:?} url={:?} loading={} back={} fwd={}",
64                 host.title(),
65                 host.url().map(|u| u.to_string()),
66                 host.loading(),
67                 host.can_go_back(),
68                 host.can_go_forward()
69             );
70         }
71         std::thread::sleep(std::time::Duration::from_millis(100));
72     }
73     println!(
74         "done: {frames} frames, tabs={} active={}",
75         host.tab_count(),
76         host.active_index()
77     );
78     assert!(frames > 0, "no frames produced");
79 }