firefox_webdriver/browser/tab/
core.rs1use std::fmt;
4use std::sync::Arc;
5
6use crate::error::{Error, Result};
7use crate::identifiers::{FrameId, SessionId, TabId};
8use crate::protocol::{Command, Request, Response};
9
10use crate::browser::Window;
11
12#[derive(Debug, Clone)]
18pub struct FrameInfo {
19 pub frame_id: FrameId,
21 pub parent_frame_id: Option<FrameId>,
23 pub url: String,
25}
26
27pub(crate) struct TabInner {
29 pub tab_id: TabId,
31 pub frame_id: FrameId,
33 pub session_id: SessionId,
35 pub window: Option<Window>,
37}
38
39#[derive(Clone)]
47pub struct Tab {
48 pub(crate) inner: Arc<TabInner>,
49}
50
51impl fmt::Debug for Tab {
52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53 f.debug_struct("Tab")
54 .field("tab_id", &self.inner.tab_id)
55 .field("frame_id", &self.inner.frame_id)
56 .field("session_id", &self.inner.session_id)
57 .finish_non_exhaustive()
58 }
59}
60
61impl Tab {
62 pub(crate) fn new(
64 tab_id: TabId,
65 frame_id: FrameId,
66 session_id: SessionId,
67 window: Option<Window>,
68 ) -> Self {
69 Self {
70 inner: Arc::new(TabInner {
71 tab_id,
72 frame_id,
73 session_id,
74 window,
75 }),
76 }
77 }
78}
79
80impl Tab {
85 #[inline]
87 #[must_use]
88 pub fn tab_id(&self) -> TabId {
89 self.inner.tab_id
90 }
91
92 #[inline]
94 #[must_use]
95 pub fn frame_id(&self) -> FrameId {
96 self.inner.frame_id
97 }
98
99 #[inline]
101 #[must_use]
102 pub fn session_id(&self) -> SessionId {
103 self.inner.session_id
104 }
105
106 #[inline]
108 #[must_use]
109 pub fn is_main_frame(&self) -> bool {
110 self.inner.frame_id.is_main()
111 }
112}
113
114impl Tab {
119 pub(crate) async fn send_command(&self, command: Command) -> Result<Response> {
121 let window = self.get_window()?;
122 let request = Request::new(self.inner.tab_id, self.inner.frame_id, command);
123 window
124 .inner
125 .pool
126 .send(window.inner.session_id, request)
127 .await
128 }
129
130 pub(crate) fn get_window(&self) -> Result<&Window> {
132 self.inner
133 .window
134 .as_ref()
135 .ok_or_else(|| Error::protocol("Tab has no associated window"))
136 }
137}
138
139#[cfg(test)]
144mod tests {
145 use super::Tab;
146
147 #[test]
148 fn test_tab_is_clone() {
149 fn assert_clone<T: Clone>() {}
150 assert_clone::<Tab>();
151 }
152
153 #[test]
154 fn test_tab_is_debug() {
155 fn assert_debug<T: std::fmt::Debug>() {}
156 assert_debug::<Tab>();
157 }
158}