File size: 11,746 Bytes
f680f3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
from __future__ import annotations

import os
import select
import shutil
import socket
import subprocess
import threading
import time
import uuid
from pathlib import Path
from typing import Any

from helpers import files, virtual_desktop
from helpers.print_style import PrintStyle


DEFAULT_WIDTH = 1024
DEFAULT_HEIGHT = 768
START_TIMEOUT_SECONDS = 15.0


def keyboard_options() -> dict[str, str]:
    """Resolve the configured XKB keyboard layout for browser displays."""
    from plugins._browser.helpers.config import (
        KEYBOARD_LAYOUT_KEY,
        KEYBOARD_VARIANT_KEY,
        get_browser_config,
    )

    config = get_browser_config()
    return {
        "layout": str(config.get(KEYBOARD_LAYOUT_KEY, "") or "").strip(),
        "variant": str(config.get(KEYBOARD_VARIANT_KEY, "") or "").strip(),
    }


def collect_status() -> dict[str, Any]:
    binaries = {
        name: shutil.which(name) or ""
        for name in ("Xvfb", "xpra", "xrandr")
    }
    html_root = virtual_desktop.find_xpra_html_root()
    missing = [name for name, path in binaries.items() if not path]
    if not html_root:
        missing.append("xpra-html5")
    return {
        "available": not missing,
        "missing": missing,
        "binaries": binaries,
        "xpra_html_root": str(html_root or ""),
    }


