rvargas@google.com | 73eb5d0 | 2011-03-25 04:00:20 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [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 | 6d36c5d | 2010-08-03 12:00:50 +0900 | [diff] [blame] | 5 | #ifndef BASE_ENVIRONMENT_H_ |
| 6 | #define BASE_ENVIRONMENT_H_ |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 7 | |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 8 | #include <map> |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 9 | #include <string> |
| 10 | |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 11 | #include "base/base_export.h" |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 12 | #include "base/memory/scoped_ptr.h" |
| 13 | #include "base/strings/string16.h" |
jhawkins@chromium.org | 8e73d06 | 2011-04-05 03:04:37 +0900 | [diff] [blame] | 14 | #include "build/build_config.h" |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 15 | |
| 16 | namespace base { |
| 17 | |
tfarina@chromium.org | 49c8f3a | 2010-07-21 11:59:28 +0900 | [diff] [blame] | 18 | namespace env_vars { |
| 19 | |
| 20 | #if defined(OS_POSIX) |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 21 | BASE_EXPORT extern const char kHome[]; |
tfarina@chromium.org | 49c8f3a | 2010-07-21 11:59:28 +0900 | [diff] [blame] | 22 | #endif |
| 23 | |
| 24 | } // namespace env_vars |
| 25 | |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 26 | class BASE_EXPORT Environment { |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 27 | public: |
tfarina@chromium.org | 6d36c5d | 2010-08-03 12:00:50 +0900 | [diff] [blame] | 28 | virtual ~Environment(); |
erg@chromium.org | 493f5f6 | 2010-07-16 06:03:54 +0900 | [diff] [blame] | 29 | |
tfarina@chromium.org | a512265 | 2010-08-01 01:55:40 +0900 | [diff] [blame] | 30 | // Static factory method that returns the implementation that provide the |
| 31 | // appropriate platform-specific instance. |
tfarina@chromium.org | 6d36c5d | 2010-08-03 12:00:50 +0900 | [diff] [blame] | 32 | static Environment* Create(); |
tfarina@chromium.org | a512265 | 2010-08-01 01:55:40 +0900 | [diff] [blame] | 33 | |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 34 | // Gets an environment variable's value and stores it in |result|. |
| 35 | // Returns false if the key is unset. |
tfarina@chromium.org | 8f115a8 | 2010-08-07 11:57:59 +0900 | [diff] [blame] | 36 | virtual bool GetVar(const char* variable_name, std::string* result) = 0; |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 37 | |
tfarina@chromium.org | 8f115a8 | 2010-08-07 11:57:59 +0900 | [diff] [blame] | 38 | // Syntactic sugar for GetVar(variable_name, NULL); |
tfarina@chromium.org | e7f0559 | 2010-08-05 08:43:20 +0900 | [diff] [blame] | 39 | virtual bool HasVar(const char* variable_name); |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 40 | |
tfarina@chromium.org | a5d6f66 | 2010-07-16 12:34:25 +0900 | [diff] [blame] | 41 | // Returns true on success, otherwise returns false. |
tfarina@chromium.org | 5dc6673 | 2010-08-06 10:03:37 +0900 | [diff] [blame] | 42 | virtual bool SetVar(const char* variable_name, |
tfarina@chromium.org | 08225be | 2010-07-08 22:32:51 +0900 | [diff] [blame] | 43 | const std::string& new_value) = 0; |
| 44 | |
tfarina@chromium.org | a512265 | 2010-08-01 01:55:40 +0900 | [diff] [blame] | 45 | // Returns true on success, otherwise returns false. |
tfarina@chromium.org | 6b2d9cc | 2010-08-04 11:13:34 +0900 | [diff] [blame] | 46 | virtual bool UnSetVar(const char* variable_name) = 0; |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 47 | }; |
| 48 | |
brettw@chromium.org | 50484af | 2013-08-31 03:00:39 +0900 | [diff] [blame] | 49 | |
| 50 | #if defined(OS_WIN) |
| 51 | |
| 52 | typedef string16 NativeEnvironmentString; |
| 53 | typedef std::map<NativeEnvironmentString, NativeEnvironmentString> |
| 54 | EnvironmentMap; |
| 55 | |
| 56 | // Returns a modified environment vector constructed from the given environment |
| 57 | // and the list of changes given in |changes|. Each key in the environment is |
| 58 | // matched against the first element of the pairs. In the event of a match, the |
| 59 | // value is replaced by the second of the pair, unless the second is empty, in |
| 60 | // which case the key-value is removed. |
| 61 | // |
| 62 | // This Windows version takes and returns a Windows-style environment block |
| 63 | // which is a concatenated list of null-terminated 16-bit strings. The end is |
| 64 | // marked by a double-null terminator. The size of the returned string will |
| 65 | // include the terminators. |
| 66 | BASE_EXPORT string16 AlterEnvironment(const wchar_t* env, |
| 67 | const EnvironmentMap& changes); |
| 68 | |
| 69 | #elif defined(OS_POSIX) |
| 70 | |
| 71 | typedef std::string NativeEnvironmentString; |
| 72 | typedef std::map<NativeEnvironmentString, NativeEnvironmentString> |
| 73 | EnvironmentMap; |
| 74 | |
| 75 | // See general comments for the Windows version above. |
| 76 | // |
| 77 | // This Posix version takes and returns a Posix-style environment block, which |
| 78 | // is a null-terminated list of pointers to null-terminated strings. The |
| 79 | // returned array will have appended to it the storage for the array itself so |
| 80 | // there is only one pointer to manage, but this means that you can't copy the |
| 81 | // array without keeping the original around. |
| 82 | BASE_EXPORT scoped_ptr<char*[]> AlterEnvironment( |
| 83 | const char* const* env, |
| 84 | const EnvironmentMap& changes); |
| 85 | |
| 86 | #endif |
| 87 | |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 88 | } // namespace base |
| 89 | |
tfarina@chromium.org | 6d36c5d | 2010-08-03 12:00:50 +0900 | [diff] [blame] | 90 | #endif // BASE_ENVIRONMENT_H_ |