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