Bug 1376964 - Part 3: Add a generation counter to the user font cache. r=jfkthame draft
authorCameron McCormack <cam@mcc.id.au>
Wed, 12 Jul 2017 16:35:45 +0800
changeset 607476 7186ed96da1b1a6710ef4f5b2f1443ddfe0ddc29
parent 607475 265a46c59b14af4d9e66688942408533c1cb94e1
child 607477 def889e9ae4a428ccc02f9b5ac18f2ce640dc652
push id67987
push userbmo:cam@mcc.id.au
push dateWed, 12 Jul 2017 08:38:54 +0000
reviewersjfkthame
bugs1376964
milestone56.0a1
Bug 1376964 - Part 3: Add a generation counter to the user font cache. r=jfkthame MozReview-Commit-ID: 3zCPC9Pfikc
gfx/thebes/gfxUserFontSet.cpp
gfx/thebes/gfxUserFontSet.h
--- a/gfx/thebes/gfxUserFontSet.cpp
+++ b/gfx/thebes/gfxUserFontSet.cpp
@@ -1094,17 +1094,20 @@ gfxUserFontSet::GetFamily(const nsAStrin
 // for a downloaded font will be kept alive by its corresponding gfxFont
 // instance(s) until they are deleted, and *that* happens using an expiration
 // tracker (gfxFontCache). The result is that the downloaded font instances
 // recorded here will persist between pages and can get reused (provided the
 // source URI and principal match, of course).
 ///////////////////////////////////////////////////////////////////////////////
 
 nsTHashtable<gfxUserFontSet::UserFontCache::Entry>*
-    gfxUserFontSet::UserFontCache::sUserFonts = nullptr;
+gfxUserFontSet::UserFontCache::sUserFonts = nullptr;
+
+uint32_t
+gfxUserFontSet::UserFontCache::sGeneration = 0;
 
 NS_IMPL_ISUPPORTS(gfxUserFontSet::UserFontCache::Flusher, nsIObserver)
 
 NS_IMETHODIMP
 gfxUserFontSet::UserFontCache::Flusher::Observe(nsISupports* aSubject,
                                                 const char* aTopic,
                                                 const char16_t* aData)
 {
@@ -1228,16 +1231,18 @@ gfxUserFontSet::UserFontCache::CacheFont
     if (IgnorePrincipal(data->mURI)) {
         principal = nullptr;
     } else {
         principal = data->mPrincipal;
     }
     sUserFonts->PutEntry(Key(data->mURI, principal, aFontEntry,
                              data->mPrivate));
 
+    ++sGeneration;
+
 #ifdef DEBUG_USERFONT_CACHE
     printf("userfontcache added fontentry: %p\n", aFontEntry);
     Dump();
 #endif
 }
 
 void
 gfxUserFontSet::UserFontCache::ForgetFont(gfxFontEntry* aFontEntry)
--- a/gfx/thebes/gfxUserFontSet.h
+++ b/gfx/thebes/gfxUserFontSet.h
@@ -296,16 +296,20 @@ public:
         // The aPrivate flag is set for requests coming from private windows,
         // so we can avoid leaking fonts cached in private windows mode out to
         // normal windows.
         static gfxFontEntry* GetFont(nsIURI* aSrcURI,
                                      nsIPrincipal* aPrincipal,
                                      gfxUserFontEntry* aUserFontEntry,
                                      bool              aPrivate);
 
+        // Generation number that is incremented whenever an entry is added to
+        // the cache.  (Removals don't increment it.)
+        static uint32_t Generation() { return sGeneration; }
+
         // Clear everything so that we don't leak URIs and Principals.
         static void Shutdown();
 
         // Memory-reporting support.
         class MemoryReporter final : public nsIMemoryReporter
         {
         private:
             ~MemoryReporter() { }
@@ -423,16 +427,18 @@ public:
             // (by calling ForgetFont()).
             gfxFontEntry* MOZ_NON_OWNING_REF mFontEntry;
 
             // Whether this font was loaded from a private window.
             bool                   mPrivate;
         };
 
         static nsTHashtable<Entry>* sUserFonts;
+
+        static uint32_t sGeneration;
     };
 
     void SetLocalRulesUsed() {
         mLocalRulesUsed = true;
     }
 
     static mozilla::LogModule* GetUserFontsLog();