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
5import logging
6import time
7
8from telemetry.core import exceptions
9
10
11class AndroidBrowserBackendSettings(object):
12
13  def __init__(self, activity, cmdline_file, package, pseudo_exec_name,
14               supports_tab_control):
15    self._activity = activity
16    self._cmdline_file = cmdline_file
17    self._package = package
18    self._pseudo_exec_name = pseudo_exec_name
19    self._supports_tab_control = supports_tab_control
20
21  @property
22  def activity(self):
23    return self._activity
24
25  @property
26  def package(self):
27    return self._package
28
29  @property
30  def pseudo_exec_name(self):
31    return self._pseudo_exec_name
32
33  @property
34  def supports_tab_control(self):
35    return self._supports_tab_control
36
37  def GetCommandLineFile(self, is_user_debug_build):
38    del is_user_debug_build  # unused
39    return self._cmdline_file
40
41  def GetDevtoolsRemotePort(self, device):
42    raise NotImplementedError()
43
44  @property
45  def profile_ignore_list(self):
46    # Don't delete lib, since it is created by the installer.
47    return ['lib']
48
49
50class ChromeBackendSettings(AndroidBrowserBackendSettings):
51  # Stores a default Preferences file, re-used to speed up "--page-repeat".
52  _default_preferences_file = None
53
54  def GetCommandLineFile(self, is_user_debug_build):
55    if is_user_debug_build:
56      return '/data/local/tmp/chrome-command-line'
57    else:
58      return '/data/local/chrome-command-line'
59
60  def __init__(self, package):
61    super(ChromeBackendSettings, self).__init__(
62        activity='com.google.android.apps.chrome.Main',
63        cmdline_file=None,
64        package=package,
65        pseudo_exec_name='chrome',
66        supports_tab_control=True)
67
68  def GetDevtoolsRemotePort(self, device):
69    return 'localabstract:chrome_devtools_remote'
70
71
72class ContentShellBackendSettings(AndroidBrowserBackendSettings):
73  def __init__(self, package):
74    super(ContentShellBackendSettings, self).__init__(
75        activity='org.chromium.content_shell_apk.ContentShellActivity',
76        cmdline_file='/data/local/tmp/content-shell-command-line',
77        package=package,
78        pseudo_exec_name='content_shell',
79        supports_tab_control=False)
80
81  def GetDevtoolsRemotePort(self, device):
82    return 'localabstract:content_shell_devtools_remote'
83
84
85class WebviewBackendSettings(AndroidBrowserBackendSettings):
86  def __init__(self,
87               package,
88               activity='org.chromium.webview_shell.TelemetryActivity',
89               cmdline_file='/data/local/tmp/webview-command-line'):
90    super(WebviewBackendSettings, self).__init__(
91        activity=activity,
92        cmdline_file=cmdline_file,
93        package=package,
94        pseudo_exec_name='webview',
95        supports_tab_control=False)
96
97  def GetDevtoolsRemotePort(self, device):
98    # The DevTools socket name for WebView depends on the activity PID's.
99    retries = 0
100    timeout = 1
101    pid = None
102    while True:
103      pids = device.GetPids(self.package)
104      if not pids or self.package not in pids:
105        time.sleep(timeout)
106        retries += 1
107        timeout *= 2
108        if retries == 4:
109          logging.critical('android_browser_backend: Timeout while waiting for '
110                           'activity %s:%s to come up',
111                           self.package,
112                           self.activity)
113          raise exceptions.BrowserGoneException(self.browser,
114                                                'Timeout waiting for PID.')
115      if len(pids[self.package]) > 1:
116        raise Exception(
117            'At most one instance of process %s expected but found pids: '
118            '%s' % (self.package, pids))
119      pid = pids[self.package][0]
120      break
121    return 'localabstract:webview_devtools_remote_%s' % str(pid)
122
123
124class WebviewShellBackendSettings(WebviewBackendSettings):
125  def __init__(self, package):
126    super(WebviewShellBackendSettings, self).__init__(
127        activity='org.chromium.android_webview.shell.AwShellActivity',
128        cmdline_file='/data/local/tmp/android-webview-command-line',
129        package=package)
130