Bug 575870 - Enable the firefox button on xp themed, classic, and aero basic. r=dao...
[mozilla-central.git] / xpcom / threads / nsEnvironment.cpp
blobee13040d851073c3a6b3ad75cdb2995298a2602f
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla embedding code.
17 * The Initial Developers of the Original Code are
18 * Benjamin Smedberg <bsmedberg@covad.net> and
19 * Roland Mainz <roland.mainz@informatik.med.uni-giessen.de>.
21 * Portions created by the Initial Developer are Copyright (C) 2003/2004
22 * the Initial Developer. All Rights Reserved.
24 * Contributor(s):
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #include "nsEnvironment.h"
41 #include "prenv.h"
42 #include "prprf.h"
43 #include "nsAutoLock.h"
44 #include "nsBaseHashtable.h"
45 #include "nsHashKeys.h"
46 #include "nsPromiseFlatString.h"
47 #include "nsDependentString.h"
48 #include "nsNativeCharsetUtils.h"
50 NS_IMPL_THREADSAFE_ISUPPORTS1(nsEnvironment, nsIEnvironment)
52 nsresult
53 nsEnvironment::Create(nsISupports *aOuter, REFNSIID aIID,
54 void **aResult)
56 nsresult rv;
57 *aResult = nsnull;
59 if (aOuter != nsnull) {
60 return NS_ERROR_NO_AGGREGATION;
63 nsEnvironment* obj = new nsEnvironment();
64 if (!obj) {
65 return NS_ERROR_OUT_OF_MEMORY;
68 obj->mLock = PR_NewLock();
69 if (!obj->mLock) {
70 delete obj;
71 return NS_ERROR_OUT_OF_MEMORY;
74 rv = obj->QueryInterface(aIID, aResult);
75 if (NS_FAILED(rv)) {
76 delete obj;
78 return rv;
81 nsEnvironment::~nsEnvironment()
83 if (mLock)
84 PR_DestroyLock(mLock);
87 NS_IMETHODIMP
88 nsEnvironment::Exists(const nsAString& aName, PRBool *aOutValue)
90 nsCAutoString nativeName;
91 nsresult rv = NS_CopyUnicodeToNative(aName, nativeName);
92 NS_ENSURE_SUCCESS(rv, rv);
94 nsCAutoString nativeVal;
95 #if defined(XP_UNIX)
96 /* For Unix/Linux platforms we follow the Unix definition:
97 * An environment variable exists when |getenv()| returns a non-NULL value.
98 * An environment variable does not exist when |getenv()| returns NULL.
100 const char *value = PR_GetEnv(nativeName.get());
101 *aOutValue = value && *value;
102 #else
103 /* For non-Unix/Linux platforms we have to fall back to a
104 * "portable" definition (which is incorrect for Unix/Linux!!!!)
105 * which simply checks whether the string returned by |Get()| is empty
106 * or not.
108 nsAutoString value;
109 Get(aName, value);
110 *aOutValue = !value.IsEmpty();
111 #endif /* XP_UNIX */
113 return NS_OK;
116 NS_IMETHODIMP
117 nsEnvironment::Get(const nsAString& aName, nsAString& aOutValue)
119 nsCAutoString nativeName;
120 nsresult rv = NS_CopyUnicodeToNative(aName, nativeName);
121 NS_ENSURE_SUCCESS(rv, rv);
123 nsCAutoString nativeVal;
124 const char *value = PR_GetEnv(nativeName.get());
125 if (value && *value) {
126 rv = NS_CopyNativeToUnicode(nsDependentCString(value), aOutValue);
127 } else {
128 aOutValue.Truncate();
129 rv = NS_OK;
132 return rv;
135 /* Environment strings must have static duration; We're gonna leak all of this
136 * at shutdown: this is by design, caused how Unix/Linux implement environment
137 * vars.
140 typedef nsBaseHashtableET<nsCharPtrHashKey,char*> EnvEntryType;
141 typedef nsTHashtable<EnvEntryType> EnvHashType;
143 static EnvHashType *gEnvHash = nsnull;
145 static PRBool
146 EnsureEnvHash()
148 if (gEnvHash)
149 return PR_TRUE;
151 gEnvHash = new EnvHashType;
152 if (!gEnvHash)
153 return PR_FALSE;
155 if(gEnvHash->Init())
156 return PR_TRUE;
158 delete gEnvHash;
159 gEnvHash = nsnull;
160 return PR_FALSE;
163 NS_IMETHODIMP
164 nsEnvironment::Set(const nsAString& aName, const nsAString& aValue)
166 nsCAutoString nativeName;
167 nsCAutoString nativeVal;
169 nsresult rv = NS_CopyUnicodeToNative(aName, nativeName);
170 NS_ENSURE_SUCCESS(rv, rv);
172 rv = NS_CopyUnicodeToNative(aValue, nativeVal);
173 NS_ENSURE_SUCCESS(rv, rv);
175 nsAutoLock lock(mLock); // autolock unlocks automagically
177 if (!EnsureEnvHash()){
178 return NS_ERROR_UNEXPECTED;
181 EnvEntryType* entry = gEnvHash->PutEntry(nativeName.get());
182 if (!entry) {
183 return NS_ERROR_OUT_OF_MEMORY;
186 char* newData = PR_smprintf("%s=%s",
187 nativeName.get(),
188 nativeVal.get());
189 if (!newData) {
190 return NS_ERROR_OUT_OF_MEMORY;
193 PR_SetEnv(newData);
194 if (entry->mData) {
195 PR_smprintf_free(entry->mData);
197 entry->mData = newData;
198 return NS_OK;