Skip to main content

Browser

Struct Browser 

Source
pub struct Browser { /* private fields */ }
Expand description

CDP Browser 实例。

代表一次成功的 connect —— 持有解析后的 URL、Connection 和 transport 类型。

@trace REQ-BAO-API-001 [level:library]

Implementations§

Source§

impl Browser

Source

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://) 完成。

§错误

@trace REQ-BAO-API-001 [level:library]

Source

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]

Source

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]

Source

pub fn send_command( &mut self, method: &str, params: Value, ) -> Result<Value, CdpError>

发送 CDP 命令(通过 Connection)。

@trace REQ-BAO-API-001 [level:library]

Source

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]

Source

pub fn version(&mut self) -> Result<Value, CdpError>

获取 Browser 版本信息(CDP Browser.getVersion)。

@trace REQ-BAO-API-001 [level:library]

Source

pub fn pages(&mut self) -> Result<Value, CdpError>

获取所有 target 列表(CDP Target.getTargets)。

@trace REQ-BAO-API-001 [level:library]

Source

pub fn new_page(&mut self, url: &str) -> Result<Value, CdpError>

创建新 page/tab(CDP Target.createTarget)。

@trace REQ-BAO-API-001 [level:library]

Source

pub fn recv_event(&mut self) -> Result<Option<CdpEvent>, CdpError>

接收一个 CDP 事件。

@trace REQ-BAO-API-001 [level:library]

Source

pub fn on_event( &mut self, method: &str, handler: Arc<dyn Fn(CdpEvent) + Sync + Send>, ) -> u64

注册事件 handler。

@trace REQ-BAO-API-001 [level:library]

Source

pub fn off_event(&mut self, method: &str, listener_id: u64) -> bool

移除事件 handler。

@trace REQ-BAO-API-001 [level:library]

Source

pub fn close_connection(&mut self) -> Result<(), CdpError>

关闭 Connection。

@trace REQ-BAO-API-001 [level:library]

Source

pub fn url(&self) -> &str

原 URL。

@trace REQ-BAO-API-001 [level:library]

Source

pub fn scheme(&self) -> &str

已解析的 scheme(memory / ws / wss / http / https)。

@trace REQ-BAO-API-001 [level:library]

Source

pub fn transport_kind(&self) -> TransportKind

Transport 类型。

@trace REQ-BAO-API-001 [level:library]

Source

pub fn is_in_memory(&self) -> bool

是否 InMemory 连接。

@trace REQ-BAO-API-001 [level:library]

Source

pub fn is_websocket(&self) -> bool

是否 WebSocket 连接(包括 ws/wss/http/https — 后者会先 discover)。

@trace REQ-BAO-API-001 [level:library]

Source

pub fn has_connection(&self) -> bool

是否有 Connection(即 transport 已建立)。

Source

pub fn connection_mut(&mut self) -> Option<&mut Connection>

获取 Connection 的可变引用。

@trace REQ-BAO-API-001 [level:library]

Source

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]

Source

pub fn build_in_memory_transport( &self, bridge: Arc<dyn InMemoryBridge>, ) -> Result<InMemoryTransport, ConnectError>

构造 InMemory transport(同进程 servo)。

@trace REQ-BAO-API-002 [interface:Transport]

Source

pub fn build_websocket_transport( &self, ) -> Result<WebSocketTransport, ConnectError>

构造 WebSocket transport(外部 Chrome)。

@trace REQ-BAO-API-002 [interface:Transport]

Trait Implementations§

Source§

impl Debug for Browser

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Display for Browser

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsCtxPtr for T
where T: ?Sized,

Source§

fn as_ctx_ptr(&self) -> *mut Self
where 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> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> Filterable for T

Source§

fn filterable( self, filter_name: &'static str, ) -> RequestFilterDataProvider<T, fn(DataRequest<'_>) -> bool>

Creates a filterable data provider with the given name for debugging. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> MaybeBoxed<Box<T>> for T

Source§

fn maybe_boxed(self) -> Box<T>

Convert
Source§

impl<T> MaybeBoxed<T> for T

Source§

fn maybe_boxed(self) -> T

Convert
Source§

impl<T> MaybeSendSync for T

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> Send for T
where T: ?Sized,

Source§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

Source§

fn simd_from(_simd: S, value: T) -> T

Source§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

Source§

fn simd_into(self, simd: S) -> T

Source§

impl<T> Sync for T
where T: ?Sized,

Source§

impl<T> ToCompactString for T
where T: Display,

Source§

fn to_compact_string(&self) -> CompactString

Converts the given value to a CompactString. Read more
Source§

impl<T> ToCompactString for T
where T: Display,

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more