Mike Frysinger | 3a446f2 | 2022-09-08 07:37:14 | [diff] [blame] | 1 | // Copyright 2014 The ChromiumOS Authors |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Hidehiko Abe | 40f1528 | 2024-10-11 05:25:21 | [diff] [blame] | 5 | #ifndef LOGIN_MANAGER_CHROMIUM_COMMAND_BUILDER_H_ |
| 6 | #define LOGIN_MANAGER_CHROMIUM_COMMAND_BUILDER_H_ |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 7 | |
| 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 Cheng | 0ab8443 | 2017-03-31 00:31:30 | [diff] [blame] | 16 | #include <base/time/time.h> |
Mike Frysinger | e9c9790 | 2022-08-17 13:44:50 | [diff] [blame] | 17 | #include <brillo/brillo_export.h> |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 18 | |
| 19 | namespace chromeos { |
| 20 | namespace ui { |
| 21 | |
| 22 | // ChromiumCommandBuilder facilitates building a command line for running a |
| 23 | // Chromium-derived binary and performing related setup. |
Mike Frysinger | e9c9790 | 2022-08-17 13:44:50 | [diff] [blame] | 24 | class BRILLO_EXPORT ChromiumCommandBuilder { |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 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 | |
Daniel Erat | a7c9515 | 2018-04-25 23:34:21 | [diff] [blame] | 42 | // 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 Erat | 173d020 | 2019-02-09 15:59:47 | [diff] [blame] | 46 | static const char kDisableFeaturesFlag[]; |
Daniel Erat | a7c9515 | 2018-04-25 23:34:21 | [diff] [blame] | 47 | static const char kEnableBlinkFeaturesFlag[]; |
Daniel Erat | 173d020 | 2019-02-09 15:59:47 | [diff] [blame] | 48 | static const char kDisableBlinkFeaturesFlag[]; |
Daniel Erat | a7c9515 | 2018-04-25 23:34:21 | [diff] [blame] | 49 | |
howardchung | ac43d27 | 2023-07-17 09:21:01 | [diff] [blame] | 50 | static const char kCrosConfigBluetoothFlagsPath[]; |
| 51 | static const char kCrosConfigBlockFlossAvailability[]; |
Zhengping Jiang | 91cabd1 | 2024-04-19 01:54:39 | [diff] [blame] | 52 | static const char kCrosConfigBlockLLPrivacyAvailability[]; |
howardchung | ac43d27 | 2023-07-17 09:21:01 | [diff] [blame] | 53 | |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 54 | ChromiumCommandBuilder(); |
Qijiang Fan | 6bc59e1 | 2020-11-10 17:51:06 | [diff] [blame] | 55 | ChromiumCommandBuilder(const ChromiumCommandBuilder&) = delete; |
| 56 | ChromiumCommandBuilder& operator=(const ChromiumCommandBuilder&) = delete; |
| 57 | |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 58 | ~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 Cheng | 0ab8443 | 2017-03-31 00:31:30 | [diff] [blame] | 64 | bool is_test_build() const { return is_test_build_; } |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 65 | 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 Frysinger | 5c0d27f | 2016-09-13 04:11:19 | [diff] [blame] | 74 | // Performs just the basic initialization needed before UseFlagIsSet() can be |
| 75 | // used. Returns true on success. |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 76 | 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 SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 83 | // Returns true on success. |
Daniel Erat | 687f1cf | 2016-12-06 19:50:26 | [diff] [blame] | 84 | bool SetUpChromium(); |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 85 | |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 86 | // 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 Erat | ff65302 | 2017-03-23 22:00:59 | [diff] [blame] | 96 | // 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 SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 103 | // |
| 104 | // NAME=VALUE |
| 105 | // Calls AddEnvVar("NAME", "VALUE"). |
| 106 | // |
Long Cheng | 0ab8443 | 2017-03-31 00:31:30 | [diff] [blame] | 107 | // Any flags beginning with prefixes in |disallowed_prefixes| are disregarded. |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 108 | // Returns true on success. |
Long Cheng | 0ab8443 | 2017-03-31 00:31:30 | [diff] [blame] | 109 | bool ApplyUserConfig(const base::FilePath& path, |
| 110 | const std::set<std::string>& disallowed_prefixes); |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 111 | |
| 112 | // Returns true if a USE flag named |flag| was set when the system image was |
Daniel Erat | b6a7b15 | 2017-07-13 01:41:06 | [diff] [blame] | 113 | // built (and additionally listed in the libchromeos-use-flags ebuild so it |
| 114 | // will be included in the file at kUseFlagsPath). |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 115 | bool UseFlagIsSet(const std::string& flag) const; |
| 116 | |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 117 | // 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 Erat | a7c9515 | 2018-04-25 23:34:21 | [diff] [blame] | 127 | // 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 SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 130 | void AddArg(const std::string& arg); |
| 131 | |
Daniel Erat | ff65302 | 2017-03-23 22:00:59 | [diff] [blame] | 132 | // Prepends |pattern| to the --vmodule flag in |arguments_|. |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 133 | void AddVmodulePattern(const std::string& pattern); |
| 134 | |
Daniel Erat | 173d020 | 2019-02-09 15:59:47 | [diff] [blame] | 135 | // Appends |feature_name| to the --enable-features or --disable-features flag |
| 136 | // in |arguments_|. |
Daniel Erat | b85bf3d | 2016-07-28 21:45:47 | [diff] [blame] | 137 | void AddFeatureEnableOverride(const std::string& feature_name); |
Daniel Erat | 173d020 | 2019-02-09 15:59:47 | [diff] [blame] | 138 | void AddFeatureDisableOverride(const std::string& feature_name); |
Daniel Erat | b85bf3d | 2016-07-28 21:45:47 | [diff] [blame] | 139 | |
Daniel Erat | 173d020 | 2019-02-09 15:59:47 | [diff] [blame] | 140 | // Appends |feature_name| to the --enable-blink-features or |
| 141 | // --disable-blink-features flag in |arguments_|. |
Daniel Erat | a7c9515 | 2018-04-25 23:34:21 | [diff] [blame] | 142 | void AddBlinkFeatureEnableOverride(const std::string& feature_name); |
Daniel Erat | 173d020 | 2019-02-09 15:59:47 | [diff] [blame] | 143 | void AddBlinkFeatureDisableOverride(const std::string& feature_name); |
Daniel Erat | a7c9515 | 2018-04-25 23:34:21 | [diff] [blame] | 144 | |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 145 | 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 Erat | aae4ab6 | 2017-11-10 14:47:12 | [diff] [blame] | 150 | // Removes arguments beginning with |prefix| from |arguments_|. |
| 151 | void DeleteArgsWithPrefix(const std::string& prefix); |
| 152 | |
Daniel Erat | b85bf3d | 2016-07-28 21:45:47 | [diff] [blame] | 153 | // Adds an entry to a flag containing a list of values. For example, for a |
Daniel Erat | a7c9515 | 2018-04-25 23:34:21 | [diff] [blame] | 154 | // flag like "--my-list=foo,bar", |flag_name| would be "my-list", |
Daniel Erat | 173d020 | 2019-02-09 15:59:47 | [diff] [blame] | 155 | // |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 Erat | b85bf3d | 2016-07-28 21:45:47 | [diff] [blame] | 159 | const std::string& entry_separator, |
Daniel Erat | ff65302 | 2017-03-23 22:00:59 | [diff] [blame] | 160 | const std::string& new_entry, |
| 161 | bool prepend); |
Daniel Erat | b85bf3d | 2016-07-28 21:45:47 | [diff] [blame] | 162 | |
Ryo Hashimoto | 50a200c | 2016-06-13 08:45:15 | [diff] [blame] | 163 | // Checks if an ASAN build was requested, doing appropriate initialization and |
| 164 | // returning true if so. Called by InitChromium(). |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 165 | bool SetUpASAN(); |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 166 | |
| 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 Erat | a7c9515 | 2018-04-25 23:34:21 | [diff] [blame] | 178 | uid_t uid_ = 0; |
| 179 | gid_t gid_ = 0; |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 180 | |
| 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 Erat | a7c9515 | 2018-04-25 23:34:21 | [diff] [blame] | 185 | bool is_chrome_os_hardware_ = false; |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 186 | |
| 187 | // True if this is a developer system, per the is_developer_end_user command. |
Daniel Erat | a7c9515 | 2018-04-25 23:34:21 | [diff] [blame] | 188 | bool is_developer_end_user_ = false; |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 189 | |
Long Cheng | 0ab8443 | 2017-03-31 00:31:30 | [diff] [blame] | 190 | // True if this is a test build, per CHROMEOS_RELEASE_TRACK in |
| 191 | // /etc/lsb-release. |
Daniel Erat | a7c9515 | 2018-04-25 23:34:21 | [diff] [blame] | 192 | bool is_test_build_ = false; |
Long Cheng | 0ab8443 | 2017-03-31 00:31:30 | [diff] [blame] | 193 | |
| 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 SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 200 | // 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 Erat | 173d020 | 2019-02-09 15:59:47 | [diff] [blame] | 207 | // 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 SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | } // namespace ui |
| 214 | } // namespace chromeos |
| 215 | |
Hidehiko Abe | 40f1528 | 2024-10-11 05:25:21 | [diff] [blame] | 216 | #endif // LOGIN_MANAGER_CHROMIUM_COMMAND_BUILDER_H_ |