blob: 54ccf0b40b6e730de5de9628094e738109be172f [file] [log] [blame]
Mike Frysinger3a446f22022-09-08 07:37:141// Copyright 2014 The ChromiumOS Authors
Bertrand SIMONNET2e2317a2015-08-26 22:40:462// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Hidehiko Abe40f15282024-10-11 05:25:215#ifndef LOGIN_MANAGER_CHROMIUM_COMMAND_BUILDER_H_
6#define LOGIN_MANAGER_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>
Mike Frysingere9c97902022-08-17 13:44:5017#include <brillo/brillo_export.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.
Mike Frysingere9c97902022-08-17 13:44:5024class BRILLO_EXPORT ChromiumCommandBuilder {
Bertrand SIMONNET2e2317a2015-08-26 22:40:4625 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
Daniel Erata7c95152018-04-25 23:34:2142 // Names of Chromium flags (without "--" prefixes) that need to be merged due
43 // to containing lists of comma-separated values.
44 static const char kVmoduleFlag[];
45 static const char kEnableFeaturesFlag[];
Daniel Erat173d0202019-02-09 15:59:4746 static const char kDisableFeaturesFlag[];
Daniel Erata7c95152018-04-25 23:34:2147 static const char kEnableBlinkFeaturesFlag[];
Daniel Erat173d0202019-02-09 15:59:4748 static const char kDisableBlinkFeaturesFlag[];
Daniel Erata7c95152018-04-25 23:34:2149
howardchungac43d272023-07-17 09:21:0150 static const char kCrosConfigBluetoothFlagsPath[];
51 static const char kCrosConfigBlockFlossAvailability[];
Zhengping Jiang91cabd12024-04-19 01:54:3952 static const char kCrosConfigBlockLLPrivacyAvailability[];
howardchungac43d272023-07-17 09:21:0153
Bertrand SIMONNET2e2317a2015-08-26 22:40:4654 ChromiumCommandBuilder();
Qijiang Fan6bc59e12020-11-10 17:51:0655 ChromiumCommandBuilder(const ChromiumCommandBuilder&) = delete;
56 ChromiumCommandBuilder& operator=(const ChromiumCommandBuilder&) = delete;
57
Bertrand SIMONNET2e2317a2015-08-26 22:40:4658 ~ChromiumCommandBuilder();
59
60 uid_t uid() const { return uid_; }
61 gid_t gid() const { return gid_; }
62 bool is_chrome_os_hardware() const { return is_chrome_os_hardware_; }
63 bool is_developer_end_user() const { return is_developer_end_user_; }
Long Cheng0ab84432017-03-31 00:31:3064 bool is_test_build() const { return is_test_build_; }
Bertrand SIMONNET2e2317a2015-08-26 22:40:4665 const StringMap& environment_variables() const {
66 return environment_variables_;
67 }
68 const StringVector& arguments() const { return arguments_; }
69
70 void set_base_path_for_testing(const base::FilePath& path) {
71 base_path_for_testing_ = path;
72 }
73
Mike Frysinger5c0d27f2016-09-13 04:11:1974 // Performs just the basic initialization needed before UseFlagIsSet() can be
75 // used. Returns true on success.
Bertrand SIMONNET2e2317a2015-08-26 22:40:4676 bool Init();
77
78 // Determines the environment variables and arguments that should be set for
79 // all Chromium-derived binaries and updates |environment_variables_| and
80 // |arguments_| accordingly. Also creates necessary directories, sets resource
81 // limits, etc.
82 //
Bertrand SIMONNET2e2317a2015-08-26 22:40:4683 // Returns true on success.
Daniel Erat687f1cf2016-12-06 19:50:2684 bool SetUpChromium();
Bertrand SIMONNET2e2317a2015-08-26 22:40:4685
Bertrand SIMONNET2e2317a2015-08-26 22:40:4686 // Reads a user-supplied file requesting modifications to the current set of
87 // arguments. The following directives are supported:
88 //
89 // # This is a comment.
90 // Lines beginning with '#' are skipped.
91 //
92 // --some-flag=some-value
93 // Calls AddArg("--some-flag=some-value").
94 //
95 // !--flag-prefix
Daniel Eratff653022017-03-23 22:00:5996 // Removes all arguments beginning with "--flag-prefix".
97 //
98 // vmodule=foo=1
99 // Prepends a "foo=1" entry to the --vmodule flag.
100 //
101 // enable-features=foo
102 // Appends a "foo" entry to the --enable-features flag.
Bertrand SIMONNET2e2317a2015-08-26 22:40:46103 //
104 // NAME=VALUE
105 // Calls AddEnvVar("NAME", "VALUE").
106 //
Long Cheng0ab84432017-03-31 00:31:30107 // Any flags beginning with prefixes in |disallowed_prefixes| are disregarded.
Bertrand SIMONNET2e2317a2015-08-26 22:40:46108 // Returns true on success.
Long Cheng0ab84432017-03-31 00:31:30109 bool ApplyUserConfig(const base::FilePath& path,
110 const std::set<std::string>& disallowed_prefixes);
Bertrand SIMONNET2e2317a2015-08-26 22:40:46111
112 // Returns true if a USE flag named |flag| was set when the system image was
Daniel Eratb6a7b152017-07-13 01:41:06113 // built (and additionally listed in the libchromeos-use-flags ebuild so it
114 // will be included in the file at kUseFlagsPath).
Bertrand SIMONNET2e2317a2015-08-26 22:40:46115 bool UseFlagIsSet(const std::string& flag) const;
116
Bertrand SIMONNET2e2317a2015-08-26 22:40:46117 // Adds an environment variable to |environment_variables_|. Note that this
118 // method does not call setenv(); it is the caller's responsibility to
119 // actually export the variables.
120 void AddEnvVar(const std::string& name, const std::string& value);
121
122 // Returns the value of an environment variable previously added via
123 // AddEnvVar(). Crashes if the variable isn't set. Note that this method does
124 // not call getenv().
125 std::string ReadEnvVar(const std::string& name) const;
126
Daniel Erata7c95152018-04-25 23:34:21127 // Adds a command-line argument. For --vmodule, --enable-features, or
128 // --enable-blink-features flags (which contain lists of values that must be
129 // merged), use the following dedicated methods instead.
Bertrand SIMONNET2e2317a2015-08-26 22:40:46130 void AddArg(const std::string& arg);
131
Daniel Eratff653022017-03-23 22:00:59132 // Prepends |pattern| to the --vmodule flag in |arguments_|.
Bertrand SIMONNET2e2317a2015-08-26 22:40:46133 void AddVmodulePattern(const std::string& pattern);
134
Daniel Erat173d0202019-02-09 15:59:47135 // Appends |feature_name| to the --enable-features or --disable-features flag
136 // in |arguments_|.
Daniel Eratb85bf3d2016-07-28 21:45:47137 void AddFeatureEnableOverride(const std::string& feature_name);
Daniel Erat173d0202019-02-09 15:59:47138 void AddFeatureDisableOverride(const std::string& feature_name);
Daniel Eratb85bf3d2016-07-28 21:45:47139
Daniel Erat173d0202019-02-09 15:59:47140 // Appends |feature_name| to the --enable-blink-features or
141 // --disable-blink-features flag in |arguments_|.
Daniel Erata7c95152018-04-25 23:34:21142 void AddBlinkFeatureEnableOverride(const std::string& feature_name);
Daniel Erat173d0202019-02-09 15:59:47143 void AddBlinkFeatureDisableOverride(const std::string& feature_name);
Daniel Erata7c95152018-04-25 23:34:21144
Bertrand SIMONNET2e2317a2015-08-26 22:40:46145 private:
146 // Converts absolute path |path| into a base::FilePath, rooting it under
147 // |base_path_for_testing_| if it's non-empty.
148 base::FilePath GetPath(const std::string& path) const;
149
Daniel Erataae4ab62017-11-10 14:47:12150 // Removes arguments beginning with |prefix| from |arguments_|.
151 void DeleteArgsWithPrefix(const std::string& prefix);
152
Daniel Eratb85bf3d2016-07-28 21:45:47153 // Adds an entry to a flag containing a list of values. For example, for a
Daniel Erata7c95152018-04-25 23:34:21154 // flag like "--my-list=foo,bar", |flag_name| would be "my-list",
Daniel Erat173d0202019-02-09 15:59:47155 // |entry_separator| would be ",", and |new_entry| would be "foo" or "bar". If
156 // |prepend| is true, |new_entry| will be prepended before existing values;
157 // otherwise it will be appended after them.
158 void AddListFlagEntry(const std::string& flag_name,
Daniel Eratb85bf3d2016-07-28 21:45:47159 const std::string& entry_separator,
Daniel Eratff653022017-03-23 22:00:59160 const std::string& new_entry,
161 bool prepend);
Daniel Eratb85bf3d2016-07-28 21:45:47162
Ryo Hashimoto50a200c2016-06-13 08:45:15163 // Checks if an ASAN build was requested, doing appropriate initialization and
164 // returning true if so. Called by InitChromium().
Bertrand SIMONNET2e2317a2015-08-26 22:40:46165 bool SetUpASAN();
Bertrand SIMONNET2e2317a2015-08-26 22:40:46166
167 // Reads .info files in |pepper_plugins_path_| and adds the appropriate
168 // arguments to |arguments_|. Called by InitChromium().
169 void SetUpPepperPlugins();
170
171 // Add UI- and compositing-related flags to |arguments_|.
172 void AddUiFlags();
173
174 // Path under which files are created when running in a test.
175 base::FilePath base_path_for_testing_;
176
177 // UID and GID of the user used to run the binary.
Daniel Erata7c95152018-04-25 23:34:21178 uid_t uid_ = 0;
179 gid_t gid_ = 0;
Bertrand SIMONNET2e2317a2015-08-26 22:40:46180
181 // USE flags that were set when the system was built.
182 std::set<std::string> use_flags_;
183
184 // True if official Chrome OS hardware is being used.
Daniel Erata7c95152018-04-25 23:34:21185 bool is_chrome_os_hardware_ = false;
Bertrand SIMONNET2e2317a2015-08-26 22:40:46186
187 // True if this is a developer system, per the is_developer_end_user command.
Daniel Erata7c95152018-04-25 23:34:21188 bool is_developer_end_user_ = false;
Bertrand SIMONNET2e2317a2015-08-26 22:40:46189
Long Cheng0ab84432017-03-31 00:31:30190 // True if this is a test build, per CHROMEOS_RELEASE_TRACK in
191 // /etc/lsb-release.
Daniel Erata7c95152018-04-25 23:34:21192 bool is_test_build_ = false;
Long Cheng0ab84432017-03-31 00:31:30193
194 // Data in /etc/lsb-release.
195 std::string lsb_data_;
196
197 // Creation time of /etc/lsb-release.
198 base::Time lsb_release_time_;
199
Bertrand SIMONNET2e2317a2015-08-26 22:40:46200 // Environment variables that the caller should export before starting the
201 // executable.
202 StringMap environment_variables_;
203
204 // Command-line arguments that the caller should pass to the executable.
205 StringVector arguments_;
206
Daniel Erat173d0202019-02-09 15:59:47207 // Index in |arguments_| of list-based flags (e.g. --vmodule,
208 // --enable-features), keyed by base flag name (e.g. "vmodule",
209 // "enable-features"). Flags that have not been set are not included.
210 std::map<std::string, int> list_argument_indexes_;
Bertrand SIMONNET2e2317a2015-08-26 22:40:46211};
212
213} // namespace ui
214} // namespace chromeos
215
Hidehiko Abe40f15282024-10-11 05:25:21216#endif // LOGIN_MANAGER_CHROMIUM_COMMAND_BUILDER_H_