blob: 6849fef88cb2484c76c510e5cf7ecdd2d054891f [file] [log] [blame]
[email protected]44106182012-04-06 03:53:021// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]9bc8cff2010-04-03 01:05:392// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]76b90d312010-08-03 03:00:505#include "base/environment.h"
[email protected]9bc8cff2010-04-03 01:05:396
7#if defined(OS_POSIX)
8#include <stdlib.h>
9#elif defined(OS_WIN)
10#include <windows.h>
11#endif
12
13#include "base/string_util.h"
14
15#if defined(OS_WIN)
[email protected]3b63f8f42011-03-28 01:54:1516#include "base/memory/scoped_ptr.h"
[email protected]9bc8cff2010-04-03 01:05:3917#include "base/utf_string_conversions.h"
18#endif
19
20namespace {
21
[email protected]76b90d312010-08-03 03:00:5022class EnvironmentImpl : public base::Environment {
[email protected]9bc8cff2010-04-03 01:05:3923 public:
[email protected]44106182012-04-06 03:53:0224 virtual bool GetVar(const char* variable_name,
25 std::string* result) OVERRIDE {
[email protected]3ba7e082010-08-07 02:57:5926 if (GetVarImpl(variable_name, result))
[email protected]9bc8cff2010-04-03 01:05:3927 return true;
28
29 // Some commonly used variable names are uppercase while others
30 // are lowercase, which is inconsistent. Let's try to be helpful
31 // and look for a variable name with the reverse case.
32 // I.e. HTTP_PROXY may be http_proxy for some users/systems.
33 char first_char = variable_name[0];
34 std::string alternate_case_var;
35 if (first_char >= 'a' && first_char <= 'z')
36 alternate_case_var = StringToUpperASCII(std::string(variable_name));
37 else if (first_char >= 'A' && first_char <= 'Z')
38 alternate_case_var = StringToLowerASCII(std::string(variable_name));
39 else
40 return false;
[email protected]3ba7e082010-08-07 02:57:5941 return GetVarImpl(alternate_case_var.c_str(), result);
[email protected]9bc8cff2010-04-03 01:05:3942 }
[email protected]ac7264c2010-07-08 13:32:5143
[email protected]44106182012-04-06 03:53:0244 virtual bool SetVar(const char* variable_name,
45 const std::string& new_value) OVERRIDE {
[email protected]c87bcf002010-08-06 01:03:3746 return SetVarImpl(variable_name, new_value);
[email protected]ac7264c2010-07-08 13:32:5147 }
48
[email protected]44106182012-04-06 03:53:0249 virtual bool UnSetVar(const char* variable_name) OVERRIDE {
[email protected]4fae3162010-08-04 02:13:3450 return UnSetVarImpl(variable_name);
[email protected]fc586c72010-07-31 16:55:4051 }
52
[email protected]9bc8cff2010-04-03 01:05:3953 private:
[email protected]3ba7e082010-08-07 02:57:5954 bool GetVarImpl(const char* variable_name, std::string* result) {
[email protected]9bc8cff2010-04-03 01:05:3955#if defined(OS_POSIX)
56 const char* env_value = getenv(variable_name);
57 if (!env_value)
58 return false;
59 // Note that the variable may be defined but empty.
60 if (result)
61 *result = env_value;
62 return true;
63#elif defined(OS_WIN)
64 DWORD value_length = ::GetEnvironmentVariable(
65 UTF8ToWide(variable_name).c_str(), NULL, 0);
66 if (value_length == 0)
67 return false;
68 if (result) {
[email protected]604eb052013-01-18 14:21:5869 scoped_ptr<wchar_t[]> value(new wchar_t[value_length]);
[email protected]9bc8cff2010-04-03 01:05:3970 ::GetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), value.get(),
71 value_length);
72 *result = WideToUTF8(value.get());
73 }
74 return true;
75#else
76#error need to port
77#endif
78 }
[email protected]ac7264c2010-07-08 13:32:5179
[email protected]c87bcf002010-08-06 01:03:3780 bool SetVarImpl(const char* variable_name, const std::string& new_value) {
[email protected]ac7264c2010-07-08 13:32:5181#if defined(OS_POSIX)
[email protected]e9032c62010-07-16 03:34:2582 // On success, zero is returned.
[email protected]c10688622010-08-17 23:33:4183 return !setenv(variable_name, new_value.c_str(), 1);
[email protected]ac7264c2010-07-08 13:32:5184#elif defined(OS_WIN)
[email protected]c10688622010-08-17 23:33:4185 // On success, a nonzero value is returned.
86 return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(),
87 UTF8ToWide(new_value).c_str());
[email protected]ac7264c2010-07-08 13:32:5188#endif
89 }
[email protected]fc586c72010-07-31 16:55:4090
[email protected]4fae3162010-08-04 02:13:3491 bool UnSetVarImpl(const char* variable_name) {
[email protected]fc586c72010-07-31 16:55:4092#if defined(OS_POSIX)
93 // On success, zero is returned.
[email protected]c10688622010-08-17 23:33:4194 return !unsetenv(variable_name);
[email protected]fc586c72010-07-31 16:55:4095#elif defined(OS_WIN)
[email protected]c10688622010-08-17 23:33:4196 // On success, a nonzero value is returned.
97 return !!SetEnvironmentVariable(UTF8ToWide(variable_name).c_str(), NULL);
[email protected]fc586c72010-07-31 16:55:4098#endif
99 }
[email protected]9bc8cff2010-04-03 01:05:39100};
101
102} // namespace
103
104namespace base {
105
[email protected]574f6f0c2010-07-21 02:59:28106namespace env_vars {
107
108#if defined(OS_POSIX)
109// On Posix systems, this variable contains the location of the user's home
110// directory. (e.g, /home/username/).
111const char kHome[] = "HOME";
112#endif
113
114} // namespace env_vars
115
[email protected]76b90d312010-08-03 03:00:50116Environment::~Environment() {}
[email protected]3a3d47472010-07-15 21:03:54117
[email protected]9bc8cff2010-04-03 01:05:39118// static
[email protected]76b90d312010-08-03 03:00:50119Environment* Environment::Create() {
120 return new EnvironmentImpl();
[email protected]9bc8cff2010-04-03 01:05:39121}
122
[email protected]9432ade2010-08-04 23:43:20123bool Environment::HasVar(const char* variable_name) {
[email protected]3ba7e082010-08-07 02:57:59124 return GetVar(variable_name, NULL);
[email protected]fc586c72010-07-31 16:55:40125}
126
[email protected]9bc8cff2010-04-03 01:05:39127} // namespace base