blob: 3a4ed04e4bb58238dbe84708406ba4d8ce38e279 [file] [log] [blame]
[email protected]f5661ca2011-03-24 19:00:201// Copyright (c) 2011 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#ifndef BASE_ENVIRONMENT_H_
6#define BASE_ENVIRONMENT_H_
[email protected]9bc8cff2010-04-03 01:05:397
[email protected]b345c482013-08-30 18:00:398#include <map>
dcheng093de9b2016-04-04 21:25:519#include <memory>
[email protected]9bc8cff2010-04-03 01:05:3910#include <string>
11
[email protected]0bea7252011-08-05 15:34:0012#include "base/base_export.h"
[email protected]b345c482013-08-30 18:00:3913#include "base/strings/string16.h"
thestig0c412e852016-06-30 08:04:4014#include "base/strings/string_piece.h"
[email protected]2edc2862011-04-04 18:04:3715#include "build/build_config.h"
[email protected]9bc8cff2010-04-03 01:05:3916
17namespace base {
18
[email protected]574f6f0c2010-07-21 02:59:2819namespace env_vars {
20
21#if defined(OS_POSIX)
[email protected]0bea7252011-08-05 15:34:0022BASE_EXPORT extern const char kHome[];
[email protected]574f6f0c2010-07-21 02:59:2823#endif
24
25} // namespace env_vars
26
[email protected]0bea7252011-08-05 15:34:0027class BASE_EXPORT Environment {
[email protected]9bc8cff2010-04-03 01:05:3928 public:
[email protected]76b90d312010-08-03 03:00:5029 virtual ~Environment();
[email protected]3a3d47472010-07-15 21:03:5430
thestig0c412e852016-06-30 08:04:4031 // Returns the appropriate platform-specific instance.
32 static std::unique_ptr<Environment> Create();
[email protected]fc586c72010-07-31 16:55:4033
[email protected]9bc8cff2010-04-03 01:05:3934 // Gets an environment variable's value and stores it in |result|.
35 // Returns false if the key is unset.
thestig0c412e852016-06-30 08:04:4036 virtual bool GetVar(StringPiece variable_name, std::string* result) = 0;
[email protected]9bc8cff2010-04-03 01:05:3937
thestig0c412e852016-06-30 08:04:4038 // Syntactic sugar for GetVar(variable_name, nullptr);
39 virtual bool HasVar(StringPiece variable_name);
[email protected]9bc8cff2010-04-03 01:05:3940
[email protected]e9032c62010-07-16 03:34:2541 // Returns true on success, otherwise returns false.
thestig0c412e852016-06-30 08:04:4042 virtual bool SetVar(StringPiece variable_name,
[email protected]ac7264c2010-07-08 13:32:5143 const std::string& new_value) = 0;
44
[email protected]fc586c72010-07-31 16:55:4045 // Returns true on success, otherwise returns false.
thestig0c412e852016-06-30 08:04:4046 virtual bool UnSetVar(StringPiece variable_name) = 0;
[email protected]9bc8cff2010-04-03 01:05:3947};
48
[email protected]b345c482013-08-30 18:00:3949
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.
dcheng093de9b2016-04-04 21:25:5182BASE_EXPORT std::unique_ptr<char* []> AlterEnvironment(
[email protected]b345c482013-08-30 18:00:3983 const char* const* env,
84 const EnvironmentMap& changes);
85
86#endif
87
[email protected]9bc8cff2010-04-03 01:05:3988} // namespace base
89
[email protected]76b90d312010-08-03 03:00:5090#endif // BASE_ENVIRONMENT_H_