nexo_plugin_browser/
admin.rs1use std::path::PathBuf;
17
18use serde_json::{json, Value};
19
20use crate::instance_registry;
21
22const PAIRED_SENTINEL: &str = ".nexo-paired";
23
24fn ok(result: Value) -> Value {
25 json!({ "ok": true, "result": result })
26}
27
28fn err(msg: impl Into<String>) -> Value {
29 json!({ "ok": false, "error": msg.into() })
30}
31
32fn arg_instance(req: &Value) -> Result<String, Value> {
33 req.get("instance")
34 .and_then(|v| v.as_str())
35 .map(|s| s.to_string())
36 .ok_or_else(|| err("`instance` arg required"))
37}
38
39pub fn list_instances() -> Value {
42 let rows: Vec<Value> = instance_registry::entries()
43 .into_iter()
44 .map(|(label, plugin)| {
45 let dir = plugin.user_data_dir().to_string();
46 let paired = PathBuf::from(&dir).join(PAIRED_SENTINEL).exists();
47 json!({
48 "instance": label,
49 "user_data_dir": dir,
50 "paired": paired,
51 })
52 })
53 .collect();
54 ok(json!({ "instances": rows }))
55}
56
57pub async fn shutdown(req: &Value) -> Value {
61 let instance = match arg_instance(req) {
62 Ok(s) => s,
63 Err(e) => return e,
64 };
65 let Some(plugin) = instance_registry::lookup(&instance) else {
66 return err(format!("instance `{instance}` not declared"));
67 };
68 plugin.shutdown_chrome().await;
69 ok(json!({ "instance": instance, "shutdown": true }))
70}
71
72pub async fn restart(req: &Value) -> Value {
77 let instance = match arg_instance(req) {
78 Ok(s) => s,
79 Err(e) => return e,
80 };
81 let Some(plugin) = instance_registry::lookup(&instance) else {
82 return err(format!("instance `{instance}` not declared"));
83 };
84 plugin.shutdown_chrome().await;
85 ok(json!({ "instance": instance, "restart": true }))
86}
87
88pub fn mark_paired(req: &Value) -> Value {
93 let instance = match arg_instance(req) {
94 Ok(s) => s,
95 Err(e) => return e,
96 };
97 let Some(plugin) = instance_registry::lookup(&instance) else {
98 return err(format!("instance `{instance}` not declared"));
99 };
100 let dir = PathBuf::from(plugin.user_data_dir());
101 if let Err(e) = std::fs::create_dir_all(&dir) {
102 return err(format!("create user_data_dir failed: {e}"));
103 }
104 let path = dir.join(PAIRED_SENTINEL);
105 match std::fs::write(&path, b"paired\n") {
106 Ok(()) => ok(json!({
107 "instance": instance,
108 "sentinel": path.to_string_lossy(),
109 })),
110 Err(e) => err(format!("write sentinel failed: {e}")),
111 }
112}
113
114pub async fn launch_visible(req: &Value) -> Value {
125 let instance = match arg_instance(req) {
126 Ok(s) => s,
127 Err(e) => return e,
128 };
129 let Some(plugin) = instance_registry::lookup(&instance) else {
130 return err(format!("instance `{instance}` not declared"));
131 };
132 plugin.request_visible_next_boot();
133 plugin.shutdown_chrome().await;
134 ok(json!({
135 "instance": instance,
136 "launched": true,
137 "note": "Chrome will spawn visible on next tool.invoke targeting this instance",
138 }))
139}
140
141pub async fn dispatch(verb: &str, params: &Value) -> Value {
144 match verb {
145 "list_instances" => list_instances(),
146 "shutdown" => shutdown(params).await,
147 "restart" => restart(params).await,
148 "mark_paired" => mark_paired(params),
149 "launch_visible" => launch_visible(params).await,
150 other => err(format!("unknown admin verb: `{other}`")),
151 }
152}