Bug 1700051: part 49) Add some documentation to `Selection::GetRangesForInterval...
[gecko.git] / xpcom / threads / nsEnvironment.cpp
blob99aae49bd5a0326d9096d2527f48d9a30204e740
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsEnvironment.h"
8 #include "prenv.h"
9 #include "nsBaseHashtable.h"
10 #include "nsHashKeys.h"
11 #include "nsPromiseFlatString.h"
12 #include "nsDependentString.h"
13 #include "nsNativeCharsetUtils.h"
14 #include "mozilla/Printf.h"
16 using namespace mozilla;
18 NS_IMPL_ISUPPORTS(nsEnvironment, nsIEnvironment)
20 nsresult nsEnvironment::Create(nsISupports* aOuter, REFNSIID aIID,
21 void** aResult) {
22 nsresult rv;
23 *aResult = nullptr;
25 if (aOuter) {
26 return NS_ERROR_NO_AGGREGATION;
29 nsEnvironment* obj = new nsEnvironment();
31 rv = obj->QueryInterface(aIID, aResult);
32 if (NS_FAILED(rv)) {
33 delete obj;
35 return rv;
38 nsEnvironment::~nsEnvironment() = default;
40 NS_IMETHODIMP
41 nsEnvironment::Exists(const nsAString& aName, bool* aOutValue) {
42 nsAutoCString nativeName;
43 nsresult rv = NS_CopyUnicodeToNative(aName, nativeName);
44 if (NS_WARN_IF(NS_FAILED(rv))) {
45 return rv;
48 nsAutoCString nativeVal;
49 #if defined(XP_UNIX)
50 /* For Unix/Linux platforms we follow the Unix definition:
51 * An environment variable exists when |getenv()| returns a non-nullptr
52 * value. An environment variable does not exist when |getenv()| returns
53 * nullptr.
55 const char* value = PR_GetEnv(nativeName.get());
56 *aOutValue = value && *value;
57 #else
58 /* For non-Unix/Linux platforms we have to fall back to a
59 * "portable" definition (which is incorrect for Unix/Linux!!!!)
60 * which simply checks whether the string returned by |Get()| is empty
61 * or not.
63 nsAutoString value;
64 Get(aName, value);
65 *aOutValue = !value.IsEmpty();
66 #endif /* XP_UNIX */
68 return NS_OK;
71 NS_IMETHODIMP
72 nsEnvironment::Get(const nsAString& aName, nsAString& aOutValue) {
73 nsAutoCString nativeName;
74 nsresult rv = NS_CopyUnicodeToNative(aName, nativeName);
75 if (NS_WARN_IF(NS_FAILED(rv))) {
76 return rv;
79 nsAutoCString nativeVal;
80 const char* value = PR_GetEnv(nativeName.get());
81 if (value && *value) {
82 rv = NS_CopyNativeToUnicode(nsDependentCString(value), aOutValue);
83 } else {
84 aOutValue.Truncate();
85 rv = NS_OK;
88 return rv;
91 /* Environment strings must have static duration; We're gonna leak all of this
92 * at shutdown: this is by design, caused how Unix/Linux implement environment
93 * vars.
96 typedef nsBaseHashtableET<nsCharPtrHashKey, char*> EnvEntryType;
97 typedef nsTHashtable<EnvEntryType> EnvHashType;
99 static EnvHashType* gEnvHash = nullptr;
101 static bool EnsureEnvHash() {
102 if (gEnvHash) {
103 return true;
106 gEnvHash = new EnvHashType;
107 if (!gEnvHash) {
108 return false;
111 return true;
114 NS_IMETHODIMP
115 nsEnvironment::Set(const nsAString& aName, const nsAString& aValue) {
116 nsAutoCString nativeName;
117 nsAutoCString nativeVal;
119 nsresult rv = NS_CopyUnicodeToNative(aName, nativeName);
120 if (NS_WARN_IF(NS_FAILED(rv))) {
121 return rv;
124 rv = NS_CopyUnicodeToNative(aValue, nativeVal);
125 if (NS_WARN_IF(NS_FAILED(rv))) {
126 return rv;
129 MutexAutoLock lock(mLock);
131 if (!EnsureEnvHash()) {
132 return NS_ERROR_UNEXPECTED;
135 EnvEntryType* entry = gEnvHash->PutEntry(nativeName.get());
136 if (!entry) {
137 return NS_ERROR_OUT_OF_MEMORY;
140 SmprintfPointer newData =
141 mozilla::Smprintf("%s=%s", nativeName.get(), nativeVal.get());
142 if (!newData) {
143 return NS_ERROR_OUT_OF_MEMORY;
146 PR_SetEnv(newData.get());
147 if (entry->GetData()) {
148 free(entry->GetData());
150 entry->SetData(newData.release());
151 return NS_OK;