Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "nsEnvironment.h" |
michael@0 | 7 | #include "prenv.h" |
michael@0 | 8 | #include "prprf.h" |
michael@0 | 9 | #include "nsBaseHashtable.h" |
michael@0 | 10 | #include "nsHashKeys.h" |
michael@0 | 11 | #include "nsPromiseFlatString.h" |
michael@0 | 12 | #include "nsDependentString.h" |
michael@0 | 13 | #include "nsNativeCharsetUtils.h" |
michael@0 | 14 | |
michael@0 | 15 | using namespace mozilla; |
michael@0 | 16 | |
michael@0 | 17 | NS_IMPL_ISUPPORTS(nsEnvironment, nsIEnvironment) |
michael@0 | 18 | |
michael@0 | 19 | nsresult |
michael@0 | 20 | nsEnvironment::Create(nsISupports *aOuter, REFNSIID aIID, |
michael@0 | 21 | void **aResult) |
michael@0 | 22 | { |
michael@0 | 23 | nsresult rv; |
michael@0 | 24 | *aResult = nullptr; |
michael@0 | 25 | |
michael@0 | 26 | if (aOuter != nullptr) { |
michael@0 | 27 | return NS_ERROR_NO_AGGREGATION; |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | nsEnvironment* obj = new nsEnvironment(); |
michael@0 | 31 | if (!obj) { |
michael@0 | 32 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | rv = obj->QueryInterface(aIID, aResult); |
michael@0 | 36 | if (NS_FAILED(rv)) { |
michael@0 | 37 | delete obj; |
michael@0 | 38 | } |
michael@0 | 39 | return rv; |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | nsEnvironment::~nsEnvironment() |
michael@0 | 43 | { |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | NS_IMETHODIMP |
michael@0 | 47 | nsEnvironment::Exists(const nsAString& aName, bool *aOutValue) |
michael@0 | 48 | { |
michael@0 | 49 | nsAutoCString nativeName; |
michael@0 | 50 | nsresult rv = NS_CopyUnicodeToNative(aName, nativeName); |
michael@0 | 51 | if (NS_WARN_IF(NS_FAILED(rv))) |
michael@0 | 52 | return rv; |
michael@0 | 53 | |
michael@0 | 54 | nsAutoCString nativeVal; |
michael@0 | 55 | #if defined(XP_UNIX) |
michael@0 | 56 | /* For Unix/Linux platforms we follow the Unix definition: |
michael@0 | 57 | * An environment variable exists when |getenv()| returns a non-nullptr |
michael@0 | 58 | * value. An environment variable does not exist when |getenv()| returns |
michael@0 | 59 | * nullptr. |
michael@0 | 60 | */ |
michael@0 | 61 | const char *value = PR_GetEnv(nativeName.get()); |
michael@0 | 62 | *aOutValue = value && *value; |
michael@0 | 63 | #else |
michael@0 | 64 | /* For non-Unix/Linux platforms we have to fall back to a |
michael@0 | 65 | * "portable" definition (which is incorrect for Unix/Linux!!!!) |
michael@0 | 66 | * which simply checks whether the string returned by |Get()| is empty |
michael@0 | 67 | * or not. |
michael@0 | 68 | */ |
michael@0 | 69 | nsAutoString value; |
michael@0 | 70 | Get(aName, value); |
michael@0 | 71 | *aOutValue = !value.IsEmpty(); |
michael@0 | 72 | #endif /* XP_UNIX */ |
michael@0 | 73 | |
michael@0 | 74 | return NS_OK; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | NS_IMETHODIMP |
michael@0 | 78 | nsEnvironment::Get(const nsAString& aName, nsAString& aOutValue) |
michael@0 | 79 | { |
michael@0 | 80 | nsAutoCString nativeName; |
michael@0 | 81 | nsresult rv = NS_CopyUnicodeToNative(aName, nativeName); |
michael@0 | 82 | if (NS_WARN_IF(NS_FAILED(rv))) |
michael@0 | 83 | return rv; |
michael@0 | 84 | |
michael@0 | 85 | nsAutoCString nativeVal; |
michael@0 | 86 | const char *value = PR_GetEnv(nativeName.get()); |
michael@0 | 87 | if (value && *value) { |
michael@0 | 88 | rv = NS_CopyNativeToUnicode(nsDependentCString(value), aOutValue); |
michael@0 | 89 | } else { |
michael@0 | 90 | aOutValue.Truncate(); |
michael@0 | 91 | rv = NS_OK; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | return rv; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | /* Environment strings must have static duration; We're gonna leak all of this |
michael@0 | 98 | * at shutdown: this is by design, caused how Unix/Linux implement environment |
michael@0 | 99 | * vars. |
michael@0 | 100 | */ |
michael@0 | 101 | |
michael@0 | 102 | typedef nsBaseHashtableET<nsCharPtrHashKey,char*> EnvEntryType; |
michael@0 | 103 | typedef nsTHashtable<EnvEntryType> EnvHashType; |
michael@0 | 104 | |
michael@0 | 105 | static EnvHashType *gEnvHash = nullptr; |
michael@0 | 106 | |
michael@0 | 107 | static bool |
michael@0 | 108 | EnsureEnvHash() |
michael@0 | 109 | { |
michael@0 | 110 | if (gEnvHash) |
michael@0 | 111 | return true; |
michael@0 | 112 | |
michael@0 | 113 | gEnvHash = new EnvHashType; |
michael@0 | 114 | if (!gEnvHash) |
michael@0 | 115 | return false; |
michael@0 | 116 | |
michael@0 | 117 | return true; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | NS_IMETHODIMP |
michael@0 | 121 | nsEnvironment::Set(const nsAString& aName, const nsAString& aValue) |
michael@0 | 122 | { |
michael@0 | 123 | nsAutoCString nativeName; |
michael@0 | 124 | nsAutoCString nativeVal; |
michael@0 | 125 | |
michael@0 | 126 | nsresult rv = NS_CopyUnicodeToNative(aName, nativeName); |
michael@0 | 127 | if (NS_WARN_IF(NS_FAILED(rv))) |
michael@0 | 128 | return rv; |
michael@0 | 129 | |
michael@0 | 130 | rv = NS_CopyUnicodeToNative(aValue, nativeVal); |
michael@0 | 131 | if (NS_WARN_IF(NS_FAILED(rv))) |
michael@0 | 132 | return rv; |
michael@0 | 133 | |
michael@0 | 134 | MutexAutoLock lock(mLock); |
michael@0 | 135 | |
michael@0 | 136 | if (!EnsureEnvHash()){ |
michael@0 | 137 | return NS_ERROR_UNEXPECTED; |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | EnvEntryType* entry = gEnvHash->PutEntry(nativeName.get()); |
michael@0 | 141 | if (!entry) { |
michael@0 | 142 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | char* newData = PR_smprintf("%s=%s", |
michael@0 | 146 | nativeName.get(), |
michael@0 | 147 | nativeVal.get()); |
michael@0 | 148 | if (!newData) { |
michael@0 | 149 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | PR_SetEnv(newData); |
michael@0 | 153 | if (entry->mData) { |
michael@0 | 154 | PR_smprintf_free(entry->mData); |
michael@0 | 155 | } |
michael@0 | 156 | entry->mData = newData; |
michael@0 | 157 | return NS_OK; |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 |