From a91052de1fc54c0b6e26aad661f9e4a53abd7e8d Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Tue, 19 Sep 2023 10:24:48 -0300 Subject: [PATCH 01/12] feat: add tracing for macOS eval --- Cargo.toml | 1 + src/webview/wkwebview/mod.rs | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index de611571a..0109a1fcf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,7 @@ linux-headers = [ "webkit2gtk/v2_36" ] [dependencies] libc = "0.2" log = "0.4" +tracing = "0.1" once_cell = "1" serde = { version = "1.0", features = [ "derive" ] } serde_json = "1.0" diff --git a/src/webview/wkwebview/mod.rs b/src/webview/wkwebview/mod.rs index ec1110f4b..99ce77221 100644 --- a/src/webview/wkwebview/mod.rs +++ b/src/webview/wkwebview/mod.rs @@ -852,7 +852,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(()) From 27abca19c1fe6e9ecd0cb7f45de5c55239d073e1 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Tue, 19 Sep 2023 10:29:54 -0300 Subject: [PATCH 02/12] custom protocol tracing macOS --- src/webview/wkwebview/mod.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/webview/wkwebview/mod.rs b/src/webview/wkwebview/mod.rs index 99ce77221..48befb558 100644 --- a/src/webview/wkwebview/mod.rs +++ b/src/webview/wkwebview/mod.rs @@ -111,6 +111,7 @@ 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); let function = this.get_ivar::<*mut c_void>("function"); if !function.is_null() { let function = &mut *(*function @@ -120,10 +121,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 = { @@ -132,9 +136,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(); @@ -158,6 +160,8 @@ impl InnerWebView { let _: () = msg_send![body_stream, close]; } + tracing::debug!("done reading body"); + // Extract all headers fields let all_headers: id = msg_send![request, allHTTPHeaderFields]; @@ -179,7 +183,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"); + 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); From 8c050689029ae1f3cb9a92d5406aaf795ae9cd9a Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Tue, 19 Sep 2023 10:51:41 -0300 Subject: [PATCH 03/12] linux impl --- src/webview/webkitgtk/mod.rs | 10 +++++++++- src/webview/webkitgtk/web_context.rs | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/webview/webkitgtk/mod.rs b/src/webview/webkitgtk/mod.rs index d3f270f21..7d5e2110e 100644 --- a/src/webview/webkitgtk/mod.rs +++ b/src/webview/webkitgtk/mod.rs @@ -401,7 +401,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(()) } @@ -490,3 +493,8 @@ pub fn platform_webview_version() -> Result { }; 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 {} diff --git a/src/webview/webkitgtk/web_context.rs b/src/webview/webkitgtk/web_context.rs index b0cca7aaf..ff5300cdf 100644 --- a/src/webview/webkitgtk/web_context.rs +++ b/src/webview/webkitgtk/web_context.rs @@ -287,9 +287,13 @@ 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); + 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"); @@ -325,7 +329,11 @@ where } }; - match handler(&http_request) { + let res = { + let _span = tracing::info_span!("wry.custom_protocol.call_handler"); + handler(&http_request) + }; + match res { Ok(http_response) => { let buffer = http_response.body(); let input = gio::MemoryInputStream::from_bytes(&glib::Bytes::from(buffer)); From 014d16de9f8c72c5fe88d2d457da03a5205a574f Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Tue, 19 Sep 2023 12:11:21 -0300 Subject: [PATCH 04/12] windows --- src/webview/webview2/mod.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/webview/webview2/mod.rs b/src/webview/webview2/mod.rs index 5d2f16f63..e4091ec48 100644 --- a/src/webview/webview2/mod.rs +++ b/src/webview/webview2/mod.rs @@ -514,6 +514,8 @@ 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); if let Some(args) = args { let webview_request = args.Request()?; let mut request = Request::builder(); @@ -570,6 +572,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}."))) @@ -589,7 +593,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"); + (custom_protocol.1)(&final_request) + }; + return match res { Ok(sent_response) => { let content = sent_response.body(); let status_code = sent_response.status(); @@ -801,9 +809,15 @@ 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(|_, _| { + Box::new(move |_, _| { + drop(span); + Ok(()) + }) + })), ) } } From b1e8cb85e8f07a2a973cef2b3e92428eb55a0bd5 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Tue, 19 Sep 2023 12:16:17 -0300 Subject: [PATCH 05/12] change separator --- src/webview/webkitgtk/mod.rs | 2 +- src/webview/webkitgtk/web_context.rs | 4 ++-- src/webview/webview2/mod.rs | 6 +++--- src/webview/wkwebview/mod.rs | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/webview/webkitgtk/mod.rs b/src/webview/webkitgtk/mod.rs index 7d5e2110e..f7e057ba0 100644 --- a/src/webview/webkitgtk/mod.rs +++ b/src/webview/webkitgtk/mod.rs @@ -401,7 +401,7 @@ impl InnerWebView { pending_scripts.push(js.into()); } else { let cancellable: Option<&Cancellable> = None; - let span = SendEnteredSpan(tracing::debug_span!("wry.eval").entered()); + let span = SendEnteredSpan(tracing::debug_span!("wry::eval").entered()); self.webview.run_javascript(js, cancellable, move |_| { drop(span); }); diff --git a/src/webview/webkitgtk/web_context.rs b/src/webview/webkitgtk/web_context.rs index ff5300cdf..9a66d2252 100644 --- a/src/webview/webkitgtk/web_context.rs +++ b/src/webview/webkitgtk/web_context.rs @@ -287,7 +287,7 @@ 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); + let span = tracing::info_span!("wry::custom_protocol::handle", uri = tracing::field::Empty); if let Some(uri) = request.uri() { let uri = uri.as_str(); @@ -330,7 +330,7 @@ where }; let res = { - let _span = tracing::info_span!("wry.custom_protocol.call_handler"); + let _span = tracing::info_span!("wry::custom_protocol::call_handler"); handler(&http_request) }; match res { diff --git a/src/webview/webview2/mod.rs b/src/webview/webview2/mod.rs index e4091ec48..2d2935dec 100644 --- a/src/webview/webview2/mod.rs +++ b/src/webview/webview2/mod.rs @@ -515,7 +515,7 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_ .add_WebResourceRequested( &WebResourceRequestedEventHandler::create(Box::new(move |_, args| { let span = - tracing::info_span!("wry.custom_protocol.handle", uri = tracing::field::Empty); + tracing::info_span!("wry::custom_protocol::handle", uri = tracing::field::Empty); if let Some(args) = args { let webview_request = args.Request()?; let mut request = Request::builder(); @@ -594,7 +594,7 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_ }; let res = { - let _span = tracing::info_span!("wry.custom_protocol.call_handler"); + let _span = tracing::info_span!("wry::custom_protocol::call_handler"); (custom_protocol.1)(&final_request) }; return match res { @@ -809,7 +809,7 @@ 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(); + let span = tracing::debug_span!("wry::eval").entered(); webview.ExecuteScript( PCWSTR::from_raw(encode_wide(js).as_ptr()), &ExecuteScriptCompletedHandler::create(Box::new(|_, _| { diff --git a/src/webview/wkwebview/mod.rs b/src/webview/wkwebview/mod.rs index 48befb558..86f069e55 100644 --- a/src/webview/wkwebview/mod.rs +++ b/src/webview/wkwebview/mod.rs @@ -111,7 +111,7 @@ 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); + let span = tracing::info_span!("wry::custom_protocol::handle", uri = tracing::field::Empty); let function = this.get_ivar::<*mut c_void>("function"); if !function.is_null() { let function = &mut *(*function @@ -184,7 +184,7 @@ impl InnerWebView { match http_request.body(sent_form_body) { Ok(final_request) => { let res = { - let _span = tracing::info_span!("wry.custom_protocol.call_handler"); + let _span = tracing::info_span!("wry::custom_protocol::call_handler"); function(&final_request) }; if let Ok(sent_response) = res { @@ -860,7 +860,7 @@ r#"Object.defineProperty(window, 'ipc', { } else { // Safety: objc runtime calls are unsafe unsafe { - let span = Mutex::new(Some(tracing::debug_span!("wry.eval").entered())); + let span = Mutex::new(Some(tracing::debug_span!("wry::eval").entered())); let handler = block::ConcreteBlock::new(move |_object, _error| { span.lock().unwrap().take(); }); From 0ac2057f78b0aafbf1ec7b8460a132c47111329c Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Tue, 19 Sep 2023 12:25:41 -0300 Subject: [PATCH 06/12] enter spans --- src/webview/webkitgtk/web_context.rs | 5 +++-- src/webview/webview2/mod.rs | 5 +++-- src/webview/wkwebview/mod.rs | 5 +++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/webview/webkitgtk/web_context.rs b/src/webview/webkitgtk/web_context.rs index 9a66d2252..dee7fbb44 100644 --- a/src/webview/webkitgtk/web_context.rs +++ b/src/webview/webkitgtk/web_context.rs @@ -287,7 +287,8 @@ 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); + 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(); @@ -330,7 +331,7 @@ where }; let res = { - let _span = tracing::info_span!("wry::custom_protocol::call_handler"); + let _span = tracing::info_span!("wry::custom_protocol::call_handler").entered(); handler(&http_request) }; match res { diff --git a/src/webview/webview2/mod.rs b/src/webview/webview2/mod.rs index 2d2935dec..4fd4241b9 100644 --- a/src/webview/webview2/mod.rs +++ b/src/webview/webview2/mod.rs @@ -515,7 +515,8 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_ .add_WebResourceRequested( &WebResourceRequestedEventHandler::create(Box::new(move |_, args| { let span = - tracing::info_span!("wry::custom_protocol::handle", uri = tracing::field::Empty); + 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(); @@ -594,7 +595,7 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_ }; let res = { - let _span = tracing::info_span!("wry::custom_protocol::call_handler"); + let _span = tracing::info_span!("wry::custom_protocol::call_handler").entered(); (custom_protocol.1)(&final_request) }; return match res { diff --git a/src/webview/wkwebview/mod.rs b/src/webview/wkwebview/mod.rs index 86f069e55..112f4ecf5 100644 --- a/src/webview/wkwebview/mod.rs +++ b/src/webview/wkwebview/mod.rs @@ -111,7 +111,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); + 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 @@ -184,7 +185,7 @@ impl InnerWebView { match http_request.body(sent_form_body) { Ok(final_request) => { let res = { - let _span = tracing::info_span!("wry::custom_protocol::call_handler"); + let _span = tracing::info_span!("wry::custom_protocol::call_handler").entered(); function(&final_request) }; if let Ok(sent_response) = res { From 4933e937c3f5e969c28c112f684f2c571bea1d75 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Tue, 19 Sep 2023 12:56:47 -0300 Subject: [PATCH 07/12] ipc message spans --- src/webview/webkitgtk/mod.rs | 1 + src/webview/webview2/mod.rs | 2 ++ src/webview/wkwebview/mod.rs | 2 ++ 3 files changed, 5 insertions(+) diff --git a/src/webview/webkitgtk/mod.rs b/src/webview/webkitgtk/mod.rs index f7e057ba0..ac808c500 100644 --- a/src/webview/webkitgtk/mod.rs +++ b/src/webview/webkitgtk/mod.rs @@ -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()); diff --git a/src/webview/webview2/mod.rs b/src/webview/webview2/mod.rs index 4fd4241b9..afb4db819 100644 --- a/src/webview/webview2/mod.rs +++ b/src/webview/webview2/mod.rs @@ -318,6 +318,8 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_ unsafe { webview.add_WebMessageReceived( &WebMessageReceivedEventHandler::create(Box::new(move |_, args| { + let _span = tracing::info_span!("wry::ipc::handle").entered(); + if let Some(args) = args { let mut js = PWSTR::null(); args.TryGetWebMessageAsString(&mut js)?; diff --git a/src/webview/wkwebview/mod.rs b/src/webview/wkwebview/mod.rs index 112f4ecf5..49ecd6bec 100644 --- a/src/webview/wkwebview/mod.rs +++ b/src/webview/wkwebview/mod.rs @@ -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 = From 52d3afbef4124360ce0b83f9b08384418eb41698 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Tue, 19 Sep 2023 13:00:50 -0300 Subject: [PATCH 08/12] fix windows --- src/webview/webview2/mod.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/webview/webview2/mod.rs b/src/webview/webview2/mod.rs index afb4db819..fde13390f 100644 --- a/src/webview/webview2/mod.rs +++ b/src/webview/webview2/mod.rs @@ -816,10 +816,8 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_ webview.ExecuteScript( PCWSTR::from_raw(encode_wide(js).as_ptr()), &ExecuteScriptCompletedHandler::create(Box::new(|_, _| { - Box::new(move |_, _| { - drop(span); - Ok(()) - }) + drop(span); + Ok(()) })), ) } From 31adbad37e2958a5e92e17aca8cb5c77ffa505b2 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Fri, 17 Nov 2023 08:10:33 -0300 Subject: [PATCH 09/12] move tracing call --- src/webview/webview2/mod.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/webview/webview2/mod.rs b/src/webview/webview2/mod.rs index fde13390f..6efc85659 100644 --- a/src/webview/webview2/mod.rs +++ b/src/webview/webview2/mod.rs @@ -318,8 +318,6 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_ unsafe { webview.add_WebMessageReceived( &WebMessageReceivedEventHandler::create(Box::new(move |_, args| { - let _span = tracing::info_span!("wry::ipc::handle").entered(); - if let Some(args) = args { let mut js = PWSTR::null(); args.TryGetWebMessageAsString(&mut js)?; @@ -360,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); } } From d55032d68a72ac91b5de5cdd57cbfd6eb6d989e0 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Wed, 22 Nov 2023 11:37:47 -0300 Subject: [PATCH 10/12] add change file --- .changes/tracing.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changes/tracing.md diff --git a/.changes/tracing.md b/.changes/tracing.md new file mode 100644 index 000000000..34a6afade --- /dev/null +++ b/.changes/tracing.md @@ -0,0 +1,5 @@ +--- +"wry": patch +--- + +Added tracing spans for `evaluate_script`, `ipc_handler` and `custom_protocols`. From f2c1543bd959e4413788e04224447798d79641d0 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Thu, 23 Nov 2023 13:47:00 -0300 Subject: [PATCH 11/12] remove leftover --- src/webview/wkwebview/mod.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/webview/wkwebview/mod.rs b/src/webview/wkwebview/mod.rs index 49ecd6bec..a6dee1609 100644 --- a/src/webview/wkwebview/mod.rs +++ b/src/webview/wkwebview/mod.rs @@ -163,8 +163,6 @@ impl InnerWebView { let _: () = msg_send![body_stream, close]; } - tracing::debug!("done reading body"); - // Extract all headers fields let all_headers: id = msg_send![request, allHTTPHeaderFields]; From 621a89ee7d350f9fcb7f390fa28f940be6fe059f Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Thu, 23 Nov 2023 13:54:09 -0300 Subject: [PATCH 12/12] add tracing feature flag --- .changes/tracing.md | 2 +- Cargo.toml | 3 ++- src/lib.rs | 2 ++ src/webview/webkitgtk/mod.rs | 19 +++++++++++++++---- src/webview/webkitgtk/web_context.rs | 3 +++ src/webview/webview2/mod.rs | 6 ++++++ src/webview/wkwebview/mod.rs | 28 +++++++++++++++++++--------- 7 files changed, 48 insertions(+), 15 deletions(-) diff --git a/.changes/tracing.md b/.changes/tracing.md index 34a6afade..27ccb5b2b 100644 --- a/.changes/tracing.md +++ b/.changes/tracing.md @@ -2,4 +2,4 @@ "wry": patch --- -Added tracing spans for `evaluate_script`, `ipc_handler` and `custom_protocols`. +Added tracing spans for `evaluate_script`, `ipc_handler` and `custom_protocols` behind the `tracing` feature flag. diff --git a/Cargo.toml b/Cargo.toml index 0109a1fcf..727b78026 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,11 +32,12 @@ devtools = [ ] transparent = [ ] fullscreen = [ ] linux-headers = [ "webkit2gtk/v2_36" ] +tracing = [ "dep:tracing" ] [dependencies] libc = "0.2" log = "0.4" -tracing = "0.1" +tracing = { version = "0.1", optional = true } once_cell = "1" serde = { version = "1.0", features = [ "derive" ] } serde_json = "1.0" diff --git a/src/lib.rs b/src/lib.rs index 375682585..f17d614ab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -66,6 +66,7 @@ //! libraries and prevent from building documentation on doc.rs fails. //! - `linux-headers`: Enables headers support of custom protocol request on Linux. Requires //! webkit2gtk v2.36 or above. +//! - `tracing`: enables [tracing] for `evaluate_script`, `ipc_handler` and `custom_protocols. //! //! [tao]: https://crates.io/crates/tao //! [`EventLoop`]: crate::application::event_loop::EventLoop @@ -73,6 +74,7 @@ //! [`WebView`]: crate::webview::WebView //! [`with_file_drop_handler`]: crate::webview::WebView::with_file_drop_handler //! [`with_custom_protocol`]: crate::webview::WebView::with_custom_protocol +//! [tracing]: https://docs.rs/tracing/latest/tracing/ #![allow(clippy::new_without_default)] #![allow(clippy::wrong_self_convention)] diff --git a/src/webview/webkitgtk/mod.rs b/src/webview/webkitgtk/mod.rs index ac808c500..dd4305338 100644 --- a/src/webview/webkitgtk/mod.rs +++ b/src/webview/webkitgtk/mod.rs @@ -89,7 +89,9 @@ impl InnerWebView { // Connect before registering as recommended by the docs manager.connect_script_message_received(None, move |_m, msg| { + #[cfg(feature = "tracing")] 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()); @@ -402,10 +404,17 @@ impl InnerWebView { pending_scripts.push(js.into()); } else { let cancellable: Option<&Cancellable> = None; - let span = SendEnteredSpan(tracing::debug_span!("wry::eval").entered()); - self.webview.run_javascript(js, cancellable, move |_| { - drop(span); - }); + #[cfg(feature = "tracing")] + { + let span = SendEnteredSpan(tracing::debug_span!("wry::eval").entered()); + self.webview.run_javascript(js, cancellable, move |_| { + drop(span); + }); + } + #[cfg(not(feature = "tracing"))] + { + self.webview.run_javascript(js, cancellable, |_| ()); + } } Ok(()) } @@ -496,6 +505,8 @@ pub fn platform_webview_version() -> Result { } // SAFETY: only use this when you are sure the span will be dropped on the same thread it was entered +#[cfg(feature = "tracing")] struct SendEnteredSpan(tracing::span::EnteredSpan); +#[cfg(feature = "tracing")] unsafe impl Send for SendEnteredSpan {} diff --git a/src/webview/webkitgtk/web_context.rs b/src/webview/webkitgtk/web_context.rs index dee7fbb44..2e4c97bb6 100644 --- a/src/webview/webkitgtk/web_context.rs +++ b/src/webview/webkitgtk/web_context.rs @@ -287,12 +287,14 @@ where .register_uri_scheme_as_secure(name); context.register_uri_scheme(name, move |request| { + #[cfg(feature = "tracing")] 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(); + #[cfg(feature = "tracing")] span.record("uri", uri); // FIXME: Read the body (forms post) @@ -331,6 +333,7 @@ where }; let res = { + #[cfg(feature = "tracing")] let _span = tracing::info_span!("wry::custom_protocol::call_handler").entered(); handler(&http_request) }; diff --git a/src/webview/webview2/mod.rs b/src/webview/webview2/mod.rs index 6efc85659..fd169dcb7 100644 --- a/src/webview/webview2/mod.rs +++ b/src/webview/webview2/mod.rs @@ -358,6 +358,7 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_ } if let Some(ipc_handler) = &ipc_handler { + #[cfg(feature = "tracing")] let _span = tracing::info_span!("wry::ipc::handle").entered(); ipc_handler(&window, js); } @@ -515,6 +516,7 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_ webview .add_WebResourceRequested( &WebResourceRequestedEventHandler::create(Box::new(move |_, args| { + #[cfg(feature = "tracing")] let span = tracing::info_span!("wry::custom_protocol::handle", uri = tracing::field::Empty) .entered(); @@ -574,6 +576,7 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_ webview_request.Uri(&mut uri)?; let uri = take_pwstr(uri); + #[cfg(feature = "tracing")] span.record("uri", &uri); if let Some(custom_protocol) = custom_protocols @@ -596,6 +599,7 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_ }; let res = { + #[cfg(feature = "tracing")] let _span = tracing::info_span!("wry::custom_protocol::call_handler").entered(); (custom_protocol.1)(&final_request) }; @@ -811,10 +815,12 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_ fn execute_script(webview: &ICoreWebView2, js: String) -> windows::core::Result<()> { unsafe { + #[cfg(feature = "tracing")] let span = tracing::debug_span!("wry::eval").entered(); webview.ExecuteScript( PCWSTR::from_raw(encode_wide(js).as_ptr()), &ExecuteScriptCompletedHandler::create(Box::new(|_, _| { + #[cfg(feature = "tracing")] drop(span); Ok(()) })), diff --git a/src/webview/wkwebview/mod.rs b/src/webview/wkwebview/mod.rs index a6dee1609..cda994019 100644 --- a/src/webview/wkwebview/mod.rs +++ b/src/webview/wkwebview/mod.rs @@ -93,6 +93,7 @@ impl InnerWebView { extern "C" fn did_receive(this: &Object, _: Sel, _: id, msg: id) { // Safety: objc runtime calls are unsafe unsafe { + #[cfg(feature = "tracing")] let _span = tracing::info_span!("wry::ipc::handle").entered(); let function = this.get_ivar::<*mut c_void>("function"); @@ -113,6 +114,7 @@ impl InnerWebView { // Task handler for custom protocol extern "C" fn start_task(this: &Object, _: Sel, _webview: id, task: id) { unsafe { + #[cfg(feature = "tracing")] let span = tracing::info_span!("wry::custom_protocol::handle", uri = tracing::field::Empty) .entered(); let function = this.get_ivar::<*mut c_void>("function"); @@ -130,6 +132,7 @@ impl InnerWebView { }; let uri = uri_nsstring.to_str(); + #[cfg(feature = "tracing")] span.record("uri", uri); // Get request method (GET, POST, PUT etc...) @@ -185,6 +188,7 @@ impl InnerWebView { match http_request.body(sent_form_body) { Ok(final_request) => { let res = { + #[cfg(feature = "tracing")] let _span = tracing::info_span!("wry::custom_protocol::call_handler").entered(); function(&final_request) }; @@ -861,15 +865,21 @@ r#"Object.defineProperty(window, 'ipc', { } else { // Safety: objc runtime calls are unsafe unsafe { - 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]; + #[cfg(feature = "tracing")] + { + 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]; + } + #[cfg(not(feature = "tracing"))] + let _: id = + msg_send![self.webview, evaluateJavaScript:NSString::new(js) completionHandler:nil]; } } Ok(())