OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 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 atexit | 5 import atexit |
6 import json | 6 import json |
7 import logging | 7 import logging |
8 import os | 8 import os |
9 import os.path | 9 import os.path |
10 import random | 10 import random |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 atexit.register(httpd.shutdown) | 144 atexit.register(httpd.shutdown) |
145 | 145 |
146 http_thread = threading.Thread(target=httpd.serve_forever) | 146 http_thread = threading.Thread(target=httpd.serve_forever) |
147 http_thread.daemon = True | 147 http_thread.daemon = True |
148 http_thread.start() | 148 http_thread.start() |
149 | 149 |
150 print 'local port=', httpd.server_address[1] | 150 print 'local port=', httpd.server_address[1] |
151 return 'http://127.0.0.1:%d/' % _MapPort(0, httpd.server_address[1]) | 151 return 'http://127.0.0.1:%d/' % _MapPort(0, httpd.server_address[1]) |
152 | 152 |
153 | 153 |
154 def PrepareShellRun(config): | 154 def PrepareShellRun(config, origin=None): |
155 """ | 155 """ Prepares for StartShell: runs adb as root and installs the apk. If no |
156 Prepares for StartShell. Returns an origin arg with the forwarded device port. | 156 --origin is specified, local http server will be set up to serve files from |
| 157 the build directory along with port forwarding. |
157 | 158 |
158 Start an internal http server to serve mojo applications, forward a local port | 159 Returns arguments that should be appended to shell argument list.""" |
159 on the device to this http server, and install the latest mojo shell version. | |
160 """ | |
161 build_dir = Paths(config).build_dir | 160 build_dir = Paths(config).build_dir |
| 161 |
162 subprocess.check_call([ADB_PATH, 'root']) | 162 subprocess.check_call([ADB_PATH, 'root']) |
163 apk_path = os.path.join(build_dir, 'apks', 'MojoShell.apk') | 163 apk_path = os.path.join(build_dir, 'apks', 'MojoShell.apk') |
164 subprocess.check_call( | 164 subprocess.check_call( |
165 [ADB_PATH, 'install', '-r', apk_path, '-i', MOJO_SHELL_PACKAGE_NAME]) | 165 [ADB_PATH, 'install', '-r', apk_path, '-i', MOJO_SHELL_PACKAGE_NAME]) |
166 | 166 |
167 atexit.register(StopShell) | 167 extra_shell_args = [] |
| 168 origin_url = origin if origin else StartHttpServerForDirectory(build_dir) |
| 169 extra_shell_args.append("--origin=" + origin_url) |
168 | 170 |
169 return '--origin=' + StartHttpServerForDirectory(build_dir) | 171 return extra_shell_args |
170 | 172 |
171 | 173 |
172 def _StartHttpServerForOriginMapping(mapping): | 174 def _StartHttpServerForOriginMapping(mapping): |
173 """If |mapping| points at a local file starts an http server to serve files | 175 """If |mapping| points at a local file starts an http server to serve files |
174 from the directory and returns the new mapping. | 176 from the directory and returns the new mapping. |
175 | 177 |
176 This is intended to be called for every --map-origin value.""" | 178 This is intended to be called for every --map-origin value.""" |
177 parts = mapping.split('=') | 179 parts = mapping.split('=') |
178 if len(parts) != 2: | 180 if len(parts) != 2: |
179 return mapping | 181 return mapping |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 '-W', | 215 '-W', |
214 '-S', | 216 '-S', |
215 '-a', 'android.intent.action.VIEW', | 217 '-a', 'android.intent.action.VIEW', |
216 '-n', '%s/.MojoShellActivity' % MOJO_SHELL_PACKAGE_NAME] | 218 '-n', '%s/.MojoShellActivity' % MOJO_SHELL_PACKAGE_NAME] |
217 | 219 |
218 parameters = [] | 220 parameters = [] |
219 if stdout or on_application_stop: | 221 if stdout or on_application_stop: |
220 subprocess.check_call([ADB_PATH, 'shell', 'rm', STDOUT_PIPE]) | 222 subprocess.check_call([ADB_PATH, 'shell', 'rm', STDOUT_PIPE]) |
221 parameters.append('--fifo-path=%s' % STDOUT_PIPE) | 223 parameters.append('--fifo-path=%s' % STDOUT_PIPE) |
222 _ReadFifo(STDOUT_PIPE, stdout, on_application_stop) | 224 _ReadFifo(STDOUT_PIPE, stdout, on_application_stop) |
223 assert any("--origin=http://127.0.0.1:" in arg for arg in arguments) | 225 # The origin has to be specified whether it's local or external. |
| 226 assert any("--origin=" in arg for arg in arguments) |
224 parameters += [_StartHttpServerForOriginMappings(arg) for arg in arguments] | 227 parameters += [_StartHttpServerForOriginMappings(arg) for arg in arguments] |
225 | 228 |
226 if parameters: | 229 if parameters: |
227 encodedParameters = json.dumps(parameters) | 230 encodedParameters = json.dumps(parameters) |
228 cmd += [ '--es', 'encodedParameters', encodedParameters] | 231 cmd += [ '--es', 'encodedParameters', encodedParameters] |
229 | 232 |
| 233 atexit.register(StopShell) |
230 with open(os.devnull, 'w') as devnull: | 234 with open(os.devnull, 'w') as devnull: |
231 subprocess.Popen(cmd, stdout=devnull).wait() | 235 subprocess.Popen(cmd, stdout=devnull).wait() |
232 | 236 |
233 | 237 |
234 def StopShell(): | 238 def StopShell(): |
235 """ | 239 """ |
236 Stops the mojo shell. | 240 Stops the mojo shell. |
237 """ | 241 """ |
238 subprocess.check_call( | 242 subprocess.check_call( |
239 [ADB_PATH, 'shell', 'am', 'force-stop', MOJO_SHELL_PACKAGE_NAME]) | 243 [ADB_PATH, 'shell', 'am', 'force-stop', MOJO_SHELL_PACKAGE_NAME]) |
(...skipping 12 matching lines...) Expand all Loading... |
252 | 256 |
253 Returns the process responsible for reading the logs. | 257 Returns the process responsible for reading the logs. |
254 """ | 258 """ |
255 logcat = subprocess.Popen([ADB_PATH, | 259 logcat = subprocess.Popen([ADB_PATH, |
256 'logcat', | 260 'logcat', |
257 '-s', | 261 '-s', |
258 ' '.join(LOGCAT_TAGS)], | 262 ' '.join(LOGCAT_TAGS)], |
259 stdout=sys.stdout) | 263 stdout=sys.stdout) |
260 atexit.register(_ExitIfNeeded, logcat) | 264 atexit.register(_ExitIfNeeded, logcat) |
261 return logcat | 265 return logcat |
OLD | NEW |