2004-01-23 13:23:12 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla embedding code.
|
|
|
|
*
|
|
|
|
* The Initial Developers of the Original Code are
|
|
|
|
* Benjamin Smedberg <bsmedberg@covad.net> and
|
|
|
|
* Roland Mainz <roland.mainz@informatik.med.uni-giessen.de>.
|
|
|
|
*
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2003/2004
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "nsEnvironment.h"
|
|
|
|
#include "prenv.h"
|
|
|
|
#include "prprf.h"
|
|
|
|
#include "nsBaseHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
|
|
|
#include "nsPromiseFlatString.h"
|
|
|
|
#include "nsDependentString.h"
|
|
|
|
#include "nsNativeCharsetUtils.h"
|
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2004-01-23 13:23:12 +03:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsEnvironment, nsIEnvironment)
|
|
|
|
|
2010-06-10 22:11:11 +04:00
|
|
|
nsresult
|
2004-01-23 13:23:12 +03:00
|
|
|
nsEnvironment::Create(nsISupports *aOuter, REFNSIID aIID,
|
|
|
|
void **aResult)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
*aResult = nsnull;
|
|
|
|
|
|
|
|
if (aOuter != nsnull) {
|
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEnvironment* obj = new nsEnvironment();
|
|
|
|
if (!obj) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = obj->QueryInterface(aIID, aResult);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
delete obj;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEnvironment::~nsEnvironment()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsEnvironment::Exists(const nsAString& aName, bool *aOutValue)
|
2004-01-23 13:23:12 +03:00
|
|
|
{
|
|
|
|
nsCAutoString nativeName;
|
|
|
|
nsresult rv = NS_CopyUnicodeToNative(aName, nativeName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCAutoString nativeVal;
|
|
|
|
#if defined(XP_UNIX)
|
|
|
|
/* For Unix/Linux platforms we follow the Unix definition:
|
|
|
|
* An environment variable exists when |getenv()| returns a non-NULL value.
|
|
|
|
* An environment variable does not exist when |getenv()| returns NULL.
|
|
|
|
*/
|
|
|
|
const char *value = PR_GetEnv(nativeName.get());
|
2006-03-19 05:52:30 +03:00
|
|
|
*aOutValue = value && *value;
|
2004-01-23 13:23:12 +03:00
|
|
|
#else
|
|
|
|
/* For non-Unix/Linux platforms we have to fall back to a
|
|
|
|
* "portable" definition (which is incorrect for Unix/Linux!!!!)
|
|
|
|
* which simply checks whether the string returned by |Get()| is empty
|
|
|
|
* or not.
|
|
|
|
*/
|
|
|
|
nsAutoString value;
|
|
|
|
Get(aName, value);
|
2006-03-19 05:52:30 +03:00
|
|
|
*aOutValue = !value.IsEmpty();
|
2004-01-23 13:23:12 +03:00
|
|
|
#endif /* XP_UNIX */
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEnvironment::Get(const nsAString& aName, nsAString& aOutValue)
|
|
|
|
{
|
|
|
|
nsCAutoString nativeName;
|
|
|
|
nsresult rv = NS_CopyUnicodeToNative(aName, nativeName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCAutoString nativeVal;
|
|
|
|
const char *value = PR_GetEnv(nativeName.get());
|
2006-03-19 05:52:30 +03:00
|
|
|
if (value && *value) {
|
2004-01-23 13:23:12 +03:00
|
|
|
rv = NS_CopyNativeToUnicode(nsDependentCString(value), aOutValue);
|
|
|
|
} else {
|
|
|
|
aOutValue.Truncate();
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Environment strings must have static duration; We're gonna leak all of this
|
|
|
|
* at shutdown: this is by design, caused how Unix/Linux implement environment
|
|
|
|
* vars.
|
|
|
|
*/
|
|
|
|
|
2010-02-10 20:57:46 +03:00
|
|
|
typedef nsBaseHashtableET<nsCharPtrHashKey,char*> EnvEntryType;
|
2004-01-23 13:23:12 +03:00
|
|
|
typedef nsTHashtable<EnvEntryType> EnvHashType;
|
|
|
|
|
|
|
|
static EnvHashType *gEnvHash = nsnull;
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2004-01-23 13:23:12 +03:00
|
|
|
EnsureEnvHash()
|
|
|
|
{
|
|
|
|
if (gEnvHash)
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2004-01-23 13:23:12 +03:00
|
|
|
|
|
|
|
gEnvHash = new EnvHashType;
|
|
|
|
if (!gEnvHash)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2004-01-23 13:23:12 +03:00
|
|
|
|
2012-05-18 21:30:49 +04:00
|
|
|
gEnvHash->Init();
|
|
|
|
return true;
|
2004-01-23 13:23:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEnvironment::Set(const nsAString& aName, const nsAString& aValue)
|
|
|
|
{
|
|
|
|
nsCAutoString nativeName;
|
|
|
|
nsCAutoString nativeVal;
|
|
|
|
|
|
|
|
nsresult rv = NS_CopyUnicodeToNative(aName, nativeName);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = NS_CopyUnicodeToNative(aValue, nativeVal);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2004-01-23 13:23:12 +03:00
|
|
|
|
|
|
|
if (!EnsureEnvHash()){
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2010-02-10 20:57:46 +03:00
|
|
|
EnvEntryType* entry = gEnvHash->PutEntry(nativeName.get());
|
2004-01-23 13:23:12 +03:00
|
|
|
if (!entry) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* newData = PR_smprintf("%s=%s",
|
|
|
|
nativeName.get(),
|
|
|
|
nativeVal.get());
|
|
|
|
if (!newData) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
PR_SetEnv(newData);
|
|
|
|
if (entry->mData) {
|
|
|
|
PR_smprintf_free(entry->mData);
|
|
|
|
}
|
|
|
|
entry->mData = newData;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|