LCOV - code coverage report
Current view: directory - xpcom/threads - nsEnvironment.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 65 55 84.6 %
Date: 2012-06-02 Functions: 9 9 100.0 %

       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 "nsBaseHashtable.h"
      44                 : #include "nsHashKeys.h"
      45                 : #include "nsPromiseFlatString.h"
      46                 : #include "nsDependentString.h"
      47                 : #include "nsNativeCharsetUtils.h"
      48                 : 
      49                 : using namespace mozilla;
      50                 : 
      51           16987 : NS_IMPL_THREADSAFE_ISUPPORTS1(nsEnvironment, nsIEnvironment)
      52                 : 
      53                 : nsresult
      54             784 : nsEnvironment::Create(nsISupports *aOuter, REFNSIID aIID,
      55                 :                       void **aResult)
      56                 : {
      57                 :     nsresult rv;
      58             784 :     *aResult = nsnull;
      59                 : 
      60             784 :     if (aOuter != nsnull) {
      61               0 :         return NS_ERROR_NO_AGGREGATION;
      62                 :     }
      63                 : 
      64             784 :     nsEnvironment* obj = new nsEnvironment();
      65             784 :     if (!obj) {
      66               0 :         return NS_ERROR_OUT_OF_MEMORY;
      67                 :     }
      68                 : 
      69             784 :     rv = obj->QueryInterface(aIID, aResult);
      70             784 :     if (NS_FAILED(rv)) {
      71               0 :       delete obj;
      72                 :     }
      73             784 :     return rv;
      74                 : }
      75                 : 
      76             784 : nsEnvironment::~nsEnvironment()
      77                 : {
      78             784 : }
      79                 : 
      80                 : NS_IMETHODIMP
      81               3 : nsEnvironment::Exists(const nsAString& aName, bool *aOutValue)
      82                 : {
      83               6 :     nsCAutoString nativeName;
      84               3 :     nsresult rv = NS_CopyUnicodeToNative(aName, nativeName);
      85               3 :     NS_ENSURE_SUCCESS(rv, rv);
      86                 : 
      87               6 :     nsCAutoString nativeVal;
      88                 : #if defined(XP_UNIX)
      89                 :     /* For Unix/Linux platforms we follow the Unix definition:
      90                 :      * An environment variable exists when |getenv()| returns a non-NULL value.
      91                 :      * An environment variable does not exist when |getenv()| returns NULL.
      92                 :      */
      93               3 :     const char *value = PR_GetEnv(nativeName.get());
      94               3 :     *aOutValue = value && *value;
      95                 : #else
      96                 :     /* For non-Unix/Linux platforms we have to fall back to a 
      97                 :      * "portable" definition (which is incorrect for Unix/Linux!!!!)
      98                 :      * which simply checks whether the string returned by |Get()| is empty
      99                 :      * or not.
     100                 :      */
     101                 :     nsAutoString value;
     102                 :     Get(aName, value);
     103                 :     *aOutValue = !value.IsEmpty();
     104                 : #endif /* XP_UNIX */
     105                 : 
     106               3 :     return NS_OK;
     107                 : }
     108                 : 
     109                 : NS_IMETHODIMP
     110             865 : nsEnvironment::Get(const nsAString& aName, nsAString& aOutValue)
     111                 : {
     112            1730 :     nsCAutoString nativeName;
     113             865 :     nsresult rv = NS_CopyUnicodeToNative(aName, nativeName);
     114             865 :     NS_ENSURE_SUCCESS(rv, rv);
     115                 : 
     116            1730 :     nsCAutoString nativeVal;
     117             865 :     const char *value = PR_GetEnv(nativeName.get());
     118             865 :     if (value && *value) {
     119             864 :         rv = NS_CopyNativeToUnicode(nsDependentCString(value), aOutValue);
     120                 :     } else {
     121               1 :         aOutValue.Truncate();
     122               1 :         rv = NS_OK;
     123                 :     }
     124                 : 
     125             865 :     return rv;
     126                 : }
     127                 : 
     128                 : /* Environment strings must have static duration; We're gonna leak all of this
     129                 :  * at shutdown: this is by design, caused how Unix/Linux implement environment
     130                 :  * vars. 
     131                 :  */
     132                 : 
     133                 : typedef nsBaseHashtableET<nsCharPtrHashKey,char*> EnvEntryType;
     134                 : typedef nsTHashtable<EnvEntryType> EnvHashType;
     135                 : 
     136                 : static EnvHashType *gEnvHash = nsnull;
     137                 : 
     138                 : static bool
     139              14 : EnsureEnvHash()
     140                 : {
     141              14 :     if (gEnvHash)
     142              10 :         return true;
     143                 : 
     144               4 :     gEnvHash = new EnvHashType;
     145               4 :     if (!gEnvHash)
     146               0 :         return false;
     147                 : 
     148               4 :     if(gEnvHash->Init())
     149               4 :         return true;
     150                 : 
     151               0 :     delete gEnvHash;
     152               0 :     gEnvHash = nsnull;
     153               0 :     return false;
     154                 : }
     155                 : 
     156                 : NS_IMETHODIMP
     157              14 : nsEnvironment::Set(const nsAString& aName, const nsAString& aValue)
     158                 : {
     159              28 :     nsCAutoString nativeName;
     160              28 :     nsCAutoString nativeVal;
     161                 : 
     162              14 :     nsresult rv = NS_CopyUnicodeToNative(aName, nativeName);
     163              14 :     NS_ENSURE_SUCCESS(rv, rv);
     164                 : 
     165              14 :     rv = NS_CopyUnicodeToNative(aValue, nativeVal);
     166              14 :     NS_ENSURE_SUCCESS(rv, rv);
     167                 : 
     168              28 :     MutexAutoLock lock(mLock);
     169                 : 
     170              14 :     if (!EnsureEnvHash()){
     171               0 :         return NS_ERROR_UNEXPECTED;
     172                 :     }
     173                 : 
     174              14 :     EnvEntryType* entry = gEnvHash->PutEntry(nativeName.get());
     175              14 :     if (!entry) {
     176               0 :         return NS_ERROR_OUT_OF_MEMORY;
     177                 :     }
     178                 :     
     179                 :     char* newData = PR_smprintf("%s=%s",
     180                 :                                 nativeName.get(),
     181              14 :                                 nativeVal.get());
     182              14 :     if (!newData) {
     183               0 :         return NS_ERROR_OUT_OF_MEMORY;
     184                 :     } 
     185                 :     
     186              14 :     PR_SetEnv(newData);
     187              14 :     if (entry->mData) {
     188               3 :         PR_smprintf_free(entry->mData);
     189                 :     }
     190              14 :     entry->mData = newData;
     191              14 :     return NS_OK;
     192                 : }
     193                 : 
     194                 : 

Generated by: LCOV version 1.7