blob: 3a4ed04e4bb58238dbe84708406ba4d8ce38e279 [file] [log] [blame]
rvargas@google.com73eb5d02011-03-25 04:00:20 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
tfarina@chromium.org6d36c5d2010-08-03 12:00:50 +09005#ifndef BASE_ENVIRONMENT_H_
6#define BASE_ENVIRONMENT_H_
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +09007
brettw@chromium.org50484af2013-08-31 03:00:39 +09008#include <map>
dchengcc8e4d82016-04-05 06:25:51 +09009#include <memory>
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +090010#include <string>
11
darin@chromium.orge585bed2011-08-06 00:34:00 +090012#include "base/base_export.h"
brettw@chromium.org50484af2013-08-31 03:00:39 +090013#include "base/strings/string16.h"
thestigaed17252016-06-30 17:04:40 +090014#include "base/strings/string_piece.h"
jhawkins@chromium.org8e73d062011-04-05 03:04:37 +090015#include "build/build_config.h"
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +090016
17namespace base {
18
tfarina@chromium.org49c8f3a2010-07-21 11:59:28 +090019namespace env_vars {
20
21#if defined(OS_POSIX)
darin@chromium.orge585bed2011-08-06 00:34:00 +090022BASE_EXPORT extern const char kHome[];
tfarina@chromium.org49c8f3a2010-07-21 11:59:28 +090023#endif
24
25} // namespace env_vars
26
darin@chromium.orge585bed2011-08-06 00:34:00 +090027class BASE_EXPORT Environment {
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +090028 public:
tfarina@chromium.org6d36c5d2010-08-03 12:00:50 +090029 virtual ~Environment();
erg@chromium.org493f5f62010-07-16 06:03:54 +090030
thestigaed17252016-06-30 17:04:40 +090031 // Returns the appropriate platform-specific instance.
32 static std::unique_ptr<Environment> Create();
tfarina@chromium.orga5122652010-08-01 01:55:40 +090033
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +090034 // Gets an environment variable's value and stores it in |result|.
35 // Returns false if the key is unset.
thestigaed17252016-06-30 17:04:40 +090036 virtual bool GetVar(StringPiece variable_name, std::string* result) = 0;
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +090037
thestigaed17252016-06-30 17:04:40 +090038 // Syntactic sugar for GetVar(variable_name, nullptr);
39 virtual bool HasVar(StringPiece variable_name);
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +090040
tfarina@chromium.orga5d6f662010-07-16 12:34:25 +090041 // Returns true on success, otherwise returns false.
thestigaed17252016-06-30 17:04:40 +090042 virtual bool SetVar(StringPiece variable_name,
tfarina@chromium.org08225be2010-07-08 22:32:51 +090043 const std::string& new_value) = 0;
44
tfarina@chromium.orga5122652010-08-01 01:55:40 +090045 // Returns true on success, otherwise returns false.
thestigaed17252016-06-30 17:04:40 +090046 virtual bool UnSetVar(StringPiece variable_name) = 0;
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +090047};
48
brettw@chromium.org50484af2013-08-31 03:00:39 +090049
50#if defined(OS_WIN)
51
52typedef string16 NativeEnvironmentString;
53typedef std::map<NativeEnvironmentString, NativeEnvironmentString>
54 EnvironmentMap;
55
56// Returns a modified environment vector constructed from the given environment
57// and the list of changes given in |changes|. Each key in the environment is
58// matched against the first element of the pairs. In the event of a match, the
59// value is replaced by the second of the pair, unless the second is empty, in
60// which case the key-value is removed.
61//
62// This Windows version takes and returns a Windows-style environment block
63// which is a concatenated list of null-terminated 16-bit strings. The end is
64// marked by a double-null terminator. The size of the returned string will
65// include the terminators.
66BASE_EXPORT string16 AlterEnvironment(const wchar_t* env,
67 const EnvironmentMap& changes);
68
69#elif defined(OS_POSIX)
70
71typedef std::string NativeEnvironmentString;
72typedef std::map<NativeEnvironmentString, NativeEnvironmentString>
73 EnvironmentMap;
74
75// See general comments for the Windows version above.
76//
77// This Posix version takes and returns a Posix-style environment block, which
78// is a null-terminated list of pointers to null-terminated strings. The
79// returned array will have appended to it the storage for the array itself so
80// there is only one pointer to manage, but this means that you can't copy the
81// array without keeping the original around.
dchengcc8e4d82016-04-05 06:25:51 +090082BASE_EXPORT std::unique_ptr<char* []> AlterEnvironment(
brettw@chromium.org50484af2013-08-31 03:00:39 +090083 const char* const* env,
84 const EnvironmentMap& changes);
85
86#endif
87
thestig@chromium.orgb6ba9432010-04-03 10:05:39 +090088} // namespace base
89
tfarina@chromium.org6d36c5d2010-08-03 12:00:50 +090090#endif // BASE_ENVIRONMENT_H_