blob: 5be542594cef0bb393f1e22be07fc7d96436d25f [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>
Long Cheng0ab84432017-03-31 00:31:3016#include <base/time/time.h>
Bertrand SIMONNET2e2317a2015-08-26 22:40:4617
18namespace chromeos {
19namespace ui {
20
21// ChromiumCommandBuilder facilitates building a command line for running a
22// Chromium-derived binary and performing related setup.
23class ChromiumCommandBuilder {
24 public:
25 typedef std::map<std::string, std::string> StringMap;
26 typedef std::vector<std::string> StringVector;
27
28 // Name of user account used to run the binary.
29 static const char kUser[];
30
31 // Location of the file containing newline-separated USE flags that were set
32 // when the system was built.
33 static const char kUseFlagsPath[];
34
35 // Location of the file containing .info files describing Pepper plugins.
36 static const char kPepperPluginsPath[];
37
38 // Location of the lsb-release file describing the system image.
39 static const char kLsbReleasePath[];
40
41 // Location of the user-writable target of the /etc/localtime symlink.
42 static const char kTimeZonePath[];
43
44 // Default zoneinfo file used if the time zone hasn't been explicitly set.
45 static const char kDefaultZoneinfoPath[];
46
Daniel Erata7c95152018-04-25 23:34:2147 // Names of Chromium flags (without "--" prefixes) that need to be merged due
48 // to containing lists of comma-separated values.
49 static const char kVmoduleFlag[];
50 static const char kEnableFeaturesFlag[];
Daniel Erat173d0202019-02-09 15:59:4751 static const char kDisableFeaturesFlag[];
Daniel Erata7c95152018-04-25 23:34:2152 static const char kEnableBlinkFeaturesFlag[];
Daniel Erat173d0202019-02-09 15:59:4753 static const char kDisableBlinkFeaturesFlag[];
Daniel Erata7c95152018-04-25 23:34:2154
Bertrand SIMONNET2e2317a2015-08-26 22:40:4655 ChromiumCommandBuilder();
Qijiang Fan6bc59e12020-11-10 17:51:0656 ChromiumCommandBuilder(const ChromiumCommandBuilder&) = delete;
57 ChromiumCommandBuilder& operator=(const ChromiumCommandBuilder&) = delete;
58
Bertrand SIMONNET2e2317a2015-08-26 22:40:4659 ~ChromiumCommandBuilder();
60
61 uid_t uid() const { return uid_; }
62 gid_t gid() const { return gid_; }
63 bool is_chrome_os_hardware() const { return is_chrome_os_hardware_; }
64 bool is_developer_end_user() const { return is_developer_end_user_; }
Long Cheng0ab84432017-03-31 00:31:3065 bool is_test_build() const { return is_test_build_; }
Bertrand SIMONNET2e2317a2015-08-26 22:40:4666 const StringMap& environment_variables() const {
67 return environment_variables_;
68 }
69 const StringVector& arguments() const { return arguments_; }
70
71 void set_base_path_for_testing(const base::FilePath& path) {
72 base_path_for_testing_ = path;
73 }
74
Mike Frysinger5c0d27f2016-09-13 04:11:1975 // Performs just the basic initialization needed before UseFlagIsSet() can be
76 // used. Returns true on success.
Bertrand SIMONNET2e2317a2015-08-26 22:40:4677 bool Init();
78
79 // Determines the environment variables and arguments that should be set for
80 // all Chromium-derived binaries and updates |environment_variables_| and
81 // |arguments_| accordingly. Also creates necessary directories, sets resource
82 // limits, etc.
83 //
Bertrand SIMONNET2e2317a2015-08-26 22:40:4684 // Returns true on success.
Daniel Erat687f1cf2016-12-06 19:50:2685 bool SetUpChromium();
Bertrand SIMONNET2e2317a2015-08-26 22:40:4686
Bertrand SIMONNET2e2317a2015-08-26 22:40:4687 // Reads a user-supplied file requesting modifications to the current set of
88 // arguments. The following directives are supported:
89 //
90 // # This is a comment.
91 // Lines beginning with '#' are skipped.
92 //
93 // --some-flag=some-value
94 // Calls AddArg("--some-flag=some-value").
95 //
96 // !--flag-prefix
Daniel Eratff653022017-03-23 22:00:5997 // Removes all arguments beginning with "--flag-prefix".
98 //
99 // vmodule=foo=1
100 // Prepends a "foo=1" entry to the --vmodule flag.
101 //
102 // enable-features=foo
103 // Appends a "foo" entry to the --enable-features flag.
Bertrand SIMONNET2e2317a2015-08-26 22:40:46104 //
105 // NAME=VALUE
106 // Calls AddEnvVar("NAME", "VALUE").
107 //
Long Cheng0ab84432017-03-31 00:31:30108 // Any flags beginning with prefixes in |disallowed_prefixes| are disregarded.
Bertrand SIMONNET2e2317a2015-08-26 22:40:46109 // Returns true on success.
Long Cheng0ab84432017-03-31 00:31:30110 bool ApplyUserConfig(const base::FilePath& path,
111 const std::set<std::string>& disallowed_prefixes);
Bertrand SIMONNET2e2317a2015-08-26 22:40:46112
113 // Returns true if a USE flag named |flag| was set when the system image was
Daniel Eratb6a7b152017-07-13 01:41:06114 // built (and additionally listed in the libchromeos-use-flags ebuild so it
115 // will be included in the file at kUseFlagsPath).
Bertrand SIMONNET2e2317a2015-08-26 22:40:46116 bool UseFlagIsSet(const std::string& flag) const;
117
Bertrand SIMONNET2e2317a2015-08-26 22:40:46118 // Adds an environment variable to |environment_variables_|. Note that this
119 // method does not call setenv(); it is the caller's responsibility to
120 // actually export the variables.
121 void AddEnvVar(const std::string& name, const std::string& value);
122
123 // Returns the value of an environment variable previously added via
124 // AddEnvVar(). Crashes if the variable isn't set. Note that this method does
125 // not call getenv().
126 std::string ReadEnvVar(const std::string& name) const;
127
Daniel Erata7c95152018-04-25 23:34:21128 // Adds a command-line argument. For --vmodule, --enable-features, or
129 // --enable-blink-features flags (which contain lists of values that must be
130 // merged), use the following dedicated methods instead.
Bertrand SIMONNET2e2317a2015-08-26 22:40:46131 void AddArg(const std::string& arg);
132
Daniel Eratff653022017-03-23 22:00:59133 // Prepends |pattern| to the --vmodule flag in |arguments_|.
Bertrand SIMONNET2e2317a2015-08-26 22:40:46134 void AddVmodulePattern(const std::string& pattern);
135
Daniel Erat173d0202019-02-09 15:59:47136 // Appends |feature_name| to the --enable-features or --disable-features flag
137 // in |arguments_|.
Daniel Eratb85bf3d2016-07-28 21:45:47138 void AddFeatureEnableOverride(const std::string& feature_name);
Daniel Erat173d0202019-02-09 15:59:47139 void AddFeatureDisableOverride(const std::string& feature_name);
Daniel Eratb85bf3d2016-07-28 21:45:47140
Daniel Erat173d0202019-02-09 15:59:47141 // Appends |feature_name| to the --enable-blink-features or
142 // --disable-blink-features flag in |arguments_|.
Daniel Erata7c95152018-04-25 23:34:21143 void AddBlinkFeatureEnableOverride(const std::string& feature_name);
Daniel Erat173d0202019-02-09 15:59:47144 void AddBlinkFeatureDisableOverride(const std::string& feature_name);
Daniel Erata7c95152018-04-25 23:34:21145
Bertrand SIMONNET2e2317a2015-08-26 22:40:46146 private:
147 // Converts absolute path |path| into a base::FilePath, rooting it under
148 // |base_path_for_testing_| if it's non-empty.
149 base::FilePath GetPath(const std::string& path) const;
150
Daniel Erataae4ab62017-11-10 14:47:12151 // Removes arguments beginning with |prefix| from |arguments_|.
152 void DeleteArgsWithPrefix(const std::string& prefix);
153
Daniel Eratb85bf3d2016-07-28 21:45:47154 // Adds an entry to a flag containing a list of values. For example, for a
Daniel Erata7c95152018-04-25 23:34:21155 // flag like "--my-list=foo,bar", |flag_name| would be "my-list",
Daniel Erat173d0202019-02-09 15:59:47156 // |entry_separator| would be ",", and |new_entry| would be "foo" or "bar". If
157 // |prepend| is true, |new_entry| will be prepended before existing values;
158 // otherwise it will be appended after them.
159 void AddListFlagEntry(const std::string& flag_name,
Daniel Eratb85bf3d2016-07-28 21:45:47160 const std::string& entry_separator,
Daniel Eratff653022017-03-23 22:00:59161 const std::string& new_entry,
162 bool prepend);
Daniel Eratb85bf3d2016-07-28 21:45:47163
Ryo Hashimoto50a200c2016-06-13 08:45:15164 // Checks if an ASAN build was requested, doing appropriate initialization and
165 // returning true if so. Called by InitChromium().
Bertrand SIMONNET2e2317a2015-08-26 22:40:46166 bool SetUpASAN();
Bertrand SIMONNET2e2317a2015-08-26 22:40:46167
168 // Reads .info files in |pepper_plugins_path_| and adds the appropriate
169 // arguments to |arguments_|. Called by InitChromium().
170 void SetUpPepperPlugins();
171
172 // Add UI- and compositing-related flags to |arguments_|.
173 void AddUiFlags();
174
175 // Path under which files are created when running in a test.
176 base::FilePath base_path_for_testing_;
177
178 // UID and GID of the user used to run the binary.
Daniel Erata7c95152018-04-25 23:34:21179 uid_t uid_ = 0;
180 gid_t gid_ = 0;
Bertrand SIMONNET2e2317a2015-08-26 22:40:46181
182 // USE flags that were set when the system was built.
183 std::set<std::string> use_flags_;
184
185 // True if official Chrome OS hardware is being used.
Daniel Erata7c95152018-04-25 23:34:21186 bool is_chrome_os_hardware_ = false;
Bertrand SIMONNET2e2317a2015-08-26 22:40:46187
188 // True if this is a developer system, per the is_developer_end_user command.
Daniel Erata7c95152018-04-25 23:34:21189 bool is_developer_end_user_ = false;
Bertrand SIMONNET2e2317a2015-08-26 22:40:46190
Long Cheng0ab84432017-03-31 00:31:30191 // True if this is a test build, per CHROMEOS_RELEASE_TRACK in
192 // /etc/lsb-release.
Daniel Erata7c95152018-04-25 23:34:21193 bool is_test_build_ = false;
Long Cheng0ab84432017-03-31 00:31:30194
195 // Data in /etc/lsb-release.
196 std::string lsb_data_;
197
198 // Creation time of /etc/lsb-release.
199 base::Time lsb_release_time_;
200
Bertrand SIMONNET2e2317a2015-08-26 22:40:46201 // Environment variables that the caller should export before starting the
202 // executable.
203 StringMap environment_variables_;
204
205 // Command-line arguments that the caller should pass to the executable.
206 StringVector arguments_;
207
Daniel Erat173d0202019-02-09 15:59:47208 // Index in |arguments_| of list-based flags (e.g. --vmodule,
209 // --enable-features), keyed by base flag name (e.g. "vmodule",
210 // "enable-features"). Flags that have not been set are not included.
211 std::map<std::string, int> list_argument_indexes_;
Bertrand SIMONNET2e2317a2015-08-26 22:40:46212};
213
214} // namespace ui
215} // namespace chromeos
216
Bertrand SIMONNET52d97362015-09-02 21:22:37217#endif // LIBCHROMEOS_UI_CHROMEOS_UI_CHROMIUM_COMMAND_BUILDER_H_