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