| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
| 12 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 14 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 15 | 16 |
| 16 #if defined(OS_POSIX) | 17 #if defined(OS_POSIX) |
| 17 #include <stdlib.h> | 18 #include <stdlib.h> |
| 18 #elif defined(OS_WIN) | 19 #elif defined(OS_WIN) |
| 19 #include <windows.h> | 20 #include <windows.h> |
| 20 #endif | 21 #endif |
| 21 | 22 |
| 22 namespace base { | 23 namespace base { |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 class EnvironmentImpl : public Environment { | 27 class EnvironmentImpl : public Environment { |
| 27 public: | 28 public: |
| 28 bool GetVar(const char* variable_name, std::string* result) override { | 29 bool GetVar(StringPiece variable_name, std::string* result) override { |
| 29 if (GetVarImpl(variable_name, result)) | 30 if (GetVarImpl(variable_name, result)) |
| 30 return true; | 31 return true; |
| 31 | 32 |
| 32 // Some commonly used variable names are uppercase while others | 33 // Some commonly used variable names are uppercase while others |
| 33 // are lowercase, which is inconsistent. Let's try to be helpful | 34 // are lowercase, which is inconsistent. Let's try to be helpful |
| 34 // and look for a variable name with the reverse case. | 35 // and look for a variable name with the reverse case. |
| 35 // I.e. HTTP_PROXY may be http_proxy for some users/systems. | 36 // I.e. HTTP_PROXY may be http_proxy for some users/systems. |
| 36 char first_char = variable_name[0]; | 37 char first_char = variable_name[0]; |
| 37 std::string alternate_case_var; | 38 std::string alternate_case_var; |
| 38 if (IsAsciiLower(first_char)) | 39 if (IsAsciiLower(first_char)) |
| 39 alternate_case_var = ToUpperASCII(variable_name); | 40 alternate_case_var = ToUpperASCII(variable_name); |
| 40 else if (IsAsciiUpper(first_char)) | 41 else if (IsAsciiUpper(first_char)) |
| 41 alternate_case_var = ToLowerASCII(variable_name); | 42 alternate_case_var = ToLowerASCII(variable_name); |
| 42 else | 43 else |
| 43 return false; | 44 return false; |
| 44 return GetVarImpl(alternate_case_var.c_str(), result); | 45 return GetVarImpl(alternate_case_var.c_str(), result); |
| 45 } | 46 } |
| 46 | 47 |
| 47 bool SetVar(const char* variable_name, | 48 bool SetVar(StringPiece variable_name, |
| 48 const std::string& new_value) override { | 49 const std::string& new_value) override { |
| 49 return SetVarImpl(variable_name, new_value); | 50 return SetVarImpl(variable_name, new_value); |
| 50 } | 51 } |
| 51 | 52 |
| 52 bool UnSetVar(const char* variable_name) override { | 53 bool UnSetVar(StringPiece variable_name) override { |
| 53 return UnSetVarImpl(variable_name); | 54 return UnSetVarImpl(variable_name); |
| 54 } | 55 } |
| 55 | 56 |
| 56 private: | 57 private: |
| 57 bool GetVarImpl(const char* variable_name, std::string* result) { | 58 bool GetVarImpl(StringPiece variable_name, std::string* result) { |
| 58 #if defined(OS_POSIX) | 59 #if defined(OS_POSIX) |
| 59 const char* env_value = getenv(variable_name); | 60 const char* env_value = getenv(variable_name.data()); |
| 60 if (!env_value) | 61 if (!env_value) |
| 61 return false; | 62 return false; |
| 62 // Note that the variable may be defined but empty. | 63 // Note that the variable may be defined but empty. |
| 63 if (result) | 64 if (result) |
| 64 *result = env_value; | 65 *result = env_value; |
| 65 return true; | 66 return true; |
| 66 #elif defined(OS_WIN) | 67 #elif defined(OS_WIN) |
| 67 DWORD value_length = ::GetEnvironmentVariable( | 68 DWORD value_length = |
| 68 UTF8ToWide(variable_name).c_str(), NULL, 0); | 69 ::GetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), nullptr, 0); |
| 69 if (value_length == 0) | 70 if (value_length == 0) |
| 70 return false; | 71 return false; |
| 71 if (result) { | 72 if (result) { |
| 72 std::unique_ptr<wchar_t[]> value(new wchar_t[value_length]); | 73 std::unique_ptr<wchar_t[]> value(new wchar_t[value_length]); |
| 73 ::GetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), value.get(), | 74 ::GetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), value.get(), |
| 74 value_length); | 75 value_length); |
| 75 *result = WideToUTF8(value.get()); | 76 *result = WideToUTF8(value.get()); |
| 76 } | 77 } |
| 77 return true; | 78 return true; |
| 78 #else | 79 #else |
| 79 #error need to port | 80 #error need to port |
| 80 #endif | 81 #endif |
| 81 } | 82 } |
| 82 | 83 |
| 83 bool SetVarImpl(const char* variable_name, const std::string& new_value) { | 84 bool SetVarImpl(StringPiece variable_name, const std::string& new_value) { |
| 84 #if defined(OS_POSIX) | 85 #if defined(OS_POSIX) |
| 85 // On success, zero is returned. | 86 // On success, zero is returned. |
| 86 return !setenv(variable_name, new_value.c_str(), 1); | 87 return !setenv(variable_name.data(), new_value.c_str(), 1); |
| 87 #elif defined(OS_WIN) | 88 #elif defined(OS_WIN) |
| 88 // On success, a nonzero value is returned. | 89 // On success, a nonzero value is returned. |
| 89 return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), | 90 return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), |
| 90 UTF8ToWide(new_value).c_str()); | 91 UTF8ToWide(new_value).c_str()); |
| 91 #endif | 92 #endif |
| 92 } | 93 } |
| 93 | 94 |
| 94 bool UnSetVarImpl(const char* variable_name) { | 95 bool UnSetVarImpl(StringPiece variable_name) { |
| 95 #if defined(OS_POSIX) | 96 #if defined(OS_POSIX) |
| 96 // On success, zero is returned. | 97 // On success, zero is returned. |
| 97 return !unsetenv(variable_name); | 98 return !unsetenv(variable_name.data()); |
| 98 #elif defined(OS_WIN) | 99 #elif defined(OS_WIN) |
| 99 // On success, a nonzero value is returned. | 100 // On success, a nonzero value is returned. |
| 100 return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), NULL); | 101 return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), nullptr); |
| 101 #endif | 102 #endif |
| 102 } | 103 } |
| 103 }; | 104 }; |
| 104 | 105 |
| 105 // Parses a null-terminated input string of an environment block. The key is | 106 // Parses a null-terminated input string of an environment block. The key is |
| 106 // placed into the given string, and the total length of the line, including | 107 // placed into the given string, and the total length of the line, including |
| 107 // the terminating null, is returned. | 108 // the terminating null, is returned. |
| 108 size_t ParseEnvLine(const NativeEnvironmentString::value_type* input, | 109 size_t ParseEnvLine(const NativeEnvironmentString::value_type* input, |
| 109 NativeEnvironmentString* key) { | 110 NativeEnvironmentString* key) { |
| 110 // Skip to the equals or end of the string, this is the key. | 111 // Skip to the equals or end of the string, this is the key. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 127 // On Posix systems, this variable contains the location of the user's home | 128 // On Posix systems, this variable contains the location of the user's home |
| 128 // directory. (e.g, /home/username/). | 129 // directory. (e.g, /home/username/). |
| 129 const char kHome[] = "HOME"; | 130 const char kHome[] = "HOME"; |
| 130 #endif | 131 #endif |
| 131 | 132 |
| 132 } // namespace env_vars | 133 } // namespace env_vars |
| 133 | 134 |
| 134 Environment::~Environment() {} | 135 Environment::~Environment() {} |
| 135 | 136 |
| 136 // static | 137 // static |
| 137 Environment* Environment::Create() { | 138 std::unique_ptr<Environment> Environment::Create() { |
| 138 return new EnvironmentImpl(); | 139 return MakeUnique<EnvironmentImpl>(); |
| 139 } | 140 } |
| 140 | 141 |
| 141 bool Environment::HasVar(const char* variable_name) { | 142 bool Environment::HasVar(StringPiece variable_name) { |
| 142 return GetVar(variable_name, NULL); | 143 return GetVar(variable_name, nullptr); |
| 143 } | 144 } |
| 144 | 145 |
| 145 #if defined(OS_WIN) | 146 #if defined(OS_WIN) |
| 146 | 147 |
| 147 string16 AlterEnvironment(const wchar_t* env, | 148 string16 AlterEnvironment(const wchar_t* env, |
| 148 const EnvironmentMap& changes) { | 149 const EnvironmentMap& changes) { |
| 149 string16 result; | 150 string16 result; |
| 150 | 151 |
| 151 // First copy all unmodified values to the output. | 152 // First copy all unmodified values to the output. |
| 152 size_t cur_env = 0; | 153 size_t cur_env = 0; |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 for (size_t i = 0; i < result_indices.size(); i++) | 231 for (size_t i = 0; i < result_indices.size(); i++) |
| 231 result[i] = &storage_data[result_indices[i]]; | 232 result[i] = &storage_data[result_indices[i]]; |
| 232 result[result_indices.size()] = 0; // Null terminator. | 233 result[result_indices.size()] = 0; // Null terminator. |
| 233 | 234 |
| 234 return result; | 235 return result; |
| 235 } | 236 } |
| 236 | 237 |
| 237 #endif // OS_POSIX | 238 #endif // OS_POSIX |
| 238 | 239 |
| 239 } // namespace base | 240 } // namespace base |
| OLD | NEW |