michael@0: // Copyright (c) 2011 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef BASE_ENVIRONMENT_H_ michael@0: #define BASE_ENVIRONMENT_H_ michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "base/base_export.h" michael@0: #include "base/memory/scoped_ptr.h" michael@0: #include "base/strings/string16.h" michael@0: #include "build/build_config.h" michael@0: michael@0: namespace base { michael@0: michael@0: namespace env_vars { michael@0: michael@0: #if defined(OS_POSIX) michael@0: BASE_EXPORT extern const char kHome[]; michael@0: #endif michael@0: michael@0: } // namespace env_vars michael@0: michael@0: class BASE_EXPORT Environment { michael@0: public: michael@0: virtual ~Environment(); michael@0: michael@0: // Static factory method that returns the implementation that provide the michael@0: // appropriate platform-specific instance. michael@0: static Environment* Create(); michael@0: michael@0: // Gets an environment variable's value and stores it in |result|. michael@0: // Returns false if the key is unset. michael@0: virtual bool GetVar(const char* variable_name, std::string* result) = 0; michael@0: michael@0: // Syntactic sugar for GetVar(variable_name, NULL); michael@0: virtual bool HasVar(const char* variable_name); michael@0: michael@0: // Returns true on success, otherwise returns false. michael@0: virtual bool SetVar(const char* variable_name, michael@0: const std::string& new_value) = 0; michael@0: michael@0: // Returns true on success, otherwise returns false. michael@0: virtual bool UnSetVar(const char* variable_name) = 0; michael@0: }; michael@0: michael@0: michael@0: #if defined(OS_WIN) michael@0: michael@0: typedef string16 NativeEnvironmentString; michael@0: typedef std::map michael@0: EnvironmentMap; michael@0: michael@0: // Returns a modified environment vector constructed from the given environment michael@0: // and the list of changes given in |changes|. Each key in the environment is michael@0: // matched against the first element of the pairs. In the event of a match, the michael@0: // value is replaced by the second of the pair, unless the second is empty, in michael@0: // which case the key-value is removed. michael@0: // michael@0: // This Windows version takes and returns a Windows-style environment block michael@0: // which is a concatenated list of null-terminated 16-bit strings. The end is michael@0: // marked by a double-null terminator. The size of the returned string will michael@0: // include the terminators. michael@0: BASE_EXPORT string16 AlterEnvironment(const wchar_t* env, michael@0: const EnvironmentMap& changes); michael@0: michael@0: #elif defined(OS_POSIX) michael@0: michael@0: typedef std::string NativeEnvironmentString; michael@0: typedef std::map michael@0: EnvironmentMap; michael@0: michael@0: // See general comments for the Windows version above. michael@0: // michael@0: // This Posix version takes and returns a Posix-style environment block, which michael@0: // is a null-terminated list of pointers to null-terminated strings. The michael@0: // returned array will have appended to it the storage for the array itself so michael@0: // there is only one pointer to manage, but this means that you can't copy the michael@0: // array without keeping the original around. michael@0: BASE_EXPORT scoped_ptr AlterEnvironment( michael@0: const char* const* env, michael@0: const EnvironmentMap& changes); michael@0: michael@0: #endif michael@0: michael@0: } // namespace base michael@0: michael@0: #endif // BASE_ENVIRONMENT_H_