Julien Brianceau | 9dcfeee | 2017-07-29 14:18:15 | [diff] [blame] | 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 | |
Maksim Sisov | 24d31b6 | 2021-11-11 08:09:36 | [diff] [blame] | 5 | #ifndef BASE_SCOPED_ENVIRONMENT_VARIABLE_OVERRIDE_H_ |
| 6 | #define BASE_SCOPED_ENVIRONMENT_VARIABLE_OVERRIDE_H_ |
Julien Brianceau | 9dcfeee | 2017-07-29 14:18:15 | [diff] [blame] | 7 | |
| 8 | #include <memory> |
| 9 | #include <string> |
| 10 | |
Maksim Sisov | 24d31b6 | 2021-11-11 08:09:36 | [diff] [blame] | 11 | #include "base/base_export.h" |
| 12 | |
Julien Brianceau | 9dcfeee | 2017-07-29 14:18:15 | [diff] [blame] | 13 | namespace base { |
| 14 | |
| 15 | class Environment; |
| 16 | |
Julien Brianceau | 9dcfeee | 2017-07-29 14:18:15 | [diff] [blame] | 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. |
Maksim Sisov | 24d31b6 | 2021-11-11 08:09:36 | [diff] [blame] | 19 | class BASE_EXPORT ScopedEnvironmentVariableOverride final { |
Julien Brianceau | 9dcfeee | 2017-07-29 14:18:15 | [diff] [blame] | 20 | public: |
| 21 | ScopedEnvironmentVariableOverride(const std::string& variable_name, |
| 22 | const std::string& value); |
Sven Zheng | 416d1fd | 2020-11-10 01:15:57 | [diff] [blame] | 23 | // Unset the variable. |
| 24 | explicit ScopedEnvironmentVariableOverride(const std::string& variable_name); |
Maksim Sisov | 24d31b6 | 2021-11-11 08:09:36 | [diff] [blame] | 25 | ScopedEnvironmentVariableOverride(ScopedEnvironmentVariableOverride&&); |
| 26 | ScopedEnvironmentVariableOverride& operator=( |
| 27 | ScopedEnvironmentVariableOverride&&); |
Julien Brianceau | 9dcfeee | 2017-07-29 14:18:15 | [diff] [blame] | 28 | ~ScopedEnvironmentVariableOverride(); |
| 29 | |
| 30 | base::Environment* GetEnv() { return environment_.get(); } |
| 31 | bool IsOverridden() { return overridden_; } |
| 32 | bool WasSet() { return was_set_; } |
| 33 | |
| 34 | private: |
Sven Zheng | 416d1fd | 2020-11-10 01:15:57 | [diff] [blame] | 35 | ScopedEnvironmentVariableOverride(const std::string& variable_name, |
| 36 | const std::string& value, |
| 37 | bool unset_var); |
Julien Brianceau | 9dcfeee | 2017-07-29 14:18:15 | [diff] [blame] | 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 | |
Julien Brianceau | 9dcfeee | 2017-07-29 14:18:15 | [diff] [blame] | 45 | } // namespace base |
| 46 | |
Maksim Sisov | 24d31b6 | 2021-11-11 08:09:36 | [diff] [blame] | 47 | #endif // BASE_SCOPED_ENVIRONMENT_VARIABLE_OVERRIDE_H_ |