|
| 1 | +// --------------------------------------------------------------------------------------------- |
| 2 | +// Mountain Protocol Handler (handlers/protocol.rs) |
| 3 | +// -------------------------------------------------------------------------------------------- |
| 4 | +// Implements the handler for custom URI protocols registered by the |
| 5 | +// application, primarily the `vscode://` (or potentially a custom `land://`) |
| 6 | +// scheme. This allows external sources (e.g., OAuth redirects, CLI arguments, |
| 7 | +// other apps) to trigger actions within the running Land instance. |
| 8 | +// |
| 9 | +// Responsibilities: |
| 10 | +// - Implementing the callback function registered with Tauri's |
| 11 | +// `register_uri_scheme_protocol`. |
| 12 | +// - Parsing the incoming URI (scheme, authority, path, query parameters) using |
| 13 | +// the `url` crate. |
| 14 | +// - Routing the request based on the URI's authority and path. |
| 15 | +// - Translating the request into an appropriate `ActionEffect` (e.g., opening a |
| 16 | +// file, executing a command, handling an auth callback). |
| 17 | +// - Dispatching the effect asynchronously using the `AppRuntime`. |
| 18 | +// - Returning an immediate, typically empty, HTTP response to the OS caller. |
| 19 | +// |
| 20 | +// Key Interactions: |
| 21 | +// - Registered with and called by Tauri's protocol handling mechanism. |
| 22 | +// - Uses the `url` crate for URI parsing. |
| 23 | +// - Creates `ActionEffect`s (e.g., `workspace_effects`, `command_effects`). |
| 24 | +// - Dispatches effects via `AppRuntime::run` (obtained via `AppHandle::state`). |
| 25 | +// -------------------------------------------------------------------------------------------- |
| 26 | + |
| 27 | +// ----- START: Element/Mountain/src/handlers/protocol.rs ----- |
| 28 | +use std::{collections::HashMap, path::PathBuf, sync::Arc}; // Added PathBuf |
| 29 | + |
| 30 | +use Land_Common::workspace_effects; |
| 31 | +use Land_Common::{command_effects, effect::ActionEffect}; /* Effects for commands // Added ActionEffect // |
| 32 | + * Added CommonError // Effects for opening files // |
| 33 | + * Updated path // Updated path */ |
| 34 | +use serde_json::{Value, json}; |
| 35 | +use tauri::{ |
| 36 | + AppHandle, |
| 37 | + Manager, // Added Manager trait for state access |
| 38 | + Runtime, |
| 39 | + api::ipc::{Request, Response}, |
| 40 | + http::ResponseBuilder, |
| 41 | +}; |
| 42 | +use url::Url; |
| 43 | + |
| 44 | +use crate::runtime::AppRuntime; // To run effects // Use url crate for parsing |
| 45 | + |
| 46 | +// Handle the registered vscode:// protocol |
| 47 | +pub fn handle_vscode_protocol<R:Runtime>( |
| 48 | + request:&Request, |
| 49 | + app_handle:AppHandle<R>, |
| 50 | +) -> Result<Response, Box<dyn std::error::Error>> { |
| 51 | + let uri_str = request.uri(); |
| 52 | + println!("[Protocol Handler] Handling vscode URI: {}", uri_str); |
| 53 | + |
| 54 | + // --- Parse the URI --- |
| 55 | + // Use Url::parse and extract host (authority), path, query params |
| 56 | + match Url::parse(uri_str) { |
| 57 | + Ok(url) => { |
| 58 | + let authority = url.host_str().unwrap_or("").to_lowercase(); |
| 59 | + let path = url.path().trim_start_matches('/'); |
| 60 | + let query_params:HashMap<String, String> = url.query_pairs().into_owned().collect(); |
| 61 | + |
| 62 | + println!( |
| 63 | + "[Protocol Handler] Authority='{}', Path='{}', Params='{:?}'", |
| 64 | + authority, path, query_params |
| 65 | + ); |
| 66 | + |
| 67 | + // --- Route based on authority/path --- |
| 68 | + // Define the effect to be run asynchronously |
| 69 | + let effect_result:Result<ActionEffect<Arc<AppRuntime>, _, _>, String> = match authority.as_str() { |
| 70 | + "file" => { |
| 71 | + // Example: vscode://file/path/to/resource |
| 72 | + // Action: Open the file in the editor |
| 73 | + let file_path = PathBuf::from(path); // May need decoding? Ensure PathBuf is imported |
| 74 | + println!("[Protocol Handler] Request to open file: {}", file_path.display()); |
| 75 | + // TODO: Create an effect to open a file/editor |
| 76 | + Ok(workspace_effects::open_file(file_path)) // Assume this effect exists |
| 77 | + // Err("open_file effect not implemented yet".to_string()) |
| 78 | + }, |
| 79 | + "vscode.git" | "vscode.github" | "vscode.vscode-remote" => { |
| 80 | + // Example: vscode://vscode.github/did-authenticate?code=...&state=... |
| 81 | + // Action: Handle authentication callback |
| 82 | + let command_id = format!("protocol.auth.{}", authority); // e.g., protocol.auth.github |
| 83 | + println!("[Protocol Handler] Request to handle auth callback for: {}", authority); |
| 84 | + // Pass relevant query params (code, state, error etc.) as args |
| 85 | + Ok(command_effects::execute_command(command_id, json!(query_params))) |
| 86 | + }, |
| 87 | + "resource" => { |
| 88 | + // Example: vscode://resource/path/to/workspace/file |
| 89 | + // Treat similarly to 'file' for now |
| 90 | + let file_path = PathBuf::from(path); |
| 91 | + println!( |
| 92 | + "[Protocol Handler] Request to open resource (treating as file): {}", |
| 93 | + file_path.display() |
| 94 | + ); |
| 95 | + Ok(workspace_effects::open_file(file_path)) |
| 96 | + // Err("resource authority handling not fully |
| 97 | + // defined".to_string()) |
| 98 | + }, |
| 99 | + // Add other authorities like 'extension', 'settings', 'command' as needed |
| 100 | + _ => Err(format!("Unknown or unhandled authority: {}", authority)), |
| 101 | + }; |
| 102 | + |
| 103 | + // --- Dispatch the Effect --- |
| 104 | + if let Ok(effect) = effect_result { |
| 105 | + let app_handle_clone = app_handle.clone(); |
| 106 | + tokio::spawn(async move { |
| 107 | + // Need runtime access - best obtained via AppHandle state |
| 108 | + let runtime_state = app_handle_clone.try_state::<Arc<AppRuntime>>(); |
| 109 | + if let Some(runtime) = runtime_state { |
| 110 | + println!("[Protocol Handler] Running effect for: {}", uri_str); |
| 111 | + if let Err(e) = runtime.run(effect).await { |
| 112 | + eprintln!("[Protocol Handler] Error running effect for {}: {}", uri_str, e); |
| 113 | + } |
| 114 | + } else { |
| 115 | + eprintln!("[Protocol Handler] AppRuntime state not found for effect execution!"); |
| 116 | + } |
| 117 | + }); |
| 118 | + } else { |
| 119 | + eprintln!( |
| 120 | + "[Protocol Handler] No effect created for URI: {}. Error: {}", |
| 121 | + uri_str, |
| 122 | + effect_result.unwrap_err() |
| 123 | + ); |
| 124 | + } |
| 125 | + }, |
| 126 | + Err(e) => { |
| 127 | + eprintln!("[Protocol Handler] Failed to parse vscode URI '{}': {}", uri_str, e); |
| 128 | + }, |
| 129 | + } |
| 130 | + |
| 131 | + // --- Return Empty Response --- |
| 132 | + // The protocol handler usually just triggers an action asynchronously. |
| 133 | + ResponseBuilder::new().status(200).body(Vec::new()) |
| 134 | +} |
| 135 | + |
| 136 | +// ----- END: Element/Mountain/src/handlers/protocol.rs ----- |
0 commit comments