2004-01-23 10:23:12 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 11:12:37 +00: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/. */
|
2004-01-23 10:23:12 +00: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-04-01 04:29:02 +00:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2013-07-19 02:31:26 +00:00
|
|
|
NS_IMPL_ISUPPORTS1(nsEnvironment, nsIEnvironment)
|
2004-01-23 10:23:12 +00:00
|
|
|
|
2010-06-10 18:11:11 +00:00
|
|
|
nsresult
|
2004-01-23 10:23:12 +00:00
|
|
|
nsEnvironment::Create(nsISupports *aOuter, REFNSIID aIID,
|
|
|
|
void **aResult)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-07-30 14:20:58 +00:00
|
|
|
*aResult = nullptr;
|
2004-01-23 10:23:12 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
if (aOuter != nullptr) {
|
2004-01-23 10:23:12 +00:00
|
|
|
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 06:19:26 +00:00
|
|
|
nsEnvironment::Exists(const nsAString& aName, bool *aOutValue)
|
2004-01-23 10:23:12 +00:00
|
|
|
{
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString nativeName;
|
2004-01-23 10:23:12 +00:00
|
|
|
nsresult rv = NS_CopyUnicodeToNative(aName, nativeName);
|
2013-11-19 21:27:37 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv)))
|
|
|
|
return rv;
|
2004-01-23 10:23:12 +00:00
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString nativeVal;
|
2004-01-23 10:23:12 +00:00
|
|
|
#if defined(XP_UNIX)
|
|
|
|
/* For Unix/Linux platforms we follow the Unix definition:
|
2013-10-10 20:42:16 +00:00
|
|
|
* An environment variable exists when |getenv()| returns a non-nullptr
|
|
|
|
* value. An environment variable does not exist when |getenv()| returns
|
|
|
|
* nullptr.
|
2004-01-23 10:23:12 +00:00
|
|
|
*/
|
|
|
|
const char *value = PR_GetEnv(nativeName.get());
|
2006-03-19 02:52:30 +00:00
|
|
|
*aOutValue = value && *value;
|
2004-01-23 10:23:12 +00: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 02:52:30 +00:00
|
|
|
*aOutValue = !value.IsEmpty();
|
2004-01-23 10:23:12 +00:00
|
|
|
#endif /* XP_UNIX */
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEnvironment::Get(const nsAString& aName, nsAString& aOutValue)
|
|
|
|
{
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString nativeName;
|
2004-01-23 10:23:12 +00:00
|
|
|
nsresult rv = NS_CopyUnicodeToNative(aName, nativeName);
|
2013-11-19 21:27:37 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv)))
|
|
|
|
return rv;
|
2004-01-23 10:23:12 +00:00
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString nativeVal;
|
2004-01-23 10:23:12 +00:00
|
|
|
const char *value = PR_GetEnv(nativeName.get());
|
2006-03-19 02:52:30 +00:00
|
|
|
if (value && *value) {
|
2004-01-23 10:23:12 +00: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 17:57:46 +00:00
|
|
|
typedef nsBaseHashtableET<nsCharPtrHashKey,char*> EnvEntryType;
|
2004-01-23 10:23:12 +00:00
|
|
|
typedef nsTHashtable<EnvEntryType> EnvHashType;
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
static EnvHashType *gEnvHash = nullptr;
|
2004-01-23 10:23:12 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
static bool
|
2004-01-23 10:23:12 +00:00
|
|
|
EnsureEnvHash()
|
|
|
|
{
|
|
|
|
if (gEnvHash)
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2004-01-23 10:23:12 +00:00
|
|
|
|
|
|
|
gEnvHash = new EnvHashType;
|
|
|
|
if (!gEnvHash)
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2004-01-23 10:23:12 +00:00
|
|
|
|
2012-05-18 17:30:49 +00:00
|
|
|
return true;
|
2004-01-23 10:23:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEnvironment::Set(const nsAString& aName, const nsAString& aValue)
|
|
|
|
{
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString nativeName;
|
|
|
|
nsAutoCString nativeVal;
|
2004-01-23 10:23:12 +00:00
|
|
|
|
|
|
|
nsresult rv = NS_CopyUnicodeToNative(aName, nativeName);
|
2013-11-19 21:27:37 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv)))
|
|
|
|
return rv;
|
2004-01-23 10:23:12 +00:00
|
|
|
|
|
|
|
rv = NS_CopyUnicodeToNative(aValue, nativeVal);
|
2013-11-19 21:27:37 +00:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv)))
|
|
|
|
return rv;
|
2004-01-23 10:23:12 +00:00
|
|
|
|
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 04:29:02 +00:00
|
|
|
MutexAutoLock lock(mLock);
|
2004-01-23 10:23:12 +00:00
|
|
|
|
|
|
|
if (!EnsureEnvHash()){
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2010-02-10 17:57:46 +00:00
|
|
|
EnvEntryType* entry = gEnvHash->PutEntry(nativeName.get());
|
2004-01-23 10:23:12 +00: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|