| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
import cockpit from "cockpit"; |
| 4 |
import { EventEmitter } from "cockpit/event"; |
| 5 |
import { Channel } from "cockpit/channel"; |
| 6 |
|
| 7 |
const _ = cockpit.gettext; |
| 8 |
|
| 9 |
/* SessionController for handling idle timeouts |
| 10 |
*/ |
| 11 |
|
| 12 |
interface SessionControllerEvents { |
| 13 |
changed: () => void; |
| 14 |
} |
| 15 |
|
| 16 |
export class SessionController extends EventEmitter<SessionControllerEvents> { |
| 17 |
active: boolean; |
| 18 |
countdown: number = -1; |
| 19 |
problem: string | null = null; |
| 20 |
|
| 21 |
#channel: Channel | null = null; |
| 22 |
#timeout: number = -1; |
| 23 |
|
| 24 |
constructor() { |
| 25 |
super(); |
| 26 |
this.active = (window.parent === window || window.name.indexOf("cockpit1:") !== 0); |
| 27 |
if (this.active) { |
| 28 |
console.debug("session controller active for", window.name); |
| 29 |
this.add_window(window); |
| 30 |
this.#open_channel(); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
add_window(win: Window) { |
| 35 |
if (!this.active) |
| 36 |
return; |
| 37 |
|
| 38 |
// NOTE: This function will be called many many times for a |
| 39 |
// given window, not just once. Calling addEventListener |
| 40 |
// multiple times is ok here, however, since we always pass |
| 41 |
// the exact same listener. |
| 42 |
win.addEventListener("mousemove", this.#record_activity, false); |
| 43 |
win.addEventListener("mousedown", this.#record_activity, false); |
| 44 |
win.addEventListener("keypress", this.#record_activity, false); |
| 45 |
win.addEventListener("touchmove", this.#record_activity, false); |
| 46 |
win.addEventListener("scroll", this.#record_activity, false); |
| 47 |
} |
| 48 |
|
| 49 |
#open_channel() { |
| 50 |
const channel = new Channel({ payload: "session-control" }); |
| 51 |
|
| 52 |
channel.on("ready", (data) => { |
| 53 |
if (typeof data.timeout === "number") |
| 54 |
this.#timeout = data.timeout; |
| 55 |
}); |
| 56 |
|
| 57 |
channel.on("control", (message) => { |
| 58 |
if (message.command === "countdown" && typeof message.counter === "number") { |
| 59 |
this.countdown = message.counter; |
| 60 |
this.emit("changed"); |
| 61 |
} else if (message.command === "logout") { |
| 62 |
cockpit.logout(true, _("You have been logged out due to inactivity.")); |
| 63 |
} |
| 64 |
}); |
| 65 |
|
| 66 |
channel.on("close", (options) => { |
| 67 |
const problem = (typeof options.problem === "string" ? options.problem : "") || "disconnected"; |
| 68 |
this.#channel = null; |
| 69 |
console.warn("session terminated: " + problem); |
| 70 |
this.problem = problem; |
| 71 |
this.emit("changed"); |
| 72 |
}); |
| 73 |
|
| 74 |
this.#channel = channel; |
| 75 |
} |
| 76 |
|
| 77 |
inhibit_activity_reporting(flag: boolean) { |
| 78 |
this.#inhibit_activity_reporting = flag; |
| 79 |
} |
| 80 |
|
| 81 |
continue_session() { |
| 82 |
if (this.countdown != -1) { |
| 83 |
this.#send_activity_notification(); |
| 84 |
this.countdown = -1; |
| 85 |
this.emit("changed"); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
#last_activity_report_time: number = 0; |
| 90 |
#inhibit_activity_reporting: boolean = false; |
| 91 |
|
| 92 |
#record_activity = () => { |
| 93 |
const now = Date.now(); |
| 94 |
if (!this.#inhibit_activity_reporting && Math.abs(now - this.#last_activity_report_time) > 10 * 1000) { |
| 95 |
this.#last_activity_report_time = now; |
| 96 |
this.#send_activity_notification(); |
| 97 |
if (this.countdown !== -1) { |
| 98 |
this.countdown = -1; |
| 99 |
this.emit("changed"); |
| 100 |
} |
| 101 |
} |
| 102 |
}; |
| 103 |
|
| 104 |
#send_activity_notification() { |
| 105 |
if (this.#channel && this.#timeout > 0) |
| 106 |
this.#channel.send_control({ command: "active" }); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
let controller : SessionController | undefined; |
| 111 |
|
| 112 |
export function get_session_controller(): SessionController { |
| 113 |
if (!controller) |
| 114 |
controller = new SessionController(); |
| 115 |
return controller; |
| 116 |
} |