LCOV - code coverage report
Current view: directory - objdir/xpcom/build - nsCategoryCache.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 84 78 92.9 %
Date: 2012-06-02 Functions: 8 8 100.0 %

       1                 : /* vim:set st=2 sts=2 ts=2 et cin: */
       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 a cache for services in a category.
      16                 :  *
      17                 :  * The Initial Developer of the Original Code is
      18                 :  * Christian Biesinger <cbiesinger@web.de>.
      19                 :  * Portions created by the Initial Developer are Copyright (C) 2005
      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 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 "nsIObserverService.h"
      39                 : #include "mozilla/Services.h"
      40                 : #include "nsISupportsPrimitives.h"
      41                 : 
      42                 : #include "nsXPCOMCID.h"
      43                 : 
      44                 : #include "nsCategoryCache.h"
      45                 : 
      46             389 : nsCategoryObserver::nsCategoryObserver(const char* aCategory,
      47                 :                                        nsCategoryListener* aListener)
      48             389 :   : mListener(nsnull), mCategory(aCategory), mObserversRemoved(false)
      49                 : {
      50             389 :   if (!mHash.Init()) {
      51                 :     // OOM
      52               0 :     return;
      53                 :   }
      54                 : 
      55             389 :   mListener = aListener;
      56                 : 
      57                 :   // First, enumerate the currently existing entries
      58                 :   nsCOMPtr<nsICategoryManager> catMan =
      59             778 :     do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
      60             389 :   if (!catMan)
      61                 :     return;
      62                 : 
      63             778 :   nsCOMPtr<nsISimpleEnumerator> enumerator;
      64             389 :   nsresult rv = catMan->EnumerateCategory(aCategory,
      65             389 :                                           getter_AddRefs(enumerator));
      66             389 :   if (NS_FAILED(rv))
      67                 :     return;
      68                 : 
      69            1167 :   nsTArray<nsCString> entries;
      70             778 :   nsCOMPtr<nsISupports> entry;
      71            1178 :   while (NS_SUCCEEDED(enumerator->GetNext(getter_AddRefs(entry)))) {
      72             800 :     nsCOMPtr<nsISupportsCString> entryName = do_QueryInterface(entry, &rv);
      73                 : 
      74             400 :     if (NS_SUCCEEDED(rv)) {
      75             800 :       nsCAutoString categoryEntry;
      76             400 :       rv = entryName->GetData(categoryEntry);
      77                 : 
      78             800 :       nsCString entryValue;
      79             400 :       catMan->GetCategoryEntry(aCategory,
      80                 :                                categoryEntry.get(),
      81             400 :                                getter_Copies(entryValue));
      82                 : 
      83             400 :       if (NS_SUCCEEDED(rv)) {
      84             400 :         mHash.Put(categoryEntry, entryValue);
      85             400 :         entries.AppendElement(entryValue);
      86                 :       }
      87                 :     }
      88                 :   }
      89                 : 
      90                 :   // Now, listen for changes
      91                 :   nsCOMPtr<nsIObserverService> serv =
      92             778 :     mozilla::services::GetObserverService();
      93             389 :   if (serv) {
      94             389 :     serv->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
      95             389 :     serv->AddObserver(this, NS_XPCOM_CATEGORY_ENTRY_ADDED_OBSERVER_ID, false);
      96             389 :     serv->AddObserver(this, NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID, false);
      97             389 :     serv->AddObserver(this, NS_XPCOM_CATEGORY_CLEARED_OBSERVER_ID, false);
      98                 :   }
      99                 : 
     100             789 :   for (PRInt32 i = entries.Length() - 1; i >= 0; --i)
     101             400 :     mListener->EntryAdded(entries[i]);
     102                 : }
     103                 : 
     104             389 : nsCategoryObserver::~nsCategoryObserver() {
     105             389 : }
     106                 : 
     107           13655 : NS_IMPL_ISUPPORTS1(nsCategoryObserver, nsIObserver)
     108                 : 
     109                 : void
     110             389 : nsCategoryObserver::ListenerDied() {
     111             389 :   mListener = nsnull;
     112             389 :   RemoveObservers();
     113             389 : }
     114                 : 
     115                 : NS_HIDDEN_(void)
     116             774 : nsCategoryObserver::RemoveObservers() {
     117             774 :   if (mObserversRemoved)
     118             385 :     return;
     119                 : 
     120             389 :   mObserversRemoved = true;
     121                 :   nsCOMPtr<nsIObserverService> obsSvc =
     122             778 :     mozilla::services::GetObserverService();
     123             389 :   if (obsSvc) {
     124             389 :     obsSvc->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID);
     125             389 :     obsSvc->RemoveObserver(this, NS_XPCOM_CATEGORY_ENTRY_ADDED_OBSERVER_ID);
     126             389 :     obsSvc->RemoveObserver(this, NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID);
     127             389 :     obsSvc->RemoveObserver(this, NS_XPCOM_CATEGORY_CLEARED_OBSERVER_ID);
     128                 :   }
     129                 : }
     130                 : 
     131                 : NS_IMETHODIMP
     132             399 : nsCategoryObserver::Observe(nsISupports* aSubject, const char* aTopic,
     133                 :                             const PRUnichar* aData) {
     134             399 :   if (!mListener)
     135               0 :     return NS_OK;
     136                 : 
     137             399 :   if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
     138             385 :     mHash.Clear();
     139             385 :     mListener->CategoryCleared();
     140             385 :     RemoveObservers();
     141                 : 
     142             385 :     return NS_OK;
     143                 :   }
     144                 : 
     145              70 :   if (!aData ||
     146              56 :       !nsDependentString(aData).Equals(NS_ConvertASCIItoUTF16(mCategory)))
     147               9 :     return NS_OK;
     148                 : 
     149              10 :   nsCAutoString str;
     150              10 :   nsCOMPtr<nsISupportsCString> strWrapper(do_QueryInterface(aSubject));
     151               5 :   if (strWrapper)
     152               5 :     strWrapper->GetData(str);
     153                 : 
     154               5 :   if (strcmp(aTopic, NS_XPCOM_CATEGORY_ENTRY_ADDED_OBSERVER_ID) == 0) {
     155                 :     // We may get an add notification even when we already have an entry. This
     156                 :     // is due to the notification happening asynchronously, so if the entry gets
     157                 :     // added and an nsCategoryObserver gets instantiated before events get
     158                 :     // processed, we'd get the notification for an existing entry.
     159                 :     // Do nothing in that case.
     160               4 :     if (mHash.Get(str, nsnull))
     161               2 :       return NS_OK;
     162                 : 
     163                 :     nsCOMPtr<nsICategoryManager> catMan =
     164               4 :       do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
     165               2 :     if (!catMan)
     166               0 :       return NS_OK;
     167                 : 
     168               6 :     nsCString entryValue;
     169               2 :     catMan->GetCategoryEntry(mCategory.get(),
     170                 :                              str.get(),
     171               2 :                              getter_Copies(entryValue));
     172                 : 
     173               2 :     mHash.Put(str, entryValue);
     174               2 :     mListener->EntryAdded(entryValue);
     175               1 :   } else if (strcmp(aTopic, NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID) == 0) {
     176               2 :     nsCAutoString val;
     177               1 :     if (mHash.Get(str, &val)) {
     178               1 :       mHash.Remove(str);
     179               1 :       mListener->EntryRemoved(val);
     180                 :     }
     181               0 :   } else if (strcmp(aTopic, NS_XPCOM_CATEGORY_CLEARED_OBSERVER_ID) == 0) {
     182               0 :     mHash.Clear();
     183               0 :     mListener->CategoryCleared();
     184                 :   }
     185               3 :   return NS_OK;
     186                 : }

Generated by: LCOV version 1.7