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 from devil
.android
import device_errors
10 class FlagChanger(object):
11 """Changes the flags Chrome runs with.
13 There are two different use cases for this file:
14 * Flags are permanently set by calling Set().
15 * Flags can be temporarily set for a particular set of unit tests. These
16 tests should call Restore() to revert the flags to their original state
17 once the tests have completed.
20 def __init__(self
, device
, cmdline_file
):
21 """Initializes the FlagChanger and records the original arguments.
24 device: A DeviceUtils instance.
25 cmdline_file: Path to the command line file on the device.
28 self
._cmdline
_file
= cmdline_file
30 # Save the original flags.
32 self
._orig
_line
= self
._device
.ReadFile(self
._cmdline
_file
).strip()
33 except device_errors
.CommandFailedError
:
36 # Parse out the flags into a list to facilitate adding and removing flags.
37 self
._current
_flags
= self
._TokenizeFlags
(self
._orig
_line
)
40 """Returns list of current flags."""
41 return self
._current
_flags
44 """Replaces all flags on the current command line with the flags given.
47 flags: A list of flags to set, eg. ['--single-process'].
50 assert flags
[0] != 'chrome'
52 self
._current
_flags
= flags
53 self
._UpdateCommandLineFile
()
55 def AddFlags(self
, flags
):
56 """Appends flags to the command line if they aren't already there.
59 flags: A list of flags to add on, eg. ['--single-process'].
62 assert flags
[0] != 'chrome'
64 # Avoid appending flags that are already present.
66 if flag
not in self
._current
_flags
:
67 self
._current
_flags
.append(flag
)
68 self
._UpdateCommandLineFile
()
70 def RemoveFlags(self
, flags
):
71 """Removes flags from the command line, if they exist.
74 flags: A list of flags to remove, eg. ['--single-process']. Note that we
75 expect a complete match when removing flags; if you want to remove
76 a switch with a value, you must use the exact string used to add
77 it in the first place.
80 assert flags
[0] != 'chrome'
83 if flag
in self
._current
_flags
:
84 self
._current
_flags
.remove(flag
)
85 self
._UpdateCommandLineFile
()
88 """Restores the flags to their original state."""
89 self
._current
_flags
= self
._TokenizeFlags
(self
._orig
_line
)
90 self
._UpdateCommandLineFile
()
92 def _UpdateCommandLineFile(self
):
93 """Writes out the command line to the file, or removes it if empty."""
94 logging
.info('Current flags: %s', self
._current
_flags
)
95 # Root is not required to write to /data/local/tmp/.
96 use_root
= '/data/local/tmp/' not in self
._cmdline
_file
97 if self
._current
_flags
:
98 # The first command line argument doesn't matter as we are not actually
99 # launching the chrome executable using this command line.
100 cmd_line
= ' '.join(['_'] + self
._current
_flags
)
101 self
._device
.WriteFile(
102 self
._cmdline
_file
, cmd_line
, as_root
=use_root
)
103 file_contents
= self
._device
.ReadFile(
104 self
._cmdline
_file
, as_root
=use_root
).rstrip()
105 assert file_contents
== cmd_line
, (
106 'Failed to set the command line file at %s' % self
._cmdline
_file
)
108 self
._device
.RunShellCommand('rm ' + self
._cmdline
_file
,
110 assert not self
._device
.FileExists(self
._cmdline
_file
), (
111 'Failed to remove the command line file at %s' % self
._cmdline
_file
)
114 def _TokenizeFlags(line
):
115 """Changes the string containing the command line into a list of flags.
117 Follows similar logic to CommandLine.java::tokenizeQuotedArguments:
118 * Flags are split using whitespace, unless the whitespace is within a
119 pair of quotation marks.
120 * Unlike the Java version, we keep the quotation marks around switch
121 values since we need them to re-create the file when new flags are
125 line: A string containing the entire command line. The first token is
126 assumed to be the program name.
133 within_quotations
= False
135 # Move through the string character by character and build up each flag
137 for c
in line
.strip():
139 if len(current_flag
) > 0 and current_flag
[-1] == '\\':
140 # Last char was a backslash; pop it, and treat this " as a literal.
141 current_flag
= current_flag
[0:-1] + '"'
143 within_quotations
= not within_quotations
145 elif not within_quotations
and (c
is ' ' or c
is '\t'):
146 if current_flag
is not "":
147 tokenized_flags
.append(current_flag
)
152 # Tack on the last flag.
154 if within_quotations
:
155 logging
.warn('Unterminated quoted argument: ' + line
)
157 tokenized_flags
.append(current_flag
)
159 # Return everything but the program name.
160 return tokenized_flags
[1:]