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