Skip to content

Commit

Permalink
WIP - move router from base_index to ShellState
Browse files Browse the repository at this point in the history
  • Loading branch information
mvollmer committed Sep 26, 2024
1 parent de5e1a1 commit 75fd4ed
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 106 deletions.
30 changes: 0 additions & 30 deletions pkg/shell/base_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import cockpit from "cockpit";

import { Router } from "./router.jsx";
import { decode_location, decode_window_location, push_window_location } from "./util.jsx";

const shell_embedded = window.location.pathname.indexOf(".html") !== -1;
Expand All @@ -34,29 +33,6 @@ function Index() {
if (typeof self.navigate !== "function")
throw Error("Index requires a prototype with a navigate function");

self.has_oops = false;

self.router = new Router(self);

/* Watchdog for disconnect */
const watchdog = cockpit.channel({ payload: "null" });
watchdog.addEventListener("close", (event, options) => {
const watchdog_problem = options.problem || "disconnected";
console.warn("transport closed: " + watchdog_problem);
self.dispatchEvent("disconnect", watchdog_problem);
});

const old_onerror = window.onerror;
window.onerror = function cockpit_error_handler(msg, url, line) {
// Errors with url == "" are not logged apparently, so let's
// not show the "Oops" for them either.
if (url != "")
self.show_oops();
if (old_onerror)
return old_onerror(msg, url, line);
return false;
};

const last_hash_for_host_path = { };

function lookup_path_hash(address, path) {
Expand Down Expand Up @@ -125,13 +101,7 @@ function Index() {
return false;
};

self.show_oops = function () {
self.has_oops = true;
self.dispatchEvent("update");
};

self.start = function() {
self.router.start([]);
};

self.ready = function () {
Expand Down
20 changes: 14 additions & 6 deletions pkg/shell/frames.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ function poll_frame_ready(state, frame, elt, count, setupFrameWindow) {
frame.ready = true;
state.update();

if (elt.getAttribute("data-ready") != "1") {
elt.setAttribute("data-ready", "1");
}

if (elt.contentWindow && setupFrameWindow)
setupFrameWindow(elt.contentWindow);

Expand Down Expand Up @@ -109,7 +105,7 @@ export const Frames = ({ state, idle_state, hidden }) => {
// attributes, put them on a list, and reuse them
// eventually.
console.log("REMOVE IFRAME", elt.getAttribute('name'));
state.index.router.unregister_name(elt.getAttribute('name'));
state.router.unregister_name(elt.getAttribute('name'));
elt.removeAttribute('name');
elt.removeAttribute('title');
elt.removeAttribute('src');
Expand Down Expand Up @@ -167,6 +163,19 @@ export const Frames = ({ state, idle_state, hidden }) => {
iframe.setAttribute("data-host", frame.host);
}

if (iframe.getAttribute("title") != frame.title)
iframe.setAttribute("title", frame.title);

if (frame.ready && iframe.getAttribute("data-ready") == null)
iframe.setAttribute("data-ready", "1");
else if (!frame.ready && iframe.getAttribute("data-ready"))
iframe.removeAttribute("data-ready");

if (frame.loaded && iframe.getAttribute("data-loaded") == null)
iframe.setAttribute("data-loaded", "1");
else if (!frame.loaded && iframe.getAttribute("data-loaded"))
iframe.removeAttribute("data-loaded");

if (iframe.getAttribute("title") != frame.title)
iframe.setAttribute("title", frame.title);

Expand All @@ -175,7 +184,6 @@ export const Frames = ({ state, idle_state, hidden }) => {
if (iframe.getAttribute('src') != src) {
console.log("SRC", name, iframe.getAttribute('src'), "->", src);


if (iframe.contentWindow) {
// This prevents the browser from creating a new
// history entry. It would do that whenever the "src"
Expand Down
38 changes: 13 additions & 25 deletions pkg/shell/router.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,16 @@
* web socket and the frames.
*
* It automatically starts processing messages for a frame as soon as
* it receives a "init" message from that frame.
* it receives the "init" message from that frame.
*
* The router needs a "callback" object that will be used for the
* following things:
*
* - ...
* The router needs a "callback" object that will be used for a number
* of things. This is provided by the ShellState instance, and more
* documentation can be found there.
*/

import cockpit from "cockpit";

import { decode_window_location, replace_window_location } from "./util.jsx";

export function Router(index) {
export function Router(callbacks) {
const self = this;

let unique_id = 0;
Expand Down Expand Up @@ -83,7 +80,7 @@ export function Router(index) {
if (control.host)
str = "/@" + encodeURIComponent(control.host) + str;

index.perform_frame_jump_command(child.name, str);
callbacks.perform_frame_jump_command(child.name, str);
}

function perform_track(child) {
Expand All @@ -93,7 +90,7 @@ export function Router(index) {
if (hash === "/")
hash = "";

index.perform_frame_hash_track(child.name, hash);
callbacks.perform_frame_hash_track(child.name, hash);
}

function on_unload(ev) {
Expand Down Expand Up @@ -164,16 +161,8 @@ export function Router(index) {
child.addEventListener("unload", on_unload);
child.addEventListener("hashchange", on_hashchange);

/*
* Setting the "data-loaded" attribute helps the testsuite
* know when it can switch into the frame and inject its
* own additions.
*/
frame.setAttribute('data-loaded', '1');

perform_track(child);

index.navigate();
return source;
}

Expand Down Expand Up @@ -241,7 +230,8 @@ export function Router(index) {
child.postMessage("\n" + JSON.stringify(reply), origin);
source.inited = true;

index.perform_frame_hidden_hint(child.frameElement.name);
callbacks.frame_is_initialized(child.frameElement.name);
callbacks.perform_frame_hidden_hint(child.frameElement.name);
}
} else if (control.command === "jump") {
perform_jump(child, control);
Expand All @@ -250,16 +240,16 @@ export function Router(index) {
if (control.hint == "restart") {
/* watchdog handles current host for now */
if (control.host != cockpit.transport.host)
index.expect_restart(control.host);
callbacks.expect_restart(control.host);
} else
cockpit.hint(control.hint, control);
return;
} else if (control.command == "oops") {
index.show_oops();
callbacks.show_oops();
return;
} else if (control.command == "notify") {
if (source)
index.handle_notifications(source.default_host, source.page, control);
callbacks.handle_notifications(source.default_host, source.page, control);
return;

/* Only control messages with a channel are forwardable */
Expand All @@ -282,10 +272,8 @@ export function Router(index) {
cockpit.transport.inject(data, true);
}

self.start = function start(messages) {
self.start = function start() {
window.addEventListener("message", message_handler, false);
for (let i = 0, len = messages.length; i < len; i++)
message_handler(messages[i]);
};

self.hint = function hint(name, data) {
Expand Down
Loading

0 comments on commit 75fd4ed

Please sign in to comment.