Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 5 import logging | |
| 6 import pipes | |
| 7 import sys | |
| 8 | |
| 9 from telemetry.core import util | |
| 10 | |
| 11 util.AddDirToPythonPath(util.GetChromiumSrcDir(), 'build', 'android') | |
| 12 from pylib.device import device_errors # pylint: disable=F0401 | |
| 13 | |
| 14 | |
| 15 def QuoteIfNeeded(arg): | |
|
nednguyen
2015/01/13 16:03:04
Please make this private & add unittest for it.
ariblue
2015/01/13 20:23:37
Done.
| |
| 16 # Properly escape "key=valueA valueB" to "key='valueA valueB'" | |
| 17 # Values without spaces, or that seem to be quoted are left untouched. | |
| 18 # This is required so CommandLine.java can parse valueB correctly rather | |
| 19 # than as a separate switch. | |
| 20 params = arg.split('=', 1) | |
| 21 if len(params) != 2: | |
| 22 return arg | |
| 23 key, values = params | |
| 24 if ' ' not in values: | |
| 25 return arg | |
| 26 if values[0] in '"\'' and values[-1] == values[0]: | |
| 27 return arg | |
| 28 return '%s=%s' % (key, pipes.quote(values)) | |
| 29 | |
| 30 | |
| 31 class AndroidCommandLineBackend(object): | |
| 32 | |
|
nednguyen
2015/01/13 16:03:04
Can you add pydoc for this class?
ariblue
2015/01/13 20:23:37
Done.
| |
| 33 def __init__(self, adb, backend_settings, startup_args): | |
| 34 self._adb = adb | |
| 35 self._backend_settings = backend_settings | |
| 36 self._startup_args = startup_args | |
| 37 self._saved_command_line_file_contents = '' | |
| 38 | |
| 39 @property | |
| 40 def command_line_file(self): | |
| 41 return self._backend_settings.GetCommandLineFile(self._adb.IsUserBuild()) | |
| 42 | |
| 43 def SetUpCommandLine(self): | |
| 44 args = [self._backend_settings.pseudo_exec_name] | |
| 45 args.extend(self._startup_args) | |
| 46 content = ' '.join(QuoteIfNeeded(arg) for arg in args) | |
| 47 | |
| 48 try: | |
| 49 # Save the current command line to restore later, except if it appears to | |
| 50 # be a Telemetry created one. This is to prevent a common bug where | |
| 51 # --host-resolver-rules borks people's browsers if something goes wrong | |
| 52 # with Telemetry. | |
| 53 self._saved_command_line_file_contents = ''.join(self._ReadFile()) | |
| 54 if '--host-resolver-rules' in self._saved_command_line_file_contents: | |
| 55 self._saved_command_line_file_contents = '' | |
| 56 self._WriteFile(content) | |
| 57 except device_errors.CommandFailedError: | |
| 58 logging.critical('Cannot set Chrome command line. ' | |
| 59 'Fix this by flashing to a userdebug build.') | |
| 60 sys.exit(1) | |
| 61 | |
| 62 def RestoreCommandLine(self): | |
| 63 self._WriteFile(self._saved_command_line_file_contents) | |
| 64 | |
| 65 def _ReadFile(self): | |
| 66 return self._adb.device().ReadFile(self.command_line_file) | |
| 67 | |
| 68 def _WriteFile(self, contents): | |
| 69 self._adb.device().WriteFile(self.command_line_file, contents, as_root=True) | |
| OLD | NEW |