dom/quota/StorageMatcher.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* vim: set ts=2 et sw=2 tw=80: */
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     5  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 #ifndef mozilla_dom_quota_patternmatcher_h__
     8 #define mozilla_dom_quota_patternmatcher_h__
    10 #include "mozilla/dom/quota/QuotaCommon.h"
    12 #include "ArrayCluster.h"
    13 #include "Utilities.h"
    15 BEGIN_QUOTA_NAMESPACE
    17 template <class ValueType, class BaseType = ArrayCluster<nsIOfflineStorage*> >
    18 class StorageMatcher : public ValueType
    19 {
    20   typedef StorageMatcher<ValueType, BaseType> SelfType;
    22   struct Closure
    23   {
    24     Closure(SelfType& aSelf)
    25     : mSelf(aSelf), mPattern(EmptyCString()), mIndexes(nullptr)
    26     { }
    28     Closure(SelfType& aSelf, const nsACString& aPattern)
    29     : mSelf(aSelf), mPattern(aPattern), mIndexes(nullptr)
    30     { }
    32     Closure(SelfType& aSelf, const nsTArray<uint32_t>* aIndexes)
    33     : mSelf(aSelf), mPattern(EmptyCString()), mIndexes(aIndexes)
    34     { }
    36     Closure(SelfType& aSelf, const nsACString& aPattern,
    37             const nsTArray<uint32_t>* aIndexes)
    38     : mSelf(aSelf), mPattern(aPattern), mIndexes(aIndexes)
    39     { }
    41     SelfType& mSelf;
    42     const nsACString& mPattern;
    43     const nsTArray<uint32_t>* mIndexes;
    44   };
    46 public:
    47   template <class T, class U, class V>
    48   void
    49   Find(const nsBaseHashtable<T, U, V>& aHashtable,
    50        const nsACString& aPattern)
    51   {
    52     SelfType::Clear();
    54     Closure closure(*this, aPattern);
    55     aHashtable.EnumerateRead(SelfType::MatchPattern, &closure);
    56   }
    58   template <class T, class U, class V>
    59   void
    60   Find(const nsBaseHashtable<T, U, V>& aHashtable,
    61        const nsTArray<uint32_t>* aIndexes)
    62   {
    63     SelfType::Clear();
    65     Closure closure(*this, aIndexes);
    66     aHashtable.EnumerateRead(SelfType::MatchIndexes, &closure);
    67   }
    69   template <class T, class U, class V>
    70   void
    71   Find(const nsBaseHashtable<T, U, V>& aHashtable,
    72        uint32_t aIndex)
    73   {
    74     nsAutoTArray<uint32_t, 1> indexes;
    75     indexes.AppendElement(aIndex);
    77     Find(aHashtable, &indexes);
    78   }
    80   template <class T, class U, class V>
    81   void
    82   Find(const nsBaseHashtable<T, U, V>& aHashtable,
    83        const nsACString& aPattern,
    84        const nsTArray<uint32_t>* aIndexes)
    85   {
    86     SelfType::Clear();
    88     Closure closure(*this, aPattern, aIndexes);
    89     aHashtable.EnumerateRead(SelfType::MatchPatternAndIndexes, &closure);
    90   }
    92   template <class T, class U, class V>
    93   void
    94   Find(const nsBaseHashtable<T, U, V>& aHashtable,
    95        const nsACString& aPattern,
    96        uint32_t aIndex)
    97   {
    98     nsAutoTArray<uint32_t, 1> indexes;
    99     indexes.AppendElement(aIndex);
   101     Find(aHashtable, aPattern, &indexes);
   102   }
   104   template <class T, class U, class V>
   105   void
   106   Find(const nsBaseHashtable<T, U, V>& aHashtable)
   107   {
   108     SelfType::Clear();
   110     Closure closure(*this);
   111     aHashtable.EnumerateRead(SelfType::MatchAll, &closure);
   112   }
   114 private:
   115   static PLDHashOperator
   116   MatchPattern(const nsACString& aKey,
   117                BaseType* aValue,
   118                void* aUserArg)
   119   {
   120     MOZ_ASSERT(!aKey.IsEmpty(), "Empty key!");
   121     MOZ_ASSERT(aValue, "Null pointer!");
   122     MOZ_ASSERT(aUserArg, "Null pointer!");
   124     Closure* closure = static_cast<Closure*>(aUserArg);
   126     if (PatternMatchesOrigin(closure->mPattern, aKey)) {
   127       aValue->AppendElementsTo(closure->mSelf);
   128     }
   130     return PL_DHASH_NEXT;
   131   }
   133   static PLDHashOperator
   134   MatchIndexes(const nsACString& aKey,
   135                BaseType* aValue,
   136                void* aUserArg)
   137   {
   138     MOZ_ASSERT(!aKey.IsEmpty(), "Empty key!");
   139     MOZ_ASSERT(aValue, "Null pointer!");
   140     MOZ_ASSERT(aUserArg, "Null pointer!");
   142     Closure* closure = static_cast<Closure*>(aUserArg);
   144     for (uint32_t index = 0; index < closure->mIndexes->Length(); index++) {
   145       aValue->AppendElementsTo(closure->mIndexes->ElementAt(index),
   146                                closure->mSelf);
   147     }
   149     return PL_DHASH_NEXT;
   150   }
   152   static PLDHashOperator
   153   MatchPatternAndIndexes(const nsACString& aKey,
   154                          BaseType* aValue,
   155                          void* aUserArg)
   156   {
   157     MOZ_ASSERT(!aKey.IsEmpty(), "Empty key!");
   158     MOZ_ASSERT(aValue, "Null pointer!");
   159     MOZ_ASSERT(aUserArg, "Null pointer!");
   161     Closure* closure = static_cast<Closure*>(aUserArg);
   163     if (PatternMatchesOrigin(closure->mPattern, aKey)) {
   164       for (uint32_t index = 0; index < closure->mIndexes->Length(); index++) {
   165         aValue->AppendElementsTo(closure->mIndexes->ElementAt(index),
   166                                  closure->mSelf);
   167       }
   168     }
   170     return PL_DHASH_NEXT;
   171   }
   173   static PLDHashOperator
   174   MatchAll(const nsACString& aKey,
   175            BaseType* aValue,
   176            void* aUserArg)
   177   {
   178     MOZ_ASSERT(!aKey.IsEmpty(), "Empty key!");
   179     MOZ_ASSERT(aValue, "Null pointer!");
   180     MOZ_ASSERT(aUserArg, "Null pointer!");
   182     Closure* closure = static_cast<Closure*>(aUserArg);
   183     aValue->AppendElementsTo(closure->mSelf);
   185     return PL_DHASH_NEXT;
   186   }
   187 };
   189 END_QUOTA_NAMESPACE
   191 #endif // mozilla_dom_quota_patternmatcher_h__

mercurial