Skip to content

Commit

Permalink
Goodbye navigaation started handler
Browse files Browse the repository at this point in the history
  • Loading branch information
jhutchins committed Jul 4, 2023
1 parent b3070da commit f51bb6b
Show file tree
Hide file tree
Showing 8 changed files with 4 additions and 98 deletions.
1 change: 0 additions & 1 deletion .changes/on-load-handler.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
"wry": "minor"
---

Add `WebViewBuilder::with_on_navigation_started_handler` for providing a callback when a page starts to navigate to a new page.
Add `WebViewBuilder::with_on_page_loading_handler` for providing a callback when a new page starts to load.
Add `WebViewBuilder::with_on_page_loaded_handler` for providing a callback when a page load is complete.
2 changes: 1 addition & 1 deletion src/webview/android/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ pub unsafe fn assetLoaderDomain(env: JNIEnv, _: JClass) -> jstring {
}

#[allow(non_snake_case)]
pub unsafe fn onPageNavigating(env: JNIEnv, _: JClass, url: JString) {
pub unsafe fn onNavigationStarted(env: JNIEnv, _: JClass, url: JString) {
match env.get_string(url) {
Ok(url) => {
let url = url.to_string_lossy().to_string();
Expand Down
5 changes: 0 additions & 5 deletions src/webview/android/kotlin/RustWebViewClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ class RustWebViewClient(context: Context): WebViewClient() {
return shouldOverride(request.url.toString())
}

override fun onLoadResource(view: WebView, url: String): Unit {
return onPageNavigating(url)
}

override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?): Unit {
return onPageLoading(url)
}
Expand All @@ -56,7 +52,6 @@ class RustWebViewClient(context: Context): WebViewClient() {
private external fun withAssetLoader(): Boolean
private external fun handleRequest(request: WebResourceRequest): WebResourceResponse?
private external fun shouldOverride(url: String): Boolean
private external fun onPageNavigating(url: String)
private external fun onPageLoading(url: String)
private external fun onPageLoaded(url: String)

Expand Down
21 changes: 0 additions & 21 deletions src/webview/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,6 @@ macro_rules! android_binding {
[JString],
jboolean
);
android_fn!(
$domain,
$package,
RustWebViewClient,
onPageNavigating,
[JString]
);
android_fn!(
$domain,
$package,
Expand Down Expand Up @@ -128,7 +121,6 @@ pub static TITLE_CHANGE_HANDLER: OnceCell<UnsafeTitleHandler> = OnceCell::new();
pub static WITH_ASSET_LOADER: OnceCell<bool> = OnceCell::new();
pub static ASSET_LOADER_DOMAIN: OnceCell<String> = OnceCell::new();
pub static URL_LOADING_OVERRIDE: OnceCell<UnsafeUrlLoadingOverride> = OnceCell::new();
pub static ON_NAVIGATING_HANDLER: OnceCell<UnsafeOnPageNavigatingHandler> = OnceCell::new();
pub static ON_LOADING_HANDLER: OnceCell<UnsafeOnPageLoadingHandler> = OnceCell::new();
pub static ON_LOADED_HANDLER: OnceCell<UnsafeOnPageLoadedHandler> = OnceCell::new();

Expand Down Expand Up @@ -170,15 +162,6 @@ impl UnsafeUrlLoadingOverride {
unsafe impl Send for UnsafeUrlLoadingOverride {}
unsafe impl Sync for UnsafeUrlLoadingOverride {}

pub struct UnsafeOnPageNavigatingHandler(Box<dyn Fn(String)>);
impl UnsafeOnPageNavigatingHandler {
pub fn new(f: Box<dyn Fn(String)>) -> Self {
Self(f)
}
}
unsafe impl Send for UnsafeOnPageNavigatingHandler {}
unsafe impl Sync for UnsafeOnPageNavigatingHandler {}

pub struct UnsafeOnPageLoadingHandler(Box<dyn Fn(String)>);
impl UnsafeOnPageLoadingHandler {
pub fn new(f: Box<dyn Fn(String)>) -> Self {
Expand Down Expand Up @@ -387,10 +370,6 @@ impl InnerWebView {
URL_LOADING_OVERRIDE.get_or_init(move || UnsafeUrlLoadingOverride::new(i));
}

if let Some(h) = attributes.on_navigation_started_handler {
ON_NAVIGATING_HANDLER.get_or_init(move || UnsafeOnPageNavigatingHandler::new(h));
}

if let Some(h) = attributes.on_page_loading_handler {
ON_LOADING_HANDLER.get_or_init(move || UnsafeOnPageLoadingHandler::new(h));
}
Expand Down
15 changes: 0 additions & 15 deletions src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,6 @@ pub struct WebViewAttributes {
/// Whether all media can be played without user interaction.
pub autoplay: bool,

/// Set a handler closure to process changes to navigation. This is called after the navigation
/// handler.
pub on_navigation_started_handler: Option<Box<dyn Fn(String)>>,

/// Set a handler closure to process to start of loading a new page.
pub on_page_loading_handler: Option<Box<dyn Fn(String)>>,

Expand Down Expand Up @@ -273,7 +269,6 @@ impl Default for WebViewAttributes {
document_title_changed_handler: None,
incognito: false,
autoplay: true,
on_navigation_started_handler: None,
on_page_loading_handler: None,
on_page_loaded_handler: None,
}
Expand Down Expand Up @@ -642,16 +637,6 @@ impl<'a> WebViewBuilder<'a> {
self
}

/// Set a handler to process when navigation to new page begins.
///
/// The handler will be called when the webview begins requesting context for a new URL.
/// This handler is called after the navigation_handler has been called to determine whether
/// nagivdation should be allow.
pub fn with_on_navigation_started_handler(mut self, handler: impl Fn(String) + 'static) -> Self {
self.webview.on_navigation_started_handler = Some(Box::new(handler));
self
}

/// Set a handler to process when a new page start loading.
///
/// The handler will be called when the webview begins loading the content for the new page.
Expand Down
15 changes: 3 additions & 12 deletions src/webview/webkitgtk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,26 +136,17 @@ impl InnerWebView {
});
}

let on_navigation_started_handler = attributes.on_navigation_started_handler.take();
let on_page_loading_handler = attributes.on_page_loading_handler.take();
let on_page_loaded_handler = attributes.on_page_loaded_handler.take();
if on_navigation_started_handler.is_some()
|| on_page_loading_handler.is_some()
|| on_page_loaded_handler.is_some()
{
if on_page_loading_handler.is_some() || on_page_loaded_handler.is_some() {
webview.connect_load_changed(move |webview, load_event| match load_event {
LoadEvent::Started => {
if let Some(f) = on_navigation_started_handler {
f(webview.uri().unwrap().to_string());
}
}
LoadEvent::Committed => {
if let Some(f) = on_page_loading_handler {
if let Some(ref f) = on_page_loading_handler {
f(webview.uri().unwrap().to_string());
}
}
LoadEvent::Finished => {
if let Some(f) = on_page_loaded_handler {
if let Some(ref f) = on_page_loaded_handler {
f(webview.uri().unwrap().to_string());
}
}
Expand Down
16 changes: 0 additions & 16 deletions src/webview/webview2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,22 +311,6 @@ impl InnerWebView {
}
}

if let Some(on_navigation_started_handler) = attributes.on_navigation_started_handler {
unsafe {
webview
.add_NavigationStarting(
&NavigationStartingEventHandler::create(Box::new(move |webview, _| {
if let Some(webview) = webview {
on_navigation_started_handler(url_from_webview(&webview))
}
Ok(())
})),
&mut token,
)
.map_err(webview2_com::Error::WindowsError)?;
}
}

if let Some(on_page_loading_handler) = attributes.on_page_loading_handler {
unsafe {
webview
Expand Down
27 changes: 0 additions & 27 deletions src/webview/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,17 +528,6 @@ impl InnerWebView {
}
}

extern "C" fn did_start_navigation(this: &Object, _: Sel, _webview: id, _navigation: id) {
unsafe {
// Call on_load_handler
let on_navigating = this.get_ivar::<*mut c_void>("on_page_navigating_function");
if !on_navigating.is_null() {
let on_navigating = &mut *(*on_navigating as *mut Box<dyn Fn()>);
on_navigating();
}
}
}

extern "C" fn did_commit_navigation(this: &Object, _: Sel, webview: id, _navigation: id) {
unsafe {
// Call on_load_handler
Expand Down Expand Up @@ -579,7 +568,6 @@ impl InnerWebView {
Some(mut cls) => {
cls.add_ivar::<*mut c_void>("pending_scripts");
cls.add_ivar::<*mut c_void>("navigation_policy_function");
cls.add_ivar::<*mut c_void>("on_page_navigating_function");
cls.add_ivar::<*mut c_void>("on_page_loading_function");
cls.add_ivar::<*mut c_void>("on_page_loaded_function");
cls.add_ivar::<*mut c_void>("HasDownloadHandler");
Expand All @@ -591,10 +579,6 @@ impl InnerWebView {
sel!(webView:decidePolicyForNavigationResponse:decisionHandler:),
navigation_policy_response as extern "C" fn(&Object, Sel, id, id, id),
);
cls.add_method(
sel!(webView:didStartProvisionalNavigation:),
did_start_navigation as extern "C" fn(&Object, Sel, id, id),
);
cls.add_method(
sel!(webView:didCommitNavigation:),
did_commit_navigation as extern "C" fn(&Object, Sel, id, id),
Expand All @@ -621,7 +605,6 @@ impl InnerWebView {
.is_some()
|| attributes.new_window_req_handler.is_some()
|| attributes.download_started_handler.is_some()
|| attributes.on_navigation_started_handler.is_some()
|| attributes.on_page_loading_handler.is_some()
|| attributes.on_page_loaded_handler.is_some()
{
Expand All @@ -647,16 +630,6 @@ impl InnerWebView {
function_ptr as *mut _ as *mut c_void,
);

if let Some(on_navigation_started_handler) = attributes.on_navigation_started_handler {
let on_navigation_started_handler = Box::into_raw(Box::new(Box::new(move || {
on_navigation_started_handler(url_from_webview(webview));
}) as Box<dyn Fn()>));
(*navigation_policy_handler).set_ivar(
"on_page_navigating_function",
on_navigation_started_handler as *mut _ as *mut c_void,
);
}

if let Some(on_page_loading_handler) = attributes.on_page_loading_handler {
let on_page_loading_handler = Box::into_raw(Box::new(Box::new(move || {
on_page_loading_handler(url_from_webview(webview));
Expand Down

0 comments on commit f51bb6b

Please sign in to comment.