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.
5 #ifndef BASE_SCOPED_ENVIRONMENT_VARIABLE_OVERRIDE_H_
6 #define BASE_SCOPED_ENVIRONMENT_VARIABLE_OVERRIDE_H_
11 #include "base/base_export.h"
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 {
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();
30 base::Environment* GetEnv() { return environment_.get(); }
31 bool IsOverridden() { return overridden_; }
32 bool WasSet() { return was_set_; }
35 ScopedEnvironmentVariableOverride(const std::string& variable_name,
36 const std::string& value,
38 std::unique_ptr<Environment> environment_;
39 std::string variable_name_;
42 std::string old_value_;
47 #endif // BASE_SCOPED_ENVIRONMENT_VARIABLE_OVERRIDE_H_