Skip to main content

millipede_browser/
nav.rs

1//! Browser pre- and post-navigation hook contexts.
2
3use std::sync::Arc;
4
5use futures_util::future::BoxFuture;
6use millipede_core::{errors::CrawlError, proxy::ProxyInfo, request::Request, session::Session};
7
8use crate::{BrowserResponse, PageHandle};
9
10/// Context supplied to browser pre-navigation hooks.
11///
12/// Unlike the INTERFACE §18 sketch, `request` is immutable because execution owns an
13/// [`Arc<Request>`]. The proposed `log` field is omitted because no `Log` type exists; use
14/// `tracing` macros instead. This context is non-exhaustive so `log` can be added later without a
15/// breaking change.
16#[non_exhaustive]
17pub struct BrowserPreHookCtx<'a> {
18    /// Request about to be navigated.
19    pub request: &'a Request,
20    /// Live page that will perform the navigation.
21    pub page: &'a PageHandle,
22    /// Session selected for this attempt, when sessions are enabled.
23    pub session: Option<&'a Session>,
24    /// Proxy assigned to the browser containing the page.
25    pub proxy: Option<&'a ProxyInfo>,
26}
27
28/// Context supplied to browser post-navigation hooks.
29///
30/// The proposed INTERFACE §18 `log` field is omitted because no `Log` type exists; use `tracing`
31/// macros instead. This context is non-exhaustive so `log` can be added later without a breaking
32/// change.
33#[non_exhaustive]
34pub struct BrowserPostHookCtx<'a> {
35    /// Request that was navigated.
36    pub request: &'a Request,
37    /// Live page after navigation and status classification.
38    pub page: &'a PageHandle,
39    /// Navigation response metadata, when exposed by the provider.
40    pub response: Option<&'a BrowserResponse>,
41    /// Session selected for this attempt, when sessions are enabled.
42    pub session: Option<&'a Session>,
43    /// Proxy assigned to the browser containing the page.
44    pub proxy: Option<&'a ProxyInfo>,
45}
46
47/// Asynchronous browser hook run after page creation and before navigation.
48pub type BrowserPreNavigationHook = Arc<
49    dyn for<'a> Fn(BrowserPreHookCtx<'a>) -> BoxFuture<'a, Result<(), CrawlError>> + Send + Sync,
50>;
51
52/// Asynchronous browser hook run after navigation and status classification.
53pub type BrowserPostNavigationHook = Arc<
54    dyn for<'a> Fn(BrowserPostHookCtx<'a>) -> BoxFuture<'a, Result<(), CrawlError>> + Send + Sync,
55>;