Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors |
thestig@chromium.org | 9bc8cff | 2010-04-03 01:05:39 | [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 | |
tfarina@chromium.org | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 5 | #ifndef BASE_ENVIRONMENT_H_ |
| 6 | #define BASE_ENVIRONMENT_H_ |
thestig@chromium.org | 9bc8cff | 2010-04-03 01:05:39 | [diff] [blame] | 7 | |
brettw@chromium.org | b345c48 | 2013-08-30 18:00:39 | [diff] [blame] | 8 | #include <map> |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 9 | #include <memory> |
thestig@chromium.org | 9bc8cff | 2010-04-03 01:05:39 | [diff] [blame] | 10 | #include <string> |
| 11 | |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 12 | #include "base/base_export.h" |
thestig | 0c412e85 | 2016-06-30 08:04:40 | [diff] [blame] | 13 | #include "base/strings/string_piece.h" |
jhawkins@chromium.org | 2edc286 | 2011-04-04 18:04:37 | [diff] [blame] | 14 | #include "build/build_config.h" |
thestig@chromium.org | 9bc8cff | 2010-04-03 01:05:39 | [diff] [blame] | 15 | |
| 16 | namespace base { |
| 17 | |
tfarina@chromium.org | 574f6f0c | 2010-07-21 02:59:28 | [diff] [blame] | 18 | namespace env_vars { |
| 19 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 20 | #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 21 | BASE_EXPORT extern const char kHome[]; |
tfarina@chromium.org | 574f6f0c | 2010-07-21 02:59:28 | [diff] [blame] | 22 | #endif |
| 23 | |
| 24 | } // namespace env_vars |
| 25 | |
darin@chromium.org | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 26 | class BASE_EXPORT Environment { |
thestig@chromium.org | 9bc8cff | 2010-04-03 01:05:39 | [diff] [blame] | 27 | public: |
tfarina@chromium.org | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 28 | virtual ~Environment(); |
erg@chromium.org | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 29 | |
thestig | 0c412e85 | 2016-06-30 08:04:40 | [diff] [blame] | 30 | // Returns the appropriate platform-specific instance. |
| 31 | static std::unique_ptr<Environment> Create(); |
tfarina@chromium.org | fc586c7 | 2010-07-31 16:55:40 | [diff] [blame] | 32 | |
thestig@chromium.org | 9bc8cff | 2010-04-03 01:05:39 | [diff] [blame] | 33 | // Gets an environment variable's value and stores it in |result|. |
| 34 | // Returns false if the key is unset. |
thestig | 0c412e85 | 2016-06-30 08:04:40 | [diff] [blame] | 35 | virtual bool GetVar(StringPiece variable_name, std::string* result) = 0; |
thestig@chromium.org | 9bc8cff | 2010-04-03 01:05:39 | [diff] [blame] | 36 | |
thestig | 0c412e85 | 2016-06-30 08:04:40 | [diff] [blame] | 37 | // Syntactic sugar for GetVar(variable_name, nullptr); |
| 38 | virtual bool HasVar(StringPiece variable_name); |
thestig@chromium.org | 9bc8cff | 2010-04-03 01:05:39 | [diff] [blame] | 39 | |
David Benjamin | 76ee79eb | 2019-03-15 17:02:09 | [diff] [blame] | 40 | // Returns true on success, otherwise returns false. This method should not |
| 41 | // be called in a multi-threaded process. |
thestig | 0c412e85 | 2016-06-30 08:04:40 | [diff] [blame] | 42 | virtual bool SetVar(StringPiece variable_name, |
tfarina@chromium.org | ac7264c | 2010-07-08 13:32:51 | [diff] [blame] | 43 | const std::string& new_value) = 0; |
| 44 | |
David Benjamin | 76ee79eb | 2019-03-15 17:02:09 | [diff] [blame] | 45 | // Returns true on success, otherwise returns false. This method should not |
| 46 | // be called in a multi-threaded process. |
thestig | 0c412e85 | 2016-06-30 08:04:40 | [diff] [blame] | 47 | virtual bool UnSetVar(StringPiece variable_name) = 0; |
thestig@chromium.org | 9bc8cff | 2010-04-03 01:05:39 | [diff] [blame] | 48 | }; |
| 49 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 50 | #if BUILDFLAG(IS_WIN) |
Jan Wilken Dörrie | 6bdce49 | 2019-11-05 11:36:50 | [diff] [blame] | 51 | using NativeEnvironmentString = std::wstring; |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 52 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
David Benjamin | 76ee79eb | 2019-03-15 17:02:09 | [diff] [blame] | 53 | using NativeEnvironmentString = std::string; |
Jan Wilken Dörrie | 6bdce49 | 2019-11-05 11:36:50 | [diff] [blame] | 54 | #endif |
David Benjamin | 76ee79eb | 2019-03-15 17:02:09 | [diff] [blame] | 55 | using EnvironmentMap = |
| 56 | std::map<NativeEnvironmentString, NativeEnvironmentString>; |
brettw@chromium.org | b345c48 | 2013-08-30 18:00:39 | [diff] [blame] | 57 | |
thestig@chromium.org | 9bc8cff | 2010-04-03 01:05:39 | [diff] [blame] | 58 | } // namespace base |
| 59 | |
tfarina@chromium.org | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 60 | #endif // BASE_ENVIRONMENT_H_ |