Skip to content

Commit

Permalink
refactor!: remove internal url parsing and data: url support (#1169)
Browse files Browse the repository at this point in the history
* refactor!: remove internal url parsing and `data:` url support

* fix doctests

* clippy and fix linux

* fix android

* fix android
  • Loading branch information
amrbashir authored Feb 14, 2024
1 parent e50ce47 commit 2ff8d9d
Show file tree
Hide file tree
Showing 21 changed files with 147 additions and 158 deletions.
5 changes: 5 additions & 0 deletions .changes/data-url.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": "minor"
---

**Breaking change**: Removed `data:` url support, as its native support in Windows and macOS are buggy and unreliable, use `Webview::with_html` instead.
11 changes: 11 additions & 0 deletions .changes/url.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"wry": "minor"
---

**Breaking change**: Removed internal url parsing which had a few side-effects such as encoded url content, now it is up to the user to pass a valid URL as a string. This also came with a few breaking changes:

- Removed `Url` struct re-export
- Removed `Error::UrlError` variant.
- Changed `WebviewAttributes::url` field type to `String`.
- Changed `WebviewBuilder::with_url` and `WebviewBuilder::with_url_and_headers` return type to `WebviewBuilder` instead of `Result<WebviewBuilder>`.
- Changed `Webview::url` getter to return a `String` instead of `Url`.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ once_cell = "1"
serde = { version = "1.0", features = [ "derive" ] }
serde_json = "1.0"
thiserror = "1.0"
url = "2.5"
http = "0.2"
raw-window-handle = { version = "0.6", features = [ "std" ] }

Expand Down
2 changes: 1 addition & 1 deletion examples/async_custom_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn main() -> wry::Result<()> {
}
})
// tell the webview to load the custom protocol
.with_url("wry://localhost")?
.with_url("wry://localhost")
.build()?;

event_loop.run(move |event, _, control_flow| {
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn main() -> wry::Result<()> {
}
})
// tell the webview to load the custom protocol
.with_url("wry://localhost")?
.with_url("wry://localhost")
.build()?;

