scoped_environment_variable_override.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2017 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_SCOPED_ENVIRONMENT_VARIABLE_OVERRIDE_H_
  5. #define BASE_SCOPED_ENVIRONMENT_VARIABLE_OVERRIDE_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/base_export.h"
  9. namespace base {
  10. class Environment;
  11. // Helper class to override |variable_name| environment variable to |value| for
  12. // the lifetime of this class. Upon destruction, the previous value is restored.
  13. class BASE_EXPORT ScopedEnvironmentVariableOverride final {
  14. public:
  15. ScopedEnvironmentVariableOverride(const std::string& variable_name,
  16. const std::string& value);
  17. // Unset the variable.
  18. explicit ScopedEnvironmentVariableOverride(const std::string& variable_name);
  19. ScopedEnvironmentVariableOverride(ScopedEnvironmentVariableOverride&&);
  20. ScopedEnvironmentVariableOverride& operator=(
  21. ScopedEnvironmentVariableOverride&&);
  22. ~ScopedEnvironmentVariableOverride();
  23. base::Environment* GetEnv() { return environment_.get(); }
  24. bool IsOverridden() { return overridden_; }
  25. bool WasSet() { return was_set_; }
  26. private:
  27. ScopedEnvironmentVariableOverride(const std::string& variable_name,
  28. const std::string& value,
  29. bool unset_var);
  30. std::unique_ptr<Environment> environment_;
  31. std::string variable_name_;
  32. bool overridden_;
  33. bool was_set_;
  34. std::string old_value_;
  35. };
  36. } // namespace base
  37. #endif // BASE_SCOPED_ENVIRONMENT_VARIABLE_OVERRIDE_H_