pub struct EventSubscriber { /* private fields */ }Expand description
事件订阅者 — servo delegate 在 servo 线程调用 on_* 方法,push 事件到 channel。
用法:
use bao_cdp_client::bridge::{ConsoleLevel, EventSubscriber};
use std::time::Duration;
let (subscriber, rx) = EventSubscriber::new();
// servo delegate 在 servo 线程调用 on_console_message 时,事件进入 channel
subscriber.on_console_message("target-1", ConsoleLevel::Info, "hello", None, None, None);
// 主线程在 InMemoryTransport 内 recv_event 时,从 rx 取出并 translate 为 CdpEvent
let servo_event = rx.recv_timeout(Duration::from_millis(100)).expect("event delivered");
assert!(matches!(servo_event,
bao_cdp_client::bridge::ServoEvent::Console { ref text, .. } if text == "hello"));§关闭语义
当 EventSubscriber drop 时,Sender 被丢弃,接收端 recv 会收到
RecvTimeoutError::Disconnected。
§线程安全
mpsc::Sender 是 Send + Sync,可被 servo delegate 在 servo 线程持有。
但注意 EventSubscriber 不 Clone(避免多 sender 混淆事件源);
如需多 sender,显式调 EventSubscriber::sender 拿到 Sender<ServoEvent>。
@trace REQ-BAO-API-003 [level:library]
Implementations§
Source§impl EventSubscriber
impl EventSubscriber
Sourcepub fn new() -> (EventSubscriber, Receiver<ServoEvent>)
pub fn new() -> (EventSubscriber, Receiver<ServoEvent>)
构造 EventSubscriber 与对应的 Receiver。
bounded(1024):channel 缓冲 1024 个事件。servo 端 push 时若 channel
满,事件被丢弃(避免阻塞 servo ScriptThread)并记录日志。
@trace REQ-BAO-API-003 [level:library]
Sourcepub fn with_capacity(capacity: usize) -> (EventSubscriber, Receiver<ServoEvent>)
pub fn with_capacity(capacity: usize) -> (EventSubscriber, Receiver<ServoEvent>)
指定 channel 容量构造。
@trace REQ-BAO-API-003 [level:library]
Sourcepub fn sender(&self) -> Sender<ServoEvent>
pub fn sender(&self) -> Sender<ServoEvent>
获取底层 sender(供复杂场景使用,如多 sender 模式)。
@trace REQ-BAO-API-003 [level:library]
Sourcepub fn on_console_message(
&self,
target_id: impl Into<String>,
level: ConsoleLevel,
text: impl Into<String>,
url: Option<String>,
line: Option<u32>,
column: Option<u32>,
)
pub fn on_console_message( &self, target_id: impl Into<String>, level: ConsoleLevel, text: impl Into<String>, url: Option<String>, line: Option<u32>, column: Option<u32>, )
servo console 消息回调。
@trace REQ-BAO-API-003 [event:Console]
Sourcepub fn on_page_error(
&self,
target_id: impl Into<String>,
text: impl Into<String>,
url: Option<String>,
line: Option<u32>,
column: Option<u32>,
stack: Option<String>,
)
pub fn on_page_error( &self, target_id: impl Into<String>, text: impl Into<String>, url: Option<String>, line: Option<u32>, column: Option<u32>, stack: Option<String>, )
servo 页面错误(JS 异常/CSS 错误)回调。
@trace REQ-BAO-API-003 [event:PageError]
Sourcepub fn on_network_request(
&self,
target_id: impl Into<String>,
request_id: impl Into<String>,
url: impl Into<String>,
method: impl Into<String>,
headers: HashMap<String, String>,
post_data: Option<Vec<u8>>,
resource_type: impl Into<String>,
frame_id: impl Into<String>,
)
pub fn on_network_request( &self, target_id: impl Into<String>, request_id: impl Into<String>, url: impl Into<String>, method: impl Into<String>, headers: HashMap<String, String>, post_data: Option<Vec<u8>>, resource_type: impl Into<String>, frame_id: impl Into<String>, )
servo 网络请求开始回调。
@trace REQ-BAO-API-003 [event:NetworkEvent]
Sourcepub fn on_network_response(
&self,
target_id: impl Into<String>,
request_id: impl Into<String>,
url: impl Into<String>,
status: u16,
status_text: impl Into<String>,
headers: HashMap<String, String>,
mime_type: impl Into<String>,
remote_ip: Option<String>,
)
pub fn on_network_response( &self, target_id: impl Into<String>, request_id: impl Into<String>, url: impl Into<String>, status: u16, status_text: impl Into<String>, headers: HashMap<String, String>, mime_type: impl Into<String>, remote_ip: Option<String>, )
servo 网络响应到达回调。
@trace REQ-BAO-API-003 [event:NetworkEvent]
Sourcepub fn on_network_loading_finish(
&self,
target_id: impl Into<String>,
request_id: impl Into<String>,
encoded_data_length: u64,
)
pub fn on_network_loading_finish( &self, target_id: impl Into<String>, request_id: impl Into<String>, encoded_data_length: u64, )
servo 网络请求成功结束回调。
@trace REQ-BAO-API-003 [event:NetworkEvent]
Sourcepub fn on_network_loading_fail(
&self,
target_id: impl Into<String>,
request_id: impl Into<String>,
error_text: impl Into<String>,
canceled: bool,
)
pub fn on_network_loading_fail( &self, target_id: impl Into<String>, request_id: impl Into<String>, error_text: impl Into<String>, canceled: bool, )
servo 网络请求失败回调。
@trace REQ-BAO-API-003 [event:NetworkEvent]
Sourcepub fn on_dom_attribute_modified(
&self,
target_id: impl Into<String>,
node_id: i64,
name: impl Into<String>,
value: impl Into<String>,
)
pub fn on_dom_attribute_modified( &self, target_id: impl Into<String>, node_id: i64, name: impl Into<String>, value: impl Into<String>, )
servo DOM 属性变更回调。
@trace REQ-BAO-API-003 [event:DomMutation]
Sourcepub fn on_dom_character_data_modified(
&self,
target_id: impl Into<String>,
node_id: i64,
old_value: impl Into<String>,
new_value: impl Into<String>,
)
pub fn on_dom_character_data_modified( &self, target_id: impl Into<String>, node_id: i64, old_value: impl Into<String>, new_value: impl Into<String>, )
servo DOM character data 变更回调。
@trace REQ-BAO-API-003 [event:DomMutation]
Sourcepub fn on_script_parsed(
&self,
target_id: impl Into<String>,
script_id: impl Into<String>,
url: impl Into<String>,
start_line: u32,
start_column: u32,
end_line: u32,
end_column: u32,
source_map_url: Option<String>,
)
pub fn on_script_parsed( &self, target_id: impl Into<String>, script_id: impl Into<String>, url: impl Into<String>, start_line: u32, start_column: u32, end_line: u32, end_column: u32, source_map_url: Option<String>, )
servo 脚本编译完成回调。
@trace REQ-BAO-API-003 [event:SourceInfo]
servo frame 导航完成回调。
@trace REQ-BAO-API-003 [event:FrameInfo]
Sourcepub fn on_frame_started_loading(
&self,
target_id: impl Into<String>,
frame_id: impl Into<String>,
)
pub fn on_frame_started_loading( &self, target_id: impl Into<String>, frame_id: impl Into<String>, )
servo frame 开始加载回调。
@trace REQ-BAO-API-003 [event:FrameInfo]
Trait Implementations§
Source§impl Debug for EventSubscriber
impl Debug for EventSubscriber
Source§impl Default for EventSubscriber
impl Default for EventSubscriber
Source§fn default() -> EventSubscriber
fn default() -> EventSubscriber
Auto Trait Implementations§
impl Freeze for EventSubscriber
impl RefUnwindSafe for EventSubscriber
impl Send for EventSubscriber
impl Sync for EventSubscriber
impl Unpin for EventSubscriber
impl UnsafeUnpin for EventSubscriber
impl UnwindSafe for EventSubscriber
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 more