Skip to content

Commit

Permalink
feat(android): implement custom protocol (#656)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored Aug 13, 2022
1 parent 74391e0 commit dc68289
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changes/android-custom-protocol.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---

Implement custom protocol on Android.
5 changes: 0 additions & 5 deletions .changes/android-url

This file was deleted.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ serde = { version = "1.0", features = [ "derive" ] }
serde_json = "1.0"
thiserror = "1.0"
url = "2.2"
tao = { version = "0.13", default-features = false, features = [ "serde" ] }
tao = { git = "https://github.com/tauri-apps/tao", branch = "dev", default-features = false, features = [ "serde" ] }
http = "0.2.8"

[dev-dependencies]
Expand Down
2 changes: 2 additions & 0 deletions src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

#![allow(unused_imports, dead_code)]

// custom wry types
mod request;
mod response;
Expand Down
2 changes: 1 addition & 1 deletion src/http/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl ResponseParts {
/// Creates a new default instance of `ResponseParts`
fn new() -> ResponseParts {
ResponseParts {
status: StatusCode::default(),
status: StatusCode::OK,
version: Version::default(),
headers: HeaderMap::default(),
mimetype: None,
Expand Down
49 changes: 44 additions & 5 deletions src/webview/android/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use super::{WebContext, WebViewAttributes};
use crate::{application::window::Window, Result};
use std::rc::Rc;
use crate::{
application::window::Window,
http::{method::Method, Request as HttpRequest, RequestParts},
Result,
};
use std::{rc::Rc, str::FromStr};
use tao::platform::android::ndk_glue::*;

pub struct InnerWebView {
Expand All @@ -18,29 +22,64 @@ impl InnerWebView {
initialization_scripts,
ipc_handler,
devtools,
custom_protocols,
..
} = attributes;

if let Some(u) = url {
let mut url_string = String::from(u.as_str());
let name = u.scheme();
let schemes = attributes
.custom_protocols
let schemes = custom_protocols
.iter()
.map(|(name, _)| name.as_str())
.collect::<Vec<_>>();
if schemes.contains(&name) {
url_string = u
.as_str()
.replace(&format!("{}://", name), "https://tauri.mobile/")
.replace(&format!("{}://", name), &format!("https://{}.", name))
}

MainPipe::send(WebViewMessage::CreateWebView(
url_string,
initialization_scripts,
devtools,
));
}

REQUEST_HANDLER.get_or_init(move || {
UnsafeRequestHandler::new(Box::new(move |request: WebResourceRequest| {
if let Some(custom_protocol) = custom_protocols
.iter()
.find(|(name, _)| request.url.starts_with(&format!("https://{}.", name)))
{
let path = request.url.replace(
&format!("https://{}.", custom_protocol.0),
&format!("{}://", custom_protocol.0),
);

let request = HttpRequest {
head: RequestParts {
method: Method::from_str(&request.method).unwrap_or(Method::GET),
uri: path,
headers: request.headers,
},
body: Vec::new(),
};

if let Ok(response) = (custom_protocol.1)(&request) {
return Some(WebResourceResponse {
status: response.head.status,
headers: response.head.headers,
mimetype: response.head.mimetype,
body: response.body,
});
}
}

None
}))
});

let w = window.clone();
if let Some(i) = ipc_handler {
IPC.get_or_init(move || UnsafeIpc::new(Box::into_raw(Box::new(i)) as *mut _, w));
Expand Down

0 comments on commit dc68289

Please sign in to comment.