LCOV - code coverage report
Current view: directory - xpcom/ds - nsObserverList.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 62 54 87.1 %
Date: 2012-06-02 Functions: 11 11 100.0 %

       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                 :  *
      24                 :  * Alternatively, the contents of this file may be used under the terms of
      25                 :  * either of the GNU General Public License Version 2 or later (the "GPL"),
      26                 :  * or 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 "nsObserverList.h"
      39                 : 
      40                 : #include "nsAutoPtr.h"
      41                 : #include "nsCOMArray.h"
      42                 : #include "nsISimpleEnumerator.h"
      43                 : 
      44                 : nsresult
      45           97407 : nsObserverList::AddObserver(nsIObserver* anObserver, bool ownsWeak)
      46                 : {
      47           97407 :     NS_ASSERTION(anObserver, "Null input");
      48                 : 
      49           97407 :     if (!ownsWeak) {
      50           37518 :         ObserverRef* o = mObservers.AppendElement(anObserver);
      51           37518 :         if (!o)
      52               0 :             return NS_ERROR_OUT_OF_MEMORY;
      53                 : 
      54           37518 :         return NS_OK;
      55                 :     }
      56                 :         
      57          119778 :     nsCOMPtr<nsIWeakReference> weak = do_GetWeakReference(anObserver);
      58           59889 :     if (!weak)
      59               0 :         return NS_NOINTERFACE;
      60                 : 
      61           59889 :     ObserverRef *o = mObservers.AppendElement(weak);
      62           59889 :     if (!o)
      63               0 :         return NS_ERROR_OUT_OF_MEMORY;
      64                 : 
      65           59889 :     return NS_OK;
      66                 : }
      67                 : 
      68                 : nsresult
      69           19787 : nsObserverList::RemoveObserver(nsIObserver* anObserver)
      70                 : {
      71           19787 :     NS_ASSERTION(anObserver, "Null input");
      72                 : 
      73           19787 :     if (mObservers.RemoveElement(static_cast<nsISupports*>(anObserver)))
      74           17714 :         return NS_OK;
      75                 : 
      76            4146 :     nsCOMPtr<nsIWeakReference> observerRef = do_GetWeakReference(anObserver);
      77            2073 :     if (!observerRef)
      78               0 :         return NS_ERROR_FAILURE;
      79                 : 
      80            2073 :     if (!mObservers.RemoveElement(observerRef))
      81               0 :         return NS_ERROR_FAILURE;
      82                 : 
      83            2073 :     return NS_OK;
      84                 : }
      85                 : 
      86                 : nsresult
      87             978 : nsObserverList::GetObserverList(nsISimpleEnumerator** anEnumerator)
      88                 : {
      89            1956 :     nsRefPtr<nsObserverEnumerator> e(new nsObserverEnumerator(this));
      90             978 :     if (!e)
      91               0 :         return NS_ERROR_OUT_OF_MEMORY;
      92                 : 
      93             978 :     NS_ADDREF(*anEnumerator = e);
      94             978 :     return NS_OK;
      95                 : }
      96                 : 
      97                 : void
      98           31401 : nsObserverList::FillObserverArray(nsCOMArray<nsIObserver> &aArray)
      99                 : {
     100           31401 :     aArray.SetCapacity(mObservers.Length());
     101                 : 
     102           62802 :     nsTArray<ObserverRef> observers(mObservers);
     103                 : 
     104           90581 :     for (PRInt32 i = observers.Length() - 1; i >= 0; --i) {
     105           59180 :         if (observers[i].isWeakRef) {
     106           77508 :             nsCOMPtr<nsIObserver> o(do_QueryReferent(observers[i].asWeak()));
     107           38754 :             if (o) {
     108           38742 :                 aArray.AppendObject(o);
     109                 :             }
     110                 :             else {
     111                 :                 // the object has gone away, remove the weakref
     112              12 :                 mObservers.RemoveElement(observers[i].asWeak());
     113                 :             }
     114                 :         }
     115                 :         else {
     116           20426 :             aArray.AppendObject(observers[i].asObserver());
     117                 :         }
     118                 :     }
     119           31401 : }
     120                 : 
     121                 : void
     122           30423 : nsObserverList::NotifyObservers(nsISupports *aSubject,
     123                 :                                 const char *aTopic,
     124                 :                                 const PRUnichar *someData)
     125                 : {
     126           60846 :     nsCOMArray<nsIObserver> observers;
     127           30423 :     FillObserverArray(observers);
     128                 : 
     129           88625 :     for (PRInt32 i = 0; i < observers.Count(); ++i) {
     130           58202 :         observers[i]->Observe(aSubject, aTopic, someData);
     131                 :     }
     132           30423 : }
     133                 : 
     134            7824 : NS_IMPL_ISUPPORTS1(nsObserverEnumerator, nsISimpleEnumerator)
     135                 : 
     136             978 : nsObserverEnumerator::nsObserverEnumerator(nsObserverList* aObserverList)
     137             978 :     : mIndex(0)
     138                 : {
     139             978 :     aObserverList->FillObserverArray(mObservers);
     140             978 : }
     141                 : 
     142                 : NS_IMETHODIMP
     143            1944 : nsObserverEnumerator::HasMoreElements(bool *aResult)
     144                 : {
     145            1944 :     *aResult = (mIndex < mObservers.Count());
     146            1944 :     return NS_OK;
     147                 : }
     148                 : 
     149                 : NS_IMETHODIMP
     150             966 : nsObserverEnumerator::GetNext(nsISupports* *aResult)
     151                 : {
     152             966 :     if (mIndex == mObservers.Count()) {
     153               0 :         NS_ERROR("Enumerating after HasMoreElements returned false.");
     154               0 :         return NS_ERROR_UNEXPECTED;
     155                 :     }
     156                 : 
     157             966 :     NS_ADDREF(*aResult = mObservers[mIndex]);
     158             966 :     ++mIndex;
     159             966 :     return NS_OK;
     160                 : }

Generated by: LCOV version 1.7