Bug 1434206 - Make LookupCache objects const as much as possible. r?gcp draft
authorFrancois Marier <francois@mozilla.com>
Mon, 21 May 2018 15:11:01 -0700
changeset 805426 5ded5f6a543a9cff3d0c133c6bf0fd1b94c76e78
parent 805425 39057ad7087e4d40cd07a60df444354d10902ecd
child 805427 34741e20c984f304c073a9feb3146b29d2f3ec30
push id112654
push userfmarier@mozilla.com
push dateThu, 07 Jun 2018 20:10:46 +0000
reviewersgcp
bugs1434206
milestone62.0a1
Bug 1434206 - Make LookupCache objects const as much as possible. r?gcp MozReview-Commit-ID: AqC6NUh6ifm
toolkit/components/url-classifier/Classifier.cpp
toolkit/components/url-classifier/LookupCache.cpp
toolkit/components/url-classifier/LookupCache.h
--- a/toolkit/components/url-classifier/Classifier.cpp
+++ b/toolkit/components/url-classifier/Classifier.cpp
@@ -868,17 +868,17 @@ Classifier::ApplyFullHashes(ConstTableUp
 
   return NS_OK;
 }
 
 void
 Classifier::GetCacheInfo(const nsACString& aTable,
                          nsIUrlClassifierCacheInfo** aCache)
 {
-  RefPtr<LookupCache> lookupCache = GetLookupCache(aTable);
+  RefPtr<const LookupCache> lookupCache = GetLookupCache(aTable);
   if (!lookupCache) {
     return;
   }
 
   lookupCache->GetCacheInfo(aCache);
 }
 
 void
@@ -893,28 +893,28 @@ Classifier::RegenActiveTables()
   mActiveTablesCache.Clear();
 
   nsTArray<nsCString> foundTables;
   ScanStoreDir(mRootStoreDirectory, foundTables);
 
   for (uint32_t i = 0; i < foundTables.Length(); i++) {
     nsCString table(foundTables[i]);
 
-    RefPtr<LookupCache> lookupCache = GetLookupCache(table);
+    RefPtr<const LookupCache> lookupCache = GetLookupCache(table);
     if (!lookupCache) {
       LOG(("Inactive table (no cache): %s", table.get()));
       continue;
     }
 
     if (!lookupCache->IsPrimed()) {
       LOG(("Inactive table (cache not primed): %s", table.get()));
       continue;
     }
 
-    if (LookupCache::Cast<LookupCacheV4>(lookupCache)) {
+    if (LookupCache::Cast<const LookupCacheV4>(lookupCache)) {
       LOG(("Active v4 table: %s", table.get()));
     } else {
       HashStore store(table, GetProvider(table), mRootStoreDirectory);
 
       nsresult rv = store.Open();
       if (NS_FAILED(rv)) {
         continue;
       }
--- a/toolkit/components/url-classifier/LookupCache.cpp
+++ b/toolkit/components/url-classifier/LookupCache.cpp
@@ -238,17 +238,17 @@ void
 LookupCache::ClearAll()
 {
   ClearCache();
   ClearPrefixes();
   mPrimed = false;
 }
 
 void
-LookupCache::GetCacheInfo(nsIUrlClassifierCacheInfo** aCache)
+LookupCache::GetCacheInfo(nsIUrlClassifierCacheInfo** aCache) const
 {
   MOZ_ASSERT(aCache);
 
   RefPtr<nsUrlClassifierCacheInfo> info = new nsUrlClassifierCacheInfo;
   info->table = mTableName;
 
   for (auto iter = mFullHashCache.ConstIter(); !iter.Done(); iter.Next()) {
     RefPtr<nsUrlClassifierCacheEntry> entry = new nsUrlClassifierCacheEntry;
@@ -503,17 +503,17 @@ nsCString GetFormattedTimeString(int64_t
 
   return nsPrintfCString(
          "%04d-%02d-%02d %02d:%02d:%02d UTC",
          pret.tm_year, pret.tm_month + 1, pret.tm_mday,
          pret.tm_hour, pret.tm_min, pret.tm_sec);
 }
 
 void
-LookupCache::DumpCache()
+LookupCache::DumpCache() const
 {
   if (!LOG_ENABLED()) {
     return;
   }
 
   for (auto iter = mFullHashCache.ConstIter(); !iter.Done(); iter.Next()) {
     CachedFullHashResponse* response = iter.Data();
 
@@ -773,17 +773,17 @@ LookupCacheV2::ConstructPrefixSet(AddPre
 
   mPrimed = true;
 
   return NS_OK;
 }
 
 #if defined(DEBUG)
 void
-LookupCacheV2::DumpCompletions()
+LookupCacheV2::DumpCompletions() const
 {
   if (!LOG_ENABLED())
     return;
 
   for (uint32_t i = 0; i < mUpdateCompletions.Length(); i++) {
     nsAutoCString str;
     mUpdateCompletions[i].ToHexString(str);
     LOG(("Update: %s", str.get()));
--- a/toolkit/components/url-classifier/LookupCache.h
+++ b/toolkit/components/url-classifier/LookupCache.h
@@ -207,20 +207,20 @@ public:
   // Clear fullhash cache from fullhash/gethash response.
   void ClearCache();
 
   // Check if completions can be found in cache.
   // Currently this is only used by testcase.
   bool IsInCache(uint32_t key) const { return mFullHashCache.Get(key); };
 
 #if DEBUG
-  void DumpCache();
+  void DumpCache() const;
 #endif
 
-  void GetCacheInfo(nsIUrlClassifierCacheInfo** aCache);
+  void GetCacheInfo(nsIUrlClassifierCacheInfo** aCache) const;
 
   virtual nsresult Open();
   virtual nsresult Init() = 0;
   virtual nsresult ClearPrefixes() = 0;
   virtual nsresult Has(const Completion& aCompletion,
                        bool* aHas,
                        uint32_t* aMatchLength,
                        bool* aConfirmed) = 0;
@@ -228,16 +228,20 @@ public:
   virtual bool IsEmpty() const = 0;
 
   virtual void ClearAll();
 
   template<typename T>
   static T* Cast(LookupCache* aThat) {
     return ((aThat && T::VER == aThat->Ver()) ? reinterpret_cast<T*>(aThat) : nullptr);
   }
+  template<typename T>
+  static const T* Cast(const LookupCache* aThat) {
+    return ((aThat && T::VER == aThat->Ver()) ? reinterpret_cast<const T*>(aThat) : nullptr);
+  }
 
 private:
   nsresult LoadPrefixSet();
 
   virtual nsresult StoreToFile(nsIFile* aFile) = 0;
   virtual nsresult LoadFromFile(nsIFile* aFile) = 0;
   virtual size_t SizeOfPrefixSet() const = 0;
 
@@ -292,17 +296,17 @@ public:
 
   // This will Clear() the passed arrays when done.
   // 'aExpirySec' is used by testcase to config an expired time.
   void AddGethashResultToCache(const AddCompleteArray& aAddCompletes,
                                const MissPrefixArray& aMissPrefixes,
                                int64_t aExpirySec = 0);
 
 #if DEBUG
-  void DumpCompletions();
+  void DumpCompletions() const;
 #endif
 
   static const int VER;
 
 protected:
   nsresult ReadCompletions();
 
   virtual nsresult ClearPrefixes() override;