blob: 88313256d9701766aa52b96cb6a9efa35faf8361 [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>
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
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
Daniel Erata7c95152018-04-25 23:34:2148 // Names of Chromium flags (without "--" prefixes) that need to be merged due
49 // to containing lists of comma-separated values.
50 static const char kVmoduleFlag[];
51 static const char kEnableFeaturesFlag[];
Daniel Erat173d0202019-02-09 15:59:4752 static const char kDisableFeaturesFlag[];
Daniel Erata7c95152018-04-25 23:34:2153 static const char kEnableBlinkFeaturesFlag[];
Daniel Erat173d0202019-02-09 15:59:4754 static const char kDisableBlinkFeaturesFlag[];
Daniel Erata7c95152018-04-25 23:34:2155
Bertrand SIMONNET2e2317a2015-08-26 22:40:4656 ChromiumCommandBuilder();
Qijiang Fan6bc59e12020-11-10 17:51:0657 ChromiumCommandBuilder(const ChromiumCommandBuilder&) = delete;
58 ChromiumCommandBuilder& operator=(const ChromiumCommandBuilder&) = delete;
59
Bertrand SIMONNET2e2317a2015-08-26 22:40:4660 ~ChromiumCommandBuilder();
61
62 uid_t uid() const { return uid_; }
63 gid_t gid() const { return gid_; }
64 bool is_chrome_os_hardware() const { return is_chrome_os_hardware_; }
65 bool is_developer_end_user() const { return is_developer_end_user_; }
Long Cheng0ab84432017-03-31 00:31:3066 bool is_test_build() const { return is_test_build_; }
Bertrand SIMONNET2e2317a2015-08-26 22:40:4667 const StringMap& environment_variables() const {
68 return environment_variables_;
69 }
70 const StringVector& arguments() const { return arguments_; }
71
72 void set_base_path_for_testing(const base::FilePath& path) {
73 base_path_for_testing_ = path;
74 }
75
Mike Frysinger5c0d27f2016-09-13 04:11:1976 // Performs just the basic initialization needed before UseFlagIsSet() can be
77 // used. Returns true on success.
Bertrand SIMONNET2e2317a2015-08-26 22:40:4678 bool Init();
79
80 // Determines the environment variables and arguments that should be set for
81 // all Chromium-derived binaries and updates |environment_variables_| and
82 // |arguments_| accordingly. Also creates necessary directories, sets resource
83 // limits, etc.
84 //
Bertrand SIMONNET2e2317a2015-08-26 22:40:4685 // Returns true on success.
Daniel Erat687f1cf2016-12-06 19:50:2686 bool SetUpChromium();
Bertrand SIMONNET2e2317a2015-08-26 22:40:4687
Bertrand SIMONNET2e2317a2015-08-26 22:40:4688 // Reads a user-supplied file requesting modifications to the current set of
89 // arguments. The following directives are supported:
90 //
91 // # This is a comment.
92 // Lines beginning with '#' are skipped.
93 //
94 // --some-flag=some-value
95 // Calls AddArg("--some-flag=some-value").
96 //
97 // !--flag-prefix
Daniel Eratff653022017-03-23 22:00:5998 // Removes all arguments beginning with "--flag-prefix".
99 //
100 // vmodule=foo=1
101 // Prepends a "foo=1" entry to the --vmodule flag.
102 //
103 // enable-features=foo
104 // Appends a "foo" entry to the --enable-features flag.
Bertrand SIMONNET2e2317a2015-08-26 22:40:46105 //
106 // NAME=VALUE
107 // Calls AddEnvVar("NAME", "VALUE").
108 //
Long Cheng0ab84432017-03-31 00:31:30109 // Any flags beginning with prefixes in |disallowed_prefixes| are disregarded.
Bertrand SIMONNET2e2317a2015-08-26 22:40:46110 // Returns true on success.
Long Cheng0ab84432017-03-31 00:31:30111 bool ApplyUserConfig(const base::FilePath& path,
112 const std::set<std::string>& disallowed_prefixes);
Bertrand SIMONNET2e2317a2015-08-26 22:40:46113
114 // Returns true if a USE flag named |flag| was set when the system image was
Daniel Eratb6a7b152017-07-13 01:41:06115 // built (and additionally listed in the libchromeos-use-flags ebuild so it
116 // will be included in the file at kUseFlagsPath).
Bertrand SIMONNET2e2317a2015-08-26 22:40:46117 bool UseFlagIsSet(const std::string& flag) const;
118
Bertrand SIMONNET2e2317a2015-08-26 22:40:46119 // Adds an environment variable to |environment_variables_|. Note that this
120 // method does not call setenv(); it is the caller's responsibility to
121 // actually export the variables.
122 void AddEnvVar(const std::string& name, const std::string& value);
123
124 // Returns the value of an environment variable previously added via
125 // AddEnvVar(). Crashes if the variable isn't set. Note that this method does
126 // not call getenv().
127 std::string ReadEnvVar(const std::string& name) const;
128
Daniel Erata7c95152018-04-25 23:34:21129 // Adds a command-line argument. For --vmodule, --enable-features, or
130 // --enable-blink-features flags (which contain lists of values that must be
131 // merged), use the following dedicated methods instead.
Bertrand SIMONNET2e2317a2015-08-26 22:40:46132 void AddArg(const std::string& arg);
133
Daniel Eratff653022017-03-23 22:00:59134 // Prepends |pattern| to the --vmodule flag in |arguments_|.
Bertrand SIMONNET2e2317a2015-08-26 22:40:46135 void AddVmodulePattern(const std::string& pattern);
136
Daniel Erat173d0202019-02-09 15:59:47137 // Appends |feature_name| to the --enable-features or --disable-features flag
138 // in |arguments_|.
Daniel Eratb85bf3d2016-07-28 21:45:47139 void AddFeatureEnableOverride(const std::string& feature_name);
Daniel Erat173d0202019-02-09 15:59:47140 void AddFeatureDisableOverride(const std::string& feature_name);
Daniel Eratb85bf3d2016-07-28 21:45:47141
Daniel Erat173d0202019-02-09 15:59:47142 // Appends |feature_name| to the --enable-blink-features or
143 // --disable-blink-features flag in |arguments_|.
Daniel Erata7c95152018-04-25 23:34:21144 void AddBlinkFeatureEnableOverride(const std::string& feature_name);
Daniel Erat173d0202019-02-09 15:59:47145 void AddBlinkFeatureDisableOverride(const std::string& feature_name);
Daniel Erata7c95152018-04-25 23:34:21146
Bertrand SIMONNET2e2317a2015-08-26 22:40:46147 private:
148 // Converts absolute path |path| into a base::FilePath, rooting it under
149 // |base_path_for_testing_| if it's non-empty.
150 base::FilePath GetPath(const std::string& path) const;
151
Daniel Erataae4ab62017-11-10 14:47:12152 // Removes arguments beginning with |prefix| from |arguments_|.
153 void DeleteArgsWithPrefix(const std::string& prefix);
154
Daniel Eratb85bf3d2016-07-28 21:45:47155 // Adds an entry to a flag containing a list of values. For example, for a
Daniel Erata7c95152018-04-25 23:34:21156 // flag like "--my-list=foo,bar", |flag_name| would be "my-list",
Daniel Erat173d0202019-02-09 15:59:47157 // |entry_separator| would be ",", and |new_entry| would be "foo" or "bar". If
158 // |prepend| is true, |new_entry| will be prepended before existing values;
159 // otherwise it will be appended after them.
160 void AddListFlagEntry(const std::string& flag_name,
Daniel Eratb85bf3d2016-07-28 21:45:47161 const std::string& entry_separator,
Daniel Eratff653022017-03-23 22:00:59162 const std::string& new_entry,
163 bool prepend);
Daniel Eratb85bf3d2016-07-28 21:45:47164
Ryo Hashimoto50a200c2016-06-13 08:45:15165 // Checks if an ASAN build was requested, doing appropriate initialization and
166 // returning true if so. Called by InitChromium().
Bertrand SIMONNET2e2317a2015-08-26 22:40:46167 bool SetUpASAN();
Bertrand SIMONNET2e2317a2015-08-26 22:40:46168
169 // Reads .info files in |pepper_plugins_path_| and adds the appropriate
170 // arguments to |arguments_|. Called by InitChromium().
171 void SetUpPepperPlugins();
172
173 // Add UI- and compositing-related flags to |arguments_|.
174 void AddUiFlags();
175
176 // Path under which files are created when running in a test.
177 base::FilePath base_path_for_testing_;
178
179 // UID and GID of the user used to run the binary.
Daniel Erata7c95152018-04-25 23:34:21180 uid_t uid_ = 0;
181 gid_t gid_ = 0;
Bertrand SIMONNET2e2317a2015-08-26 22:40:46182
183 // USE flags that were set when the system was built.
184 std::set<std::string> use_flags_;
185
186 // True if official Chrome OS hardware is being used.
Daniel Erata7c95152018-04-25 23:34:21187 bool is_chrome_os_hardware_ = false;
Bertrand SIMONNET2e2317a2015-08-26 22:40:46188
189 // True if this is a developer system, per the is_developer_end_user command.
Daniel Erata7c95152018-04-25 23:34:21190 bool is_developer_end_user_ = false;
Bertrand SIMONNET2e2317a2015-08-26 22:40:46191
Long Cheng0ab84432017-03-31 00:31:30192 // True if this is a test build, per CHROMEOS_RELEASE_TRACK in
193 // /etc/lsb-release.
Daniel Erata7c95152018-04-25 23:34:21194 bool is_test_build_ = false;
Long Cheng0ab84432017-03-31 00:31:30195
196 // Data in /etc/lsb-release.
197 std::string lsb_data_;
198
199 // Creation time of /etc/lsb-release.
200 base::Time lsb_release_time_;
201
Bertrand SIMONNET2e2317a2015-08-26 22:40:46202 // Environment variables that the caller should export before starting the
203 // executable.
204 StringMap environment_variables_;
205
206 // Command-line arguments that the caller should pass to the executable.
207 StringVector arguments_;
208
Daniel Erat173d0202019-02-09 15:59:47209 // Index in |arguments_| of list-based flags (e.g. --vmodule,
210 // --enable-features), keyed by base flag name (e.g. "vmodule",
211 // "enable-features"). Flags that have not been set are not included.
212 std::map<std::string, int> list_argument_indexes_;
Bertrand SIMONNET2e2317a2015-08-26 22:40:46213};
214
215} // namespace ui
216} // namespace chromeos
217
Bertrand SIMONNET52d97362015-09-02 21:22:37218#endif // LIBCHROMEOS_UI_CHROMEOS_UI_CHROMIUM_COMMAND_BUILDER_H_