pub struct Browser { /* private fields */ }Expand description
CDP Browser 实例。
代表一次成功的 connect —— 持有解析后的 URL、Connection 和 transport 类型。
@trace REQ-BAO-API-001 [level:library]
Implementations§
Source§impl Browser
impl Browser
Sourcepub fn connect(url: &str) -> Result<Browser, ConnectError>
pub fn connect(url: &str) -> Result<Browser, ConnectError>
连接到 CDP 目标(解析 URL,按需创建 Connection)。
根据输入 URL 的 scheme 自动路由到三种 transport 模式:
| Scheme | 行为 |
|---|---|
memory:// | 解析 URL,返回 Browser(无 Connection,需 connect_with_bridge 接入) |
ws:///wss:// | 解析 URL,返回 Browser(无 Connection,需 connect_with_discovered_ws 接入) |
http:///https:// | 解析 URL,返回 Browser(无 Connection,同 ws://) |
这是 lazy connect 模式:不触发网络 I/O,不阻塞,瞬时返回。
实际的 Transport 建立通过 connect_with_bridge(memory://)
或 connect_with_discovered_ws(ws://) 完成。
§错误
ConnectError::InvalidUrl: 空 URL 或无://ConnectError::InvalidScheme: scheme 不在支持列表(如ftp)
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn connect_with_bridge(
url: &str,
bridge: Arc<dyn InMemoryBridge>,
) -> Result<Browser, ConnectError>
pub fn connect_with_bridge( url: &str, bridge: Arc<dyn InMemoryBridge>, ) -> Result<Browser, ConnectError>
连接到 CDP 目标并传入 InMemoryBridge(memory:// 模式)。
与 connect 类似,但立即为 memory:// 创建 Connection。
其他 scheme 仍返回 Browser(无 Connection)。
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn connect_with_discovered_ws(ws_url: &str) -> Result<Browser, ConnectError>
pub fn connect_with_discovered_ws(ws_url: &str) -> Result<Browser, ConnectError>
HTTP 发现完成后,用发现的 ws URL 创建 Connection。
触发实际的 WebSocket 连接(会阻塞直到 TCP+WS 握手完成或超时)。
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn send_command(
&mut self,
method: &str,
params: Value,
) -> Result<Value, CdpError>
pub fn send_command( &mut self, method: &str, params: Value, ) -> Result<Value, CdpError>
发送 CDP 命令(通过 Connection)。
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn send_command_with_session(
&mut self,
method: &str,
params: Value,
session_id: &str,
) -> Result<Value, CdpError>
pub fn send_command_with_session( &mut self, method: &str, params: Value, session_id: &str, ) -> Result<Value, CdpError>
发送 CDP 命令(带 session_id)。
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn version(&mut self) -> Result<Value, CdpError>
pub fn version(&mut self) -> Result<Value, CdpError>
获取 Browser 版本信息(CDP Browser.getVersion)。
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn pages(&mut self) -> Result<Value, CdpError>
pub fn pages(&mut self) -> Result<Value, CdpError>
获取所有 target 列表(CDP Target.getTargets)。
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn new_page(&mut self, url: &str) -> Result<Value, CdpError>
pub fn new_page(&mut self, url: &str) -> Result<Value, CdpError>
创建新 page/tab(CDP Target.createTarget)。
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn recv_event(&mut self) -> Result<Option<CdpEvent>, CdpError>
pub fn recv_event(&mut self) -> Result<Option<CdpEvent>, CdpError>
接收一个 CDP 事件。
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn on_event(
&mut self,
method: &str,
handler: Arc<dyn Fn(CdpEvent) + Sync + Send>,
) -> u64
pub fn on_event( &mut self, method: &str, handler: Arc<dyn Fn(CdpEvent) + Sync + Send>, ) -> u64
注册事件 handler。
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn off_event(&mut self, method: &str, listener_id: u64) -> bool
pub fn off_event(&mut self, method: &str, listener_id: u64) -> bool
移除事件 handler。
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn close_connection(&mut self) -> Result<(), CdpError>
pub fn close_connection(&mut self) -> Result<(), CdpError>
关闭 Connection。
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn scheme(&self) -> &str
pub fn scheme(&self) -> &str
已解析的 scheme(memory / ws / wss / http / https)。
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn transport_kind(&self) -> TransportKind
pub fn transport_kind(&self) -> TransportKind
Transport 类型。
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn is_in_memory(&self) -> bool
pub fn is_in_memory(&self) -> bool
是否 InMemory 连接。
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn is_websocket(&self) -> bool
pub fn is_websocket(&self) -> bool
是否 WebSocket 连接(包括 ws/wss/http/https — 后者会先 discover)。
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn has_connection(&self) -> bool
pub fn has_connection(&self) -> bool
是否有 Connection(即 transport 已建立)。
Sourcepub fn connection_mut(&mut self) -> Option<&mut Connection>
pub fn connection_mut(&mut self) -> Option<&mut Connection>
获取 Connection 的可变引用。
@trace REQ-BAO-API-001 [level:library]
Sourcepub fn build_transport(&self) -> Result<Box<dyn Transport>, ConnectError>
pub fn build_transport(&self) -> Result<Box<dyn Transport>, ConnectError>
根据 URL scheme 构造对应 Transport。
- InMemory URL(
memory://)需要调用方传入 servo bridge → 调 [build_in_memory_transport] - WebSocket URL(
ws://)→ 调 [build_websocket_transport] 触发 TCP + WebSocket 握手
返回 Box<dyn Transport> 便于上层 Connection 持有 trait 对象。
@trace REQ-BAO-API-002 [interface:Transport]
Sourcepub fn build_in_memory_transport(
&self,
bridge: Arc<dyn InMemoryBridge>,
) -> Result<InMemoryTransport, ConnectError>
pub fn build_in_memory_transport( &self, bridge: Arc<dyn InMemoryBridge>, ) -> Result<InMemoryTransport, ConnectError>
构造 InMemory transport(同进程 servo)。
@trace REQ-BAO-API-002 [interface:Transport]
Sourcepub fn build_websocket_transport(
&self,
) -> Result<WebSocketTransport, ConnectError>
pub fn build_websocket_transport( &self, ) -> Result<WebSocketTransport, ConnectError>
构造 WebSocket transport(外部 Chrome)。
@trace REQ-BAO-API-002 [interface:Transport]
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Browser
impl !Sync for Browser
impl !UnwindSafe for Browser
impl Freeze for Browser
impl Send for Browser
impl Unpin for Browser
impl UnsafeUnpin for Browser
Blanket Implementations§
Source§impl<T> AsCtxPtr for Twhere
T: ?Sized,
impl<T> AsCtxPtr for Twhere
T: ?Sized,
Source§fn as_ctx_ptr(&self) -> *mut Selfwhere
Self: Sized,
fn as_ctx_ptr(&self) -> *mut Selfwhere
Self: Sized,
self’s address as *mut Self for deferred-task / scopeguard /
ref_guard ctx slots. The closures/trampolines deref it as shared
(&*p) — every method they reach is &self post-R-2, so no write
provenance is required; the *mut spelling is purely to match the
existing DerefOnDrop / HasAutoFlush / RefCount ABI.Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> ErasedDestructor for Twhere
T: 'static,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Filterable for T
impl<T> Filterable for T
Source§fn filterable(
self,
filter_name: &'static str,
) -> RequestFilterDataProvider<T, fn(DataRequest<'_>) -> bool>
fn filterable( self, filter_name: &'static str, ) -> RequestFilterDataProvider<T, fn(DataRequest<'_>) -> bool>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> MaybeBoxed<Box<T>> for T
impl<T> MaybeBoxed<Box<T>> for T
Source§fn maybe_boxed(self) -> Box<T>
fn maybe_boxed(self) -> Box<T>
Source§impl<T> MaybeBoxed<T> for T
impl<T> MaybeBoxed<T> for T
Source§fn maybe_boxed(self) -> T
fn maybe_boxed(self) -> T
impl<T> MaybeSendSync for T
Source§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
impl<T> Send for Twhere
T: ?Sized,
impl<T> Sync for Twhere
T: ?Sized,
Source§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
Source§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString. Read moreSource§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
Source§fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
ToCompactString::to_compact_string() Read moreSource§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString. Read more