use std::sync::Arc;
use axum::extract::{Path, State};
use axum::Json;
use serde::Deserialize;
use tauri::Runtime;
use crate::server::response::{WebDriverResponse, WebDriverResult};
use crate::server::AppState;
use crate::webdriver::ActionState;
#[derive(Debug, Deserialize)]
pub struct NavigateRequest {
pub url: String,
}
pub async fn navigate<R: Runtime + 'static>(
State(state): State<Arc<AppState<R>>>,
Path(session_id): Path<String>,
Json(request): Json<NavigateRequest>,
) -> WebDriverResult {
let mut sessions = state.sessions.write().await;
let session = sessions.get_mut(&session_id)?;
let current_window = session.current_window.clone();
let timeouts = session.timeouts.clone();
let frame_context = session.frame_context.clone();
session.action_state = ActionState::default();
drop(sessions);
let executor = state.get_executor_for_window(¤t_window, timeouts, frame_context)?;
executor.navigate(&request.url).await?;
Ok(WebDriverResponse::null())
}
pub async fn get_url<R: Runtime + 'static>(
State(state): State<Arc<AppState<R>>>,
Path(session_id): Path<String>,
) -> WebDriverResult {
let sessions = state.sessions.read().await;
let session = sessions.get(&session_id)?;
let current_window = session.current_window.clone();
let timeouts = session.timeouts.clone();
let frame_context = session.frame_context.clone();
drop(sessions);
let executor = state.get_executor_for_window(¤t_window, timeouts, frame_context)?;
let url = executor.get_url().await?;
Ok(WebDriverResponse::success(url))
}
pub async fn get_title<R: Runtime + 'static>(
State(state): State<Arc<AppState<R>>>,
Path(session_id): Path<String>,
) -> WebDriverResult {
let sessions = state.sessions.read().await;
let session = sessions.get(&session_id)?;
let current_window = session.current_window.clone();
let timeouts = session.timeouts.clone();
let frame_context = session.frame_context.clone();
drop(sessions);
let executor = state.get_executor_for_window(¤t_window, timeouts, frame_context)?;
let title = executor.get_title().await?;
Ok(WebDriverResponse::success(title))
}
pub async fn back<R: Runtime + 'static>(
State(state): State<Arc<AppState<R>>>,
Path(session_id): Path<String>,
) -> WebDriverResult {
let mut sessions = state.sessions.write().await;
let session = sessions.get_mut(&session_id)?;
let current_window = session.current_window.clone();
let timeouts = session.timeouts.clone();
let frame_context = session.frame_context.clone();
session.action_state = ActionState::default();
drop(sessions);
let executor = state.get_executor_for_window(¤t_window, timeouts, frame_context)?;
executor.go_back().await?;
Ok(WebDriverResponse::null())
}
pub async fn forward<R: Runtime + 'static>(
State(state): State<Arc<AppState<R>>>,
Path(session_id): Path<String>,
) -> WebDriverResult {
let mut sessions = state.sessions.write().await;
let session = sessions.get_mut(&session_id)?;
let current_window = session.current_window.clone();
let timeouts = session.timeouts.clone();
let frame_context = session.frame_context.clone();
session.action_state = ActionState::default();
drop(sessions);
let executor = state.get_executor_for_window(¤t_window, timeouts, frame_context)?;
executor.go_forward().await?;
Ok(WebDriverResponse::null())
}
pub async fn refresh<R: Runtime + 'static>(
State(state): State<Arc<AppState<R>>>,
Path(session_id): Path<String>,
) -> WebDriverResult {
let mut sessions = state.sessions.write().await;
let session = sessions.get_mut(&session_id)?;
let current_window = session.current_window.clone();
let timeouts = session.timeouts.clone();
let frame_context = session.frame_context.clone();
session.action_state = ActionState::default();
drop(sessions);
let executor = state.get_executor_for_window(¤t_window, timeouts, frame_context)?;
executor.refresh().await?;
Ok(WebDriverResponse::null())
}