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

Unify custom porotocol across Android/iOS #546

Merged
merged 3 commits into from
Apr 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
#[build]
#target = "aarch64-linux-android"
[target.x86_64-apple-darwin]
rustflags = ["-C", "link-arg=-mmacosx-version-min=10.12"]
5 changes: 5 additions & 0 deletions .changes/android-url.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---

Update how android handles url
18 changes: 14 additions & 4 deletions src/webview/android/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{ffi::c_void, ptr::null_mut, rc::Rc, sync::RwLock};
use std::{collections::HashSet, ffi::c_void, ptr::null_mut, rc::Rc, sync::RwLock};

use crate::{application::window::Window, Result};

Expand Down Expand Up @@ -57,21 +57,31 @@ impl InnerWebView {
// )?;
let WebViewAttributes {
url,
custom_protocols,
initialization_scripts,
ipc_handler,
..
} = self.attributes;

// todo
// ipc too?
if let Some(i) = ipc_handler {
let i = UnsafeIpc(Box::into_raw(Box::new(i)) as *mut _);
let mut ipc = IPC.write().unwrap();
*ipc = i;
}

if let Some(u) = url {
let url = env.new_string(u)?;
let mut url_string = String::from(u.as_str());
let schemes = custom_protocols
.into_iter()
.map(|(s, _)| s)
.collect::<HashSet<_>>();
let name = u.scheme();
if schemes.contains(name) {
url_string = u
.as_str()
.replace(&format!("{}://", name), "https://tauri.wry/")
}
let url = env.new_string(url_string)?;
env.call_method(jobject, "loadUrl", "(Ljava/lang/String;)V", &[url.into()])?;
}

Expand Down
16 changes: 16 additions & 0 deletions src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ pub struct WebViewAttributes {
/// - macOS: `http://localhost`
/// - Linux: `http://localhost`
/// - Windows: `null`
/// - Android: Not supported
/// - iOS: Not supported
pub html: Option<String>,
/// Initialize javascript code when loading new pages. When webview load a new page, this
/// initialization code will be executed. It is guaranteed that code is executed before
Expand All @@ -100,6 +102,12 @@ pub struct WebViewAttributes {
/// - Linux: Though it's same as macOS, there's a [bug] that Origin header in the request will be
/// empty. So the only way to pass the server is setting `Access-Control-Allow-Origin: *`.
/// - Windows: `https://<scheme_name>.<path>` (so it will be `https://wry.examples` in `custom_protocol` example)
/// - Android: Custom protocol on Android is fixed to `https://tauri.wry/` due to its design and
/// our approach to use it. On Android, We only handle the scheme name and ignore the closure. So
/// when you load the url like `wry://assets/index.html`, it will become
/// `https://tauri.wry/assets/index.html`. Android has `assets` and `resource` path finder to
/// locate your files in those directories. For more information, see [Loading in-app content](https://developer.android.com/guide/webapps/load-local-content) page.
/// - iOS: Same as macOS. To get the path of your assets, you can call [`CFBundle::resources_path`](https://docs.rs/core-foundation/latest/core_foundation/bundle/struct.CFBundle.html#method.resources_path). So url like `wry://assets/index.html` could get the html file in assets directory.
///
/// [bug]: https://bugs.webkit.org/show_bug.cgi?id=229034
pub custom_protocols: Vec<(String, Box<dyn Fn(&HttpRequest) -> Result<HttpResponse>>)>,
Expand Down Expand Up @@ -222,6 +230,12 @@ impl<'a> WebViewBuilder<'a> {
/// - Linux: Though it's same as macOS, there's a [bug] that Origin header in the request will be
/// empty. So the only way to pass the server is setting `Access-Control-Allow-Origin: *`.
/// - Windows: `https://<scheme_name>.<path>` (so it will be `https://wry.examples` in `custom_protocol` example)
/// - Android: Custom protocol on Android is fixed to `https://tauri.wry/` due to its design and
/// our approach to use it. On Android, We only handle the scheme name and ignore the closure. So
/// when you load the url like `wry://assets/index.html`, it will become
/// `https://tauri.wry/assets/index.html`. Android has `assets` and `resource` path finder to
/// locate your files in those directories. For more information, see [Loading in-app content](https://developer.android.com/guide/webapps/load-local-content) page.
/// - iOS: Same as macOS. To get the path of your assets, you can call [`CFBundle::resources_path`](https://docs.rs/core-foundation/latest/core_foundation/bundle/struct.CFBundle.html#method.resources_path). So url like `wry://assets/index.html` could get the html file in assets directory.
///
/// [bug]: https://bugs.webkit.org/show_bug.cgi?id=229034
#[cfg(feature = "protocol")]
Expand Down Expand Up @@ -283,6 +297,8 @@ impl<'a> WebViewBuilder<'a> {
/// - macOS: `http://localhost`
/// - Linux: `http://localhost`
/// - Windows: `null`
/// - Android: Not supported
/// - iOS: Not supported
pub fn with_html(mut self, html: impl Into<String>) -> Result<Self> {
self.webview.html = Some(html.into());
Ok(self)
Expand Down