| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | |
| 5 import json | 4 import json |
| 6 import logging | 5 import logging |
| 7 import os | 6 import os |
| 8 import subprocess | 7 import subprocess |
| 9 import sys | 8 import sys |
| 10 import tempfile | 9 import tempfile |
| 11 import time | 10 import time |
| 12 | 11 |
| 13 from telemetry import adb_commands | 12 from telemetry.core import exceptions |
| 14 from telemetry import browser_backend | 13 from telemetry.core.chrome import adb_commands |
| 15 from telemetry import browser_gone_exception | 14 from telemetry.core.chrome import browser_backend |
| 16 | 15 |
| 17 class AndroidBrowserBackend(browser_backend.BrowserBackend): | 16 class AndroidBrowserBackend(browser_backend.BrowserBackend): |
| 18 """The backend for controlling a browser instance running on Android. | 17 """The backend for controlling a browser instance running on Android. |
| 19 """ | 18 """ |
| 20 def __init__(self, options, adb, package, is_content_shell, | 19 def __init__(self, options, adb, package, is_content_shell, |
| 21 cmdline_file, activity, devtools_remote_port): | 20 cmdline_file, activity, devtools_remote_port): |
| 22 super(AndroidBrowserBackend, self).__init__( | 21 super(AndroidBrowserBackend, self).__init__( |
| 23 is_content_shell=is_content_shell, | 22 is_content_shell=is_content_shell, |
| 24 supports_extensions=False, options=options) | 23 supports_extensions=False, options=options) |
| 25 if len(options.extensions_to_load) > 0: | 24 if len(options.extensions_to_load) > 0: |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 timeout = 3 | 90 timeout = 3 |
| 92 time.sleep(timeout) | 91 time.sleep(timeout) |
| 93 while not self._adb.Adb().GetProtectedFileContents(prefs_file): | 92 while not self._adb.Adb().GetProtectedFileContents(prefs_file): |
| 94 time.sleep(timeout) | 93 time.sleep(timeout) |
| 95 retries += 1 | 94 retries += 1 |
| 96 timeout *= 2 | 95 timeout *= 2 |
| 97 if retries == 3: | 96 if retries == 3: |
| 98 logging.critical('android_browser_backend: Could not find ' | 97 logging.critical('android_browser_backend: Could not find ' |
| 99 'preferences file %s for %s', | 98 'preferences file %s for %s', |
| 100 prefs_file, self._package) | 99 prefs_file, self._package) |
| 101 raise browser_gone_exception.BrowserGoneException( | 100 raise exceptions.BrowserGoneException('Missing preferences file.') |
| 102 'Missing preferences file.') | |
| 103 self._adb.CloseApplication(self._package) | 101 self._adb.CloseApplication(self._package) |
| 104 | 102 |
| 105 preferences = json.loads(''.join( | 103 preferences = json.loads(''.join( |
| 106 self._adb.Adb().GetProtectedFileContents(prefs_file))) | 104 self._adb.Adb().GetProtectedFileContents(prefs_file))) |
| 107 changed = False | 105 changed = False |
| 108 if 'devtools' not in preferences: | 106 if 'devtools' not in preferences: |
| 109 preferences['devtools'] = {} | 107 preferences['devtools'] = {} |
| 110 changed = True | 108 changed = True |
| 111 if not preferences['devtools'].get('remote_enabled'): | 109 if not preferences['devtools'].get('remote_enabled'): |
| 112 preferences['devtools']['remote_enabled'] = True | 110 preferences['devtools']['remote_enabled'] = True |
| 113 changed = True | 111 changed = True |
| 114 if changed: | 112 if changed: |
| 115 logging.warning('Manually enabled devtools protocol on %s' % | 113 logging.warning('Manually enabled devtools protocol on %s' % |
| 116 self._package) | 114 self._package) |
| 117 txt = json.dumps(preferences, indent=2) | 115 txt = json.dumps(preferences, indent=2) |
| 118 self._adb.Adb().SetProtectedFileContents(prefs_file, txt) | 116 self._adb.Adb().SetProtectedFileContents(prefs_file, txt) |
| 119 | 117 |
| 120 # Start it up with a fresh log. | 118 # Start it up with a fresh log. |
| 121 self._adb.RunShellCommand('logcat -c') | 119 self._adb.RunShellCommand('logcat -c') |
| 122 self._adb.StartActivity(self._package, | 120 self._adb.StartActivity(self._package, |
| 123 self._activity, | 121 self._activity, |
| 124 True, | 122 True, |
| 125 None, | 123 None, |
| 126 'chrome://newtab/') | 124 'chrome://newtab/') |
| 127 try: | 125 try: |
| 128 self._WaitForBrowserToComeUp() | 126 self._WaitForBrowserToComeUp() |
| 129 self._PostBrowserStartupInitialization() | 127 self._PostBrowserStartupInitialization() |
| 130 except browser_gone_exception.BrowserGoneException: | 128 except exceptions.BrowserGoneException: |
| 131 logging.critical('Failed to connect to browser.') | 129 logging.critical('Failed to connect to browser.') |
| 132 if not self._adb.IsRootEnabled(): | 130 if not self._adb.IsRootEnabled(): |
| 133 logging.critical( | 131 logging.critical( |
| 134 'Ensure web debugging is enabled in Chrome at ' | 132 'Ensure web debugging is enabled in Chrome at ' |
| 135 '"Settings > Developer tools > Enable USB Web debugging".') | 133 '"Settings > Developer tools > Enable USB Web debugging".') |
| 136 sys.exit(1) | 134 sys.exit(1) |
| 137 except: | 135 except: |
| 138 import traceback | 136 import traceback |
| 139 traceback.print_exc() | 137 traceback.print_exc() |
| 140 self.Close() | 138 self.Close() |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 '-dump', f.name], stdout=subprocess.PIPE).communicate()[0] | 179 '-dump', f.name], stdout=subprocess.PIPE).communicate()[0] |
| 182 except Exception: | 180 except Exception: |
| 183 pass | 181 pass |
| 184 if symbolized_stack: | 182 if symbolized_stack: |
| 185 return symbolized_stack | 183 return symbolized_stack |
| 186 # Otherwise, just return the last 100 lines of logcat. | 184 # Otherwise, just return the last 100 lines of logcat. |
| 187 return '\n'.join(self._adb.RunShellCommand('logcat -d -t 100')) | 185 return '\n'.join(self._adb.RunShellCommand('logcat -d -t 100')) |
| 188 | 186 |
| 189 def CreateForwarder(self, *port_pairs): | 187 def CreateForwarder(self, *port_pairs): |
| 190 return adb_commands.Forwarder(self._adb, *port_pairs) | 188 return adb_commands.Forwarder(self._adb, *port_pairs) |
| OLD | NEW |