chromiumoxide/handler/
domworld.rs

1use chromiumoxide_cdp::cdp::js_protocol::runtime::ExecutionContextId;
2
3#[derive(Debug, Clone, Default)]
4pub struct DOMWorld {
5    execution_ctx: Option<ExecutionContextId>,
6    execution_ctx_unique_id: Option<String>,
7    detached: bool,
8}
9
10impl DOMWorld {
11    pub fn main_world() -> Self {
12        Self {
13            execution_ctx: None,
14            execution_ctx_unique_id: None,
15            detached: false,
16        }
17    }
18
19    pub fn secondary_world() -> Self {
20        Self {
21            execution_ctx: None,
22            execution_ctx_unique_id: None,
23            detached: false,
24        }
25    }
26
27    pub fn execution_context(&self) -> Option<ExecutionContextId> {
28        self.execution_ctx
29    }
30
31    pub fn execution_context_unique_id(&self) -> Option<&str> {
32        self.execution_ctx_unique_id.as_deref()
33    }
34
35    pub fn set_context(&mut self, ctx: ExecutionContextId, unique_id: String) {
36        self.execution_ctx = Some(ctx);
37        self.execution_ctx_unique_id = Some(unique_id);
38    }
39
40    pub fn take_context(&mut self) -> (Option<ExecutionContextId>, Option<String>) {
41        (
42            self.execution_ctx.take(),
43            self.execution_ctx_unique_id.take(),
44        )
45    }
46
47    pub fn is_detached(&self) -> bool {
48        self.detached
49    }
50}
51
52/// There are two different kinds of worlds tracked for each `Frame`, that
53/// represent a context for JavaScript execution. A `Page` might have many
54/// execution contexts
55/// - each [iframe](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe)
56///   has a "default" execution context that is always created after the frame
57///   is attached to DOM.
58///   [Extension's](https://developer.chrome.com/extensions) content scripts create additional execution contexts.
59///
60/// Besides pages, execution contexts can be found in
61/// [Web Workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API).
62#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
63pub enum DOMWorldKind {
64    /// The main world of a frame that represents the default execution context
65    /// of a frame and is also created.
66    #[default]
67    Main,
68    /// Each frame gets its own isolated world with universal access
69    Secondary,
70}