Bug 1068477 - Add pref callback to style sheet cache to handle UA style sheet reloads. r=bzbarsky
authorCameron McCormack <cam@mcc.id.au>
Fri, 14 Nov 2014 09:11:19 +1100
changeset 215643 ba794ec8a0f070fe81b752ce0c6367f2269a6733
parent 215642 a697e3ca8fb88637b37e2e0bfd5d66a4cccd241b
child 215644 5922f13f84397b5a8bc39254ff8506796e37571a
push id51809
push usercmccormack@mozilla.com
push dateThu, 13 Nov 2014 22:14:26 +0000
treeherdermozilla-inbound@5922f13f8439 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbzbarsky
bugs1068477
milestone36.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1068477 - Add pref callback to style sheet cache to handle UA style sheet reloads. r=bzbarsky
layout/style/nsLayoutStylesheetCache.cpp
layout/style/nsLayoutStylesheetCache.h
--- a/layout/style/nsLayoutStylesheetCache.cpp
+++ b/layout/style/nsLayoutStylesheetCache.cpp
@@ -287,16 +287,23 @@ nsLayoutStylesheetCache::EnsureGlobal()
   if (gStyleCache) return;
 
   gStyleCache = new nsLayoutStylesheetCache();
 
   gStyleCache->InitMemoryReporter();
 
   Preferences::AddBoolVarCache(&sNumberControlEnabled, NUMBER_CONTROL_PREF,
                                true);
+
+  // For each pref that controls a CSS feature that a UA style sheet depends
+  // on (such as a pref that enables a property that a UA style sheet uses),
+  // register DependentPrefChanged as a callback to ensure that the relevant
+  // style sheets will be re-parsed.
+  // Preferences::RegisterCallback(&DependentPrefChanged,
+  //                               "layout.css.example-pref.enabled");
 }
 
 void
 nsLayoutStylesheetCache::InitFromProfile()
 {
   nsCOMPtr<nsIXULRuntime> appInfo = do_GetService("@mozilla.org/xre/app-info;1");
   if (appInfo) {
     bool inSafeMode = false;
@@ -385,13 +392,36 @@ nsLayoutStylesheetCache::LoadSheet(nsIUR
   nsresult rv = gCSSLoader->LoadSheetSync(aURI, aEnableUnsafeRules, true,
                                           getter_AddRefs(aSheet));
   if (NS_FAILED(rv)) {
     ErrorLoadingBuiltinSheet(aURI,
       nsPrintfCString("LoadSheetSync failed with error %x", rv).get());
   }
 }
 
+/* static */ void
+nsLayoutStylesheetCache::InvalidateSheet(nsRefPtr<CSSStyleSheet>& aSheet)
+{
+  MOZ_ASSERT(gCSSLoader, "pref changed before we loaded a sheet?");
+
+  if (aSheet) {
+    gCSSLoader->ObsoleteSheet(aSheet->GetSheetURI());
+    aSheet = nullptr;
+  }
+}
+
+/* static */ void
+nsLayoutStylesheetCache::DependentPrefChanged(const char* aPref, void* aData)
+{
+  MOZ_ASSERT(gStyleCache, "pref changed after shutdown?");
+
+  // Cause any UA style sheets whose parsing depends on the value of prefs
+  // to be re-parsed by dropping the sheet from gCSSLoader's cache then
+  // setting our cached sheet pointer to null.  This will only work for sheets
+  // that are loaded lazily.
+  // InvalidateSheet(gStyleCache->mSomeLazilyLoadedSheet);
+}
+
 mozilla::StaticRefPtr<nsLayoutStylesheetCache>
 nsLayoutStylesheetCache::gStyleCache;
 
 mozilla::css::Loader*
 nsLayoutStylesheetCache::gCSSLoader = nullptr;
--- a/layout/style/nsLayoutStylesheetCache.h
+++ b/layout/style/nsLayoutStylesheetCache.h
@@ -62,16 +62,18 @@ private:
   void InitMemoryReporter();
   static void LoadSheetURL(const char* aURL,
                            nsRefPtr<mozilla::CSSStyleSheet>& aSheet,
                            bool aEnableUnsafeRules);
   static void LoadSheetFile(nsIFile* aFile,
                             nsRefPtr<mozilla::CSSStyleSheet>& aSheet);
   static void LoadSheet(nsIURI* aURI, nsRefPtr<mozilla::CSSStyleSheet>& aSheet,
                         bool aEnableUnsafeRules);
+  static void InvalidateSheet(nsRefPtr<mozilla::CSSStyleSheet>& aSheet);
+  static void DependentPrefChanged(const char* aPref, void* aData);
 
   static mozilla::StaticRefPtr<nsLayoutStylesheetCache> gStyleCache;
   static mozilla::css::Loader* gCSSLoader;
   nsRefPtr<mozilla::CSSStyleSheet> mCounterStylesSheet;
   nsRefPtr<mozilla::CSSStyleSheet> mFormsSheet;
   nsRefPtr<mozilla::CSSStyleSheet> mFullScreenOverrideSheet;
   nsRefPtr<mozilla::CSSStyleSheet> mHTMLSheet;
   nsRefPtr<mozilla::CSSStyleSheet> mMathMLSheet;