2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#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-03-31 21:29:02 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsEnvironment, nsIEnvironment)
|
|
|
|
|
2010-06-10 11:11:11 -07:00
|
|
|
nsresult
|
2007-03-22 10:30:00 -07: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-28 23:19:26 -07:00
|
|
|
nsEnvironment::Exists(const nsAString& aName, bool *aOutValue)
|
2007-03-22 10:30:00 -07: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());
|
|
|
|
*aOutValue = value && *value;
|
|
|
|
#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);
|
|
|
|
*aOutValue = !value.IsEmpty();
|
|
|
|
#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());
|
|
|
|
if (value && *value) {
|
|
|
|
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 09:57:46 -08:00
|
|
|
typedef nsBaseHashtableET<nsCharPtrHashKey,char*> EnvEntryType;
|
2007-03-22 10:30:00 -07:00
|
|
|
typedef nsTHashtable<EnvEntryType> EnvHashType;
|
|
|
|
|
|
|
|
static EnvHashType *gEnvHash = nsnull;
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2007-03-22 10:30:00 -07:00
|
|
|
EnsureEnvHash()
|
|
|
|
{
|
|
|
|
if (gEnvHash)
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
gEnvHash = new EnvHashType;
|
|
|
|
if (!gEnvHash)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-05-18 10:30:49 -07:00
|
|
|
gEnvHash->Init();
|
|
|
|
return true;
|
2007-03-22 10:30:00 -07: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-03-31 21:29:02 -07:00
|
|
|
MutexAutoLock lock(mLock);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!EnsureEnvHash()){
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2010-02-10 09:57:46 -08:00
|
|
|
EnvEntryType* entry = gEnvHash->PutEntry(nativeName.get());
|
2007-03-22 10:30:00 -07: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|