Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tracing for evaluate_script, ipc_handler and custom_protocols #1093

Merged
merged 12 commits into from
Nov 23, 2023
5 changes: 5 additions & 0 deletions .changes/tracing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---

Added tracing spans for `evaluate_script`, `ipc_handler` and `custom_protocols`.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ linux-headers = [ "webkit2gtk/v2_36" ]
[dependencies]
libc = "0.2"
log = "0.4"
tracing = "0.1"
amrbashir marked this conversation as resolved.
Show resolved Hide resolved
once_cell = "1"
serde = { version = "1.0", features = [ "derive" ] }
serde_json = "1.0"
Expand Down
11 changes: 10 additions & 1 deletion src/webview/webkitgtk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ impl InnerWebView {

// Connect before registering as recommended by the docs
manager.connect_script_message_received(None, move |_m, msg| {
let _span = tracing::info_span!("wry::ipc::handle").entered();
if let Some(js) = msg.js_value() {
if let Some(ipc_handler) = &ipc_handler {
ipc_handler(&w, js.to_string());
Expand Down Expand Up @@ -401,7 +402,10 @@ impl InnerWebView {
pending_scripts.push(js.into());
} else {
let cancellable: Option<&Cancellable> = None;
self.webview.run_javascript(js, cancellable, |_| ());
let span = SendEnteredSpan(tracing::debug_span!("wry::eval").entered());
self.webview.run_javascript(js, cancellable, move |_| {
drop(span);
});
}
Ok(())
}
Expand Down Expand Up @@ -490,3 +494,8 @@ pub fn platform_webview_version() -> Result<String> {
};
Ok(format!("{}.{}.{}", major, minor, patch))
}

// SAFETY: only use this when you are sure the span will be dropped on the same thread it was entered
struct SendEnteredSpan(tracing::span::EnteredSpan);

unsafe impl Send for SendEnteredSpan {}
11 changes: 10 additions & 1 deletion src/webview/webkitgtk/web_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,14 @@ where
.register_uri_scheme_as_secure(name);

context.register_uri_scheme(name, move |request| {
let span =
tracing::info_span!("wry::custom_protocol::handle", uri = tracing::field::Empty).entered();

if let Some(uri) = request.uri() {
let uri = uri.as_str();

span.record("uri", uri);

// FIXME: Read the body (forms post)
#[allow(unused_mut)]
let mut http_request = Request::builder().uri(uri).method("GET");
Expand Down Expand Up @@ -325,7 +330,11 @@ where
}
};

match handler(&http_request) {
let res = {
let _span = tracing::info_span!("wry::custom_protocol::call_handler").entered();
handler(&http_request)
};
match res {
Ok(http_response) => {
let buffer = http_response.body();
let input = gio::MemoryInputStream::from_bytes(&glib::Bytes::from(buffer));
Expand Down
18 changes: 16 additions & 2 deletions src/webview/webview2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_
}

if let Some(ipc_handler) = &ipc_handler {
let _span = tracing::info_span!("wry::ipc::handle").entered();
ipc_handler(&window, js);
}
}
Expand Down Expand Up @@ -514,6 +515,9 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_
webview
.add_WebResourceRequested(
&WebResourceRequestedEventHandler::create(Box::new(move |_, args| {
let span =
tracing::info_span!("wry::custom_protocol::handle", uri = tracing::field::Empty)
.entered();
if let Some(args) = args {
let webview_request = args.Request()?;
let mut request = Request::builder();
Expand Down Expand Up @@ -570,6 +574,8 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_
webview_request.Uri(&mut uri)?;
let uri = take_pwstr(uri);

span.record("uri", &uri);

if let Some(custom_protocol) = custom_protocols
.iter()
.find(|(name, _)| uri.starts_with(&format!("{scheme}://{name}.")))
Expand All @@ -589,7 +595,11 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_
Err(_) => return Err(E_FAIL.into()),
};

return match (custom_protocol.1)(&final_request) {
let res = {
let _span = tracing::info_span!("wry::custom_protocol::call_handler").entered();
(custom_protocol.1)(&final_request)
};
return match res {
Ok(sent_response) => {
let content = sent_response.body();
let status_code = sent_response.status();
Expand Down Expand Up @@ -801,9 +811,13 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_

fn execute_script(webview: &ICoreWebView2, js: String) -> windows::core::Result<()> {
unsafe {
let span = tracing::debug_span!("wry::eval").entered();
webview.ExecuteScript(
PCWSTR::from_raw(encode_wide(js).as_ptr()),
&ExecuteScriptCompletedHandler::create(Box::new(|_, _| (Ok(())))),
&ExecuteScriptCompletedHandler::create(Box::new(|_, _| {
drop(span);
Ok(())
})),
)
}
}
Expand Down
31 changes: 25 additions & 6 deletions src/webview/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ impl InnerWebView {
extern "C" fn did_receive(this: &Object, _: Sel, _: id, msg: id) {
// Safety: objc runtime calls are unsafe
unsafe {
let _span = tracing::info_span!("wry::ipc::handle").entered();

let function = this.get_ivar::<*mut c_void>("function");
if !function.is_null() {
let function =
Expand All @@ -111,6 +113,8 @@ impl InnerWebView {
// Task handler for custom protocol
extern "C" fn start_task(this: &Object, _: Sel, _webview: id, task: id) {
unsafe {
let span = tracing::info_span!("wry::custom_protocol::handle", uri = tracing::field::Empty)
.entered();
let function = this.get_ivar::<*mut c_void>("function");
if !function.is_null() {
let function = &mut *(*function
Expand All @@ -120,10 +124,13 @@ impl InnerWebView {
let request: id = msg_send![task, request];
let url: id = msg_send![request, URL];

let nsstring = {
let uri_nsstring = {
let s: id = msg_send![url, absoluteString];
NSString(s)
};
let uri = uri_nsstring.to_str();

span.record("uri", uri);

// Get request method (GET, POST, PUT etc...)
let method = {
Expand All @@ -132,9 +139,7 @@ impl InnerWebView {
};

// Prepare our HttpRequest
let mut http_request = Request::builder()
.uri(nsstring.to_str())
.method(method.to_str());
let mut http_request = Request::builder().uri(uri).method(method.to_str());

// Get body
let mut sent_form_body = Vec::new();
Expand All @@ -158,6 +163,8 @@ impl InnerWebView {
let _: () = msg_send![body_stream, close];
}

tracing::debug!("done reading body");
amrbashir marked this conversation as resolved.
Show resolved Hide resolved

// Extract all headers fields
let all_headers: id = msg_send![request, allHTTPHeaderFields];

Expand All @@ -179,7 +186,11 @@ impl InnerWebView {
// send response
match http_request.body(sent_form_body) {
Ok(final_request) => {
if let Ok(sent_response) = function(&final_request) {
let res = {
let _span = tracing::info_span!("wry::custom_protocol::call_handler").entered();
function(&final_request)
};
if let Ok(sent_response) = res {
let content = sent_response.body();
// default: application/octet-stream, but should be provided by the client
let wanted_mime = sent_response.headers().get(CONTENT_TYPE);
Expand Down Expand Up @@ -852,7 +863,15 @@ r#"Object.defineProperty(window, 'ipc', {
} else {
// Safety: objc runtime calls are unsafe
unsafe {
let _: id = msg_send![self.webview, evaluateJavaScript:NSString::new(js) completionHandler:null::<*const c_void>()];
let span = Mutex::new(Some(tracing::debug_span!("wry::eval").entered()));
let handler = block::ConcreteBlock::new(move |_object, _error| {
span.lock().unwrap().take();
});
// Put the block on the heap
let handler = handler.copy();
let completion_handler: &block::Block<(id, id), ()> = &handler;

let _: id = msg_send![self.webview, evaluateJavaScript:NSString::new(js) completionHandler:completion_handler];
}
}
Ok(())
Expand Down
Loading