Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / core / backends / chrome / cros_browser_finder.py
1 # Copyright 2013 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
5 """Finds CrOS browsers that can be controlled by telemetry."""
6
7 import logging
8
9 from telemetry import decorators
10 from telemetry.core import browser
11 from telemetry.core import possible_browser
12 from telemetry.core.backends.chrome import cros_browser_with_oobe
13 from telemetry.core.backends.chrome import cros_browser_backend
14 from telemetry.core.backends.chrome import cros_interface
15 from telemetry.core.platform import cros_platform_backend
16
17 ALL_BROWSER_TYPES = [
18     'cros-chrome',
19     'cros-chrome-guest',
20     'system',
21     'system-guest',
22     ]
23
24
25 class PossibleCrOSBrowser(possible_browser.PossibleBrowser):
26   """A launchable CrOS browser instance."""
27   def __init__(self, browser_type, finder_options, cri, is_guest):
28     super(PossibleCrOSBrowser, self).__init__(browser_type, 'cros',
29         finder_options)
30     assert browser_type in ALL_BROWSER_TYPES, \
31         'Please add %s to ALL_BROWSER_TYPES' % browser_type
32     self._cri = cri
33     self._is_guest = is_guest
34     self._platform = None
35
36   def __repr__(self):
37     return 'PossibleCrOSBrowser(browser_type=%s)' % self.browser_type
38
39   @property
40   @decorators.Cache
41   def _platform_backend(self):
42     return cros_platform_backend.CrosPlatformBackend(self._cri)
43
44   def Create(self):
45     if self.finder_options.output_profile_path:
46       raise NotImplementedError(
47           'Profile generation is not yet supported on CrOS.')
48
49     browser_options = self.finder_options.browser_options
50     backend = cros_browser_backend.CrOSBrowserBackend(
51         self.browser_type, browser_options, self._cri, self._is_guest,
52         extensions_to_load=self.finder_options.extensions_to_load)
53     if browser_options.create_browser_with_oobe:
54       return cros_browser_with_oobe.CrOSBrowserWithOOBE(
55           backend, self._platform_backend)
56     return browser.Browser(backend, self._platform_backend)
57
58   def SupportsOptions(self, finder_options):
59     if (len(finder_options.extensions_to_load) != 0) and self._is_guest:
60       return False
61     return True
62
63   def UpdateExecutableIfNeeded(self):
64     pass
65
66 def SelectDefaultBrowser(possible_browsers):
67   if cros_interface.IsRunningOnCrosDevice():
68     for b in possible_browsers:
69       if b.browser_type == 'system':
70         return b
71   return None
72
73 def CanFindAvailableBrowsers(finder_options):
74   return (cros_interface.IsRunningOnCrosDevice() or
75           finder_options.cros_remote or
76           cros_interface.HasSSH())
77
78 def FindAllAvailableBrowsers(finder_options):
79   """Finds all available CrOS browsers, locally and remotely."""
80   if cros_interface.IsRunningOnCrosDevice():
81     return [PossibleCrOSBrowser('system', finder_options,
82                                 cros_interface.CrOSInterface(),
83                                 is_guest=False),
84             PossibleCrOSBrowser('system-guest', finder_options,
85                                 cros_interface.CrOSInterface(),
86                                 is_guest=True)]
87
88   if finder_options.cros_remote == None:
89     logging.debug('No --remote specified, will not probe for CrOS.')
90     return []
91
92   if not cros_interface.HasSSH():
93     logging.debug('ssh not found. Cannot talk to CrOS devices.')
94     return []
95   cri = cros_interface.CrOSInterface(finder_options.cros_remote,
96                                      finder_options.cros_ssh_identity)
97
98   # Check ssh
99   try:
100     cri.TryLogin()
101   except cros_interface.LoginException, ex:
102     if isinstance(ex, cros_interface.KeylessLoginRequiredException):
103       logging.warn('Could not ssh into %s. Your device must be configured',
104                    finder_options.cros_remote)
105       logging.warn('to allow passwordless login as root.')
106       logging.warn('For a test-build device, pass this to your script:')
107       logging.warn('   --identity $(CHROMITE)/ssh_keys/testing_rsa')
108       logging.warn('')
109       logging.warn('For a developer-mode device, the steps are:')
110       logging.warn(' - Ensure you have an id_rsa.pub (etc) on this computer')
111       logging.warn(' - On the chromebook:')
112       logging.warn('   -  Control-Alt-T; shell; sudo -s')
113       logging.warn('   -  openssh-server start')
114       logging.warn('   -  scp <this machine>:.ssh/id_rsa.pub /tmp/')
115       logging.warn('   -  mkdir /root/.ssh')
116       logging.warn('   -  chown go-rx /root/.ssh')
117       logging.warn('   -  cat /tmp/id_rsa.pub >> /root/.ssh/authorized_keys')
118       logging.warn('   -  chown 0600 /root/.ssh/authorized_keys')
119       logging.warn('There, that was easy!')
120       logging.warn('')
121       logging.warn('P.S. Please, tell your manager how INANE this is.')
122
123     from telemetry.core import browser_finder
124     raise browser_finder.BrowserFinderException(str(ex))
125
126   if not cri.FileExistsOnDevice('/opt/google/chrome/chrome'):
127     logging.warn('Could not find a chrome on ' % cri.hostname)
128
129   return [PossibleCrOSBrowser('cros-chrome', finder_options, cri,
130                               is_guest=False),
131           PossibleCrOSBrowser('cros-chrome-guest', finder_options, cri,
132                               is_guest=True)]