-
-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathhello-world.rs
More file actions
353 lines (319 loc) · 13 KB
/
Copy pathhello-world.rs
File metadata and controls
353 lines (319 loc) · 13 KB
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
// Example code: panicking on bad input is the desired behavior, as in any test code.
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use cow_utils::CowUtils;
use gosub_engine::events::{MouseButton, NavigationEvent, ResourceEvent, TabCommand};
use gosub_engine::net::types::FetchResultMeta;
use gosub_engine::net::DecisionToken;
use gosub_engine::tab::{TabDefaults, TabHandle};
use gosub_engine::{
cookies::DefaultCookieJar,
events::EngineEvent,
storage::{InMemoryLocalStore, InMemorySessionStore, PartitionPolicy, StorageService},
zone::ZoneConfig,
zone::ZoneServices,
Action, DefaultRenderConfig, EngineConfig, EngineError, GosubEngine, NavigationId,
};
use gosub_render_pipeline::render::{DefaultCompositor, Viewport};
use http::header;
use std::sync::Arc;
use std::time::Duration;
fn init_tracing() {
use tracing_subscriber::{fmt, layer::SubscriberExt, EnvFilter, Registry};
let _ = tracing_log::LogTracer::init();
// Default to warn. The tab worker emits a WARN for every unimplemented TabCommand
// (e.g. SetTitle), which is expected during development - suppress it here so the example
// output focuses on navigation and resource events.
// Override via RUST_LOG, e.g. RUST_LOG=gosub_engine=debug for deeper inspection.
let filter = EnvFilter::try_from_default_env()
.unwrap_or_else(|_| EnvFilter::new("warn,gosub_engine::engine::tab::worker=error"));
let fmt_layer = fmt::layer().with_target(true).with_level(true);
let subscriber = Registry::default().with(filter).with(fmt_layer);
let _ = tracing::subscriber::set_global_default(subscriber);
}
fn fmt_elapsed(d: std::time::Duration) -> String {
if d.as_secs() >= 1 {
format!("{:.2}s", d.as_secs_f64())
} else {
format!("{}ms", d.as_millis())
}
}
fn short(id: &impl std::fmt::Display) -> String {
let s = id.to_string();
s.chars().take(8).collect()
}
#[tokio::main]
async fn main() -> Result<(), EngineError> {
init_tracing();
// Configure the engine through the engine config builder. This will set up the main runtime
// configuration of the engine. It's possible for some values to be changed at runtime, but
// not all of them
let engine_cfg = EngineConfig::builder()
.max_zones(5)
.build()
.expect("Configuration is not valid");
// Set up a render backend. In this example we use the NullBackend which does not render
// anything.
let backend = gosub_render_pipeline::render::backends::null::NullBackend::new();
// Instantiate and start the engine
let mut engine = GosubEngine::<DefaultRenderConfig<_>>::new(
Some(engine_cfg),
Arc::new(backend),
Arc::new(DefaultCompositor::default()),
);
let engine_join_handle = tokio::spawn(engine.start().expect("cannot start engine"));
// Get our event channel to receive events from the engine. Note that you will only receive events
// send from this point on.
let mut event_rx = engine.subscribe_events();
// Configure a zone. This works the same way as the engine config, using a builder
// pattern to set up the configuration before building it.
let zone_cfg = ZoneConfig::builder()
.do_not_track(true)
.accept_languages("fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5")
.build()
.expect("ZoneConfig is not valid");
// Create the services for this zone. These services are automatically provided to the tabs
// created in the zone, but can be overridden on a per-tab basis if needed.
let zone_services = ZoneServices {
storage: Arc::new(StorageService::new(
Arc::new(InMemoryLocalStore::new()),
Arc::new(InMemorySessionStore::new()),
)),
cookie_store: None,
cookie_jar: Some(DefaultCookieJar::new().into()),
partition_policy: PartitionPolicy::None,
};
// Create the zone. Note that we can define our own zone ID to keep zones deterministic
// (like a user profile), and we give the zone handle to the event channel so we can
// receive events related to the zone.
let mut zone = engine.create_zone(Some(zone_cfg), zone_services, None)?;
// Next, we create a tab in the zone. For now, we don't provide anything, but we should
// be able to provide tab-specific services (like a different cookie jar, etc.)
let def_values = TabDefaults {
url: None,
title: Some("New Tab".into()),
viewport: Some(Viewport::new(0, 0, 800, 600)),
};
let tab = zone.create_tab(def_values, None).await.expect("cannot create tab");
// From the tab handle, we can now send commands to the engine to control the tab.
tab.send(TabCommand::SetViewport {
x: 0,
y: 0,
width: 1024,
height: 768,
})
.await?;
// Navigate somewhere
tab.send(TabCommand::Navigate {
url: "https://news.ycombinator.com".into(),
})
.await?;
// Simulate a little user input (mouse move + click at 100,100)
tab.send(TabCommand::MouseMove { x: 100.0, y: 100.0 }).await?;
tab.send(TabCommand::MouseDown {
x: 100.0,
y: 100.0,
button: MouseButton::Left,
})
.await?;
tab.send(TabCommand::MouseUp {
x: 100.0,
y: 100.0,
button: MouseButton::Left,
})
.await?;
// We can set metadata on the zone like this
zone.set_title("My first Zone");
zone.set_description("This is the new description");
zone.set_color([255, 128, 64, 255]);
// This is the application's main loop, where we receive events from the engine and
// act on them. In a real application, you would probably want to run this in
// a separate task/thread, and not block the main thread.
let mut seen_intervals = 0usize;
let mut interval = tokio::time::interval(Duration::from_secs(1));
let tab_clone = tab.clone();
loop {
tokio::select! {
Ok(ev) = event_rx.recv() => {
handle_event(ev, tab_clone.clone()).await;
}
_ = tokio::signal::ctrl_c() => {
println!("Shutting down...");
break;
}
_ = interval.tick() => {
seen_intervals += 1;
if seen_intervals >= 1000 {
break;
}
}
}
}
engine.shutdown().await?;
// Wait for the engine task to finish
if let Err(join_err) = engine_join_handle.await {
eprintln!("engine task panicked: {join_err}");
}
println!("Done. Exiting.");
Ok(())
}
async fn on_decision_required(
tab_handle: TabHandle,
nav_id: NavigationId,
meta: FetchResultMeta,
decision_token: DecisionToken,
) {
let ct: String = meta
.headers
.get(header::CONTENT_TYPE)
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream")
.to_string();
let action = if let Some(disp) = meta.headers.get(http::header::CONTENT_DISPOSITION) {
let s = disp.to_str().unwrap_or_default().cow_to_ascii_lowercase();
if s.contains("attachment") {
Action::Download {
dest: std::path::PathBuf::from("/tmp/downloaded.bin"),
}
} else {
Action::Render
}
} else if ct.starts_with("text/html") || ct.starts_with("text/") || ct == "application/json" {
Action::Render
} else {
Action::Download {
dest: std::path::PathBuf::from("/tmp/downloaded.bin"),
}
};
// Send back to the engine what we like to do with this navigation
let _ = tab_handle
.cmd_tx
.send(TabCommand::SubmitDecision {
nav_id,
decision_token,
action,
})
.await;
}
async fn handle_event(ev: EngineEvent, tab_handle: TabHandle) {
match ev {
EngineEvent::ZoneCreated { zone_id } => {
println!("[zone] created {}", short(&zone_id));
}
EngineEvent::TabCreated { tab_id, .. } => {
println!("[tab ] created {}", short(&tab_id));
}
EngineEvent::Navigation { tab_id, event } => {
let t = short(&tab_id);
match event {
NavigationEvent::Started { url, .. } => {
println!("[nav ] → [{t}] {url}");
}
NavigationEvent::Committed { url, .. } => {
println!("[nav ] committed [{t}] {url}");
}
NavigationEvent::Finished { url, .. } => {
println!("[nav ] finished [{t}] {url}");
}
NavigationEvent::Failed { url, error, .. } => {
println!("[nav ] FAILED [{t}] {url} ({error})");
}
NavigationEvent::Cancelled { url, reason, .. } => {
println!("[nav ] cancelled [{t}] {url} ({reason:?})");
}
NavigationEvent::FailedUrl { url, error, .. } => {
println!("[nav ] failed-url [{t}] {url} ({error:?})");
}
NavigationEvent::Progress {
received_bytes,
expected_length,
elapsed,
..
} => {
let kb = received_bytes / 1024;
let total = expected_length
.map(|n| format!("{} KB", n / 1024))
.unwrap_or_else(|| "?".into());
println!("[nav ] progress [{t}] {kb} KB / {total} ({})", fmt_elapsed(elapsed));
}
NavigationEvent::DecisionRequired {
nav_id,
meta,
decision_token,
} => {
// The engine fetched response headers and needs us to decide: render or download?
// We inspect content-type and content-disposition and reply with Action::Render or Action::Download.
println!("[nav ] decision [{t}] {}", short(&nav_id));
if tab_id != tab_handle.tab_id {
eprintln!("[nav ] warning: DecisionRequired for unexpected tab {t}");
return;
}
on_decision_required(tab_handle, nav_id, meta, decision_token).await;
}
}
}
EngineEvent::Resource { tab_id, event } => {
let t = short(&tab_id);
match event {
ResourceEvent::Queued {
url, kind, priority, ..
} => {
println!("[res ] queued [{t}] {kind:?} pri={priority} {url}");
}
ResourceEvent::Started { url, .. } => {
println!("[res ] started [{t}] {url}");
}
ResourceEvent::Redirected { from, to, status, .. } => {
println!("[res ] redirect [{t}] {status} {from} → {to}");
}
ResourceEvent::Headers {
url,
status,
content_type,
content_length,
..
} => {
let ct = content_type.as_deref().unwrap_or("-");
let cl = content_length
.map(|n| format!("{n} B"))
.unwrap_or_else(|| "unknown".into());
println!("[res ] headers [{t}] {status} {ct} {cl} {url}");
}
ResourceEvent::Progress {
received_bytes,
expected_length,
elapsed,
..
} => {
let kb = received_bytes / 1024;
let total = expected_length
.map(|n| format!("{} KB", n / 1024))
.unwrap_or_else(|| "?".into());
println!("[res ] progress [{t}] {kb} KB / {total} ({})", fmt_elapsed(elapsed));
}
ResourceEvent::Finished {
url,
received_bytes,
elapsed,
..
} => {
let kb = received_bytes as f64 / 1024.0;
let elapsed = elapsed.map(fmt_elapsed).unwrap_or_else(|| "-".into());
println!("[res ] finished [{t}] {kb:.1} KB {elapsed} {url}");
}
ResourceEvent::Failed { url, error, .. } => {
println!("[res ] FAILED [{t}] {url} ({error})");
}
ResourceEvent::Cancelled { url, reason, .. } => {
println!("[res ] cancelled [{t}] {url} ({reason:?})");
}
}
}
EngineEvent::Redraw { tab_id, .. } => {
// With a real backend you'd composite a frame here.
println!("[draw] frame [{}]", short(&tab_id));
}
other => {
println!("[? ] {other:?}");
}
}
}