xpcom/glue/nsCategoryCache.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/glue/nsCategoryCache.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,137 @@
     1.4 +/* vim:set st=2 sts=2 ts=2 et cin: */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsIObserverService.h"
    1.10 +#include "mozilla/Services.h"
    1.11 +#include "nsISupportsPrimitives.h"
    1.12 +#include "nsIStringEnumerator.h"
    1.13 +
    1.14 +#include "nsXPCOMCID.h"
    1.15 +
    1.16 +#include "nsCategoryCache.h"
    1.17 +
    1.18 +nsCategoryObserver::nsCategoryObserver(const char* aCategory)
    1.19 +  : mCategory(aCategory)
    1.20 +  , mObserversRemoved(false)
    1.21 +{
    1.22 +  // First, enumerate the currently existing entries
    1.23 +  nsCOMPtr<nsICategoryManager> catMan =
    1.24 +    do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
    1.25 +  if (!catMan)
    1.26 +    return;
    1.27 +
    1.28 +  nsCOMPtr<nsISimpleEnumerator> enumerator;
    1.29 +  nsresult rv = catMan->EnumerateCategory(aCategory,
    1.30 +                                          getter_AddRefs(enumerator));
    1.31 +  if (NS_FAILED(rv))
    1.32 +    return;
    1.33 +
    1.34 +  nsCOMPtr<nsIUTF8StringEnumerator> strings = do_QueryInterface(enumerator);
    1.35 +  MOZ_ASSERT(strings);
    1.36 +
    1.37 +  bool more;
    1.38 +  while (NS_SUCCEEDED(strings->HasMore(&more)) && more) {
    1.39 +    nsAutoCString entryName;
    1.40 +    strings->GetNext(entryName);
    1.41 +
    1.42 +    nsCString entryValue;
    1.43 +    rv = catMan->GetCategoryEntry(aCategory,
    1.44 +				  entryName.get(),
    1.45 +				  getter_Copies(entryValue));
    1.46 +    if (NS_SUCCEEDED(rv)) {
    1.47 +      nsCOMPtr<nsISupports> service = do_GetService(entryValue.get());
    1.48 +      if (service) {
    1.49 +	mHash.Put(entryName, service);
    1.50 +      }
    1.51 +    }
    1.52 +  }
    1.53 +
    1.54 +  // Now, listen for changes
    1.55 +  nsCOMPtr<nsIObserverService> serv =
    1.56 +    mozilla::services::GetObserverService();
    1.57 +  if (serv) {
    1.58 +    serv->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
    1.59 +    serv->AddObserver(this, NS_XPCOM_CATEGORY_ENTRY_ADDED_OBSERVER_ID, false);
    1.60 +    serv->AddObserver(this, NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID, false);
    1.61 +    serv->AddObserver(this, NS_XPCOM_CATEGORY_CLEARED_OBSERVER_ID, false);
    1.62 +  }
    1.63 +}
    1.64 +
    1.65 +nsCategoryObserver::~nsCategoryObserver() {
    1.66 +}
    1.67 +
    1.68 +NS_IMPL_ISUPPORTS(nsCategoryObserver, nsIObserver)
    1.69 +
    1.70 +void
    1.71 +nsCategoryObserver::ListenerDied() {
    1.72 +  RemoveObservers();
    1.73 +}
    1.74 +
    1.75 +void
    1.76 +nsCategoryObserver::RemoveObservers() {
    1.77 +  if (mObserversRemoved)
    1.78 +    return;
    1.79 +
    1.80 +  mObserversRemoved = true;
    1.81 +  nsCOMPtr<nsIObserverService> obsSvc =
    1.82 +    mozilla::services::GetObserverService();
    1.83 +  if (obsSvc) {
    1.84 +    obsSvc->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID);
    1.85 +    obsSvc->RemoveObserver(this, NS_XPCOM_CATEGORY_ENTRY_ADDED_OBSERVER_ID);
    1.86 +    obsSvc->RemoveObserver(this, NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID);
    1.87 +    obsSvc->RemoveObserver(this, NS_XPCOM_CATEGORY_CLEARED_OBSERVER_ID);
    1.88 +  }
    1.89 +}
    1.90 +
    1.91 +NS_IMETHODIMP
    1.92 +nsCategoryObserver::Observe(nsISupports* aSubject, const char* aTopic,
    1.93 +                            const char16_t* aData) {
    1.94 +  if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
    1.95 +    mHash.Clear();
    1.96 +    RemoveObservers();
    1.97 +
    1.98 +    return NS_OK;
    1.99 +  }
   1.100 +
   1.101 +  if (!aData ||
   1.102 +      !nsDependentString(aData).Equals(NS_ConvertASCIItoUTF16(mCategory)))
   1.103 +    return NS_OK;
   1.104 +
   1.105 +  nsAutoCString str;
   1.106 +  nsCOMPtr<nsISupportsCString> strWrapper(do_QueryInterface(aSubject));
   1.107 +  if (strWrapper)
   1.108 +    strWrapper->GetData(str);
   1.109 +
   1.110 +  if (strcmp(aTopic, NS_XPCOM_CATEGORY_ENTRY_ADDED_OBSERVER_ID) == 0) {
   1.111 +    // We may get an add notification even when we already have an entry. This
   1.112 +    // is due to the notification happening asynchronously, so if the entry gets
   1.113 +    // added and an nsCategoryObserver gets instantiated before events get
   1.114 +    // processed, we'd get the notification for an existing entry.
   1.115 +    // Do nothing in that case.
   1.116 +    if (mHash.GetWeak(str))
   1.117 +      return NS_OK;
   1.118 +
   1.119 +    nsCOMPtr<nsICategoryManager> catMan =
   1.120 +      do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
   1.121 +    if (!catMan)
   1.122 +      return NS_OK;
   1.123 +
   1.124 +    nsCString entryValue;
   1.125 +    catMan->GetCategoryEntry(mCategory.get(),
   1.126 +                             str.get(),
   1.127 +                             getter_Copies(entryValue));
   1.128 +
   1.129 +    nsCOMPtr<nsISupports> service = do_GetService(entryValue.get());
   1.130 +
   1.131 +    if (service) {
   1.132 +      mHash.Put(str, service);
   1.133 +    }
   1.134 +  } else if (strcmp(aTopic, NS_XPCOM_CATEGORY_ENTRY_REMOVED_OBSERVER_ID) == 0) {
   1.135 +    mHash.Remove(str);
   1.136 +  } else if (strcmp(aTopic, NS_XPCOM_CATEGORY_CLEARED_OBSERVER_ID) == 0) {
   1.137 +    mHash.Clear();
   1.138 +  }
   1.139 +  return NS_OK;
   1.140 +}

mercurial