Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 1 | // 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 SIMONNET | 52d9736 | 2015-09-02 21:22:37 | [diff] [blame] | 5 | #ifndef LIBCHROMEOS_UI_CHROMEOS_UI_CHROMIUM_COMMAND_BUILDER_H_ |
| 6 | #define LIBCHROMEOS_UI_CHROMEOS_UI_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> |
| 16 | #include <base/macros.h> |
Long Cheng | 0ab8443 | 2017-03-31 00:31:30 | [diff] [blame] | 17 | #include <base/time/time.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. |
| 24 | class 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 SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 48 | 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 Cheng | 0ab8443 | 2017-03-31 00:31:30 | [diff] [blame] | 55 | bool is_test_build() const { return is_test_build_; } |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 56 | 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 Frysinger | 5c0d27f | 2016-09-13 04:11:19 | [diff] [blame] | 65 | // Performs just the basic initialization needed before UseFlagIsSet() can be |
| 66 | // used. Returns true on success. |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 67 | 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 SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 74 | // Returns true on success. |
Daniel Erat | 687f1cf | 2016-12-06 19:50:26 | [diff] [blame] | 75 | bool SetUpChromium(); |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 76 | |
| 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 Erat | ff65302 | 2017-03-23 22:00:59 | [diff] [blame] | 91 | // 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 SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 98 | // |
| 99 | // NAME=VALUE |
| 100 | // Calls AddEnvVar("NAME", "VALUE"). |
| 101 | // |
Long Cheng | 0ab8443 | 2017-03-31 00:31:30 | [diff] [blame] | 102 | // Any flags beginning with prefixes in |disallowed_prefixes| are disregarded. |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 103 | // Returns true on success. |
Long Cheng | 0ab8443 | 2017-03-31 00:31:30 | [diff] [blame] | 104 | bool ApplyUserConfig(const base::FilePath& path, |
| 105 | const std::set<std::string>& disallowed_prefixes); |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 106 | |
| 107 | // 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] | 108 | // built (and additionally listed in the libchromeos-use-flags ebuild so it |
| 109 | // will be included in the file at kUseFlagsPath). |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 110 | bool UseFlagIsSet(const std::string& flag) const; |
| 111 | |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 112 | // 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 Erat | ff65302 | 2017-03-23 22:00:59 | [diff] [blame] | 125 | // Prepends |pattern| to the --vmodule flag in |arguments_|. |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 126 | void AddVmodulePattern(const std::string& pattern); |
| 127 | |
Daniel Erat | ff65302 | 2017-03-23 22:00:59 | [diff] [blame] | 128 | // Appends |feature_name| to the --enable-features flag in |arguments_|. |
Daniel Erat | b85bf3d | 2016-07-28 21:45:47 | [diff] [blame] | 129 | void AddFeatureEnableOverride(const std::string& feature_name); |
| 130 | |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 131 | 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 Erat | aae4ab6 | 2017-11-10 14:47:12 | [diff] [blame^] | 136 | // Removes arguments beginning with |prefix| from |arguments_|. |
| 137 | void DeleteArgsWithPrefix(const std::string& prefix); |
| 138 | |
Daniel Erat | b85bf3d | 2016-07-28 21:45:47 | [diff] [blame] | 139 | // 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 Erat | ff65302 | 2017-03-23 22:00:59 | [diff] [blame] | 143 | // |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 Erat | b85bf3d | 2016-07-28 21:45:47 | [diff] [blame] | 146 | void AddListFlagEntry(int* flag_argument_index, |
| 147 | const std::string& flag_prefix, |
| 148 | const std::string& entry_separator, |
Daniel Erat | ff65302 | 2017-03-23 22:00:59 | [diff] [blame] | 149 | const std::string& new_entry, |
| 150 | bool prepend); |
Daniel Erat | b85bf3d | 2016-07-28 21:45:47 | [diff] [blame] | 151 | |
Ryo Hashimoto | 50a200c | 2016-06-13 08:45:15 | [diff] [blame] | 152 | // Checks if an ASAN build was requested, doing appropriate initialization and |
| 153 | // returning true if so. Called by InitChromium(). |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 154 | bool SetUpASAN(); |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 155 | |
| 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 Cheng | 0ab8443 | 2017-03-31 00:31:30 | [diff] [blame] | 179 | // 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 SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 189 | // 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 Erat | b85bf3d | 2016-07-28 21:45:47 | [diff] [blame] | 196 | // Index in |arguments_| of the --vmodule and --enable-features flags. -1 if |
| 197 | // the flags haven't been set. |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 198 | int vmodule_argument_index_; |
Daniel Erat | b85bf3d | 2016-07-28 21:45:47 | [diff] [blame] | 199 | int enable_features_argument_index_; |
Bertrand SIMONNET | 2e2317a | 2015-08-26 22:40:46 | [diff] [blame] | 200 | |
| 201 | DISALLOW_COPY_AND_ASSIGN(ChromiumCommandBuilder); |
| 202 | }; |
| 203 | |
| 204 | } // namespace ui |
| 205 | } // namespace chromeos |
| 206 | |
Bertrand SIMONNET | 52d9736 | 2015-09-02 21:22:37 | [diff] [blame] | 207 | #endif // LIBCHROMEOS_UI_CHROMEOS_UI_CHROMIUM_COMMAND_BUILDER_H_ |