use std::path::PathBuf;
use crate::{assets::*, webview::WebviewEdits};
use crate::{document::NATIVE_EVAL_JS, file_upload::FileDialogRequest};
use base64::prelude::BASE64_STANDARD;
use dioxus_core::AnyhowContext;
use dioxus_html::{SerializedFileData, SerializedFormObject};
use dioxus_interpreter_js::unified_bindings::SLEDGEHAMMER_JS;
use dioxus_interpreter_js::NATIVE_JS;
use wry::{
http::{status::StatusCode, Request, Response},
RequestAsyncResponder,
};
#[cfg(target_os = "android")]
const BASE_URI: &str = "https://dioxus.index.html/";
#[cfg(target_os = "windows")]
const BASE_URI: &str = "http://dioxus.index.html/";
#[cfg(not(any(target_os = "android", target_os = "windows")))]
const BASE_URI: &str = "dioxus://index.html/";
#[cfg(debug_assertions)]
static DEFAULT_INDEX: &str = include_str!("./assets/dev.index.html");
#[cfg(not(debug_assertions))]
static DEFAULT_INDEX: &str = include_str!("./assets/prod.index.html");
#[allow(clippy::too_many_arguments)] pub(super) fn desktop_handler(
request: Request<Vec<u8>>,
asset_handlers: AssetHandlerRegistry,
responder: RequestAsyncResponder,
edit_state: &WebviewEdits,
custom_head: Option<String>,
custom_index: Option<String>,
root_name: &str,
headless: bool,
) {
if let Some(index_bytes) = index_request(
&request,
custom_head,
custom_index,
root_name,
headless,
edit_state,
) {
return responder.respond(index_bytes);
}
let trimmed_uri = request.uri().path().trim_matches('/');
if trimmed_uri == "__events" {
return edit_state.handle_event(request, responder);
}
if trimmed_uri == "__file_dialog" {
if let Err(err) = file_dialog_responder_sync(request, responder) {
tracing::error!("Failed to handle file dialog request: {err:?}");
}
return;
}
if let Some(name) = request.uri().path().split('/').nth(1) {
if asset_handlers.has_handler(name) {
let _name = name.to_string();
return asset_handlers.handle_request(&_name, request, responder);
}
}
match dioxus_asset_resolver::native::serve_asset(request.uri().path()) {
Ok(res) => responder.respond(res),
Err(_e) => responder.respond(
Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(String::from("Failed to serve asset").into_bytes())
.unwrap(),
),
}
}
fn index_request(
request: &Request<Vec<u8>>,
custom_head: Option<String>,
custom_index: Option<String>,
root_name: &str,
headless: bool,
edit_state: &WebviewEdits,
) -> Option<Response<Vec<u8>>> {
if request.uri().path() != "/" {
return None;
}
let mut index = custom_index.unwrap_or_else(|| DEFAULT_INDEX.to_string());
if let Some(head) = custom_head {
index.insert_str(index.find("</head>").expect("Head element to exist"), &head);
}
index.insert_str(
index.find("</body>").expect("Body element to exist"),
&module_loader(root_name, headless, edit_state),
);
Response::builder()
.header("Content-Type", "text/html")
.header("Access-Control-Allow-Origin", "*")
.body(index.into())
.ok()
}
fn module_loader(root_id: &str, headless: bool, edit_state: &WebviewEdits) -> String {
let edits_path = edit_state.wry_queue.edits_path();
let expected_key = edit_state.wry_queue.required_server_key();
format!(
r#"
<script type="module">
// Bring the sledgehammer code
{SLEDGEHAMMER_JS}
// And then extend it with our native bindings
{NATIVE_JS}
// The native interpreter extends the sledgehammer interpreter with a few extra methods that we use for IPC
window.interpreter = new NativeInterpreter("{BASE_URI}", {headless});
// Wait for the page to load before sending the initialize message
window.onload = function() {{
let root_element = window.document.getElementById("{root_id}");
if (root_element != null) {{
window.interpreter.initialize(root_element);
window.interpreter.sendIpcMessage("initialize");
}}
window.interpreter.waitForRequest("{edits_path}", "{expected_key}");
}}
</script>
<script type="module">
// Include the code for eval
{NATIVE_EVAL_JS}
</script>
"#
)
}
fn file_dialog_responder_sync(
request: wry::http::Request<Vec<u8>>,
responder: wry::RequestAsyncResponder,
) -> dioxus_core::Result<()> {
let header = request
.headers()
.get("x-dioxus-data")
.context("Failed to get x-dioxus-data header")?;
let data_from_header = base64::Engine::decode(&BASE64_STANDARD, header.as_bytes())
.context("Failed to decode x-dioxus-data header from base64")?;
let file_dialog: FileDialogRequest = serde_json::from_slice(&data_from_header)
.context("Failed to parse x-dioxus-data header as JSON")?;
#[cfg(feature = "tokio_runtime")]
tokio::spawn(async move {
let file_list = file_dialog.get_file_event_async().await;
_ = respond_to_file_dialog(file_dialog, file_list, responder);
});
#[cfg(not(feature = "tokio_runtime"))]
{
let file_list = file_dialog.get_file_event_sync();
respond_to_file_dialog(file_dialog, file_list, responder)?;
}
Ok(())
}
fn respond_to_file_dialog(
mut file_dialog: FileDialogRequest,
file_list: Vec<PathBuf>,
responder: wry::RequestAsyncResponder,
) -> dioxus_core::Result<()> {
let position_of_entry = file_dialog
.values
.iter()
.position(|x| x.key == file_dialog.target_name)
.unwrap_or(file_dialog.values.len());
file_dialog
.values
.retain(|x| x.key != file_dialog.target_name);
for path in file_list {
let file = std::fs::metadata(&path).context("Failed to get file metadata")?;
file_dialog.values.insert(
position_of_entry,
SerializedFormObject {
key: file_dialog.target_name.clone(),
text: None,
file: Some(SerializedFileData {
size: file.len(),
last_modified: file
.modified()
.context("Failed to get file modified time")?
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_millis())
.unwrap_or_default() as _,
content_type: Some(
dioxus_asset_resolver::native::get_mime_from_ext(
path.extension().and_then(|s| s.to_str()),
)
.to_string(),
),
contents: Default::default(),
path,
}),
},
);
}
let response_data = serde_json::to_vec(&file_dialog)
.context("Failed to serialize FileDialogRequest to JSON")?;
responder.respond(
Response::builder()
.status(StatusCode::OK)
.header("Content-Type", "application/json")
.body(response_data)
.context("Failed to build response")?,
);
Ok(())
}