xref: /aosp_15_r20/external/libchrome/base/environment.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_ENVIRONMENT_H_
6*635a8641SAndroid Build Coastguard Worker #define BASE_ENVIRONMENT_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include <map>
9*635a8641SAndroid Build Coastguard Worker #include <memory>
10*635a8641SAndroid Build Coastguard Worker #include <string>
11*635a8641SAndroid Build Coastguard Worker 
12*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h"
13*635a8641SAndroid Build Coastguard Worker #include "base/strings/string16.h"
14*635a8641SAndroid Build Coastguard Worker #include "base/strings/string_piece.h"
15*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
16*635a8641SAndroid Build Coastguard Worker 
17*635a8641SAndroid Build Coastguard Worker namespace base {
18*635a8641SAndroid Build Coastguard Worker 
19*635a8641SAndroid Build Coastguard Worker namespace env_vars {
20*635a8641SAndroid Build Coastguard Worker 
21*635a8641SAndroid Build Coastguard Worker #if defined(OS_POSIX) || defined(OS_FUCHSIA)
22*635a8641SAndroid Build Coastguard Worker BASE_EXPORT extern const char kHome[];
23*635a8641SAndroid Build Coastguard Worker #endif
24*635a8641SAndroid Build Coastguard Worker 
25*635a8641SAndroid Build Coastguard Worker }  // namespace env_vars
26*635a8641SAndroid Build Coastguard Worker 
27*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT Environment {
28*635a8641SAndroid Build Coastguard Worker  public:
29*635a8641SAndroid Build Coastguard Worker   virtual ~Environment();
30*635a8641SAndroid Build Coastguard Worker 
31*635a8641SAndroid Build Coastguard Worker   // Returns the appropriate platform-specific instance.
32*635a8641SAndroid Build Coastguard Worker   static std::unique_ptr<Environment> Create();
33*635a8641SAndroid Build Coastguard Worker 
34*635a8641SAndroid Build Coastguard Worker   // Gets an environment variable's value and stores it in |result|.
35*635a8641SAndroid Build Coastguard Worker   // Returns false if the key is unset.
36*635a8641SAndroid Build Coastguard Worker   virtual bool GetVar(StringPiece variable_name, std::string* result) = 0;
37*635a8641SAndroid Build Coastguard Worker 
38*635a8641SAndroid Build Coastguard Worker   // Syntactic sugar for GetVar(variable_name, nullptr);
39*635a8641SAndroid Build Coastguard Worker   virtual bool HasVar(StringPiece variable_name);
40*635a8641SAndroid Build Coastguard Worker 
41*635a8641SAndroid Build Coastguard Worker   // Returns true on success, otherwise returns false.
42*635a8641SAndroid Build Coastguard Worker   virtual bool SetVar(StringPiece variable_name,
43*635a8641SAndroid Build Coastguard Worker                       const std::string& new_value) = 0;
44*635a8641SAndroid Build Coastguard Worker 
45*635a8641SAndroid Build Coastguard Worker   // Returns true on success, otherwise returns false.
46*635a8641SAndroid Build Coastguard Worker   virtual bool UnSetVar(StringPiece variable_name) = 0;
47*635a8641SAndroid Build Coastguard Worker };
48*635a8641SAndroid Build Coastguard Worker 
49*635a8641SAndroid Build Coastguard Worker 
50*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
51*635a8641SAndroid Build Coastguard Worker 
52*635a8641SAndroid Build Coastguard Worker typedef string16 NativeEnvironmentString;
53*635a8641SAndroid Build Coastguard Worker typedef std::map<NativeEnvironmentString, NativeEnvironmentString>
54*635a8641SAndroid Build Coastguard Worker     EnvironmentMap;
55*635a8641SAndroid Build Coastguard Worker 
56*635a8641SAndroid Build Coastguard Worker // Returns a modified environment vector constructed from the given environment
57*635a8641SAndroid Build Coastguard Worker // and the list of changes given in |changes|. Each key in the environment is
58*635a8641SAndroid Build Coastguard Worker // matched against the first element of the pairs. In the event of a match, the
59*635a8641SAndroid Build Coastguard Worker // value is replaced by the second of the pair, unless the second is empty, in
60*635a8641SAndroid Build Coastguard Worker // which case the key-value is removed.
61*635a8641SAndroid Build Coastguard Worker //
62*635a8641SAndroid Build Coastguard Worker // This Windows version takes and returns a Windows-style environment block
63*635a8641SAndroid Build Coastguard Worker // which is a concatenated list of null-terminated 16-bit strings. The end is
64*635a8641SAndroid Build Coastguard Worker // marked by a double-null terminator. The size of the returned string will
65*635a8641SAndroid Build Coastguard Worker // include the terminators.
66*635a8641SAndroid Build Coastguard Worker BASE_EXPORT string16 AlterEnvironment(const wchar_t* env,
67*635a8641SAndroid Build Coastguard Worker                                       const EnvironmentMap& changes);
68*635a8641SAndroid Build Coastguard Worker 
69*635a8641SAndroid Build Coastguard Worker #elif defined(OS_POSIX) || defined(OS_FUCHSIA)
70*635a8641SAndroid Build Coastguard Worker 
71*635a8641SAndroid Build Coastguard Worker typedef std::string NativeEnvironmentString;
72*635a8641SAndroid Build Coastguard Worker typedef std::map<NativeEnvironmentString, NativeEnvironmentString>
73*635a8641SAndroid Build Coastguard Worker     EnvironmentMap;
74*635a8641SAndroid Build Coastguard Worker 
75*635a8641SAndroid Build Coastguard Worker // See general comments for the Windows version above.
76*635a8641SAndroid Build Coastguard Worker //
77*635a8641SAndroid Build Coastguard Worker // This Posix version takes and returns a Posix-style environment block, which
78*635a8641SAndroid Build Coastguard Worker // is a null-terminated list of pointers to null-terminated strings. The
79*635a8641SAndroid Build Coastguard Worker // returned array will have appended to it the storage for the array itself so
80*635a8641SAndroid Build Coastguard Worker // there is only one pointer to manage, but this means that you can't copy the
81*635a8641SAndroid Build Coastguard Worker // array without keeping the original around.
82*635a8641SAndroid Build Coastguard Worker BASE_EXPORT std::unique_ptr<char* []> AlterEnvironment(
83*635a8641SAndroid Build Coastguard Worker     const char* const* env,
84*635a8641SAndroid Build Coastguard Worker     const EnvironmentMap& changes);
85*635a8641SAndroid Build Coastguard Worker 
86*635a8641SAndroid Build Coastguard Worker #endif
87*635a8641SAndroid Build Coastguard Worker 
88*635a8641SAndroid Build Coastguard Worker }  // namespace base
89*635a8641SAndroid Build Coastguard Worker 
90*635a8641SAndroid Build Coastguard Worker #endif  // BASE_ENVIRONMENT_H_
91