1# Copyright (c) 2012 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4"""Finds CrOS browsers that can be controlled by telemetry."""
5
6import logging
7
8from telemetry.core import browser
9from telemetry.core import possible_browser
10from telemetry.core import profile_types
11from telemetry.core.chrome import cros_browser_backend
12from telemetry.core.chrome import cros_interface
13from telemetry.core.platform import cros_platform_backend
14
15ALL_BROWSER_TYPES = ','.join([
16    'cros-chrome',
17    'cros-chrome-guest',
18    'system-guest',
19    ])
20
21class PossibleCrOSBrowser(possible_browser.PossibleBrowser):
22  """A launchable chromeos browser instance."""
23  def __init__(self, browser_type, options, cri, is_guest):
24    super(PossibleCrOSBrowser, self).__init__(browser_type, options)
25    self._cri = cri
26    self._is_guest = is_guest
27
28  def __repr__(self):
29    return 'PossibleCrOSBrowser(browser_type=%s)' % self.browser_type
30
31  def Create(self):
32    if profile_types.GetProfileCreator(self.options.profile_type):
33      raise Exception("Profile creation not currently supported on Chrome OS")
34
35    backend = cros_browser_backend.CrOSBrowserBackend(
36        self.browser_type, self._options, self._cri, self._is_guest)
37    b = browser.Browser(backend,
38                        cros_platform_backend.CrosPlatformBackend(self._cri))
39    return b
40
41  def SupportsOptions(self, options):
42    if (len(options.extensions_to_load) != 0) and self._is_guest:
43      return False
44    return True
45
46def SelectDefaultBrowser(possible_browsers):
47  if cros_interface.IsRunningOnCrosDevice():
48    for b in possible_browsers:
49      if b.browser_type == 'system':
50        return b
51  return None
52
53def CanFindAvailableBrowsers(options):
54  return (cros_interface.IsRunningOnCrosDevice() or
55          options.cros_remote or
56          cros_interface.HasSSH())
57
58def FindAllAvailableBrowsers(options):
59  """Finds all available chromeos browsers, locally and remotely."""
60  if cros_interface.IsRunningOnCrosDevice():
61    return [PossibleCrOSBrowser('system', options,
62                                cros_interface.CrOSInterface(),
63                                is_guest=False),
64            PossibleCrOSBrowser('system-guest', options,
65                                cros_interface.CrOSInterface(),
66                                is_guest=True)]
67
68  if options.cros_remote == None:
69    logging.debug('No --remote specified, will not probe for CrOS.')
70    return []
71
72  if not cros_interface.HasSSH():
73    logging.debug('ssh not found. Cannot talk to CrOS devices.')
74    return []
75  cri = cros_interface.CrOSInterface(options.cros_remote,
76                                     options.cros_ssh_identity)
77
78  # Check ssh
79  try:
80    cri.TryLogin()
81  except cros_interface.LoginException, ex:
82    if isinstance(ex, cros_interface.KeylessLoginRequiredException):
83      logging.warn('Could not ssh into %s. Your device must be configured',
84                      options.cros_remote)
85      logging.warn('to allow passwordless login as root.')
86      logging.warn('For a test-build device, pass this to your script:')
87      logging.warn('   --identity $(CHROMITE)/ssh_keys/testing_rsa')
88      logging.warn('')
89      logging.warn('For a developer-mode device, the steps are:')
90      logging.warn(' - Ensure you have an id_rsa.pub (etc) on this computer')
91      logging.warn(' - On the chromebook:')
92      logging.warn('   -  Control-Alt-T; shell; sudo -s')
93      logging.warn('   -  openssh-server start')
94      logging.warn('   -  scp <this machine>:.ssh/id_rsa.pub /tmp/')
95      logging.warn('   -  mkdir /root/.ssh')
96      logging.warn('   -  chown go-rx /root/.ssh')
97      logging.warn('   -  cat /tmp/id_rsa.pub >> /root/.ssh/authorized_keys')
98      logging.warn('   -  chown 0600 /root/.ssh/authorized_keys')
99      logging.warn('There, that was easy!')
100      logging.warn('')
101      logging.warn('P.S. Please, tell your manager how INANE this is.')
102
103    from telemetry.core import browser_finder
104    raise browser_finder.BrowserFinderException(str(ex))
105
106  if not cri.FileExistsOnDevice('/opt/google/chrome/chrome'):
107    logging.warn('Could not find a chrome on ' % cri.hostname)
108
109  return [PossibleCrOSBrowser('cros-chrome', options, cri, is_guest=False),
110          PossibleCrOSBrowser('cros-chrome-guest', options, cri, is_guest=True)]
111