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 constants | 5 import constants |
6 import logging | 6 import logging |
7 import traceback | 7 import traceback |
8 import warnings | 8 import warnings |
9 | 9 |
10 | 10 |
11 # Location where chrome reads command line flags from | |
12 CHROME_COMMAND_FILE = '/data/local/chrome-command-line' | |
13 | |
14 class FlagChanger(object): | 11 class FlagChanger(object): |
15 """Changes the flags Chrome runs with. | 12 """Changes the flags Chrome runs with. |
16 | 13 |
17 There are two different use cases for this file: | 14 There are two different use cases for this file: |
18 * Flags are permanently set by calling Set(). | 15 * Flags are permanently set by calling Set(). |
19 * 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 |
20 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 |
21 once the tests have completed. | 18 once the tests have completed. |
22 """ | 19 """ |
23 | 20 |
24 def __init__(self, android_cmd): | 21 def __init__(self, android_cmd, |
| 22 command_line_file=constants.CHROME_COMMAND_LINE_FILE): |
25 self._android_cmd = android_cmd | 23 self._android_cmd = android_cmd |
| 24 self._command_line_file = command_line_file |
| 25 logging.info('Command line file: %s', self._command_line_file) |
26 | 26 |
27 # Save the original flags. | 27 # Save the original flags. |
28 self._orig_line = self._android_cmd.GetFileContents(CHROME_COMMAND_FILE) | 28 self._orig_line = self._android_cmd.GetFileContents(self._command_line_file) |
29 if self._orig_line: | 29 if self._orig_line: |
30 self._orig_line = self._orig_line[0].strip() | 30 self._orig_line = self._orig_line[0].strip() |
31 | 31 |
32 # Parse out the flags into a list to facilitate adding and removing flags. | 32 # Parse out the flags into a list to facilitate adding and removing flags. |
33 self._current_flags = self._TokenizeFlags(self._orig_line) | 33 self._current_flags = self._TokenizeFlags(self._orig_line) |
34 | 34 |
35 def Get(self): | 35 def Get(self): |
36 """Returns list of current flags.""" | 36 """Returns list of current flags.""" |
37 return self._current_flags | 37 return self._current_flags |
38 | 38 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 def Restore(self): | 83 def Restore(self): |
84 """Restores the flags to their original state.""" | 84 """Restores the flags to their original state.""" |
85 self._current_flags = self._TokenizeFlags(self._orig_line) | 85 self._current_flags = self._TokenizeFlags(self._orig_line) |
86 self._UpdateCommandLineFile() | 86 self._UpdateCommandLineFile() |
87 | 87 |
88 def _UpdateCommandLineFile(self): | 88 def _UpdateCommandLineFile(self): |
89 """Writes out the command line to the file, or removes it if empty.""" | 89 """Writes out the command line to the file, or removes it if empty.""" |
90 logging.info('Current flags: %s', self._current_flags) | 90 logging.info('Current flags: %s', self._current_flags) |
91 | 91 |
92 if self._current_flags: | 92 if self._current_flags: |
93 self._android_cmd.SetProtectedFileContents(CHROME_COMMAND_FILE, | 93 self._android_cmd.SetProtectedFileContents(self._command_line_file, |
94 'chrome ' + | 94 'chrome ' + |
95 ' '.join(self._current_flags)) | 95 ' '.join(self._current_flags)) |
96 else: | 96 else: |
97 self._android_cmd.RunShellCommand('su -c rm ' + CHROME_COMMAND_FILE) | 97 self._android_cmd.RunShellCommand('su -c rm ' + self._command_line_file) |
98 | 98 |
99 def _TokenizeFlags(self, line): | 99 def _TokenizeFlags(self, line): |
100 """Changes the string containing the command line into a list of flags. | 100 """Changes the string containing the command line into a list of flags. |
101 | 101 |
102 Follows similar logic to CommandLine.java::tokenizeQuotedArguments: | 102 Follows similar logic to CommandLine.java::tokenizeQuotedArguments: |
103 * Flags are split using whitespace, unless the whitespace is within a | 103 * Flags are split using whitespace, unless the whitespace is within a |
104 pair of quotation marks. | 104 pair of quotation marks. |
105 * Unlike the Java version, we keep the quotation marks around switch | 105 * Unlike the Java version, we keep the quotation marks around switch |
106 values since we need them to re-create the file when new flags are | 106 values since we need them to re-create the file when new flags are |
107 appended. | 107 appended. |
(...skipping 28 matching lines...) Expand all Loading... |
136 | 136 |
137 # Tack on the last flag. | 137 # Tack on the last flag. |
138 if not current_flag: | 138 if not current_flag: |
139 if within_quotations: | 139 if within_quotations: |
140 warnings.warn("Unterminated quoted string: " + current_flag) | 140 warnings.warn("Unterminated quoted string: " + current_flag) |
141 else: | 141 else: |
142 tokenized_flags.append(current_flag) | 142 tokenized_flags.append(current_flag) |
143 | 143 |
144 # Return everything but the program name. | 144 # Return everything but the program name. |
145 return tokenized_flags[1:] | 145 return tokenized_flags[1:] |
OLD | NEW |