Mountain/Vine/Server/Notification/WebviewLifecycle.rs
1#![allow(non_snake_case)]
2//! Cocoon → Mountain `webview.setTitle` / `webview.setIconPath` /
3//! `webview.setHtml` / `webview.postMessage` / `webview.updateView` /
4//! `webview.viewState` / `webview.dispose` notifications. Shared atom
5//! because the methods all map to the same suffix-split pattern; keeping
6//! them in one file avoids near-identical 5-line files while still
7//! pinning the handler to a discoverable filename.
8//!
9//! Wire-shape canonicalisation MIRRORS `Track/Effect/CreateEffectForRequest/
10//! Webview.rs` so notification-path payloads land on the same named-key
11//! shapes as the request path. SkyBridge's listeners read `Payload.viewId`,
12//! `Payload.html`, `Payload.message` etc. directly; without this Cocoon's
13//! legacy positional `[Handle, Value]` notifications would emit a payload
14//! whose only top-level keys are `0`/`1` (array indices), the listener
15//! would early-return on the missing named keys, and the iframe would
16//! stay blank even when the request path canonicalised correctly.
17//!
18//! For per-extension isolation and payload inspection, split this into
19//! per-method atoms (`WebviewSetTitle`, `WebviewSetIconPath`, etc.) when
20//! the divergence is worth it.
21
22use serde_json::{Map, Value, json};
23use tauri::Emitter;
24
25use crate::{Vine::Server::MountainVinegRPCService::MountainVinegRPCService, dev_log};
26
27pub async fn WebviewLifecycle(Service:&MountainVinegRPCService, MethodName:&str, Parameter:&Value) {
28 // Suffix mapping: stock VS Code wire methods are camelCase
29 // (`webview.setHtml`, `webview.setIconPath`), but Sky's canonical
30 // channel registry (`Common/Source/IPC/SkyEvent.rs`) standardises
31 // kebab-case for set-html (`sky://webview/set-html`) since the
32 // `setWebviewHtml` typed-RPC and `Window.rs::SetHtml` request both
33 // emit kebab. Translate `setHtml` and `postMessage` here so every
34 // producer of those wire shapes lands on the same Sky channel; other
35 // suffixes pass through camelCase (Sky listeners use camel for
36 // `setTitle` / `setIconPath`).
37 let RawSuffix = &MethodName["webview.".len()..];
38 let Suffix = match RawSuffix {
39 "setHtml" => "set-html",
40 "postMessage" => "post-message",
41 Other => Other,
42 };
43 let EventName = format!("sky://webview/{}", Suffix);
44
45 // Canonicalise payload shapes to the same named-key form the request
46 // path produces. Three observed cases (matching `Webview.rs`):
47 // 1. Object: pass through (Cocoon's modern named-key
48 // `SendToMountain("webview.setHtml", { handle, viewId, html })`).
49 // 2. Array `[<obj>]`: unwrap.
50 // 3. Array `[Handle, Second?, ...]`: positional - preserve the original args
51 // slot AND project to the per-method named alias so listeners that read
52 // `Payload.html` / `Payload.viewId` / `Payload.message` etc. stay
53 // decoupled from the wire shape.
54 let CanonicalPayload:Value = if Parameter.is_object() {
55 Parameter.clone()
56 } else if let Some(First) = Parameter.get(0) {
57 if First.is_object() {
58 First.clone()
59 } else {
60 let mut Object = Map::new();
61 Object.insert("method".to_string(), Value::String(MethodName.to_string()));
62 Object.insert("handle".to_string(), First.clone());
63 Object.insert("args".to_string(), Parameter.clone());
64 if let Some(Second) = Parameter.get(1) {
65 let Alias = match MethodName {
66 "webview.setHtml" => "html",
67 "webview.postMessage" => "message",
68 "webview.registerView" | "webview.unregisterView" => "viewId",
69 "webview.registerCustomEditor" | "webview.unregisterCustomEditor" | "webview.create" => "viewType",
70 _ => "value",
71 };
72 Object.insert(Alias.to_string(), Second.clone());
73 if MethodName == "webview.create" {
74 if let Some(Third) = Parameter.get(2) {
75 Object.insert("title".to_string(), Third.clone());
76 }
77 }
78 }
79 Value::Object(Object)
80 }
81 } else {
82 json!({
83 "method": MethodName,
84 "handle": Parameter.clone(),
85 })
86 };
87
88 if let Err(Error) = Service.ApplicationHandle().emit(&EventName, &CanonicalPayload) {
89 dev_log!("grpc", "warn: [MountainVinegRPCService] {} emit failed: {}", EventName, Error);
90 }
91}