LCOV - code coverage report
Current view: directory - security/manager/ssl/src - nsNSSShutDown.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 191 136 71.2 %
Date: 2012-04-21 Functions: 36 27 75.0 %

       1                 : /* ***** BEGIN LICENSE BLOCK *****
       2                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       3                 :  *
       4                 :  * The contents of this file are subject to the Mozilla Public License Version
       5                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       6                 :  * the License. You may obtain a copy of the License at
       7                 :  * http://www.mozilla.org/MPL/
       8                 :  *
       9                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      10                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      11                 :  * for the specific language governing rights and limitations under the
      12                 :  * License.
      13                 :  *
      14                 :  * The Original Code is Mozilla Communicator.
      15                 :  *
      16                 :  * The Initial Developer of the Original Code is
      17                 :  * Netscape Communications Corporation.
      18                 :  * Portions created by the Initial Developer are Copyright (C) 2002
      19                 :  * the Initial Developer. All Rights Reserved.
      20                 :  *
      21                 :  * Contributor(s):
      22                 :  *   Kai Engert <kaie@netscape.com>
      23                 :  *
      24                 :  * Alternatively, the contents of this file may be used under the terms of
      25                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      26                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      27                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      28                 :  * of those above. If you wish to allow use of your version of this file only
      29                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      30                 :  * use your version of this file under the terms of the MPL, indicate your
      31                 :  * decision by deleting the provisions above and replace them with the notice
      32                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      33                 :  * the provisions above, a recipient may use your version of this file under
      34                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      35                 :  *
      36                 :  * ***** END LICENSE BLOCK ***** */
      37                 : 
      38                 : #include "nsNSSShutDown.h"
      39                 : #include "nsCOMPtr.h"
      40                 : 
      41                 : using namespace mozilla;
      42                 : 
      43                 : #ifdef PR_LOGGING
      44                 : extern PRLogModuleInfo* gPIPNSSLog;
      45                 : #endif
      46                 : 
      47                 : struct ObjectHashEntry : PLDHashEntryHdr {
      48                 :   nsNSSShutDownObject *obj;
      49                 : };
      50                 : 
      51                 : PR_STATIC_CALLBACK(bool)
      52            2109 : ObjectSetMatchEntry(PLDHashTable *table, const PLDHashEntryHdr *hdr,
      53                 :                          const void *key)
      54                 : {
      55            2109 :   const ObjectHashEntry *entry = static_cast<const ObjectHashEntry*>(hdr);
      56            2109 :   return entry->obj == static_cast<const nsNSSShutDownObject*>(key);
      57                 : }
      58                 : 
      59                 : PR_STATIC_CALLBACK(bool)
      60            3913 : ObjectSetInitEntry(PLDHashTable *table, PLDHashEntryHdr *hdr,
      61                 :                      const void *key)
      62                 : {
      63            3913 :   ObjectHashEntry *entry = static_cast<ObjectHashEntry*>(hdr);
      64            3913 :   entry->obj = const_cast<nsNSSShutDownObject*>(static_cast<const nsNSSShutDownObject*>(key));
      65            3913 :   return true;
      66                 : }
      67                 : 
      68                 : static PLDHashTableOps gSetOps = {
      69                 :   PL_DHashAllocTable,
      70                 :   PL_DHashFreeTable,
      71                 :   PL_DHashVoidPtrKeyStub,
      72                 :   ObjectSetMatchEntry,
      73                 :   PL_DHashMoveEntryStub,
      74                 :   PL_DHashClearEntryStub,
      75                 :   PL_DHashFinalizeStub,
      76                 :   ObjectSetInitEntry
      77                 : };
      78                 : 
      79                 : nsNSSShutDownList *nsNSSShutDownList::singleton = nsnull;
      80                 : 
      81             327 : nsNSSShutDownList::nsNSSShutDownList()
      82             327 : :mListLock("nsNSSShutDownList.mListLock")
      83                 : {
      84             327 :   mActiveSSLSockets = 0;
      85             327 :   mPK11LogoutCancelObjects.ops = nsnull;
      86             327 :   mObjects.ops = nsnull;
      87                 :   PL_DHashTableInit(&mObjects, &gSetOps, nsnull,
      88             327 :                     sizeof(ObjectHashEntry), 16);
      89                 :   PL_DHashTableInit(&mPK11LogoutCancelObjects, &gSetOps, nsnull,
      90             327 :                     sizeof(ObjectHashEntry), 16);
      91             327 : }
      92                 : 
      93             654 : nsNSSShutDownList::~nsNSSShutDownList()
      94                 : {
      95             327 :   if (mObjects.ops) {
      96             327 :     PL_DHashTableFinish(&mObjects);
      97             327 :     mObjects.ops = nsnull;
      98                 :   }
      99             327 :   if (mPK11LogoutCancelObjects.ops) {
     100             327 :     PL_DHashTableFinish(&mPK11LogoutCancelObjects);
     101             327 :     mPK11LogoutCancelObjects.ops = nsnull;
     102                 :   }
     103             327 :   PR_ASSERT(this == singleton);
     104             327 :   singleton = nsnull;
     105             327 : }
     106                 : 
     107            3909 : void nsNSSShutDownList::remember(nsNSSShutDownObject *o)
     108                 : {
     109            3909 :   if (!singleton)
     110               0 :     return;
     111                 :   
     112            3909 :   PR_ASSERT(o);
     113            7818 :   MutexAutoLock lock(singleton->mListLock);
     114            3909 :     PL_DHashTableOperate(&singleton->mObjects, o, PL_DHASH_ADD);
     115                 : }
     116                 : 
     117            2109 : void nsNSSShutDownList::forget(nsNSSShutDownObject *o)
     118                 : {
     119            2109 :   if (!singleton)
     120               0 :     return;
     121                 :   
     122            2109 :   PR_ASSERT(o);
     123            4218 :   MutexAutoLock lock(singleton->mListLock);
     124            2109 :   PL_DHashTableOperate(&singleton->mObjects, o, PL_DHASH_REMOVE);
     125                 : }
     126                 : 
     127               4 : void nsNSSShutDownList::remember(nsOnPK11LogoutCancelObject *o)
     128                 : {
     129               4 :   if (!singleton)
     130               0 :     return;
     131                 :   
     132               4 :   PR_ASSERT(o);
     133               8 :   MutexAutoLock lock(singleton->mListLock);
     134               4 :   PL_DHashTableOperate(&singleton->mPK11LogoutCancelObjects, o, PL_DHASH_ADD);
     135                 : }
     136                 : 
     137               4 : void nsNSSShutDownList::forget(nsOnPK11LogoutCancelObject *o)
     138                 : {
     139               4 :   if (!singleton)
     140               4 :     return;
     141                 :   
     142               0 :   PR_ASSERT(o);
     143               0 :   MutexAutoLock lock(singleton->mListLock);
     144               0 :   PL_DHashTableOperate(&singleton->mPK11LogoutCancelObjects, o, PL_DHASH_REMOVE);
     145                 : }
     146                 : 
     147               4 : void nsNSSShutDownList::trackSSLSocketCreate()
     148                 : {
     149               4 :   if (!singleton)
     150               0 :     return;
     151                 :   
     152               8 :   MutexAutoLock lock(singleton->mListLock);
     153               4 :   ++singleton->mActiveSSLSockets;
     154                 : }
     155                 : 
     156               4 : void nsNSSShutDownList::trackSSLSocketClose()
     157                 : {
     158               4 :   if (!singleton)
     159               0 :     return;
     160                 :   
     161               8 :   MutexAutoLock lock(singleton->mListLock);
     162               4 :   --singleton->mActiveSSLSockets;
     163                 : }
     164                 :   
     165             314 : bool nsNSSShutDownList::areSSLSocketsActive()
     166                 : {
     167             314 :   if (!singleton) {
     168                 :     // I'd rather prefer to be pessimistic and return true.
     169                 :     // However, maybe we will get called at a time when the singleton
     170                 :     // has already been freed, and returning true would bring up an 
     171                 :     // unnecessary warning.
     172               0 :     return false;
     173                 :   }
     174                 :   
     175             628 :   MutexAutoLock lock(singleton->mListLock);
     176             314 :   return (singleton->mActiveSSLSockets > 0);
     177                 : }
     178                 : 
     179             142 : nsresult nsNSSShutDownList::doPK11Logout()
     180                 : {
     181             142 :     PR_LOG(gPIPNSSLog, PR_LOG_DEBUG, ("canceling all open SSL sockets to disallow future IO\n"));
     182                 :   // During our iteration we will set a bunch of PRBools to true.
     183                 :   // Nobody else ever modifies that bool, only we do.
     184                 :   // We only must ensure that our objects do not go away.
     185                 :   // This is guaranteed by holding the list lock.
     186                 : 
     187             284 :   MutexAutoLock lock(singleton->mListLock);
     188             142 :   PL_DHashTableEnumerate(&mPK11LogoutCancelObjects, doPK11LogoutHelper, 0);
     189                 : 
     190             142 :   return NS_OK;
     191                 : }
     192                 : 
     193                 : PLDHashOperator PR_CALLBACK
     194               0 : nsNSSShutDownList::doPK11LogoutHelper(PLDHashTable *table, 
     195                 :   PLDHashEntryHdr *hdr, PRUint32 number, void *arg)
     196                 : {
     197               0 :   ObjectHashEntry *entry = static_cast<ObjectHashEntry*>(hdr);
     198                 : 
     199                 :   nsOnPK11LogoutCancelObject *pklco = 
     200               0 :     reinterpret_cast<nsOnPK11LogoutCancelObject*>(entry->obj);
     201                 : 
     202               0 :   if (pklco) {
     203               0 :     pklco->logout();
     204                 :   }
     205                 : 
     206               0 :   return PL_DHASH_NEXT;
     207                 : }
     208                 : 
     209               0 : bool nsNSSShutDownList::isUIActive()
     210                 : {
     211               0 :   bool canDisallow = mActivityState.ifPossibleDisallowUI(nsNSSActivityState::test_only);
     212               0 :   bool bIsUIActive = !canDisallow;
     213               0 :   return bIsUIActive;
     214                 : }
     215                 : 
     216             314 : bool nsNSSShutDownList::ifPossibleDisallowUI()
     217                 : {
     218             314 :   bool isNowDisallowed = mActivityState.ifPossibleDisallowUI(nsNSSActivityState::do_it_for_real);
     219             314 :   return isNowDisallowed;
     220                 : }
     221                 : 
     222             314 : void nsNSSShutDownList::allowUI()
     223                 : {
     224             314 :   mActivityState.allowUI();
     225             314 : }
     226                 : 
     227             327 : nsresult nsNSSShutDownList::evaporateAllNSSResources()
     228                 : {
     229             327 :   if (PR_SUCCESS != mActivityState.restrictActivityToCurrentThread()) {
     230               0 :     PR_LOG(gPIPNSSLog, PR_LOG_DEBUG, ("failed to restrict activity to current thread\n"));
     231               0 :     return NS_ERROR_FAILURE;
     232                 :   }
     233                 : 
     234             327 :   PR_LOG(gPIPNSSLog, PR_LOG_DEBUG, ("now evaporating NSS resources\n"));
     235                 :   int removedCount;
     236            2127 :   do {
     237            4254 :     MutexAutoLock lock(mListLock);
     238            2127 :     removedCount = PL_DHashTableEnumerate(&mObjects, evaporateAllNSSResourcesHelper, 0);
     239                 :   } while (removedCount > 0);
     240                 : 
     241             327 :   mActivityState.releaseCurrentThreadActivityRestriction();
     242             327 :   return NS_OK;
     243                 : }
     244                 : 
     245                 : PLDHashOperator PR_CALLBACK
     246            1800 : nsNSSShutDownList::evaporateAllNSSResourcesHelper(PLDHashTable *table, 
     247                 :   PLDHashEntryHdr *hdr, PRUint32 number, void *arg)
     248                 : {
     249            1800 :   ObjectHashEntry *entry = static_cast<ObjectHashEntry*>(hdr);
     250                 :   {
     251            3600 :     MutexAutoUnlock unlock(singleton->mListLock);
     252            1800 :     entry->obj->shutdown(nsNSSShutDownObject::calledFromList);
     253                 :   }
     254                 :   // Never free more than one entry, because other threads might be calling
     255                 :   // us and remove themselves while we are iterating over the list,
     256                 :   // and the behaviour of changing the list while iterating is undefined.
     257            1800 :   return (PLDHashOperator)(PL_DHASH_STOP | PL_DHASH_REMOVE);
     258                 : }
     259                 : 
     260             327 : nsNSSShutDownList *nsNSSShutDownList::construct()
     261                 : {
     262             327 :   if (singleton) {
     263                 :     // we should never ever be called twice
     264               0 :     return nsnull;
     265                 :   }
     266                 : 
     267             327 :   singleton = new nsNSSShutDownList();
     268             327 :   return singleton;
     269                 : }
     270                 : 
     271             327 : nsNSSActivityState::nsNSSActivityState()
     272                 : :mNSSActivityStateLock("nsNSSActivityState.mNSSActivityStateLock"), 
     273                 :  mNSSActivityChanged(mNSSActivityStateLock,
     274                 :                      "nsNSSActivityState.mNSSActivityStateLock"),
     275                 :  mNSSActivityCounter(0),
     276                 :  mBlockingUICounter(0),
     277                 :  mIsUIForbidden(false),
     278             327 :  mNSSRestrictedThread(nsnull)
     279                 : {
     280             327 : }
     281                 : 
     282             327 : nsNSSActivityState::~nsNSSActivityState()
     283                 : {
     284             327 : }
     285                 : 
     286           95147 : void nsNSSActivityState::enter()
     287                 : {
     288          190294 :   MutexAutoLock lock(mNSSActivityStateLock);
     289                 : 
     290          190294 :   while (mNSSRestrictedThread && mNSSRestrictedThread != PR_GetCurrentThread()) {
     291               0 :     mNSSActivityChanged.Wait();
     292                 :   }
     293                 : 
     294           95147 :   ++mNSSActivityCounter;
     295           95147 : }
     296                 : 
     297           95147 : void nsNSSActivityState::leave()
     298                 : {
     299          190294 :   MutexAutoLock lock(mNSSActivityStateLock);
     300                 : 
     301           95147 :   --mNSSActivityCounter;
     302                 : 
     303           95147 :   mNSSActivityChanged.NotifyAll();
     304           95147 : }
     305                 : 
     306               0 : void nsNSSActivityState::enterBlockingUIState()
     307                 : {
     308               0 :   MutexAutoLock lock(mNSSActivityStateLock);
     309                 : 
     310               0 :   ++mBlockingUICounter;
     311               0 : }
     312                 : 
     313               0 : void nsNSSActivityState::leaveBlockingUIState()
     314                 : {
     315               0 :   MutexAutoLock lock(mNSSActivityStateLock);
     316                 : 
     317               0 :   --mBlockingUICounter;
     318               0 : }
     319                 : 
     320               0 : bool nsNSSActivityState::isBlockingUIActive()
     321                 : {
     322               0 :   MutexAutoLock lock(mNSSActivityStateLock);
     323               0 :   return (mBlockingUICounter > 0);
     324                 : }
     325                 : 
     326               0 : bool nsNSSActivityState::isUIForbidden()
     327                 : {
     328               0 :   MutexAutoLock lock(mNSSActivityStateLock);
     329               0 :   return mIsUIForbidden;
     330                 : }
     331                 : 
     332             314 : bool nsNSSActivityState::ifPossibleDisallowUI(RealOrTesting rot)
     333                 : {
     334             314 :   bool retval = false;
     335             628 :   MutexAutoLock lock(mNSSActivityStateLock);
     336                 : 
     337                 :   // Checking and disallowing the UI must be done atomically.
     338                 : 
     339             314 :   if (!mBlockingUICounter) {
     340                 :     // No UI is currently shown, we are able to evaporate.
     341             314 :     retval = true;
     342             314 :     if (rot == do_it_for_real) {
     343                 :       // Remember to disallow UI.
     344             314 :       mIsUIForbidden = true;
     345                 :         
     346                 :       // to clear the "forbidden" state,
     347                 :       // one must either call 
     348                 :       // restrictActivityToCurrentThread() + releaseCurrentThreadActivityRestriction()
     349                 :       // or cancel the operation by calling
     350                 :       // unprepareCurrentThreadRestriction()
     351                 :     }
     352                 :   }
     353             314 :   return retval;
     354                 : }
     355                 : 
     356             314 : void nsNSSActivityState::allowUI()
     357                 : {
     358             628 :   MutexAutoLock lock(mNSSActivityStateLock);
     359                 : 
     360             314 :   mIsUIForbidden = false;
     361             314 : }
     362                 : 
     363             327 : PRStatus nsNSSActivityState::restrictActivityToCurrentThread()
     364                 : {
     365             327 :   PRStatus retval = PR_FAILURE;
     366             654 :   MutexAutoLock lock(mNSSActivityStateLock);
     367                 :   
     368             327 :   if (!mBlockingUICounter) {
     369             654 :     while (0 < mNSSActivityCounter && !mBlockingUICounter) {
     370               0 :       mNSSActivityChanged.Wait(PR_TicksPerSecond());
     371                 :     }
     372                 :       
     373             327 :     if (mBlockingUICounter) {
     374                 :       // This should never happen.
     375                 :       // If we arrive here, our logic is broken.
     376               0 :       PR_ASSERT(0);
     377                 :     }
     378                 :     else {
     379             327 :       mNSSRestrictedThread = PR_GetCurrentThread();
     380             327 :       retval = PR_SUCCESS;
     381                 :     }
     382                 :   }
     383                 : 
     384             327 :   return retval;
     385                 : }
     386                 : 
     387             327 : void nsNSSActivityState::releaseCurrentThreadActivityRestriction()
     388                 : {
     389             654 :   MutexAutoLock lock(mNSSActivityStateLock);
     390                 : 
     391             327 :   mNSSRestrictedThread = nsnull;
     392             327 :   mIsUIForbidden = false;
     393                 : 
     394             327 :   mNSSActivityChanged.NotifyAll();
     395             327 : }
     396                 : 
     397           95217 : nsNSSShutDownPreventionLock::nsNSSShutDownPreventionLock()
     398                 : {
     399           95217 :   nsNSSActivityState *state = nsNSSShutDownList::getActivityState();
     400           95217 :   if (!state)
     401              70 :     return;
     402                 : 
     403           95147 :   state->enter();
     404                 : }
     405                 : 
     406           95217 : nsNSSShutDownPreventionLock::~nsNSSShutDownPreventionLock()
     407                 : {
     408           95217 :   nsNSSActivityState *state = nsNSSShutDownList::getActivityState();
     409           95217 :   if (!state)
     410              70 :     return;
     411                 :   
     412           95147 :   state->leave();
     413           95217 : }
     414                 : 
     415               0 : nsPSMUITracker::nsPSMUITracker()
     416                 : {
     417               0 :   nsNSSActivityState *state = nsNSSShutDownList::getActivityState();
     418               0 :   if (!state)
     419               0 :     return;
     420                 :   
     421               0 :   state->enterBlockingUIState();
     422                 : }
     423                 : 
     424               0 : nsPSMUITracker::~nsPSMUITracker()
     425                 : {
     426               0 :   nsNSSActivityState *state = nsNSSShutDownList::getActivityState();
     427               0 :   if (!state)
     428               0 :     return;
     429                 :   
     430               0 :   state->leaveBlockingUIState();
     431               0 : }
     432                 : 
     433               0 : bool nsPSMUITracker::isUIForbidden()
     434                 : {
     435               0 :   nsNSSActivityState *state = nsNSSShutDownList::getActivityState();
     436               0 :   if (!state)
     437               0 :     return false;
     438                 : 
     439               0 :   return state->isUIForbidden();
     440                 : }

Generated by: LCOV version 1.7