environment.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #ifndef BASE_ENVIRONMENT_H_
  5. #define BASE_ENVIRONMENT_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include "base/base_export.h"
  10. #include "base/strings/string16.h"
  11. #include "base/strings/string_piece.h"
  12. #include "build/build_config.h"
  13. namespace base {
  14. namespace env_vars {
  15. #if defined(OS_POSIX) || defined(OS_FUCHSIA)
  16. BASE_EXPORT extern const char kHome[];
  17. #endif
  18. } // namespace env_vars
  19. class BASE_EXPORT Environment {
  20. public:
  21. virtual ~Environment();
  22. // Returns the appropriate platform-specific instance.
  23. static std::unique_ptr<Environment> Create();
  24. // Gets an environment variable's value and stores it in |result|.
  25. // Returns false if the key is unset.
  26. virtual bool GetVar(StringPiece variable_name, std::string* result) = 0;
  27. // Syntactic sugar for GetVar(variable_name, nullptr);
  28. virtual bool HasVar(StringPiece variable_name);
  29. // Returns true on success, otherwise returns false. This method should not
  30. // be called in a multi-threaded process.
  31. virtual bool SetVar(StringPiece variable_name,
  32. const std::string& new_value) = 0;
  33. // Returns true on success, otherwise returns false. This method should not
  34. // be called in a multi-threaded process.
  35. virtual bool UnSetVar(StringPiece variable_name) = 0;
  36. };
  37. #if defined(OS_WIN)
  38. using NativeEnvironmentString = std::wstring;
  39. #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
  40. using NativeEnvironmentString = std::string;
  41. #endif
  42. using EnvironmentMap =
  43. std::map<NativeEnvironmentString, NativeEnvironmentString>;
  44. } // namespace base
  45. #endif // BASE_ENVIRONMENT_H_