xpcom/glue/nsCategoryCache.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/glue/nsCategoryCache.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,95 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#ifndef nsCategoryCache_h_
     1.9 +#define nsCategoryCache_h_
    1.10 +
    1.11 +#include "mozilla/Attributes.h"
    1.12 +
    1.13 +#include "nsICategoryManager.h"
    1.14 +#include "nsIObserver.h"
    1.15 +#include "nsISimpleEnumerator.h"
    1.16 +#include "nsISupportsPrimitives.h"
    1.17 +
    1.18 +#include "nsServiceManagerUtils.h"
    1.19 +
    1.20 +#include "nsAutoPtr.h"
    1.21 +#include "nsCOMArray.h"
    1.22 +#include "nsInterfaceHashtable.h"
    1.23 +
    1.24 +#include "nsXPCOM.h"
    1.25 +
    1.26 +class NS_COM_GLUE nsCategoryObserver MOZ_FINAL : public nsIObserver
    1.27 +{
    1.28 +  public:
    1.29 +    nsCategoryObserver(const char* aCategory);
    1.30 +    ~nsCategoryObserver();
    1.31 +
    1.32 +    void ListenerDied();
    1.33 +    nsInterfaceHashtable<nsCStringHashKey, nsISupports>& GetHash()
    1.34 +    {
    1.35 +      return mHash;
    1.36 +    }
    1.37 +
    1.38 +    NS_DECL_ISUPPORTS
    1.39 +    NS_DECL_NSIOBSERVER
    1.40 +  private:
    1.41 +    void RemoveObservers();
    1.42 +
    1.43 +    nsInterfaceHashtable<nsCStringHashKey, nsISupports> mHash;
    1.44 +    nsCString mCategory;
    1.45 +    bool mObserversRemoved;
    1.46 +};
    1.47 +
    1.48 +/**
    1.49 + * This is a helper class that caches services that are registered in a certain
    1.50 + * category. The intended usage is that a service stores a variable of type
    1.51 + * nsCategoryCache<nsIFoo> in a member variable, where nsIFoo is the interface
    1.52 + * that these services should implement. The constructor of this class should
    1.53 + * then get the name of the category.
    1.54 + */
    1.55 +template<class T>
    1.56 +class nsCategoryCache MOZ_FINAL
    1.57 +{
    1.58 +  public:
    1.59 +    explicit nsCategoryCache(const char* aCategory)
    1.60 +      : mCategoryName(aCategory)
    1.61 +    {
    1.62 +    }
    1.63 +    ~nsCategoryCache() {
    1.64 +      if (mObserver)
    1.65 +	mObserver->ListenerDied();
    1.66 +    }
    1.67 +
    1.68 +    void GetEntries(nsCOMArray<T>& result) {
    1.69 +      // Lazy initialization, so that services in this category can't
    1.70 +      // cause reentrant getService (bug 386376)
    1.71 +      if (!mObserver)
    1.72 +        mObserver = new nsCategoryObserver(mCategoryName.get());
    1.73 +
    1.74 +      mObserver->GetHash().EnumerateRead(EntriesToArray, &result);
    1.75 +    }
    1.76 +
    1.77 +  private:
    1.78 +    // Not to be implemented
    1.79 +    nsCategoryCache(const nsCategoryCache<T>&);
    1.80 +
    1.81 +    static PLDHashOperator EntriesToArray(const nsACString& key,
    1.82 +					  nsISupports* entry, void* arg)
    1.83 +    {
    1.84 +      nsCOMArray<T>& entries = *static_cast<nsCOMArray<T>*>(arg);
    1.85 +
    1.86 +      nsCOMPtr<T> service = do_QueryInterface(entry);
    1.87 +      if (service) {
    1.88 +	entries.AppendObject(service);
    1.89 +      }
    1.90 +      return PL_DHASH_NEXT;
    1.91 +    }
    1.92 +
    1.93 +    nsCString mCategoryName;
    1.94 +    nsRefPtr<nsCategoryObserver> mObserver;
    1.95 +
    1.96 +};
    1.97 +
    1.98 +#endif

mercurial