Bug 1514697 - Lazy loading for URL-Classifier features, r=dimi
authorAndrea Marchesini <amarchesini@mozilla.com>
Sat, 05 Jan 2019 09:11:06 +0100
changeset 509737 dfd83fc53601bd3966fa28628b501f59c43d0821
parent 509736 4e04d5fd29aa51a57662ff65fa64c9ed6e504b11
child 509738 ab2bdfc3132a2f88658b554ca12fae9fd2977928
push id10547
push userffxbld-merge
push dateMon, 21 Jan 2019 13:03:58 +0000
treeherdermozilla-beta@24ec1916bffe [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdimi
bugs1514697
milestone66.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 1514697 - Lazy loading for URL-Classifier features, r=dimi
layout/build/nsLayoutStatics.cpp
netwerk/url-classifier/UrlClassifierFeatureFactory.cpp
netwerk/url-classifier/UrlClassifierFeatureFactory.h
netwerk/url-classifier/UrlClassifierFeatureFlash.cpp
netwerk/url-classifier/UrlClassifierFeatureFlash.h
netwerk/url-classifier/UrlClassifierFeatureTrackingAnnotation.cpp
netwerk/url-classifier/UrlClassifierFeatureTrackingAnnotation.h
netwerk/url-classifier/UrlClassifierFeatureTrackingProtection.cpp
netwerk/url-classifier/UrlClassifierFeatureTrackingProtection.h
--- a/layout/build/nsLayoutStatics.cpp
+++ b/layout/build/nsLayoutStatics.cpp
@@ -290,18 +290,16 @@ nsresult nsLayoutStatics::Initialize() {
 
   mozilla::Fuzzyfox::Start();
 
   ClearSiteData::Initialize();
 
   // Reporting API.
   ReportingHeader::Initialize();
 
-  mozilla::net::UrlClassifierFeatureFactory::Initialize();
-
   return NS_OK;
 }
 
 void nsLayoutStatics::Shutdown() {
   // Don't need to shutdown nsWindowMemoryReporter, that will be done by the
   // memory reporter manager.
 
   if (XRE_IsParentProcess() || XRE_IsContentProcess()) {
--- a/netwerk/url-classifier/UrlClassifierFeatureFactory.cpp
+++ b/netwerk/url-classifier/UrlClassifierFeatureFactory.cpp
@@ -13,37 +13,26 @@
 #include "UrlClassifierFeatureTrackingAnnotation.h"
 #include "UrlClassifierFeatureCustomTables.h"
 
 #include "nsAppRunner.h"
 
 namespace mozilla {
 namespace net {
 
-/* static */ void UrlClassifierFeatureFactory::Initialize() {
-  // We want to expose Features only in the parent process.
-  if (!XRE_IsParentProcess()) {
-    return;
-  }
-
-  UrlClassifierFeatureFlash::Initialize();
-  UrlClassifierFeatureTrackingAnnotation::Initialize();
-  UrlClassifierFeatureTrackingProtection::Initialize();
-}
-
 /* static */ void UrlClassifierFeatureFactory::Shutdown() {
   // We want to expose Features only in the parent process.
   if (!XRE_IsParentProcess()) {
     return;
   }
 
-  UrlClassifierFeatureFlash::Shutdown();
+  UrlClassifierFeatureFlash::MaybeShutdown();
   UrlClassifierFeatureLoginReputation::MaybeShutdown();
-  UrlClassifierFeatureTrackingAnnotation::Shutdown();
-  UrlClassifierFeatureTrackingProtection::Shutdown();
+  UrlClassifierFeatureTrackingAnnotation::MaybeShutdown();
+  UrlClassifierFeatureTrackingProtection::MaybeShutdown();
 }
 
 /* static */ void UrlClassifierFeatureFactory::GetFeaturesFromChannel(
     nsIChannel* aChannel,
     nsTArray<nsCOMPtr<nsIUrlClassifierFeature>>& aFeatures) {
   MOZ_ASSERT(XRE_IsParentProcess());
   MOZ_ASSERT(aChannel);
 
@@ -75,16 +64,20 @@ namespace net {
 /* static */
 nsIUrlClassifierFeature*
 UrlClassifierFeatureFactory::GetFeatureLoginReputation() {
   return UrlClassifierFeatureLoginReputation::MaybeGetOrCreate();
 }
 
 /* static */ already_AddRefed<nsIUrlClassifierFeature>
 UrlClassifierFeatureFactory::GetFeatureByName(const nsACString& aName) {
+  if (!XRE_IsParentProcess()) {
+    return nullptr;
+  }
+
   nsCOMPtr<nsIUrlClassifierFeature> feature;
 
   // Tracking Protection
   feature = UrlClassifierFeatureTrackingProtection::GetIfNameMatches(aName);
   if (feature) {
     return feature.forget();
   }
 
--- a/netwerk/url-classifier/UrlClassifierFeatureFactory.h
+++ b/netwerk/url-classifier/UrlClassifierFeatureFactory.h
@@ -13,18 +13,16 @@
 class nsIChannel;
 class nsIUrlClassifierFeature;
 
 namespace mozilla {
 namespace net {
 
 class UrlClassifierFeatureFactory final {
  public:
-  static void Initialize();
-
   static void Shutdown();
 
   static void GetFeaturesFromChannel(
       nsIChannel* aChannel,
       nsTArray<nsCOMPtr<nsIUrlClassifierFeature>>& aFeatures);
 
   static nsIUrlClassifierFeature* GetFeatureLoginReputation();
 
--- a/netwerk/url-classifier/UrlClassifierFeatureFlash.cpp
+++ b/netwerk/url-classifier/UrlClassifierFeatureFlash.cpp
@@ -29,16 +29,18 @@ static FlashFeatures sFlashFeaturesMap[]
     {"flash-allow", "urlclassifier.flashAllowTable",
      "urlclassifier.flashAllowExceptTable", false,
      nsIHttpChannel::FlashPluginAllowed},
     {"flash-deny-subdoc", "urlclassifier.flashSubDocTable",
      "urlclassifier.flashSubDocExceptTable", true,
      nsIHttpChannel::FlashPluginDeniedInSubdocuments},
 };
 
+bool IsInitialized() { return !!sFlashFeaturesMap[0].mFeature; }
+
 }  // namespace
 
 UrlClassifierFeatureFlash::UrlClassifierFeatureFlash(uint32_t aId)
     : UrlClassifierFeatureBase(
           nsDependentCString(sFlashFeaturesMap[aId].mName),
           nsDependentCString(sFlashFeaturesMap[aId].mBlacklistPrefTables),
           nsDependentCString(sFlashFeaturesMap[aId].mWhitelistPrefTables),
           EmptyCString(),  // aPrefBlacklistHosts
@@ -48,27 +50,37 @@ UrlClassifierFeatureFlash::UrlClassifier
           EmptyCString())  // aPrefSkipHosts
       ,
       mFlashPluginState(sFlashFeaturesMap[aId].mFlashPluginState) {
   static_assert(nsIHttpChannel::FlashPluginDeniedInSubdocuments ==
                     nsIHttpChannel::FlashPluginLastValue,
                 "nsIHttpChannel::FlashPluginLastValue is out-of-sync!");
 }
 
-/* static */ void UrlClassifierFeatureFlash::Initialize() {
+/* static */ void UrlClassifierFeatureFlash::MaybeInitialize() {
+  MOZ_ASSERT(XRE_IsParentProcess());
+
+  if (IsInitialized()) {
+    return;
+  }
+
   uint32_t numFeatures =
       (sizeof(sFlashFeaturesMap) / sizeof(sFlashFeaturesMap[0]));
   for (uint32_t i = 0; i < numFeatures; ++i) {
     MOZ_ASSERT(!sFlashFeaturesMap[i].mFeature);
     sFlashFeaturesMap[i].mFeature = new UrlClassifierFeatureFlash(i);
     sFlashFeaturesMap[i].mFeature->InitializePreferences();
   }
 }
 
-/* static */ void UrlClassifierFeatureFlash::Shutdown() {
+/* static */ void UrlClassifierFeatureFlash::MaybeShutdown() {
+  if (!IsInitialized()) {
+    return;
+  }
+
   uint32_t numFeatures =
       (sizeof(sFlashFeaturesMap) / sizeof(sFlashFeaturesMap[0]));
   for (uint32_t i = 0; i < numFeatures; ++i) {
     MOZ_ASSERT(sFlashFeaturesMap[i].mFeature);
     sFlashFeaturesMap[i].mFeature->ShutdownPreferences();
     sFlashFeaturesMap[i].mFeature = nullptr;
   }
 }
@@ -95,29 +107,33 @@ UrlClassifierFeatureFlash::UrlClassifier
   // Only allow plugins for documents from an HTTP/HTTPS origin.
   if (StaticPrefs::plugins_http_https_only()) {
     nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aChannel);
     if (!httpChannel) {
       return;
     }
   }
 
+  MaybeInitialize();
+
   uint32_t numFeatures =
       (sizeof(sFlashFeaturesMap) / sizeof(sFlashFeaturesMap[0]));
   for (uint32_t i = 0; i < numFeatures; ++i) {
     MOZ_ASSERT(sFlashFeaturesMap[i].mFeature);
     if (!sFlashFeaturesMap[i].mSubdocumentOnly ||
         contentPolicyType == nsIContentPolicy::TYPE_SUBDOCUMENT) {
       aFeatures.AppendElement(sFlashFeaturesMap[i].mFeature);
     }
   }
 }
 
 /* static */ already_AddRefed<nsIUrlClassifierFeature>
 UrlClassifierFeatureFlash::GetIfNameMatches(const nsACString& aName) {
+  MaybeInitialize();
+
   uint32_t numFeatures =
       (sizeof(sFlashFeaturesMap) / sizeof(sFlashFeaturesMap[0]));
   for (uint32_t i = 0; i < numFeatures; ++i) {
     MOZ_ASSERT(sFlashFeaturesMap[i].mFeature);
     if (aName.Equals(sFlashFeaturesMap[i].mName)) {
       nsCOMPtr<nsIUrlClassifierFeature> self =
           sFlashFeaturesMap[i].mFeature.get();
       return self.forget();
--- a/netwerk/url-classifier/UrlClassifierFeatureFlash.h
+++ b/netwerk/url-classifier/UrlClassifierFeatureFlash.h
@@ -9,18 +9,17 @@
 
 #include "UrlClassifierFeatureBase.h"
 
 namespace mozilla {
 namespace net {
 
 class UrlClassifierFeatureFlash final : public UrlClassifierFeatureBase {
  public:
-  static void Initialize();
-  static void Shutdown();
+  static void MaybeShutdown();
 
   static void MaybeCreate(
       nsIChannel* aChannel,
       nsTArray<nsCOMPtr<nsIUrlClassifierFeature>>& aFeatures);
 
   static already_AddRefed<nsIUrlClassifierFeature> GetIfNameMatches(
       const nsACString& aName);
 
@@ -30,15 +29,17 @@ class UrlClassifierFeatureFlash final : 
 
   NS_IMETHOD GetURIByListType(nsIChannel* aChannel,
                               nsIUrlClassifierFeature::listType aListType,
                               nsIURI** aURI) override;
 
  private:
   explicit UrlClassifierFeatureFlash(uint32_t aId);
 
+  static void MaybeInitialize();
+
   nsIHttpChannel::FlashPluginState mFlashPluginState;
 };
 
 }  // namespace net
 }  // namespace mozilla
 
 #endif  // mozilla_UrlClassifierFeatureFlash_h
--- a/netwerk/url-classifier/UrlClassifierFeatureTrackingAnnotation.cpp
+++ b/netwerk/url-classifier/UrlClassifierFeatureTrackingAnnotation.cpp
@@ -115,63 +115,69 @@ UrlClassifierFeatureTrackingAnnotation::
           NS_LITERAL_CSTRING(URLCLASSIFIER_ANNOTATION_BLACKLIST),
           NS_LITERAL_CSTRING(URLCLASSIFIER_ANNOTATION_WHITELIST),
           NS_LITERAL_CSTRING(URLCLASSIFIER_ANNOTATION_BLACKLIST_TEST_ENTRIES),
           NS_LITERAL_CSTRING(URLCLASSIFIER_ANNOTATION_WHITELIST_TEST_ENTRIES),
           NS_LITERAL_CSTRING(TABLE_ANNOTATION_BLACKLIST_PREF),
           NS_LITERAL_CSTRING(TABLE_ANNOTATION_WHITELIST_PREF),
           NS_LITERAL_CSTRING(URLCLASSIFIER_TRACKING_ANNOTATION_SKIP_URLS)) {}
 
-/* static */ void UrlClassifierFeatureTrackingAnnotation::Initialize() {
-  UC_LOG(("UrlClassifierFeatureTrackingAnnotation: Initializing"));
-  MOZ_ASSERT(!gFeatureTrackingAnnotation);
+/* static */ void UrlClassifierFeatureTrackingAnnotation::MaybeInitialize() {
+  MOZ_ASSERT(XRE_IsParentProcess());
+  UC_LOG(("UrlClassifierFeatureTrackingAnnotation: MaybeInitialize"));
 
-  gFeatureTrackingAnnotation = new UrlClassifierFeatureTrackingAnnotation();
-  gFeatureTrackingAnnotation->InitializePreferences();
+  if (!gFeatureTrackingAnnotation) {
+    gFeatureTrackingAnnotation = new UrlClassifierFeatureTrackingAnnotation();
+    gFeatureTrackingAnnotation->InitializePreferences();
+  }
 }
 
-/* static */ void UrlClassifierFeatureTrackingAnnotation::Shutdown() {
-  UC_LOG(("UrlClassifierFeatureTrackingAnnotation: Shutdown"));
-  MOZ_ASSERT(gFeatureTrackingAnnotation);
+/* static */ void UrlClassifierFeatureTrackingAnnotation::MaybeShutdown() {
+  UC_LOG(("UrlClassifierFeatureTrackingAnnotation: MaybeShutdown"));
 
-  gFeatureTrackingAnnotation->ShutdownPreferences();
-  gFeatureTrackingAnnotation = nullptr;
+  if (gFeatureTrackingAnnotation) {
+    gFeatureTrackingAnnotation->ShutdownPreferences();
+    gFeatureTrackingAnnotation = nullptr;
+  }
 }
 
 /* static */ already_AddRefed<UrlClassifierFeatureTrackingAnnotation>
 UrlClassifierFeatureTrackingAnnotation::MaybeCreate(nsIChannel* aChannel) {
-  MOZ_ASSERT(gFeatureTrackingAnnotation);
   MOZ_ASSERT(aChannel);
 
   UC_LOG(("UrlClassifierFeatureTrackingAnnotation: MaybeCreate for channel %p",
           aChannel));
 
   if (!StaticPrefs::privacy_trackingprotection_annotate_channels()) {
     return nullptr;
   }
 
   if (!UrlClassifierCommon::ShouldEnableTrackingProtectionOrAnnotation(
           aChannel, AntiTrackingCommon::eTrackingAnnotations)) {
     return nullptr;
   }
 
+  MaybeInitialize();
+  MOZ_ASSERT(gFeatureTrackingAnnotation);
+
   RefPtr<UrlClassifierFeatureTrackingAnnotation> self =
       gFeatureTrackingAnnotation;
   return self.forget();
 }
 
 /* static */ already_AddRefed<nsIUrlClassifierFeature>
 UrlClassifierFeatureTrackingAnnotation::GetIfNameMatches(
     const nsACString& aName) {
-  MOZ_ASSERT(gFeatureTrackingAnnotation);
-
   if (!aName.EqualsLiteral(TRACKING_ANNOTATION_FEATURE_NAME)) {
     return nullptr;
   }
 
+  MaybeInitialize();
+  MOZ_ASSERT(gFeatureTrackingAnnotation);
+
   RefPtr<UrlClassifierFeatureTrackingAnnotation> self =
       gFeatureTrackingAnnotation;
   return self.forget();
 }
 
 NS_IMETHODIMP
 UrlClassifierFeatureTrackingAnnotation::ProcessChannel(nsIChannel* aChannel,
                                                        const nsACString& aList,
--- a/netwerk/url-classifier/UrlClassifierFeatureTrackingAnnotation.h
+++ b/netwerk/url-classifier/UrlClassifierFeatureTrackingAnnotation.h
@@ -12,33 +12,33 @@
 class nsIChannel;
 
 namespace mozilla {
 namespace net {
 
 class UrlClassifierFeatureTrackingAnnotation final
     : public UrlClassifierFeatureBase {
  public:
-  static void Initialize();
-
-  static void Shutdown();
+  static void MaybeShutdown();
 
   static already_AddRefed<UrlClassifierFeatureTrackingAnnotation> MaybeCreate(
       nsIChannel* aChannel);
 
   static already_AddRefed<nsIUrlClassifierFeature> GetIfNameMatches(
       const nsACString& aName);
 
   NS_IMETHOD ProcessChannel(nsIChannel* aChannel, const nsACString& aList,
                             bool* aShouldContinue) override;
 
   NS_IMETHOD GetURIByListType(nsIChannel* aChannel,
                               nsIUrlClassifierFeature::listType aListType,
                               nsIURI** aURI) override;
 
  private:
   UrlClassifierFeatureTrackingAnnotation();
+
+  static void MaybeInitialize();
 };
 
 }  // namespace net
 }  // namespace mozilla
 
 #endif  // mozilla_net_UrlClassifierFeatureTrackingAnnotation_h
--- a/netwerk/url-classifier/UrlClassifierFeatureTrackingProtection.cpp
+++ b/netwerk/url-classifier/UrlClassifierFeatureTrackingProtection.cpp
@@ -37,35 +37,37 @@ UrlClassifierFeatureTrackingProtection::
           NS_LITERAL_CSTRING(TRACKING_PROTECTION_FEATURE_NAME),
           NS_LITERAL_CSTRING(URLCLASSIFIER_TRACKING_BLACKLIST),
           NS_LITERAL_CSTRING(URLCLASSIFIER_TRACKING_WHITELIST),
           NS_LITERAL_CSTRING(URLCLASSIFIER_TRACKING_BLACKLIST_TEST_ENTRIES),
           NS_LITERAL_CSTRING(URLCLASSIFIER_TRACKING_WHITELIST_TEST_ENTRIES),
           NS_LITERAL_CSTRING(TABLE_TRACKING_BLACKLIST_PREF),
           NS_LITERAL_CSTRING(TABLE_TRACKING_WHITELIST_PREF), EmptyCString()) {}
 
-/* static */ void UrlClassifierFeatureTrackingProtection::Initialize() {
-  UC_LOG(("UrlClassifierFeatureTrackingProtection: Initializing"));
-  MOZ_ASSERT(!gFeatureTrackingProtection);
+/* static */ void UrlClassifierFeatureTrackingProtection::MaybeInitialize() {
+  MOZ_ASSERT(XRE_IsParentProcess());
+  UC_LOG(("UrlClassifierFeatureTrackingProtection: MaybeInitialize"));
 
-  gFeatureTrackingProtection = new UrlClassifierFeatureTrackingProtection();
-  gFeatureTrackingProtection->InitializePreferences();
+  if (!gFeatureTrackingProtection) {
+    gFeatureTrackingProtection = new UrlClassifierFeatureTrackingProtection();
+    gFeatureTrackingProtection->InitializePreferences();
+  }
 }
 
-/* static */ void UrlClassifierFeatureTrackingProtection::Shutdown() {
+/* static */ void UrlClassifierFeatureTrackingProtection::MaybeShutdown() {
   UC_LOG(("UrlClassifierFeatureTrackingProtection: Shutdown"));
-  MOZ_ASSERT(gFeatureTrackingProtection);
 
-  gFeatureTrackingProtection->ShutdownPreferences();
-  gFeatureTrackingProtection = nullptr;
+  if (gFeatureTrackingProtection) {
+    gFeatureTrackingProtection->ShutdownPreferences();
+    gFeatureTrackingProtection = nullptr;
+  }
 }
 
 /* static */ already_AddRefed<UrlClassifierFeatureTrackingProtection>
 UrlClassifierFeatureTrackingProtection::MaybeCreate(nsIChannel* aChannel) {
-  MOZ_ASSERT(gFeatureTrackingProtection);
   MOZ_ASSERT(aChannel);
 
   UC_LOG(("UrlClassifierFeatureTrackingProtection: MaybeCreate for channel %p",
           aChannel));
 
   nsCOMPtr<nsILoadContext> loadContext;
   NS_QueryNotificationCallbacks(aChannel, loadContext);
   if (!loadContext || !loadContext->UseTrackingProtection()) {
@@ -95,30 +97,34 @@ UrlClassifierFeatureTrackingProtection::
     return nullptr;
   }
 
   if (!UrlClassifierCommon::ShouldEnableTrackingProtectionOrAnnotation(
           aChannel, AntiTrackingCommon::eTrackingProtection)) {
     return nullptr;
   }
 
+  MaybeInitialize();
+  MOZ_ASSERT(gFeatureTrackingProtection);
+
   RefPtr<UrlClassifierFeatureTrackingProtection> self =
       gFeatureTrackingProtection;
   return self.forget();
 }
 
 /* static */ already_AddRefed<nsIUrlClassifierFeature>
 UrlClassifierFeatureTrackingProtection::GetIfNameMatches(
     const nsACString& aName) {
-  MOZ_ASSERT(gFeatureTrackingProtection);
-
   if (!aName.EqualsLiteral(TRACKING_PROTECTION_FEATURE_NAME)) {
     return nullptr;
   }
 
+  MaybeInitialize();
+  MOZ_ASSERT(gFeatureTrackingProtection);
+
   RefPtr<UrlClassifierFeatureTrackingProtection> self =
       gFeatureTrackingProtection;
   return self.forget();
 }
 
 NS_IMETHODIMP
 UrlClassifierFeatureTrackingProtection::ProcessChannel(nsIChannel* aChannel,
                                                        const nsACString& aList,
--- a/netwerk/url-classifier/UrlClassifierFeatureTrackingProtection.h
+++ b/netwerk/url-classifier/UrlClassifierFeatureTrackingProtection.h
@@ -12,33 +12,33 @@
 class nsIChannel;
 
 namespace mozilla {
 namespace net {
 
 class UrlClassifierFeatureTrackingProtection final
     : public UrlClassifierFeatureBase {
  public:
-  static void Initialize();
-
-  static void Shutdown();
+  static void MaybeShutdown();
 
   static already_AddRefed<UrlClassifierFeatureTrackingProtection> MaybeCreate(
       nsIChannel* aChannel);
 
   static already_AddRefed<nsIUrlClassifierFeature> GetIfNameMatches(
       const nsACString& aName);
 
   NS_IMETHOD ProcessChannel(nsIChannel* aChannel, const nsACString& aList,
                             bool* aShouldContinue) override;
 
   NS_IMETHOD GetURIByListType(nsIChannel* aChannel,
                               nsIUrlClassifierFeature::listType aListType,
                               nsIURI** aURI) override;
 
  private:
   UrlClassifierFeatureTrackingProtection();
+
+  static void MaybeInitialize();
 };
 
 }  // namespace net
 }  // namespace mozilla
 
 #endif  // mozilla_net_UrlClassifierFeatureTrackingProtection_h