environment.h 1.7 KB

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