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