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