blob: 0f9760189c3985cb881c47d4d761e232fc7fb066 [file] [log] [blame]
Bertrand SIMONNET2e2317a2015-08-26 22:40:461// Copyright 2014 The Chromium OS 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.
4
Bertrand SIMONNET52d97362015-09-02 21:22:375#ifndef LIBCHROMEOS_UI_CHROMEOS_UI_CHROMIUM_COMMAND_BUILDER_H_
6#define LIBCHROMEOS_UI_CHROMEOS_UI_CHROMIUM_COMMAND_BUILDER_H_
Bertrand SIMONNET2e2317a2015-08-26 22:40:467
8#include <sys/types.h>
9
10#include <map>
11#include <set>
12#include <string>
13#include <vector>
14
15#include <base/files/file_path.h>
16#include <base/macros.h>
Long Cheng0ab84432017-03-31 00:31:3017#include <base/time/time.h>
Bertrand SIMONNET2e2317a2015-08-26 22:40:4618
19namespace chromeos {
20namespace ui {
21
22// ChromiumCommandBuilder facilitates building a command line for running a
23// Chromium-derived binary and performing related setup.
24class ChromiumCommandBuilder {
25 public:
26 typedef std::map<std::string, std::string> StringMap;
27 typedef std::vector<std::string> StringVector;
28
29 // Name of user account used to run the binary.
30 static const char kUser[];
31
32 // Location of the file containing newline-separated USE flags that were set
33 // when the system was built.
34 static const char kUseFlagsPath[];
35
36 // Location of the file containing .info files describing Pepper plugins.
37 static const char kPepperPluginsPath[];
38
39 // Location of the lsb-release file describing the system image.
40 static const char kLsbReleasePath[];
41
42 // Location of the user-writable target of the /etc/localtime symlink.
43 static const char kTimeZonePath[];
44
45 // Default zoneinfo file used if the time zone hasn't been explicitly set.
46 static const char kDefaultZoneinfoPath[];
47
Bertrand SIMONNET2e2317a2015-08-26 22:40:4648 ChromiumCommandBuilder();
49 ~ChromiumCommandBuilder();
50
51 uid_t uid() const { return uid_; }
52 gid_t gid() const { return gid_; }
53 bool is_chrome_os_hardware() const { return is_chrome_os_hardware_; }
54 bool is_developer_end_user() const { return is_developer_end_user_; }
Long Cheng0ab84432017-03-31 00:31:3055 bool is_test_build() const { return is_test_build_; }
Bertrand SIMONNET2e2317a2015-08-26 22:40:4656 const StringMap& environment_variables() const {
57 return environment_variables_;
58 }
59 const StringVector& arguments() const { return arguments_; }
60
61 void set_base_path_for_testing(const base::FilePath& path) {
62 base_path_for_testing_ = path;
63 }
64
Mike Frysinger5c0d27f2016-09-13 04:11:1965 // Performs just the basic initialization needed before UseFlagIsSet() can be
66 // used. Returns true on success.
Bertrand SIMONNET2e2317a2015-08-26 22:40:4667 bool Init();
68
69 // Determines the environment variables and arguments that should be set for
70 // all Chromium-derived binaries and updates |environment_variables_| and
71 // |arguments_| accordingly. Also creates necessary directories, sets resource
72 // limits, etc.
73 //
Bertrand SIMONNET2e2317a2015-08-26 22:40:4674 // Returns true on success.
Daniel Erat687f1cf2016-12-06 19:50:2675 bool SetUpChromium();
Bertrand SIMONNET2e2317a2015-08-26 22:40:4676
77 // Configures the environment so a core dump will be written when the binary
78 // crashes.
79 void EnableCoreDumps();
80
81 // Reads a user-supplied file requesting modifications to the current set of
82 // arguments. The following directives are supported:
83 //
84 // # This is a comment.
85 // Lines beginning with '#' are skipped.
86 //
87 // --some-flag=some-value
88 // Calls AddArg("--some-flag=some-value").
89 //
90 // !--flag-prefix
Daniel Eratff653022017-03-23 22:00:5991 // Removes all arguments beginning with "--flag-prefix".
92 //
93 // vmodule=foo=1
94 // Prepends a "foo=1" entry to the --vmodule flag.
95 //
96 // enable-features=foo
97 // Appends a "foo" entry to the --enable-features flag.
Bertrand SIMONNET2e2317a2015-08-26 22:40:4698 //
99 // NAME=VALUE
100 // Calls AddEnvVar("NAME", "VALUE").
101 //
Long Cheng0ab84432017-03-31 00:31:30102 // Any flags beginning with prefixes in |disallowed_prefixes| are disregarded.
Bertrand SIMONNET2e2317a2015-08-26 22:40:46103 // Returns true on success.
Long Cheng0ab84432017-03-31 00:31:30104 bool ApplyUserConfig(const base::FilePath& path,
105 const std::set<std::string>& disallowed_prefixes);
Bertrand SIMONNET2e2317a2015-08-26 22:40:46106
107 // Returns true if a USE flag named |flag| was set when the system image was
Daniel Eratb6a7b152017-07-13 01:41:06108 // built (and additionally listed in the libchromeos-use-flags ebuild so it
109 // will be included in the file at kUseFlagsPath).
Bertrand SIMONNET2e2317a2015-08-26 22:40:46110 bool UseFlagIsSet(const std::string& flag) const;
111
Bertrand SIMONNET2e2317a2015-08-26 22:40:46112 // Adds an environment variable to |environment_variables_|. Note that this
113 // method does not call setenv(); it is the caller's responsibility to
114 // actually export the variables.
115 void AddEnvVar(const std::string& name, const std::string& value);
116
117 // Returns the value of an environment variable previously added via
118 // AddEnvVar(). Crashes if the variable isn't set. Note that this method does
119 // not call getenv().
120 std::string ReadEnvVar(const std::string& name) const;
121
122 // Adds a command-line argument.
123 void AddArg(const std::string& arg);
124
Daniel Eratff653022017-03-23 22:00:59125 // Prepends |pattern| to the --vmodule flag in |arguments_|.
Bertrand SIMONNET2e2317a2015-08-26 22:40:46126 void AddVmodulePattern(const std::string& pattern);
127
Daniel Eratff653022017-03-23 22:00:59128 // Appends |feature_name| to the --enable-features flag in |arguments_|.
Daniel Eratb85bf3d2016-07-28 21:45:47129 void AddFeatureEnableOverride(const std::string& feature_name);
130
Bertrand SIMONNET2e2317a2015-08-26 22:40:46131 private:
132 // Converts absolute path |path| into a base::FilePath, rooting it under
133 // |base_path_for_testing_| if it's non-empty.
134 base::FilePath GetPath(const std::string& path) const;
135
Daniel Erataae4ab62017-11-10 14:47:12136 // Removes arguments beginning with |prefix| from |arguments_|.
137 void DeleteArgsWithPrefix(const std::string& prefix);
138
Daniel Eratb85bf3d2016-07-28 21:45:47139 // Adds an entry to a flag containing a list of values. For example, for a
140 // flag like "--my-list=foo,bar", |flag_prefix| would be "--my-list=",
141 // |entry_separator| would be ",", and |new_entry| would be "foo" or "bar".
142 // |flag_argument_index|'s memory holds the flag's position within
Daniel Eratff653022017-03-23 22:00:59143 // |arguments_| or -1 if the flag is not yet set. If |prepend| is true,
144 // |new_entry| will be prepended before existing values; otherwise it will be
145 // appended after them.
Daniel Eratb85bf3d2016-07-28 21:45:47146 void AddListFlagEntry(int* flag_argument_index,
147 const std::string& flag_prefix,
148 const std::string& entry_separator,
Daniel Eratff653022017-03-23 22:00:59149 const std::string& new_entry,
150 bool prepend);
Daniel Eratb85bf3d2016-07-28 21:45:47151
Ryo Hashimoto50a200c2016-06-13 08:45:15152 // Checks if an ASAN build was requested, doing appropriate initialization and
153 // returning true if so. Called by InitChromium().
Bertrand SIMONNET2e2317a2015-08-26 22:40:46154 bool SetUpASAN();
Bertrand SIMONNET2e2317a2015-08-26 22:40:46155
156 // Reads .info files in |pepper_plugins_path_| and adds the appropriate
157 // arguments to |arguments_|. Called by InitChromium().
158 void SetUpPepperPlugins();
159
160 // Add UI- and compositing-related flags to |arguments_|.
161 void AddUiFlags();
162
163 // Path under which files are created when running in a test.
164 base::FilePath base_path_for_testing_;
165
166 // UID and GID of the user used to run the binary.
167 uid_t uid_;
168 gid_t gid_;
169
170 // USE flags that were set when the system was built.
171 std::set<std::string> use_flags_;
172
173 // True if official Chrome OS hardware is being used.
174 bool is_chrome_os_hardware_;
175
176 // True if this is a developer system, per the is_developer_end_user command.
177 bool is_developer_end_user_;
178
Long Cheng0ab84432017-03-31 00:31:30179 // True if this is a test build, per CHROMEOS_RELEASE_TRACK in
180 // /etc/lsb-release.
181 bool is_test_build_;
182
183 // Data in /etc/lsb-release.
184 std::string lsb_data_;
185
186 // Creation time of /etc/lsb-release.
187 base::Time lsb_release_time_;
188
Bertrand SIMONNET2e2317a2015-08-26 22:40:46189 // Environment variables that the caller should export before starting the
190 // executable.
191 StringMap environment_variables_;
192
193 // Command-line arguments that the caller should pass to the executable.
194 StringVector arguments_;
195
Daniel Eratb85bf3d2016-07-28 21:45:47196 // Index in |arguments_| of the --vmodule and --enable-features flags. -1 if
197 // the flags haven't been set.
Bertrand SIMONNET2e2317a2015-08-26 22:40:46198 int vmodule_argument_index_;
Daniel Eratb85bf3d2016-07-28 21:45:47199 int enable_features_argument_index_;
Bertrand SIMONNET2e2317a2015-08-26 22:40:46200
201 DISALLOW_COPY_AND_ASSIGN(ChromiumCommandBuilder);
202};
203
204} // namespace ui
205} // namespace chromeos
206
Bertrand SIMONNET52d97362015-09-02 21:22:37207#endif // LIBCHROMEOS_UI_CHROMEOS_UI_CHROMIUM_COMMAND_BUILDER_H_