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
3 # found in the LICENSE file.
7 import pylib
.device
.device_utils
9 from pylib
.device
import device_errors
12 class FlagChanger(object):
13 """Changes the flags Chrome runs with.
15 There are two different use cases for this file:
16 * Flags are permanently set by calling Set().
17 * Flags can be temporarily set for a particular set of unit tests. These
18 tests should call Restore() to revert the flags to their original state
19 once the tests have completed.
22 def __init__(self
, device
, cmdline_file
):
23 """Initializes the FlagChanger and records the original arguments.
26 device: A DeviceUtils instance.
27 cmdline_file: Path to the command line file on the device.
30 self
._cmdline
_file
= cmdline_file
32 # Save the original flags.
34 self
._orig
_line
= self
._device
.ReadFile(self
._cmdline
_file
).strip()
35 except device_errors
.CommandFailedError
:
38 # Parse out the flags into a list to facilitate adding and removing flags.
39 self
._current
_flags
= self
._TokenizeFlags
(self
._orig
_line
)
42 """Returns list of current flags."""
43 return self
._current
_flags
46 """Replaces all flags on the current command line with the flags given.
49 flags: A list of flags to set, eg. ['--single-process'].
52 assert flags
[0] != 'chrome'
54 self
._current
_flags
= flags
55 self
._UpdateCommandLineFile
()
57 def AddFlags(self
, flags
):
58 """Appends flags to the command line if they aren't already there.
61 flags: A list of flags to add on, eg. ['--single-process'].
64 assert flags
[0] != 'chrome'
66 # Avoid appending flags that are already present.
68 if flag
not in self
._current
_flags
:
69 self
._current
_flags
.append(flag
)
70 self
._UpdateCommandLineFile
()
72 def RemoveFlags(self
, flags
):
73 """Removes flags from the command line, if they exist.
76 flags: A list of flags to remove, eg. ['--single-process']. Note that we
77 expect a complete match when removing flags; if you want to remove
78 a switch with a value, you must use the exact string used to add
79 it in the first place.
82 assert flags
[0] != 'chrome'
85 if flag
in self
._current
_flags
:
86 self
._current
_flags
.remove(flag
)
87 self
._UpdateCommandLineFile
()
90 """Restores the flags to their original state."""
91 self
._current
_flags
= self
._TokenizeFlags
(self
._orig
_line
)
92 self
._UpdateCommandLineFile
()
94 def _UpdateCommandLineFile(self
):
95 """Writes out the command line to the file, or removes it if empty."""
96 logging
.info('Current flags: %s', self
._current
_flags
)
97 # Root is not required to write to /data/local/tmp/.
98 use_root
= '/data/local/tmp/' not in self
._cmdline
_file
99 if self
._current
_flags
:
100 # The first command line argument doesn't matter as we are not actually
101 # launching the chrome executable using this command line.
102 cmd_line
= ' '.join(['_'] + self
._current
_flags
)
103 self
._device
.WriteFile(
104 self
._cmdline
_file
, cmd_line
, as_root
=use_root
)
105 file_contents
= self
._device
.ReadFile(
106 self
._cmdline
_file
, as_root
=use_root
).rstrip()
107 assert file_contents
== cmd_line
, (
108 'Failed to set the command line file at %s' % self
._cmdline
_file
)
110 self
._device
.RunShellCommand('rm ' + self
._cmdline
_file
,
112 assert not self
._device
.FileExists(self
._cmdline
_file
), (
113 'Failed to remove the command line file at %s' % self
._cmdline
_file
)
116 def _TokenizeFlags(line
):
117 """Changes the string containing the command line into a list of flags.
119 Follows similar logic to CommandLine.java::tokenizeQuotedArguments:
120 * Flags are split using whitespace, unless the whitespace is within a
121 pair of quotation marks.
122 * Unlike the Java version, we keep the quotation marks around switch
123 values since we need them to re-create the file when new flags are
127 line: A string containing the entire command line. The first token is
128 assumed to be the program name.
135 within_quotations
= False
137 # Move through the string character by character and build up each flag
139 for c
in line
.strip():
141 if len(current_flag
) > 0 and current_flag
[-1] == '\\':
142 # Last char was a backslash; pop it, and treat this " as a literal.
143 current_flag
= current_flag
[0:-1] + '"'
145 within_quotations
= not within_quotations
147 elif not within_quotations
and (c
is ' ' or c
is '\t'):
148 if current_flag
is not "":
149 tokenized_flags
.append(current_flag
)
154 # Tack on the last flag.
156 if within_quotations
:
157 logging
.warn('Unterminated quoted argument: ' + line
)
159 tokenized_flags
.append(current_flag
)
161 # Return everything but the program name.
162 return tokenized_flags
[1:]