Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
Julien Brianceau | 9dcfeee | 2017-07-29 14:18:15 | [diff] [blame] | 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 | #include "base/scoped_environment_variable_override.h" |
Julien Brianceau | 9dcfeee | 2017-07-29 14:18:15 | [diff] [blame] | 6 | |
| 7 | #include "base/environment.h" |
| 8 | |
| 9 | namespace base { |
Julien Brianceau | 9dcfeee | 2017-07-29 14:18:15 | [diff] [blame] | 10 | |
| 11 | ScopedEnvironmentVariableOverride::ScopedEnvironmentVariableOverride( |
| 12 | const std::string& variable_name, |
Sven Zheng | 416d1fd | 2020-11-10 01:15:57 | [diff] [blame] | 13 | const std::string& value, |
| 14 | bool unset_var) |
Julien Brianceau | 9dcfeee | 2017-07-29 14:18:15 | [diff] [blame] | 15 | : environment_(Environment::Create()), |
| 16 | variable_name_(variable_name), |
| 17 | overridden_(false), |
Helmut Januschka | 726658b | 2025-03-21 22:44:57 | [diff] [blame] | 18 | old_value_(environment_->GetVar(variable_name)) { |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 19 | if (unset_var) { |
Sven Zheng | 416d1fd | 2020-11-10 01:15:57 | [diff] [blame] | 20 | overridden_ = environment_->UnSetVar(variable_name); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 21 | } else { |
Sven Zheng | 416d1fd | 2020-11-10 01:15:57 | [diff] [blame] | 22 | overridden_ = environment_->SetVar(variable_name, value); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 23 | } |
Julien Brianceau | 9dcfeee | 2017-07-29 14:18:15 | [diff] [blame] | 24 | } |
| 25 | |
Sven Zheng | 416d1fd | 2020-11-10 01:15:57 | [diff] [blame] | 26 | ScopedEnvironmentVariableOverride::ScopedEnvironmentVariableOverride( |
| 27 | const std::string& variable_name, |
| 28 | const std::string& value) |
| 29 | : ScopedEnvironmentVariableOverride(variable_name, value, false) {} |
| 30 | |
| 31 | ScopedEnvironmentVariableOverride::ScopedEnvironmentVariableOverride( |
| 32 | const std::string& variable_name) |
| 33 | : ScopedEnvironmentVariableOverride(variable_name, "", true) {} |
| 34 | |
Maksim Sisov | 24d31b6 | 2021-11-11 08:09:36 | [diff] [blame] | 35 | ScopedEnvironmentVariableOverride::ScopedEnvironmentVariableOverride( |
| 36 | ScopedEnvironmentVariableOverride&&) = default; |
| 37 | |
| 38 | ScopedEnvironmentVariableOverride& ScopedEnvironmentVariableOverride::operator=( |
| 39 | ScopedEnvironmentVariableOverride&&) = default; |
| 40 | |
Julien Brianceau | 9dcfeee | 2017-07-29 14:18:15 | [diff] [blame] | 41 | ScopedEnvironmentVariableOverride::~ScopedEnvironmentVariableOverride() { |
Sunny Sachanandani | 7f785c966 | 2022-03-21 19:23:49 | [diff] [blame] | 42 | if (environment_ && overridden_) { |
Helmut Januschka | 726658b | 2025-03-21 22:44:57 | [diff] [blame] | 43 | if (old_value_.has_value()) { |
| 44 | environment_->SetVar(variable_name_, old_value_.value()); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 45 | } else { |
Julien Brianceau | 9dcfeee | 2017-07-29 14:18:15 | [diff] [blame] | 46 | environment_->UnSetVar(variable_name_); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 47 | } |
Julien Brianceau | 9dcfeee | 2017-07-29 14:18:15 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
Julien Brianceau | 9dcfeee | 2017-07-29 14:18:15 | [diff] [blame] | 51 | } // namespace base |