xref: /aosp_15_r20/external/cronet/base/environment.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_ENVIRONMENT_H_
6 #define BASE_ENVIRONMENT_H_
7 
8 #include <map>
9 #include <memory>
10 #include <string>
11 #include <string_view>
12 
13 #include "base/base_export.h"
14 #include "build/build_config.h"
15 
16 namespace base {
17 
18 namespace env_vars {
19 
20 #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
21 BASE_EXPORT extern const char kHome[];
22 #endif
23 
24 }  // namespace env_vars
25 
26 class BASE_EXPORT Environment {
27  public:
28   virtual ~Environment();
29 
30   // Returns the appropriate platform-specific instance.
31   static std::unique_ptr<Environment> Create();
32 
33   // Gets an environment variable's value and stores it in |result|.
34   // Returns false if the key is unset.
35   virtual bool GetVar(std::string_view variable_name, std::string* result) = 0;
36 
37   // Syntactic sugar for GetVar(variable_name, nullptr);
38   virtual bool HasVar(std::string_view variable_name);
39 
40   // Returns true on success, otherwise returns false. This method should not
41   // be called in a multi-threaded process.
42   virtual bool SetVar(std::string_view variable_name,
43                       const std::string& new_value) = 0;
44 
45   // Returns true on success, otherwise returns false. This method should not
46   // be called in a multi-threaded process.
47   virtual bool UnSetVar(std::string_view variable_name) = 0;
48 };
49 
50 #if BUILDFLAG(IS_WIN)
51 using NativeEnvironmentString = std::wstring;
52 #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
53 using NativeEnvironmentString = std::string;
54 #endif
55 using EnvironmentMap =
56     std::map<NativeEnvironmentString, NativeEnvironmentString>;
57 
58 }  // namespace base
59 
60 #endif  // BASE_ENVIRONMENT_H_
61