LCOV - code coverage report
Current view: directory - xpcom/ds - nsObserverService.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 56 53 94.6 %
Date: 2012-06-02 Functions: 14 13 92.9 %

       1                 : /* -*- Mode: C++; tab-width: 4; 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.org code.
      16                 :  *
      17                 :  * The Initial Developer of the Original Code is
      18                 :  * Netscape Communications Corporation.
      19                 :  * Portions created by the Initial Developer are Copyright (C) 1998
      20                 :  * the Initial Developer. All Rights Reserved.
      21                 :  *
      22                 :  * Contributor(s):
      23                 :  *   Pierre Phaneuf <pp@ludusdesign.com>
      24                 :  *
      25                 :  * Alternatively, the contents of this file may be used under the terms of
      26                 :  * either of the GNU General Public License Version 2 or later (the "GPL"),
      27                 :  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      28                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      29                 :  * of those above. If you wish to allow use of your version of this file only
      30                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      31                 :  * use your version of this file under the terms of the MPL, indicate your
      32                 :  * decision by deleting the provisions above and replace them with the notice
      33                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      34                 :  * the provisions above, a recipient may use your version of this file under
      35                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      36                 :  *
      37                 :  * ***** END LICENSE BLOCK ***** */
      38                 : 
      39                 : #include "prlog.h"
      40                 : #include "nsAutoPtr.h"
      41                 : #include "nsIFactory.h"
      42                 : #include "nsIServiceManager.h"
      43                 : #include "nsIComponentManager.h"
      44                 : #include "nsIObserverService.h"
      45                 : #include "nsIObserver.h"
      46                 : #include "nsISimpleEnumerator.h"
      47                 : #include "nsObserverService.h"
      48                 : #include "nsObserverList.h"
      49                 : #include "nsHashtable.h"
      50                 : #include "nsThreadUtils.h"
      51                 : #include "nsIWeakReference.h"
      52                 : #include "nsEnumeratorUtils.h"
      53                 : 
      54                 : #define NOTIFY_GLOBAL_OBSERVERS
      55                 : 
      56                 : #if defined(PR_LOGGING)
      57                 : // Log module for nsObserverService logging...
      58                 : //
      59                 : // To enable logging (see prlog.h for full details):
      60                 : //
      61                 : //    set NSPR_LOG_MODULES=ObserverService:5
      62                 : //    set NSPR_LOG_FILE=nspr.log
      63                 : //
      64                 : // this enables PR_LOG_DEBUG level information and places all output in
      65                 : // the file nspr.log
      66            1464 :   PRLogModuleInfo* observerServiceLog = PR_NewLogModule("ObserverService");
      67                 :   #define LOG(x)  PR_LOG(observerServiceLog, PR_LOG_DEBUG, x)
      68                 : #else
      69                 :   #define LOG(x)
      70                 : #endif /* PR_LOGGING */
      71                 : 
      72                 : ////////////////////////////////////////////////////////////////////////////////
      73                 : // nsObserverService Implementation
      74                 : 
      75                 : 
      76          452878 : NS_IMPL_THREADSAFE_ISUPPORTS2(nsObserverService, nsIObserverService, nsObserverService)
      77                 : 
      78            1419 : nsObserverService::nsObserverService() :
      79            1419 :     mShuttingDown(false)
      80                 : {
      81            1419 :     mObserverTopicTable.Init();
      82            1419 : }
      83                 : 
      84            2820 : nsObserverService::~nsObserverService(void)
      85                 : {
      86            1410 :     Shutdown();
      87            1410 : }
      88                 : 
      89                 : void
      90            2829 : nsObserverService::Shutdown()
      91                 : {
      92            2829 :     mShuttingDown = true;
      93                 : 
      94            2829 :     if (mObserverTopicTable.IsInitialized())
      95            2829 :         mObserverTopicTable.Clear();
      96            2829 : }
      97                 : 
      98                 : nsresult
      99            1419 : nsObserverService::Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr)
     100                 : {
     101            1419 :     LOG(("nsObserverService::Create()"));
     102                 : 
     103            2838 :     nsRefPtr<nsObserverService> os = new nsObserverService();
     104                 : 
     105            1419 :     if (!os || !os->mObserverTopicTable.IsInitialized())
     106               0 :         return NS_ERROR_OUT_OF_MEMORY;
     107                 : 
     108            1419 :     return os->QueryInterface(aIID, aInstancePtr);
     109                 : }
     110                 : 
     111                 : #define NS_ENSURE_VALIDCALL \
     112                 :     if (!NS_IsMainThread()) {                                     \
     113                 :         NS_ERROR("Using observer service off the main thread!");  \
     114                 :         return NS_ERROR_UNEXPECTED;                               \
     115                 :     }                                                             \
     116                 :     if (mShuttingDown) {                                          \
     117                 :         NS_ERROR("Using observer service after XPCOM shutdown!"); \
     118                 :         return NS_ERROR_ILLEGAL_DURING_SHUTDOWN;                  \
     119                 :     }
     120                 : 
     121                 : NS_IMETHODIMP
     122           97407 : nsObserverService::AddObserver(nsIObserver* anObserver, const char* aTopic,
     123                 :                                bool ownsWeak)
     124                 : {
     125           97407 :     LOG(("nsObserverService::AddObserver(%p: %s)",
     126                 :          (void*) anObserver, aTopic));
     127                 : 
     128           97407 :     NS_ENSURE_VALIDCALL
     129           97407 :     NS_ENSURE_ARG(anObserver && aTopic);
     130                 : 
     131           97407 :     nsObserverList *observerList = mObserverTopicTable.PutEntry(aTopic);
     132           97407 :     if (!observerList)
     133               0 :         return NS_ERROR_OUT_OF_MEMORY;
     134                 : 
     135           97407 :     return observerList->AddObserver(anObserver, ownsWeak);
     136                 : }
     137                 : 
     138                 : NS_IMETHODIMP
     139           19800 : nsObserverService::RemoveObserver(nsIObserver* anObserver, const char* aTopic)
     140                 : {
     141           19800 :     LOG(("nsObserverService::RemoveObserver(%p: %s)",
     142                 :          (void*) anObserver, aTopic));
     143           19800 :     NS_ENSURE_VALIDCALL
     144           19800 :     NS_ENSURE_ARG(anObserver && aTopic);
     145                 : 
     146           19800 :     nsObserverList *observerList = mObserverTopicTable.GetEntry(aTopic);
     147           19800 :     if (!observerList)
     148              13 :         return NS_ERROR_FAILURE;
     149                 : 
     150                 :     /* This death grip is to protect against stupid consumers who call
     151                 :        RemoveObserver from their Destructor, see bug 485834/bug 325392. */
     152           39574 :     nsCOMPtr<nsIObserver> kungFuDeathGrip(anObserver);
     153           19787 :     return observerList->RemoveObserver(anObserver);
     154                 : }
     155                 : 
     156                 : NS_IMETHODIMP
     157            1684 : nsObserverService::EnumerateObservers(const char* aTopic,
     158                 :                                       nsISimpleEnumerator** anEnumerator)
     159                 : {
     160            1684 :     NS_ENSURE_VALIDCALL
     161            1684 :     NS_ENSURE_ARG(aTopic && anEnumerator);
     162                 : 
     163            1684 :     nsObserverList *observerList = mObserverTopicTable.GetEntry(aTopic);
     164            1684 :     if (!observerList)
     165             706 :         return NS_NewEmptyEnumerator(anEnumerator);
     166                 : 
     167             978 :     return observerList->GetObserverList(anEnumerator);
     168                 : }
     169                 : 
     170                 : // Enumerate observers of aTopic and call Observe on each.
     171           66812 : NS_IMETHODIMP nsObserverService::NotifyObservers(nsISupports *aSubject,
     172                 :                                                  const char *aTopic,
     173                 :                                                  const PRUnichar *someData)
     174                 : {
     175           66812 :     LOG(("nsObserverService::NotifyObservers(%s)", aTopic));
     176                 : 
     177           66812 :     NS_ENSURE_VALIDCALL
     178           66812 :     NS_ENSURE_ARG(aTopic);
     179                 : 
     180           66812 :     nsObserverList *observerList = mObserverTopicTable.GetEntry(aTopic);
     181           66812 :     if (observerList)
     182           30423 :         observerList->NotifyObservers(aSubject, aTopic, someData);
     183                 : 
     184                 : #ifdef NOTIFY_GLOBAL_OBSERVERS
     185           66812 :     observerList = mObserverTopicTable.GetEntry("*");
     186           66812 :     if (observerList)
     187               0 :         observerList->NotifyObservers(aSubject, aTopic, someData);
     188                 : #endif
     189                 : 
     190           66812 :     return NS_OK;
     191            4392 : }
     192                 : 

Generated by: LCOV version 1.7