Debian Codegraph @

pkg/shell/router.jsx

raw
1 /*
2 * Copyright (C) 2024 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 /* THE MESSAGE ROUTER
7 *
8 * The message router forwards Cockpit protocol messages between the
9 * web socket and the frames.
10 *
11 * It automatically starts processing messages for a frame as soon as
12 * it receives the "init" message from that frame.
13 *
14 * The router needs a "callback" object that it uses to hook into the
15 * rest of the shell. This is provided by the ShellState instance,
16 * and more documentation can be found there.
17 */
18
19 import cockpit from "cockpit";
20
21 export function Router(callbacks) {
22 const self = this;
23
24 let unique_id = 0;
25 const origin = cockpit.transport.origin;
26 const source_by_seed = { };
27 const source_by_name = { };
28
29 cockpit.transport.filter(function(message, channel, control) {
30 /* Only control messages with a channel are forwardable */
31 if (control) {
32 if (control.channel !== undefined) {
33 for (const seed in source_by_seed) {
34 const source = source_by_seed[seed];
35 if (!source.window.closed)
36 source.window.postMessage(message, origin);
37 }
38 } else if (control.command == "hint") {
39 /* This is where we handle hint messages directed at
40 * the shell. Right now, there aren't any.
41 */
42 }
43
44 /* Forward message to relevant frame */
45 } else if (channel) {
46 const pos = channel.indexOf('!');
47 if (pos !== -1) {
48 const seed = channel.substring(0, pos + 1);
49 const source = source_by_seed[seed];
50 if (source) {
51 if (!source.window.closed)
52 source.window.postMessage(message, origin);
53 return false; /* Stop delivery */
54 }
55 }
56 }
57
58 /* Still deliver the message locally */
59 return true;
60 }, false);
61
62 function perform_jump(child, control) {
63 let str = control.location || "";
64 if (str[0] != "/")
65 str = "/" + str;
66 if (control.host)
67 str = "/@" + encodeURIComponent(control.host) + str;
68
69 callbacks.perform_frame_jump_command(child.name, str);
70 }
71
72 function perform_track(child) {
73 let hash = child.location.hash;
74 if (hash.indexOf("#") === 0)
75 hash = hash.substring(1);
76 if (hash === "/")
77 hash = "";
78
79 callbacks.perform_frame_hash_track(child.name, hash);
80 }
81
82 function on_unload(ev) {
83 let source;
84 if (ev.target.defaultView)
85 source = source_by_name[ev.target.defaultView.name];
86 else if (ev.view)
87 source = source_by_name[ev.view.name];
88 if (source)
89 unregister(source);
90 }
91
92 function on_hashchange(ev) {
93 const source = source_by_name[ev.target.name];
94 if (source)
95 perform_track(source.window);
96 }
97
98 function on_load(ev) {
99 const source = source_by_name[ev.target.contentWindow.name];
100 if (source)
101 perform_track(source.window);
102 }
103
104 function unregister(source) {
105 const child = source.window;
106 cockpit.kill(null, child.name);
107 const frame = child.frameElement;
108 if (frame)
109 frame.removeEventListener("load", on_load);
110 /* This is often invalid when the window is closed */
111 if (child.removeEventListener) {
112 child.removeEventListener("unload", on_unload);
113 child.removeEventListener("hashchange", on_hashchange);
114 }
115 delete source_by_seed[source.channel_seed];
116 delete source_by_name[source.name];
117 }
118
119 function register(child) {
120 let host;
121 let page;
122 const name = child.name || "";
123 if (name.indexOf("cockpit1:") === 0) {
124 const parts = name.substring(9).split("/");
125 host = parts[0];
126 page = parts.slice(1).join("/");
127 }
128 if (!name || !host || !page) {
129 console.warn("invalid child window name", child, name);
130 return;
131 }
132
133 unique_id += 1;
134 const seed = (cockpit.transport.options["channel-seed"] || "undefined:") + unique_id.toString() + "!";
135 const source = {
136 name,
137 window: child,
138 channel_seed: seed,
139 default_host: host,
140 page,
141 inited: false,
142 };
143 source_by_seed[seed] = source;
144 source_by_name[name] = source;
145
146 const frame = child.frameElement;
147 frame.addEventListener("load", on_load);
148 child.addEventListener("unload", on_unload);
149 child.addEventListener("hashchange", on_hashchange);
150
151 perform_track(child);
152
153 return source;
154 }
155
156 function message_handler(event) {
157 if (event.origin !== origin)
158 return;
159
160 let data = event.data;
161 const child = event.source;
162 if (!child)
163 return;
164
165 /* If it's binary data just send it.
166 * TODO: Once we start restricting what frames can
167 * talk to which hosts, we need to parse control
168 * messages here, and cross check channels */
169 if (data instanceof window.ArrayBuffer) {
170 cockpit.transport.inject(data, true);
171 return;
172 }
173
174 if (typeof data !== "string")
175 return;
176
177 let source;
178 let control;
179
180 /*
181 * On Internet Explorer we see Access Denied when non Cockpit
182 * frames send messages (such as Javascript console). This also
183 * happens when the window is closed.
184 */
185 try {
186 source = source_by_name[child.name];
187 } catch (ex) {
188 console.log("received message from child with inaccessible name: ", ex);
189 return;
190 }
191
192 /* Closing the transport */
193 if (data.length === 0) {
194 if (source)
195 unregister(source);
196 return;
197 }
198
199 /* A control message */
200 if (data[0] == '\n') {
201 control = JSON.parse(data.substring(1));
202 if (control.command === "init") {
203 if (source)
204 unregister(source);
205 if (control.problem) {
206 console.warn("child frame failed to init: " + control.problem);
207 source = null;
208 } else {
209 source = register(child);
210 }
211 if (source) {
212 const reply = {
213 ...cockpit.transport.options,
214 command: "init",
215 host: source.default_host,
216 "channel-seed": source.channel_seed,
217 };
218 child.postMessage("\n" + JSON.stringify(reply), origin);
219 source.inited = true;
220
221 callbacks.frame_is_initialized(child.frameElement.name);
222 }
223 return;
224 } else if (control.command === "jump") {
225 perform_jump(child, control);
226 return;
227 } else if (control.command === "hint") {
228 if (control.hint == "restart") {
229 /* watchdog handles current host for now */
230 if (control.host != cockpit.transport.host)
231 callbacks.expect_restart(control.host);
232 } else
233 cockpit.hint(control.hint, control);
234 return;
235 } else if (control.command == "oops") {
236 callbacks.show_oops();
237 return;
238 } else if (control.command == "notify") {
239 if (source)
240 callbacks.handle_notifications(source.default_host, source.page, control);
241 return;
242
243 /* Only control messages with a channel are forwardable */
244 } else if (control.channel === undefined && (control.command !== "logout" && control.command !== "kill")) {
245 return;
246
247 /* Add the child's group to all open channel messages */
248 } else if (control.command == "open") {
249 control.group = child.name;
250 data = "\n" + JSON.stringify(control);
251 }
252 }
253
254 if (!source) {
255 console.warn("child frame " + child.name + " sending data without init");
256 return;
257 }
258
259 /* Everything else gets forwarded */
260 cockpit.transport.inject(data, true);
261 }
262
263 self.hint = function hint(name, data) {
264 const source = source_by_name[name];
265 /* This is often invalid when the window is closed */
266 if (source && source.inited && !source.window.closed) {
267 data.command = "hint";
268 const message = "\n" + JSON.stringify(data);
269 source.window.postMessage(message, origin);
270 }
271 };
272
273 self.unregister_name = (name) => {
274 const source = source_by_name[name];
275 if (source)
276 unregister(source);
277 };
278
279 cockpit.transport.wait(function() {
280 window.addEventListener("message", message_handler, false);
281 });
282 }