1# Copyright 2024 The Chromium Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5from __future__ import annotations 6 7from typing import TYPE_CHECKING 8 9from crossbench import path as pth 10from crossbench import plt 11from crossbench.plt.linux_ssh import LinuxSshPlatform 12 13if TYPE_CHECKING: 14 from typing import Optional 15 16 from crossbench.flags.chrome import ChromeFlags 17 from crossbench.plt.base import ListCmdArgs 18 19 20class ChromeOsSshPlatform(LinuxSshPlatform): 21 22 AUTOLOGIN_PATH = pth.AnyPosixPath("/usr/local/autotest/bin/autologin.py") 23 DEVTOOLS_PORT_PATH = pth.AnyPosixPath("/home/chronos/DevToolsActivePort") 24 25 def __init__(self, *args, **kwargs): 26 self._username: Optional[str] = None 27 super().__init__(*args, **kwargs) 28 29 @property 30 def name(self) -> str: 31 return "chromeos_ssh" 32 33 @property 34 def username(self) -> Optional[str]: 35 return self._username 36 37 @property 38 def is_chromeos(self) -> bool: 39 return True 40 41 def create_debugging_session(self, 42 browser_flags: Optional[ChromeFlags] = None, 43 username: Optional[str] = None, 44 password: Optional[str] = None) -> int: 45 try: 46 args: ListCmdArgs = [self.AUTOLOGIN_PATH] 47 if username and password: 48 self._username = username 49 args.extend(("-u", username, "-p", password)) 50 if browser_flags: 51 args.append("--") 52 args.extend(browser_flags) 53 self.sh(*args) 54 except plt.SubprocessError as e: 55 raise RuntimeError("Autologin failed.") from e 56 try: 57 dbg_port = self.cat(self.DEVTOOLS_PORT_PATH).splitlines()[0].strip() 58 except plt.SubprocessError as e: 59 raise RuntimeError("Could not read remote debugging port.") from e 60 return int(dbg_port) 61