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

examples/wpe_host.rs (2.2K)

 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 #[path = "../src/wpe/mod.rs"]
23 mod wpe;
24 
25 #[cfg(feature = "wpe")]
26 fn main() {
27     let url = std::env::args()
28         .nth(1)
29         .unwrap_or_else(|| "https://example.com".into());
30     let mut host = wpe::WebKitHost::new(url::Url::parse(&url).unwrap(), (1200, 800));
31 
32     let mut frames = 0;
33     let mut navigated = false;
34     for i in 0..60 {
35         // Once the first page settles, navigate and come back — exercises
36         // load / can_go_back / back on a live history.
37         if !navigated && !host.loading() && frames > 1 && i > 10 {
38             navigated = true;
39             println!("-- navigating to example.org --");
40             host.load(url::Url::parse("https://example.org").unwrap());
41         }
42         if navigated && i == 40 {
43             println!("-- back (can_go_back={}) --", host.can_go_back());
44             host.back();
45         }
46         let (new_frame, dirty) = host.pump();
47         if new_frame {
48             frames += 1;
49             println!(
50                 "t={:>4}ms frame#{frames} image={:?}",
51                 i * 100,
52                 host.image()
53             );
54         }
55         if dirty {
56             println!(
57                 "  state: title={:?} url={:?} loading={} back={} fwd={}",
58                 host.title(),
59                 host.url().map(|u| u.to_string()),
60                 host.loading(),
61                 host.can_go_back(),
62                 host.can_go_forward()
63             );
64         }
65         std::thread::sleep(std::time::Duration::from_millis(100));
66     }
67     println!(
68         "done: {frames} frames, tabs={} active={}",
69         host.tab_count(),
70         host.active_index()
71     );
72     assert!(frames > 0, "no frames produced");
73 }