VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/threads/nsEnvironment.cpp@ 4837

Last change on this file since 4837 was 1, checked in by vboxsync, 54 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
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
4 *
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/
9 *
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.
14 *
15 * The Original Code is mozilla embedding code.
16 *
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>.
20 *
21 * Portions created by the Initial Developer are Copyright (C) 2003/2004
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
25 *
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.
37 *
38 * ***** END LICENSE BLOCK ***** */
39
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"
49
50NS_IMPL_THREADSAFE_ISUPPORTS1(nsEnvironment, nsIEnvironment)
51
52NS_METHOD
53nsEnvironment::Create(nsISupports *aOuter, REFNSIID aIID,
54 void **aResult)
55{
56 nsresult rv;
57 *aResult = nsnull;
58
59 if (aOuter != nsnull) {
60 return NS_ERROR_NO_AGGREGATION;
61 }
62
63 nsEnvironment* obj = new nsEnvironment();
64 if (!obj) {
65 return NS_ERROR_OUT_OF_MEMORY;
66 }
67
68 obj->mLock = PR_NewLock();
69 if (!obj->mLock) {
70 delete obj;
71 return NS_ERROR_OUT_OF_MEMORY;
72 }
73
74 rv = obj->QueryInterface(aIID, aResult);
75 if (NS_FAILED(rv)) {
76 delete obj;
77 }
78 return rv;
79}
80
81nsEnvironment::~nsEnvironment()
82{
83 if (mLock)
84 PR_DestroyLock(mLock);
85}
86
87NS_IMETHODIMP
88nsEnvironment::Exists(const nsAString& aName, PRBool *aOutValue)
89{
90 nsCAutoString nativeName;
91 nsresult rv = NS_CopyUnicodeToNative(aName, nativeName);
92 NS_ENSURE_SUCCESS(rv, rv);
93
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.
99 */
100 const char *value = PR_GetEnv(nativeName.get());
101
102 *aOutValue = (value)?(PR_TRUE):(PR_FALSE);
103#else
104 /* For non-Unix/Linux platforms we have to fall back to a
105 * "portable" definition (which is incorrect for Unix/Linux!!!!)
106 * which simply checks whether the string returned by |Get()| is empty
107 * or not.
108 */
109 nsAutoString value;
110 Get(aName, value);
111 *aOutValue = (value.IsEmpty())?(PR_FALSE):(PR_TRUE);
112#endif /* XP_UNIX */
113
114 return NS_OK;
115}
116
117NS_IMETHODIMP
118nsEnvironment::Get(const nsAString& aName, nsAString& aOutValue)
119{
120 nsCAutoString nativeName;
121 nsresult rv = NS_CopyUnicodeToNative(aName, nativeName);
122 NS_ENSURE_SUCCESS(rv, rv);
123
124 nsCAutoString nativeVal;
125 const char *value = PR_GetEnv(nativeName.get());
126 if (value) {
127 rv = NS_CopyNativeToUnicode(nsDependentCString(value), aOutValue);
128 } else {
129 aOutValue.Truncate();
130 rv = NS_OK;
131 }
132
133 return rv;
134}
135
136/* Environment strings must have static duration; We're gonna leak all of this
137 * at shutdown: this is by design, caused how Unix/Linux implement environment
138 * vars.
139 */
140
141typedef nsBaseHashtableET<nsCStringHashKey,char*> EnvEntryType;
142typedef nsTHashtable<EnvEntryType> EnvHashType;
143
144static EnvHashType *gEnvHash = nsnull;
145
146static PRBool
147EnsureEnvHash()
148{
149 if (gEnvHash)
150 return PR_TRUE;
151
152 gEnvHash = new EnvHashType;
153 if (!gEnvHash)
154 return PR_FALSE;
155
156 if(gEnvHash->Init())
157 return PR_TRUE;
158
159 delete gEnvHash;
160 gEnvHash = nsnull;
161 return PR_FALSE;
162}
163
164NS_IMETHODIMP
165nsEnvironment::Set(const nsAString& aName, const nsAString& aValue)
166{
167 nsCAutoString nativeName;
168 nsCAutoString nativeVal;
169
170 nsresult rv = NS_CopyUnicodeToNative(aName, nativeName);
171 NS_ENSURE_SUCCESS(rv, rv);
172
173 rv = NS_CopyUnicodeToNative(aValue, nativeVal);
174 NS_ENSURE_SUCCESS(rv, rv);
175
176 nsAutoLock lock(mLock); // autolock unlocks automagically
177
178 if (!EnsureEnvHash()){
179 return NS_ERROR_UNEXPECTED;
180 }
181
182 EnvEntryType* entry = gEnvHash->PutEntry(nativeName);
183 if (!entry) {
184 return NS_ERROR_OUT_OF_MEMORY;
185 }
186
187 char* newData = PR_smprintf("%s=%s",
188 nativeName.get(),
189 nativeVal.get());
190 if (!newData) {
191 return NS_ERROR_OUT_OF_MEMORY;
192 }
193
194 PR_SetEnv(newData);
195 if (entry->mData) {
196 PR_smprintf_free(entry->mData);
197 }
198 entry->mData = newData;
199 return NS_OK;
200}
201
202
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use