OLD | NEW |
1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 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 | 4 |
5 import contextlib | 5 import contextlib |
6 import logging | 6 import logging |
7 import os | 7 import os |
8 import shutil | 8 import shutil |
9 import subprocess | 9 import subprocess |
10 import sys | 10 import sys |
11 import tempfile | 11 import tempfile |
12 import time | 12 import time |
13 | 13 |
14 _SRC_DIR = os.path.abspath(os.path.join( | 14 _SRC_DIR = os.path.abspath(os.path.join( |
15 os.path.dirname(__file__), '..', '..', '..')) | 15 os.path.dirname(__file__), '..', '..', '..')) |
16 | 16 |
17 sys.path.append(os.path.join(_SRC_DIR, 'third_party', 'catapult', 'devil')) | 17 sys.path.append(os.path.join(_SRC_DIR, 'third_party', 'catapult', 'devil')) |
18 from devil.android import device_utils | 18 from devil.android import device_utils |
| 19 from devil.android import forwarder |
19 from devil.android.sdk import intent | 20 from devil.android.sdk import intent |
20 | 21 |
21 sys.path.append(os.path.join(_SRC_DIR, 'build', 'android')) | 22 sys.path.append(os.path.join(_SRC_DIR, 'build', 'android')) |
22 from pylib import constants | 23 from pylib import constants |
23 from pylib import flag_changer | 24 from pylib import flag_changer |
24 | 25 |
| 26 sys.path.append(os.path.join(_SRC_DIR, 'tools', 'perf')) |
| 27 from chrome_telemetry_build import chromium_config |
| 28 |
| 29 sys.path.append(chromium_config.GetTelemetryDir()) |
| 30 from telemetry.internal.util import webpagereplay |
| 31 |
25 import devtools_monitor | 32 import devtools_monitor |
26 | 33 |
27 DEVTOOLS_PORT = 9222 | 34 DEVTOOLS_PORT = 9222 |
28 DEVTOOLS_HOSTNAME = 'localhost' | 35 DEVTOOLS_HOSTNAME = 'localhost' |
29 DEFAULT_CHROME_PACKAGE = 'chrome' | 36 DEFAULT_CHROME_PACKAGE = 'chrome' |
30 | 37 |
31 | 38 |
32 @contextlib.contextmanager | 39 @contextlib.contextmanager |
33 def TemporaryDirectory(): | 40 def TemporaryDirectory(): |
34 """Returns a freshly-created directory that gets automatically deleted after | 41 """Returns a freshly-created directory that gets automatically deleted after |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 device.adb.ForwardRemove(local) | 104 device.adb.ForwardRemove(local) |
98 | 105 |
99 | 106 |
100 def _SetUpDevice(device, package_info): | 107 def _SetUpDevice(device, package_info): |
101 """Enables root and closes Chrome on a device.""" | 108 """Enables root and closes Chrome on a device.""" |
102 device.EnableRoot() | 109 device.EnableRoot() |
103 device.KillAll(package_info.package, quiet=True) | 110 device.KillAll(package_info.package, quiet=True) |
104 | 111 |
105 | 112 |
106 @contextlib.contextmanager | 113 @contextlib.contextmanager |
| 114 def WprHost(device, wpr_archive_path, record=False): |
| 115 """Launches web page replay host. |
| 116 |
| 117 Args: |
| 118 device: Android device. |
| 119 wpr_archive_path: host sided WPR archive's path. |
| 120 record: Enables or disables WPR archive recording. |
| 121 |
| 122 Returns: |
| 123 Additional flags list that may be used for chromium to load web page through |
| 124 the running web page replay host. |
| 125 """ |
| 126 assert device |
| 127 if wpr_archive_path == None: |
| 128 yield [] |
| 129 return |
| 130 |
| 131 wpr_server_args = ['--use_closest_match'] |
| 132 if record: |
| 133 wpr_server_args.append('--record') |
| 134 if os.path.exists(wpr_archive_path): |
| 135 os.remove(wpr_archive_path) |
| 136 else: |
| 137 assert os.path.exists(wpr_archive_path) |
| 138 wpr_server = webpagereplay.ReplayServer(wpr_archive_path, |
| 139 '127.0.0.1', 0, 0, None, wpr_server_args) |
| 140 ports = wpr_server.StartServer()[:-1] |
| 141 host_http_port = ports[0] |
| 142 host_https_port = ports[1] |
| 143 |
| 144 forwarder.Forwarder.Map([(0, host_http_port), (0, host_https_port)], device) |
| 145 device_http_port = forwarder.Forwarder.DevicePortForHostPort(host_http_port) |
| 146 device_https_port = forwarder.Forwarder.DevicePortForHostPort(host_https_port) |
| 147 |
| 148 try: |
| 149 yield [ |
| 150 '--host-resolver-rules="MAP * 127.0.0.1,EXCLUDE localhost"', |
| 151 '--testing-fixed-http-port={}'.format(device_http_port), |
| 152 '--testing-fixed-https-port={}'.format(device_https_port)] |
| 153 finally: |
| 154 forwarder.Forwarder.UnmapDevicePort(device_http_port, device) |
| 155 forwarder.Forwarder.UnmapDevicePort(device_https_port, device) |
| 156 wpr_server.StopServer() |
| 157 |
| 158 @contextlib.contextmanager |
107 def DeviceConnection(device, | 159 def DeviceConnection(device, |
108 package=DEFAULT_CHROME_PACKAGE, | 160 package=DEFAULT_CHROME_PACKAGE, |
109 hostname=DEVTOOLS_HOSTNAME, | 161 hostname=DEVTOOLS_HOSTNAME, |
110 port=DEVTOOLS_PORT, | 162 port=DEVTOOLS_PORT, |
111 host_exe='out/Release/chrome', | 163 host_exe='out/Release/chrome', |
112 host_profile_dir=None): | 164 host_profile_dir=None, |
| 165 additional_flags=None): |
113 """Context for starting recording on a device. | 166 """Context for starting recording on a device. |
114 | 167 |
115 Sets up and restores any device and tracing appropriately | 168 Sets up and restores any device and tracing appropriately |
116 | 169 |
117 Args: | 170 Args: |
118 device: Android device, or None for a local run (in which case chrome needs | 171 device: Android device, or None for a local run (in which case chrome needs |
119 to have been started with --remote-debugging-port=XXX). | 172 to have been started with --remote-debugging-port=XXX). |
120 package: The key for chrome package info. | 173 package: The key for chrome package info. |
121 port: The port on which to enable remote debugging. | 174 port: The port on which to enable remote debugging. |
122 host_exe: The binary to execute when running on the host. | 175 host_exe: The binary to execute when running on the host. |
123 host_profile_dir: The profile dir to use when running on the host (if None, | 176 host_profile_dir: The profile dir to use when running on the host (if None, |
124 a fresh profile dir will be used). | 177 a fresh profile dir will be used). |
| 178 additional_flags: Additional chromium arguments. |
125 | 179 |
126 Returns: | 180 Returns: |
127 A context manager type which evaluates to a DevToolsConnection. | 181 A context manager type which evaluates to a DevToolsConnection. |
128 """ | 182 """ |
129 package_info = constants.PACKAGE_INFO[package] | 183 package_info = constants.PACKAGE_INFO[package] |
130 command_line_path = '/data/local/chrome-command-line' | 184 command_line_path = '/data/local/chrome-command-line' |
131 new_flags = ['--disable-fre', | 185 new_flags = ['--disable-fre', |
132 '--enable-test-events', | 186 '--enable-test-events', |
133 '--remote-debugging-port=%d' % port] | 187 '--remote-debugging-port=%d' % port] |
| 188 if additional_flags != None: |
| 189 new_flags.extend(additional_flags) |
134 if device: | 190 if device: |
135 _SetUpDevice(device, package_info) | 191 _SetUpDevice(device, package_info) |
136 with FlagReplacer(device, command_line_path, new_flags): | 192 with FlagReplacer(device, command_line_path, new_flags): |
137 host_process = None | 193 host_process = None |
138 if device: | 194 if device: |
139 start_intent = intent.Intent( | 195 start_intent = intent.Intent( |
140 package=package_info.package, activity=package_info.activity, | 196 package=package_info.package, activity=package_info.activity, |
141 data='about:blank') | 197 data='about:blank') |
142 device.StartActivity(start_intent, blocking=True) | 198 device.StartActivity(start_intent, blocking=True) |
143 else: | 199 else: |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 package: the key for chrome package info. | 231 package: the key for chrome package info. |
176 fn: the function to execute that launches chrome and performs the | 232 fn: the function to execute that launches chrome and performs the |
177 appropriate instrumentation. The function will receive a | 233 appropriate instrumentation. The function will receive a |
178 DevToolsConnection as its sole parameter. | 234 DevToolsConnection as its sole parameter. |
179 | 235 |
180 Returns: | 236 Returns: |
181 As fn() returns. | 237 As fn() returns. |
182 """ | 238 """ |
183 with DeviceConnection(device, package) as connection: | 239 with DeviceConnection(device, package) as connection: |
184 return fn(connection) | 240 return fn(connection) |
OLD | NEW |