browser_runtime.py 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """Read-only full-Chromium selection shared by browser launchers.
  2. Nothing here installs, downloads or starts anything: a caller that may provision (the launcher's
  3. one-shot ``pm.ensure("chromium")``) does so itself and then asks again.
  4. """
  5. from __future__ import annotations
  6. import os
  7. from pathlib import Path
  8. import pm
  9. # Written by the Docker image build with the pm Chromium binary it staged. The binary's path differs
  10. # per architecture (chrome-linux64/chrome vs chromium-linux-arm64/chromium), so the build resolves it
  11. # once; it also survives a volume over the runtime dir that hides pm's facts.
  12. BAKED_EXECUTABLE_FILE = Path("/etc/hermes/chromium-executable-path")
  13. # Distribution command names for a system Chromium, used only where pm builds none for this target
  14. # (Termux: `pkg install chromium` from x11-repo).
  15. _SYSTEM_CHROMIUM_COMMANDS = ("chromium", "chromium-browser")
  16. def _configured_executable() -> str | None:
  17. """``browser.executable_path`` from the active profile's config (managed overlay applied)."""
  18. from hermes_cli.config import cfg_get, load_config_readonly
  19. value = cfg_get(load_config_readonly(), "browser", "executable_path", default="")
  20. value = os.path.expanduser(value.strip()) if isinstance(value, str) else ""
  21. return value or None
  22. def _pm_executable() -> str | None:
  23. installed = pm.installed_package("chromium")
  24. return str(installed.binary) if installed and installed.binary is not None else None
  25. def _baked_executable() -> str | None:
  26. try:
  27. value = BAKED_EXECUTABLE_FILE.read_text(encoding="utf-8-sig").strip()
  28. except OSError: # not a Hermes Docker image: nothing baked
  29. return None
  30. return value if value and os.path.isfile(value) else None
  31. def _pm_has_no_chromium() -> bool:
  32. """pm publishes no Chromium for this target (Termux/Android, musl)."""
  33. return pm.get_package("chromium").missing_reason(pm.current_target()) is not None
  34. def _system_executable() -> str | None:
  35. from hermes_platform.resolver import locate_command
  36. for name in _SYSTEM_CHROMIUM_COMMANDS:
  37. found = locate_command(name).command
  38. if found:
  39. return found[0]
  40. return None
  41. def chromium_executable(*, allow_override: bool = True) -> str | None:
  42. """The full Chromium a browser launcher should run, or ``None`` when none is available.
  43. Order: ``browser.executable_path`` (skipped with ``allow_override=False``, for callers that need
  44. pm's own build) -> pm's installed ``chromium`` -> the Docker image's baked path -> a system
  45. ``chromium`` / ``chromium-browser`` on PATH, only on targets pm builds no Chromium for (Termux).
  46. """
  47. if allow_override and (override := _configured_executable()):
  48. return override
  49. if found := _pm_executable() or _baked_executable():
  50. return found
  51. return _system_executable() if _pm_has_no_chromium() else None
  52. def chromium_missing_hint() -> str:
  53. """One actionable line for "no Chromium" on this machine. Never suggests npm."""
  54. from hermes_platform.host.runtime import is_termux
  55. if pinned := _configured_executable(): # the pin wins over every other source, so name it
  56. return (f"browser.executable_path is set to {pinned}, which is not a browser on this machine: fix the "
  57. "path, or clear it to use Hermes's Chromium.")
  58. if is_termux():
  59. return ("No Chromium found: pm builds none for Termux. Install one with "
  60. "`pkg install x11-repo && pkg install chromium`, or set browser.executable_path.")
  61. if BAKED_EXECUTABLE_FILE.exists():
  62. return ("The image's Chromium is missing: pull the current Hermes image and recreate the "
  63. "container (a volume mounted over the image's tool store hides it).")
  64. if _pm_has_no_chromium():
  65. return ("No Chromium found: pm builds none for this platform. Install your distribution's "
  66. "chromium package, or set browser.executable_path.")
  67. return "Chromium is not installed: run `hermes pm install chromium`."