| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/environment.h" | 5 #include "base/environment.h" |
| 6 | 6 |
| 7 #if defined(OS_POSIX) | 7 #if defined(OS_POSIX) |
| 8 #include <stdlib.h> | 8 #include <stdlib.h> |
| 9 #elif defined(OS_WIN) | 9 #elif defined(OS_WIN) |
| 10 #include <windows.h> | 10 #include <windows.h> |
| 11 #endif | 11 #endif |
| 12 | 12 |
| 13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 14 | 14 |
| 15 #if defined(OS_WIN) | 15 #if defined(OS_WIN) |
| 16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
| 18 #endif | 18 #endif |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 class EnvironmentImpl : public base::Environment { | 22 class EnvironmentImpl : public base::Environment { |
| 23 public: | 23 public: |
| 24 virtual bool GetVar(const char* variable_name, std::string* result) { | 24 virtual bool GetVar(const char* variable_name, |
| 25 std::string* result) OVERRIDE { |
| 25 if (GetVarImpl(variable_name, result)) | 26 if (GetVarImpl(variable_name, result)) |
| 26 return true; | 27 return true; |
| 27 | 28 |
| 28 // Some commonly used variable names are uppercase while others | 29 // Some commonly used variable names are uppercase while others |
| 29 // are lowercase, which is inconsistent. Let's try to be helpful | 30 // are lowercase, which is inconsistent. Let's try to be helpful |
| 30 // and look for a variable name with the reverse case. | 31 // and look for a variable name with the reverse case. |
| 31 // I.e. HTTP_PROXY may be http_proxy for some users/systems. | 32 // I.e. HTTP_PROXY may be http_proxy for some users/systems. |
| 32 char first_char = variable_name[0]; | 33 char first_char = variable_name[0]; |
| 33 std::string alternate_case_var; | 34 std::string alternate_case_var; |
| 34 if (first_char >= 'a' && first_char <= 'z') | 35 if (first_char >= 'a' && first_char <= 'z') |
| 35 alternate_case_var = StringToUpperASCII(std::string(variable_name)); | 36 alternate_case_var = StringToUpperASCII(std::string(variable_name)); |
| 36 else if (first_char >= 'A' && first_char <= 'Z') | 37 else if (first_char >= 'A' && first_char <= 'Z') |
| 37 alternate_case_var = StringToLowerASCII(std::string(variable_name)); | 38 alternate_case_var = StringToLowerASCII(std::string(variable_name)); |
| 38 else | 39 else |
| 39 return false; | 40 return false; |
| 40 return GetVarImpl(alternate_case_var.c_str(), result); | 41 return GetVarImpl(alternate_case_var.c_str(), result); |
| 41 } | 42 } |
| 42 | 43 |
| 43 virtual bool SetVar(const char* variable_name, const std::string& new_value) { | 44 virtual bool SetVar(const char* variable_name, |
| 45 const std::string& new_value) OVERRIDE { |
| 44 return SetVarImpl(variable_name, new_value); | 46 return SetVarImpl(variable_name, new_value); |
| 45 } | 47 } |
| 46 | 48 |
| 47 virtual bool UnSetVar(const char* variable_name) { | 49 virtual bool UnSetVar(const char* variable_name) OVERRIDE { |
| 48 return UnSetVarImpl(variable_name); | 50 return UnSetVarImpl(variable_name); |
| 49 } | 51 } |
| 50 | 52 |
| 51 private: | 53 private: |
| 52 bool GetVarImpl(const char* variable_name, std::string* result) { | 54 bool GetVarImpl(const char* variable_name, std::string* result) { |
| 53 #if defined(OS_POSIX) | 55 #if defined(OS_POSIX) |
| 54 const char* env_value = getenv(variable_name); | 56 const char* env_value = getenv(variable_name); |
| 55 if (!env_value) | 57 if (!env_value) |
| 56 return false; | 58 return false; |
| 57 // Note that the variable may be defined but empty. | 59 // Note that the variable may be defined but empty. |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 // static | 118 // static |
| 117 Environment* Environment::Create() { | 119 Environment* Environment::Create() { |
| 118 return new EnvironmentImpl(); | 120 return new EnvironmentImpl(); |
| 119 } | 121 } |
| 120 | 122 |
| 121 bool Environment::HasVar(const char* variable_name) { | 123 bool Environment::HasVar(const char* variable_name) { |
| 122 return GetVar(variable_name, NULL); | 124 return GetVar(variable_name, NULL); |
| 123 } | 125 } |
| 124 | 126 |
| 125 } // namespace base | 127 } // namespace base |
| OLD | NEW |