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 | |
9 | 8 |
10 | 9 |
11 # Location where chrome reads command line flags from | |
12 CHROME_COMMAND_FILE = '/data/local/chrome-command-line' | |
13 | |
14 class FlagChanger(object): | 10 class FlagChanger(object): |
15 """Changes the flags Chrome runs with. | 11 """Changes the flags Chrome runs with. |
16 | 12 |
17 There are two different use cases for this file: | 13 There are two different use cases for this file: |
18 * Flags are permanently set by calling Set(). | 14 * Flags are permanently set by calling Set(). |
19 * Flags can be temporarily set for a particular set of unit tests. These | 15 * 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 | 16 tests should call Restore() to revert the flags to their original state |
21 once the tests have completed. | 17 once the tests have completed. |
22 """ | 18 """ |
23 | 19 |
24 def __init__(self, android_cmd): | 20 def __init__(self, adb, |
25 self._android_cmd = android_cmd | 21 cmdline_file=constants.PACKAGE_INFO['chrome'].cmdline_file): |
| 22 """Initializes the FlagChanger and records the original arguments. |
| 23 |
| 24 Args: |
| 25 adb: An instance of AndroidCommands. |
| 26 cmdline_file: Path to the command line file on the device. |
| 27 """ |
| 28 self._adb = adb |
| 29 self._cmdline_file = cmdline_file |
26 | 30 |
27 # Save the original flags. | 31 # Save the original flags. |
28 self._orig_line = self._android_cmd.GetFileContents(CHROME_COMMAND_FILE) | 32 self._orig_line = self._adb.GetFileContents(self._cmdline_file) |
29 if self._orig_line: | 33 if self._orig_line: |
30 self._orig_line = self._orig_line[0].strip() | 34 self._orig_line = self._orig_line[0].strip() |
31 | 35 |
32 # Parse out the flags into a list to facilitate adding and removing flags. | 36 # Parse out the flags into a list to facilitate adding and removing flags. |
33 self._current_flags = self._TokenizeFlags(self._orig_line) | 37 self._current_flags = self._TokenizeFlags(self._orig_line) |
34 | 38 |
35 def Get(self): | 39 def Get(self): |
36 """Returns list of current flags.""" | 40 """Returns list of current flags.""" |
37 return self._current_flags | 41 return self._current_flags |
38 | 42 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 def Restore(self): | 87 def Restore(self): |
84 """Restores the flags to their original state.""" | 88 """Restores the flags to their original state.""" |
85 self._current_flags = self._TokenizeFlags(self._orig_line) | 89 self._current_flags = self._TokenizeFlags(self._orig_line) |
86 self._UpdateCommandLineFile() | 90 self._UpdateCommandLineFile() |
87 | 91 |
88 def _UpdateCommandLineFile(self): | 92 def _UpdateCommandLineFile(self): |
89 """Writes out the command line to the file, or removes it if empty.""" | 93 """Writes out the command line to the file, or removes it if empty.""" |
90 logging.info('Current flags: %s', self._current_flags) | 94 logging.info('Current flags: %s', self._current_flags) |
91 | 95 |
92 if self._current_flags: | 96 if self._current_flags: |
93 self._android_cmd.SetProtectedFileContents(CHROME_COMMAND_FILE, | 97 # The first command line argument doesn't matter as we are not actually |
94 'chrome ' + | 98 # launching the chrome executable using this command line. |
95 ' '.join(self._current_flags)) | 99 self._adb.SetProtectedFileContents(self._cmdline_file, |
| 100 ' '.join(['_'] + self._current_flags)) |
96 else: | 101 else: |
97 self._android_cmd.RunShellCommand('su -c rm ' + CHROME_COMMAND_FILE) | 102 self._adb.RunShellCommand('su -c rm ' + self._cmdline_file) |
98 | 103 |
99 def _TokenizeFlags(self, line): | 104 def _TokenizeFlags(self, line): |
100 """Changes the string containing the command line into a list of flags. | 105 """Changes the string containing the command line into a list of flags. |
101 | 106 |
102 Follows similar logic to CommandLine.java::tokenizeQuotedArguments: | 107 Follows similar logic to CommandLine.java::tokenizeQuotedArguments: |
103 * Flags are split using whitespace, unless the whitespace is within a | 108 * Flags are split using whitespace, unless the whitespace is within a |
104 pair of quotation marks. | 109 pair of quotation marks. |
105 * Unlike the Java version, we keep the quotation marks around switch | 110 * 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 | 111 values since we need them to re-create the file when new flags are |
107 appended. | 112 appended. |
(...skipping 22 matching lines...) Expand all Loading... |
130 elif not within_quotations and (c is ' ' or c is '\t'): | 135 elif not within_quotations and (c is ' ' or c is '\t'): |
131 if current_flag is not "": | 136 if current_flag is not "": |
132 tokenized_flags.append(current_flag) | 137 tokenized_flags.append(current_flag) |
133 current_flag = "" | 138 current_flag = "" |
134 else: | 139 else: |
135 current_flag += c | 140 current_flag += c |
136 | 141 |
137 # Tack on the last flag. | 142 # Tack on the last flag. |
138 if not current_flag: | 143 if not current_flag: |
139 if within_quotations: | 144 if within_quotations: |
140 warnings.warn("Unterminated quoted string: " + current_flag) | 145 logging.warn('Unterminated quoted argument: ' + line) |
141 else: | 146 else: |
142 tokenized_flags.append(current_flag) | 147 tokenized_flags.append(current_flag) |
143 | 148 |
144 # Return everything but the program name. | 149 # Return everything but the program name. |
145 return tokenized_flags[1:] | 150 return tokenized_flags[1:] |
OLD | NEW |