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 | 4 |
5 import logging | 5 import logging |
6 | 6 |
| 7 import pylib.android_commands |
| 8 import pylib.device.device_utils |
| 9 |
7 | 10 |
8 class FlagChanger(object): | 11 class FlagChanger(object): |
9 """Changes the flags Chrome runs with. | 12 """Changes the flags Chrome runs with. |
10 | 13 |
11 There are two different use cases for this file: | 14 There are two different use cases for this file: |
12 * Flags are permanently set by calling Set(). | 15 * Flags are permanently set by calling Set(). |
13 * Flags can be temporarily set for a particular set of unit tests. These | 16 * Flags can be temporarily set for a particular set of unit tests. These |
14 tests should call Restore() to revert the flags to their original state | 17 tests should call Restore() to revert the flags to their original state |
15 once the tests have completed. | 18 once the tests have completed. |
16 """ | 19 """ |
17 | 20 |
18 def __init__(self, adb, cmdline_file): | 21 def __init__(self, device, cmdline_file): |
19 """Initializes the FlagChanger and records the original arguments. | 22 """Initializes the FlagChanger and records the original arguments. |
20 | 23 |
21 Args: | 24 Args: |
22 adb: An instance of AndroidCommands. | 25 device: A DeviceUtils instance. |
23 cmdline_file: Path to the command line file on the device. | 26 cmdline_file: Path to the command line file on the device. |
24 """ | 27 """ |
25 self._adb = adb | 28 # TODO(jbudorick) Remove once telemetry switches over. |
| 29 if isinstance(device, pylib.android_commands.AndroidCommands): |
| 30 device = pylib.device.device_utils.DeviceUtils(device) |
| 31 self._device = device |
26 self._cmdline_file = cmdline_file | 32 self._cmdline_file = cmdline_file |
27 | 33 |
28 # Save the original flags. | 34 # Save the original flags. |
29 self._orig_line = self._adb.GetFileContents(self._cmdline_file) | 35 self._orig_line = self._device.old_interface.GetFileContents( |
| 36 self._cmdline_file) |
30 if self._orig_line: | 37 if self._orig_line: |
31 self._orig_line = self._orig_line[0].strip() | 38 self._orig_line = self._orig_line[0].strip() |
32 | 39 |
33 # Parse out the flags into a list to facilitate adding and removing flags. | 40 # Parse out the flags into a list to facilitate adding and removing flags. |
34 self._current_flags = self._TokenizeFlags(self._orig_line) | 41 self._current_flags = self._TokenizeFlags(self._orig_line) |
35 | 42 |
36 def Get(self): | 43 def Get(self): |
37 """Returns list of current flags.""" | 44 """Returns list of current flags.""" |
38 return self._current_flags | 45 return self._current_flags |
39 | 46 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 def _UpdateCommandLineFile(self): | 96 def _UpdateCommandLineFile(self): |
90 """Writes out the command line to the file, or removes it if empty.""" | 97 """Writes out the command line to the file, or removes it if empty.""" |
91 logging.info('Current flags: %s', self._current_flags) | 98 logging.info('Current flags: %s', self._current_flags) |
92 # Root is not required to write to /data/local/tmp/. | 99 # Root is not required to write to /data/local/tmp/. |
93 use_root = '/data/local/tmp/' not in self._cmdline_file | 100 use_root = '/data/local/tmp/' not in self._cmdline_file |
94 if self._current_flags: | 101 if self._current_flags: |
95 # The first command line argument doesn't matter as we are not actually | 102 # The first command line argument doesn't matter as we are not actually |
96 # launching the chrome executable using this command line. | 103 # launching the chrome executable using this command line. |
97 cmd_line = ' '.join(['_'] + self._current_flags) | 104 cmd_line = ' '.join(['_'] + self._current_flags) |
98 if use_root: | 105 if use_root: |
99 self._adb.SetProtectedFileContents(self._cmdline_file, cmd_line) | 106 self._device.old_interface.SetProtectedFileContents( |
100 file_contents = self._adb.GetProtectedFileContents(self._cmdline_file) | 107 self._cmdline_file, cmd_line) |
| 108 file_contents = self._device.old_interface.GetProtectedFileContents( |
| 109 self._cmdline_file) |
101 else: | 110 else: |
102 self._adb.SetFileContents(self._cmdline_file, cmd_line) | 111 self._device.old_interface.SetFileContents(self._cmdline_file, cmd_line) |
103 file_contents = self._adb.GetFileContents(self._cmdline_file) | 112 file_contents = self._device.old_interface.GetFileContents( |
| 113 self._cmdline_file) |
104 assert len(file_contents) == 1 and file_contents[0] == cmd_line, ( | 114 assert len(file_contents) == 1 and file_contents[0] == cmd_line, ( |
105 'Failed to set the command line file at %s' % self._cmdline_file) | 115 'Failed to set the command line file at %s' % self._cmdline_file) |
106 else: | 116 else: |
107 if use_root: | 117 if use_root: |
108 self._adb.RunShellCommandWithSU('rm ' + self._cmdline_file) | 118 self._device.old_interface.RunShellCommandWithSU( |
| 119 'rm ' + self._cmdline_file) |
109 else: | 120 else: |
110 self._adb.RunShellCommand('rm ' + self._cmdline_file) | 121 self._device.old_interface.RunShellCommand('rm ' + self._cmdline_file) |
111 assert not self._adb.FileExistsOnDevice(self._cmdline_file), ( | 122 assert ( |
| 123 not self._device.old_interface.FileExistsOnDevice( |
| 124 self._cmdline_file)), ( |
112 'Failed to remove the command line file at %s' % self._cmdline_file) | 125 'Failed to remove the command line file at %s' % self._cmdline_file) |
113 | 126 |
114 @staticmethod | 127 @staticmethod |
115 def _TokenizeFlags(line): | 128 def _TokenizeFlags(line): |
116 """Changes the string containing the command line into a list of flags. | 129 """Changes the string containing the command line into a list of flags. |
117 | 130 |
118 Follows similar logic to CommandLine.java::tokenizeQuotedArguments: | 131 Follows similar logic to CommandLine.java::tokenizeQuotedArguments: |
119 * Flags are split using whitespace, unless the whitespace is within a | 132 * Flags are split using whitespace, unless the whitespace is within a |
120 pair of quotation marks. | 133 pair of quotation marks. |
121 * Unlike the Java version, we keep the quotation marks around switch | 134 * Unlike the Java version, we keep the quotation marks around switch |
(...skipping 30 matching lines...) Expand all Loading... |
152 | 165 |
153 # Tack on the last flag. | 166 # Tack on the last flag. |
154 if not current_flag: | 167 if not current_flag: |
155 if within_quotations: | 168 if within_quotations: |
156 logging.warn('Unterminated quoted argument: ' + line) | 169 logging.warn('Unterminated quoted argument: ' + line) |
157 else: | 170 else: |
158 tokenized_flags.append(current_flag) | 171 tokenized_flags.append(current_flag) |
159 | 172 |
160 # Return everything but the program name. | 173 # Return everything but the program name. |
161 return tokenized_flags[1:] | 174 return tokenized_flags[1:] |
OLD | NEW |