1424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)# Copyright 2013 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)"""Finds CrOS browsers that can be controlled by telemetry."""
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)import logging
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)from telemetry.core import platform as platform_module
106e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)from telemetry.core import browser
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)from telemetry.core import possible_browser
121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccifrom telemetry.core.platform import cros_device
131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccifrom telemetry.core.platform import cros_interface
14424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)from telemetry.core.backends.chrome import cros_browser_backend
156e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)from telemetry.core.backends.chrome import cros_browser_with_oobe
161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccidef _IsRunningOnCrOS():
191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return platform_module.GetHostPlatform().GetOSName() == 'chromeos'
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class PossibleCrOSBrowser(possible_browser.PossibleBrowser):
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  """A launchable CrOS browser instance."""
241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  def __init__(self, browser_type, finder_options, cros_platform, is_guest):
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    super(PossibleCrOSBrowser, self).__init__(browser_type, 'cros',
26116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        finder_options, True)
2703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    assert browser_type in FindAllBrowserTypes(finder_options), \
286e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        ('Please add %s to cros_browser_finder.FindAllBrowserTypes()' %
296e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)         browser_type)
301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    self._platform = cros_platform
311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    self._platform_backend = (
321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        cros_platform._platform_backend)  # pylint: disable=W0212
33c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    self._is_guest = is_guest
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  def __repr__(self):
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return 'PossibleCrOSBrowser(browser_type=%s)' % self.browser_type
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  def _InitPlatformIfNeeded(self):
391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    pass
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  def Create(self):
42424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    if self.finder_options.output_profile_path:
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      raise NotImplementedError(
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          'Profile generation is not yet supported on CrOS.')
4590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    browser_options = self.finder_options.browser_options
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    backend = cros_browser_backend.CrOSBrowserBackend(
481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        browser_options, self._platform_backend.cri, self._is_guest,
4958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        extensions_to_load=self.finder_options.extensions_to_load)
504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    if browser_options.create_browser_with_oobe:
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      return cros_browser_with_oobe.CrOSBrowserWithOOBE(
521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          backend,
531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          self._platform_backend,
541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          self._archive_path,
551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          self._append_to_existing_wpr,
561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          self._make_javascript_deterministic,
571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          self._credentials_path)
581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    return browser.Browser(backend,
591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                           self._platform_backend,
601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                           self._archive_path,
611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                           self._append_to_existing_wpr,
621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                           self._make_javascript_deterministic,
631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                           self._credentials_path)
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
65424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  def SupportsOptions(self, finder_options):
66424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    if (len(finder_options.extensions_to_load) != 0) and self._is_guest:
67eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      return False
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return True
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  def UpdateExecutableIfNeeded(self):
714e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    pass
724e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
73bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdochdef SelectDefaultBrowser(possible_browsers):
741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if _IsRunningOnCrOS():
75bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch    for b in possible_browsers:
76bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch      if b.browser_type == 'system':
77bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch        return b
78bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  return None
79bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch
80424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)def CanFindAvailableBrowsers(finder_options):
811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return (_IsRunningOnCrOS() or
82424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          finder_options.cros_remote or
83558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch          cros_interface.HasSSH())
84558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch
8503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)def FindAllBrowserTypes(_):
866e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return [
876e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      'cros-chrome',
886e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      'cros-chrome-guest',
896e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      'system',
906e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      'system-guest',
916e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  ]
926e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
93424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)def FindAllAvailableBrowsers(finder_options):
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  """Finds all available CrOS browsers, locally and remotely."""
951320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if _IsRunningOnCrOS():
96424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return [PossibleCrOSBrowser('system', finder_options,
971320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                platform_module.GetHostPlatform(),
987dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                is_guest=False),
99424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)            PossibleCrOSBrowser('system-guest', finder_options,
1001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                platform_module.GetHostPlatform(),
1017dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                is_guest=True)]
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
103424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if finder_options.cros_remote == None:
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    logging.debug('No --remote specified, will not probe for CrOS.')
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return []
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if not cros_interface.HasSSH():
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    logging.debug('ssh not found. Cannot talk to CrOS devices.')
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return []
1101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  device = cros_device.CrOSDevice(
1111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      finder_options.cros_remote, finder_options.cros_ssh_identity)
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  # Check ssh
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  try:
1141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    platform = platform_module.GetPlatformForDevice(device)
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  except cros_interface.LoginException, ex:
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if isinstance(ex, cros_interface.KeylessLoginRequiredException):
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      logging.warn('Could not ssh into %s. Your device must be configured',
118424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                   finder_options.cros_remote)
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      logging.warn('to allow passwordless login as root.')
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      logging.warn('For a test-build device, pass this to your script:')
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      logging.warn('   --identity $(CHROMITE)/ssh_keys/testing_rsa')
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      logging.warn('')
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      logging.warn('For a developer-mode device, the steps are:')
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      logging.warn(' - Ensure you have an id_rsa.pub (etc) on this computer')
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      logging.warn(' - On the chromebook:')
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      logging.warn('   -  Control-Alt-T; shell; sudo -s')
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      logging.warn('   -  openssh-server start')
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      logging.warn('   -  scp <this machine>:.ssh/id_rsa.pub /tmp/')
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      logging.warn('   -  mkdir /root/.ssh')
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      logging.warn('   -  chown go-rx /root/.ssh')
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      logging.warn('   -  cat /tmp/id_rsa.pub >> /root/.ssh/authorized_keys')
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      logging.warn('   -  chown 0600 /root/.ssh/authorized_keys')
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      logging.warn('There, that was easy!')
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      logging.warn('')
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      logging.warn('P.S. Please, tell your manager how INANE this is.')
136c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    from telemetry.core import browser_finder
137c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    raise browser_finder.BrowserFinderException(str(ex))
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return [PossibleCrOSBrowser('cros-chrome', finder_options, platform,
140424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                              is_guest=False),
1411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          PossibleCrOSBrowser('cros-chrome-guest', finder_options, platform,
142424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                              is_guest=True)]
143