class BrowserInteractiveView:
    """Own one private X display and its optional Xpra viewer."""

    def __init__(self, context_id: str) -> None:
        self.context_id = str(context_id)
        self.token = f"browser-{uuid.uuid4().hex}"
        self.state_dir = Path(files.get_abs_path("tmp", "browser", "displays", self.token))
        self.display: int | None = None
        self.port = 0
        self.width = DEFAULT_WIDTH
        self.height = DEFAULT_HEIGHT
        self._xvfb: subprocess.Popen[Any] | None = None
        self._xpra: subprocess.Popen[Any] | None = None
        self._lock = threading.RLock()

    @property
    def display_name(self) -> str:
        return f":{self.display}" if self.display is not None else ""

    def ensure_display(self) -> str:
        with self._lock:
            if self._running(self._xvfb) and self.display is not None:
                return self.display_name

            self._stop_locked()
            xvfb = shutil.which("Xvfb")
            if not xvfb:
                return ""

            self.state_dir.mkdir(parents=True, exist_ok=True)
            self.state_dir.chmod(0o700)
            read_fd, write_fd = os.pipe()
            try:
                process = subprocess.Popen(
                    [
                        xvfb,
                        "-displayfd",
                        str(write_fd),
                        "-screen",
                        "0",
                        f"{virtual_desktop.MAX_WIDTH}x{virtual_desktop.MAX_HEIGHT}x24",
                        "+extension",
                        "GLX",
                        "+extension",
                        "RANDR",
                        "+extension",
                        "RENDER",
                        "+extension",
                        "Composite",
                        "-nolisten",
                        "tcp",
                        "-noreset",
                        "-ac",
                    ],
                    stdin=subprocess.DEVNULL,
                    stdout=subprocess.DEVNULL,
                    stderr=subprocess.DEVNULL,
                    pass_fds=(write_fd,),
                )
            except OSError:
                os.close(read_fd)
                return ""
            finally:
                os.close(write_fd)

            try:
                ready, _, _ = select.select([read_fd], [], [], 5)
                display_number = os.read(read_fd, 32).decode().strip() if ready else ""
            finally:
                os.close(read_fd)

            if not display_number.isdigit() or process.poll() is not None:
                self._terminate(process)
                return ""

            self._xvfb = process
            self.display = int(display_number)
            self._apply_keyboard_layout()
            self.resize(self.width, self.height)
            return self.display_name

    def ensure_viewer(self, width: int = 0, height: int = 0) -> dict[str, Any]:
        with self._lock:
            display_name = self.ensure_display()
            if not display_name:
                return self._unavailable("Xvfb is unavailable.")

            status = collect_status()
            if not status["available"]:
                return self._unavailable(
                    f"Interactive Browser runtime needs: {', '.join(status['missing'])}."
                )

            self.resize(width or self.width, height or self.height)
            if not self._running(self._xpra):
                try:
                    self._start_xpra(str(status["binaries"]["xpra"]))
                except Exception as exc:
                    PrintStyle.warning(f"Interactive Browser viewer failed to start: {exc}")
                    self._terminate(self._xpra)
                    self._xpra = None
                    self.port = 0
                    virtual_desktop.unregister_session(self.token)
                    return self._unavailable(str(exc))

            virtual_desktop.register_session(
                token=self.token,
                host="127.0.0.1",
                port=self.port,
                owner="browser",
                title="Browser",
                resize=self.resize,
            )
            return {
                "available": True,
                "token": self.token,
                "url": virtual_desktop.session_url(
                    self.token,
                    title="Browser",
                    encoding="",
                    quality=90,
                    speed=90,
                    file_transfer=False,
                    printing=False,
                ),
                "width": self.width,
                "height": self.height,
            }

    def resize(self, width: int, height: int) -> dict[str, Any]:
        with self._lock:
            target_width, target_height = virtual_desktop.normalize_size(width, height)
            self.width = target_width
            self.height = target_height
            if self.display is None or not self._running(self._xvfb):
                return {
                    "ok": False,
                    "error": "Browser display is unavailable.",
                    "width": target_width,
                    "height": target_height,
                }
            result = virtual_desktop.resize_display(
                display=self.display,
                width=target_width,
                height=target_height,
                settle_seconds=0,
            )
            return result

    def close(self) -> None:
        with self._lock:
            self._stop_locked()
        shutil.rmtree(self.state_dir, ignore_errors=True)

    def _apply_keyboard_layout(self) -> None:
        """Apply the configured XKB layout to this private display."""
        if self.display is None:
            return
        options = keyboard_options()
        if not options["layout"]:
            return
        setxkbmap = shutil.which("setxkbmap")
        if not setxkbmap:
            return
        command = [setxkbmap, "-display", self.display_name, "-layout", options["layout"]]
        if options["variant"]:
            command.extend(["-variant", options["variant"]])
        try:
            subprocess.run(
                command,
                check=False,
                stdin=subprocess.DEVNULL,
                stdout=subprocess.DEVNULL,
                stderr=subprocess.DEVNULL,
                timeout=5,
            )
        except (OSError, subprocess.TimeoutExpired):
            pass

    def _keyboard_xpra_args(self) -> list[str]:
        options = keyboard_options()
        if not options["layout"]:
            return []
        args = [
            "--keyboard-sync=no",
            "--keyboard-layout", options["layout"],
        ]
        if options["variant"]:
            args.extend(["--keyboard-variant", options["variant"]])
        return args

    def _start_xpra(self, xpra: str) -> None:
        self.port = self._free_port()
        runtime_dir = self.state_dir / "runtime"
        socket_dir = self.state_dir / "sockets"
        runtime_dir.mkdir(parents=True, exist_ok=True)
        socket_dir.mkdir(parents=True, exist_ok=True)
        runtime_dir.chmod(0o700)
        env = {
            **os.environ,
            "DISPLAY": self.display_name,
            "XDG_RUNTIME_DIR": str(runtime_dir),
        }
        self._xpra = subprocess.Popen(
            [
                xpra,
                "shadow",
                self.display_name,
                "--daemon=no",
                "--mdns=no",
                "--html=on",
                "--tray=no",
                "--system-tray=no",
                "--notifications=no",
                "--clipboard=yes",
                "--clipboard-direction=both",
                "--file-transfer=no",
                "--open-files=no",
                "--open-url=no",
                "--printing=no",
                "--audio=no",
                "--speaker=off",
                "--microphone=off",
                "--sharing=yes",
                "--resize-display=yes",
                "--encoding=auto",
                "--quality=90",
                "--speed=90",
                *self._keyboard_xpra_args(),
                f"--bind-tcp=127.0.0.1:{self.port}",
                f"--socket-dir={socket_dir}",
                f"--log-dir={self.state_dir}",
                "--log-file=xpra.log",
            ],
            stdin=subprocess.DEVNULL,
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL,
            env=env,
        )
        self._wait_for_port(self._xpra, self.port)

    def _stop_locked(self) -> None:
        virtual_desktop.unregister_session(self.token)
        self._terminate(self._xpra)
        self._terminate(self._xvfb)
        self._xpra = None
        self._xvfb = None
        self.port = 0
        self.display = None

    def _unavailable(self, error: str) -> dict[str, Any]:
        return {
            "available": False,
            "error": str(error or "Interactive Browser viewer is unavailable."),
        }

    @staticmethod
    def _running(process: subprocess.Popen[Any] | None) -> bool:
        return bool(process and process.poll() is None)

    @staticmethod
    def _terminate(process: subprocess.Popen[Any] | None) -> None:
        if not process or process.poll() is not None:
            return
        process.terminate()
        try:
            process.wait(timeout=2)
        except subprocess.TimeoutExpired:
            process.kill()
            process.wait(timeout=2)

    @staticmethod
    def _free_port() -> int:
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as probe:
            probe.bind(("127.0.0.1", 0))
            return int(probe.getsockname()[1])

    @staticmethod
    def _wait_for_port(
        process: subprocess.Popen[Any],
        port: int,
        timeout: float = START_TIMEOUT_SECONDS,
    ) -> None:
        deadline = time.monotonic() + timeout
        while time.monotonic() < deadline:
            if process.poll() is not None:
                raise RuntimeError("Xpra exited before its Browser endpoint was ready.")
            try:
                with socket.create_connection(("127.0.0.1", port), timeout=0.2):
                    return
            except OSError:
                time.sleep(0.1)
        raise TimeoutError("Timed out waiting for the interactive Browser endpoint.")