event_loop.run(move |event, _, control_flow| {
Expand Down
78 changes: 39 additions & 39 deletions examples/custom_titlebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,43 @@ use wry::WebViewBuilder;

#[derive(Debug)]
enum HitTestResult {
CLIENT,
LEFT,
RIGHT,
TOP,
BOTTOM,
TOPLEFT,
TOPRIGHT,
BOTTOMLEFT,
BOTTOMRIGHT,
NOWHERE,
Client,
Left,
Right,
Top,
Bottom,
TopLeft,
TopRight,
BottomLeft,
BottomRight,
NoWhere,
}

impl HitTestResult {
fn drag_resize_window(&self, window: &Window) {
let _ = window.drag_resize_window(match self {
HitTestResult::LEFT => ResizeDirection::West,
HitTestResult::RIGHT => ResizeDirection::East,
HitTestResult::TOP => ResizeDirection::North,
HitTestResult::BOTTOM => ResizeDirection::South,
HitTestResult::TOPLEFT => ResizeDirection::NorthWest,
HitTestResult::TOPRIGHT => ResizeDirection::NorthEast,
HitTestResult::BOTTOMLEFT => ResizeDirection::SouthWest,
HitTestResult::BOTTOMRIGHT => ResizeDirection::SouthEast,
HitTestResult::Left => ResizeDirection::West,
HitTestResult::Right => ResizeDirection::East,
HitTestResult::Top => ResizeDirection::North,
HitTestResult::Bottom => ResizeDirection::South,
HitTestResult::TopLeft => ResizeDirection::NorthWest,
HitTestResult::TopRight => ResizeDirection::NorthEast,
HitTestResult::BottomLeft => ResizeDirection::SouthWest,
HitTestResult::BottomRight => ResizeDirection::SouthEast,
_ => unreachable!(),
});
}

fn change_cursor(&self, window: &Window) {
let _ = window.set_cursor_icon(match self {
HitTestResult::LEFT => CursorIcon::WResize,
HitTestResult::RIGHT => CursorIcon::EResize,
HitTestResult::TOP => CursorIcon::NResize,
HitTestResult::BOTTOM => CursorIcon::SResize,
HitTestResult::TOPLEFT => CursorIcon::NwResize,
HitTestResult::TOPRIGHT => CursorIcon::NeResize,
HitTestResult::BOTTOMLEFT => CursorIcon::SwResize,
HitTestResult::BOTTOMRIGHT => CursorIcon::SeResize,
window.set_cursor_icon(match self {
HitTestResult::Left => CursorIcon::WResize,
HitTestResult::Right => CursorIcon::EResize,
HitTestResult::Top => CursorIcon::NResize,
HitTestResult::Bottom => CursorIcon::SResize,
HitTestResult::TopLeft => CursorIcon::NwResize,
HitTestResult::TopRight => CursorIcon::NeResize,
HitTestResult::BottomLeft => CursorIcon::SwResize,
HitTestResult::BottomRight => CursorIcon::SeResize,
_ => CursorIcon::Default,
});
}
Expand Down Expand Up @@ -82,16 +82,16 @@ fn hit_test(window_size: PhysicalSize<u32>, x: i32, y: i32, scale: f64) -> HitTe
| (BOTTOM * (if y >= (bottom - inset) { 1 } else { 0 }));

match result {
CLIENT => HitTestResult::CLIENT,
LEFT => HitTestResult::LEFT,
RIGHT => HitTestResult::RIGHT,
TOP => HitTestResult::TOP,
BOTTOM => HitTestResult::BOTTOM,
TOPLEFT => HitTestResult::TOPLEFT,
TOPRIGHT => HitTestResult::TOPRIGHT,
BOTTOMLEFT => HitTestResult::BOTTOMLEFT,
BOTTOMRIGHT => HitTestResult::BOTTOMRIGHT,
_ => HitTestResult::NOWHERE,
CLIENT => HitTestResult::Client,
LEFT => HitTestResult::Left,
RIGHT => HitTestResult::Right,
TOP => HitTestResult::Top,
BOTTOM => HitTestResult::Bottom,
TOPLEFT => HitTestResult::TopLeft,
TOPRIGHT => HitTestResult::TopRight,
BOTTOMLEFT => HitTestResult::BottomLeft,
BOTTOMRIGHT => HitTestResult::BottomRight,
_ => HitTestResult::NoWhere,
}
}

Expand Down Expand Up @@ -259,7 +259,7 @@ fn main() -> wry::Result<()> {

let mut webview = Some(
builder
.with_html(HTML)?
.with_html(HTML)
.with_ipc_handler(handler)
.with_accept_first_mouse(true)
.build()?,
Expand All @@ -286,7 +286,7 @@ fn main() -> wry::Result<()> {
UserEvent::MouseDown(x, y) => {
let res = hit_test(window.inner_size(), x, y, window.scale_factor());
match res {
HitTestResult::CLIENT | HitTestResult::NOWHERE => {}
HitTestResult::Client | HitTestResult::NoWhere => {}
_ => res.drag_resize_window(&window),
}
}
Expand Down
8 changes: 4 additions & 4 deletions examples/gtk_multiwebview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn main() -> wry::Result<()> {
width: size.width / 2,
height: size.height / 2,
})
.with_url("https://tauri.app")?
.with_url("https://tauri.app")
.build()?;
let webview2 = create_webview_builder()
.with_bounds(Rect {
Expand All @@ -69,7 +69,7 @@ fn main() -> wry::Result<()> {
width: size.width / 2,
height: size.height / 2,
})
.with_url("https://github.com/tauri-apps/wry")?
.with_url("https://github.com/tauri-apps/wry")
.build()?;
let webview3 = create_webview_builder()
.with_bounds(Rect {
Expand All @@ -78,7 +78,7 @@ fn main() -> wry::Result<()> {
width: size.width / 2,
height: size.height / 2,
})
.with_url("https://twitter.com/TauriApps")?
.with_url("https://twitter.com/TauriApps")
.build()?;
let webview4 = create_webview_builder()
.with_bounds(Rect {
Expand All @@ -87,7 +87,7 @@ fn main() -> wry::Result<()> {
width: size.width / 2,
height: size.height / 2,
})
.with_url("https://google.com")?
.with_url("https://google.com")
.build()?;

event_loop.run(move |event, _, control_flow| {
Expand Down
8 changes: 4 additions & 4 deletions examples/multiwebview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn main() -> wry::Result<()> {
width: size.width / 2,
height: size.height / 2,
})
.with_url("https://tauri.app")?
.with_url("https://tauri.app")
.build()?;
let webview2 = WebViewBuilder::new_as_child(&window)
.with_bounds(Rect {
Expand All @@ -58,7 +58,7 @@ fn main() -> wry::Result<()> {
width: size.width / 2,
height: size.height / 2,
})
.with_url("https://github.com/tauri-apps/wry")?
.with_url("https://github.com/tauri-apps/wry")
.build()?;
let webview3 = WebViewBuilder::new_as_child(&window)
.with_bounds(Rect {
Expand All @@ -67,7 +67,7 @@ fn main() -> wry::Result<()> {
width: size.width / 2,
height: size.height / 2,
})
.with_url("https://twitter.com/TauriApps")?
.with_url("https://twitter.com/TauriApps")
.build()?;
let webview4 = WebViewBuilder::new_as_child(&window)
.with_bounds(Rect {
Expand All @@ -76,7 +76,7 @@ fn main() -> wry::Result<()> {
width: size.width / 2,
height: size.height / 2,
})
.with_url("https://google.com")?
.with_url("https://google.com")
.build()?;

event_loop
Expand Down
1 change: 0 additions & 1 deletion examples/multiwindow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ fn create_new_window(
<input oninput="window.ipc.postMessage(`change-title:${this.value}`)" />
"#,
)
.unwrap()
.with_ipc_handler(handler)
.build()
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion examples/reparent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn main() -> wry::Result<()> {
WebViewBuilder::new_gtk(vbox)
};

let webview = builder.with_url("https://tauri.app")?.build()?;
let webview = builder.with_url("https://tauri.app").build()?;

let mut webview_container = window.id();

Expand Down
6 changes: 5 additions & 1 deletion examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ fn main() -> wry::Result<()> {
WebViewBuilder::new_gtk(vbox)
};

let _webview = builder.with_url("https://tauri.app")?.build()?;
let _webview = builder
.with_html(
r#"<html><body>special character incoming thai characters incoming สระ</body></html>"#,
)
.build()?;

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
Expand Down
2 changes: 1 addition & 1 deletion examples/transparent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn main() -> wry::Result<()> {
};
</script>
</html>"#,
)?
)
.build()?;

event_loop.run(move |event, _, control_flow| {
Expand Down
1 change: 0 additions & 1 deletion examples/wgpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ fn fs_main() -> @location(0) vec4<f32> {
</script>
</html>"#,
)
.unwrap()
.build()
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion examples/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn main() -> wry::Result<()> {

#[allow(unused_mut)]
let mut builder = WebViewBuilder::new(&window);
let _webview = builder.with_url("https://tauri.app")?.build()?;
let _webview = builder.with_url("https://tauri.app").build()?;

event_loop
.run(move |event, evl| {
Expand Down
33 changes: 15 additions & 18 deletions src/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use std::{
collections::HashMap,
sync::{atomic::AtomicI32, mpsc::channel, Mutex},
};
use url::Url;

pub(crate) mod binding;
mod main_pipe;
Expand Down Expand Up @@ -162,19 +161,18 @@ impl InnerWebView {
https_scheme,
} = pl_attrs;

let custom_protocol_scheme = if https_scheme { "https" } else { "http" };

let url = if let Some(u) = url {
let mut url_string = String::from(u.as_str());
let name = u.scheme();
let is_custom_protocol = custom_protocols.iter().any(|(n, _)| n == name);
if is_custom_protocol {
url_string = u.as_str().replace(
&format!("{name}://"),
&format!("{custom_protocol_scheme}://{name}."),
)
let scheme = if https_scheme { "https" } else { "http" };

let url = if let Some(mut url) = url {
if let Some(pos) = url.find("://") {
let name = &url[..pos];
let is_custom_protocol = custom_protocols.iter().any(|(n, _)| n == name);
if is_custom_protocol {
url = url.replace(&format!("{name}://"), &format!("{scheme}://{name}."))
}
}
Some(url_string)

Some(url)
} else {
None
};
Expand Down Expand Up @@ -205,13 +203,13 @@ impl InnerWebView {
request
.uri()
.to_string()
.starts_with(&format!("{custom_protocol_scheme}://{}.", name))
.starts_with(&format!("{scheme}://{}.", name))
}) {
*request.uri_mut() = request
.uri()
.to_string()
.replace(
&format!("{custom_protocol_scheme}://{}.", custom_protocol.0),
&format!("{scheme}://{}.", custom_protocol.0),
&format!("{}://", custom_protocol.0),
)
.parse()
Expand Down Expand Up @@ -303,11 +301,10 @@ impl InnerWebView {

pub fn print(&self) {}

pub fn url(&self) -> Url {
pub fn url(&self) -> String {
let (tx, rx) = bounded(1);
MainPipe::send(WebViewMessage::GetUrl(tx));
let uri = rx.recv().unwrap();
Url::parse(uri.as_str()).unwrap()
rx.recv().unwrap()
}

pub fn eval(&self, js: &str, callback: Option<impl Fn(String) + Send + 'static>) -> Result<()> {
Expand Down
2 changes: 0 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ pub enum Error {
MessageSender,
#[error(transparent)]
Json(#[from] serde_json::Error),
#[error(transparent)]
UrlError(#[from] url::ParseError),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[cfg(target_os = "windows")]
Expand Down
Loading

0 comments on commit 2ff8d9d

Please sign in to comment.