2014-05-27 11:15:35 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04: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 13:23:12 +03:00
|
|
|
|
|
|
|
#include "nsEnvironment.h"
|
|
|
|
#include "prenv.h"
|
|
|
|
#include "nsBaseHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
|
|
|
#include "nsPromiseFlatString.h"
|
|
|
|
#include "nsDependentString.h"
|
|
|
|
#include "nsNativeCharsetUtils.h"
|
2016-12-09 23:53:59 +03:00
|
|
|
#include "mozilla/Printf.h"
|
2004-01-23 13:23:12 +03: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 08:29:02 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsEnvironment, nsIEnvironment)
|
2004-01-23 13:23:12 +03:00
|
|
|
|
2010-06-10 22:11:11 +04:00
|
|
|
nsresult
|
2014-05-27 11:15:35 +04:00
|
|
|
nsEnvironment::Create(nsISupports* aOuter, REFNSIID aIID, void** aResult)
|
2004-01-23 13:23:12 +03:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
nsresult rv;
|
|
|
|
*aResult = nullptr;
|
|
|
|
|
|
|
|
if (aOuter) {
|
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEnvironment* obj = new nsEnvironment();
|
|
|
|
|
|
|
|
rv = obj->QueryInterface(aIID, aResult);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
delete obj;
|
|
|
|
}
|
|
|
|
return rv;
|
2004-01-23 13:23:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsEnvironment::~nsEnvironment()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-27 11:15:35 +04:00
|
|
|
nsEnvironment::Exists(const nsAString& aName, bool* aOutValue)
|
2004-01-23 13:23:12 +03:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
nsAutoCString nativeName;
|
|
|
|
nsresult rv = NS_CopyUnicodeToNative(aName, nativeName);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2004-01-23 13:23:12 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
nsAutoCString nativeVal;
|
2004-01-23 13:23:12 +03:00
|
|
|
#if defined(XP_UNIX)
|
2014-05-27 11:15:35 +04:00
|
|
|
/* For Unix/Linux platforms we follow the Unix definition:
|
|
|
|
* An environment variable exists when |getenv()| returns a non-nullptr
|
|
|
|
* value. An environment variable does not exist when |getenv()| returns
|
|
|
|
* nullptr.
|
|
|
|
*/
|
|
|
|
const char* value = PR_GetEnv(nativeName.get());
|
|
|
|
*aOutValue = value && *value;
|
2004-01-23 13:23:12 +03:00
|
|
|
#else
|
2014-05-27 11:15:35 +04:00
|
|
|
/* 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();
|
2004-01-23 13:23:12 +03:00
|
|
|
#endif /* XP_UNIX */
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
return NS_OK;
|
2004-01-23 13:23:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEnvironment::Get(const nsAString& aName, nsAString& aOutValue)
|
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
nsAutoCString nativeName;
|
|
|
|
nsresult rv = NS_CopyUnicodeToNative(aName, nativeName);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2004-01-23 13:23:12 +03:00
|
|
|
return rv;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString 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;
|
2004-01-23 13:23:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
2014-05-27 11:15:35 +04:00
|
|
|
* vars.
|
2004-01-23 13:23:12 +03:00
|
|
|
*/
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
typedef nsBaseHashtableET<nsCharPtrHashKey, char*> EnvEntryType;
|
2004-01-23 13:23:12 +03:00
|
|
|
typedef nsTHashtable<EnvEntryType> EnvHashType;
|
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
static EnvHashType* gEnvHash = nullptr;
|
2004-01-23 13:23:12 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2004-01-23 13:23:12 +03:00
|
|
|
EnsureEnvHash()
|
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
if (gEnvHash) {
|
|
|
|
return true;
|
|
|
|
}
|
2004-01-23 13:23:12 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
gEnvHash = new EnvHashType;
|
|
|
|
if (!gEnvHash) {
|
|
|
|
return false;
|
|
|
|
}
|
2004-01-23 13:23:12 +03:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
return true;
|
2004-01-23 13:23:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsEnvironment::Set(const nsAString& aName, const nsAString& aValue)
|
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
nsAutoCString nativeName;
|
|
|
|
nsAutoCString nativeVal;
|
|
|
|
|
|
|
|
nsresult rv = NS_CopyUnicodeToNative(aName, nativeName);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = NS_CopyUnicodeToNative(aValue, nativeVal);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
if (!EnsureEnvHash()) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
EnvEntryType* entry = gEnvHash->PutEntry(nativeName.get());
|
|
|
|
if (!entry) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2017-03-03 18:17:27 +03:00
|
|
|
SmprintfPointer newData = mozilla::Smprintf("%s=%s",
|
|
|
|
nativeName.get(),
|
|
|
|
nativeVal.get());
|
2014-05-27 11:15:35 +04:00
|
|
|
if (!newData) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2017-03-03 18:17:27 +03:00
|
|
|
PR_SetEnv(newData.get());
|
2014-05-27 11:15:35 +04:00
|
|
|
if (entry->mData) {
|
2017-07-25 02:09:25 +03:00
|
|
|
free(entry->mData);
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2017-03-03 18:17:27 +03:00
|
|
|
entry->mData = newData.release();
|
2014-05-27 11:15:35 +04:00
|
|
|
return NS_OK;
|
2004-01-23 13:23:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|