1 // Copyright (c) 2011 The Chromium 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 5 #ifndef BASE_ENVIRONMENT_H_ 6 #define BASE_ENVIRONMENT_H_ 7 8 #include <map> 9 #include <memory> 10 #include <string> 11 #include <string_view> 12 13 #include "util/build_config.h" 14 15 namespace base { 16 17 namespace env_vars { 18 19 #if defined(OS_POSIX) || defined(OS_FUCHSIA) 20 extern const char kHome[]; 21 #endif 22 23 } // namespace env_vars 24 25 class Environment { 26 public: 27 virtual ~Environment(); 28 29 // Returns the appropriate platform-specific instance. 30 static std::unique_ptr<Environment> Create(); 31 32 // Gets an environment variable's value and stores it in |result|. 33 // Returns false if the key is unset. 34 virtual bool GetVar(std::string_view variable_name, std::string* result) = 0; 35 36 // Syntactic sugar for GetVar(variable_name, nullptr); 37 virtual bool HasVar(std::string_view variable_name); 38 39 // Returns true on success, otherwise returns false. 40 virtual bool SetVar(std::string_view variable_name, 41 const std::string& new_value) = 0; 42 43 // Returns true on success, otherwise returns false. 44 virtual bool UnSetVar(std::string_view variable_name) = 0; 45 }; 46 47 #if defined(OS_WIN) 48 49 typedef std::u16string NativeEnvironmentString; 50 typedef std::map<NativeEnvironmentString, NativeEnvironmentString> 51 EnvironmentMap; 52 53 // Returns a modified environment vector constructed from the given environment 54 // and the list of changes given in |changes|. Each key in the environment is 55 // matched against the first element of the pairs. In the event of a match, the 56 // value is replaced by the second of the pair, unless the second is empty, in 57 // which case the key-value is removed. 58 // 59 // This Windows version takes and returns a Windows-style environment block 60 // which is a concatenated list of null-terminated 16-bit strings. The end is 61 // marked by a double-null terminator. The size of the returned string will 62 // include the terminators. 63 std::u16string AlterEnvironment(const char16_t* env, 64 const EnvironmentMap& changes); 65 66 #elif defined(OS_POSIX) || defined(OS_FUCHSIA) 67 68 typedef std::string NativeEnvironmentString; 69 typedef std::map<NativeEnvironmentString, NativeEnvironmentString> 70 EnvironmentMap; 71 72 // See general comments for the Windows version above. 73 // 74 // This Posix version takes and returns a Posix-style environment block, which 75 // is a null-terminated list of pointers to null-terminated strings. The 76 // returned array will have appended to it the storage for the array itself so 77 // there is only one pointer to manage, but this means that you can't copy the 78 // array without keeping the original around. 79 std::unique_ptr<char*[]> AlterEnvironment(const char* const* env, 80 const EnvironmentMap& changes); 81 82 #endif 83 84 } // namespace base 85 86 #endif // BASE_ENVIRONMENT_H_ 87