Bug 1473631: Part 6 - Replace pref observers with callbacks in IDNService. r=njn
authorKris Maglione <maglione.k@gmail.com>
Thu, 05 Jul 2018 14:01:51 -0700
changeset 426850 cae48c105b0c6e218e856d3f9044386f43968099
parent 426849 d521f084e9918df2c748b35ad85d13309278f7ae
child 426851 6ad1956d8e153c9726cb35c838eb0fcefdda3866
push id105341
push usermaglione.k@gmail.com
push dateTue, 17 Jul 2018 05:38:58 +0000
treeherdermozilla-inbound@25ab77df8336 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnjn
bugs1473631
milestone63.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 1473631: Part 6 - Replace pref observers with callbacks in IDNService. r=njn MozReview-Commit-ID: FlFC2HxYo4v
netwerk/dns/nsIDNService.cpp
netwerk/dns/nsIDNService.h
--- a/netwerk/dns/nsIDNService.cpp
+++ b/netwerk/dns/nsIDNService.cpp
@@ -1,38 +1,38 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+#include "mozilla/Preferences.h"
 #include "nsIDNService.h"
 #include "nsReadableUtils.h"
 #include "nsCRT.h"
 #include "nsUnicharUtils.h"
 #include "nsUnicodeProperties.h"
 #include "nsUnicodeScriptCodes.h"
 #include "harfbuzz/hb.h"
 #include "nsIServiceManager.h"
-#include "nsIPrefService.h"
-#include "nsIPrefBranch.h"
 #include "nsIObserverService.h"
 #include "nsISupportsPrimitives.h"
 #include "punycode.h"
 
 // Currently we use the non-transitional processing option -- see
 // http://unicode.org/reports/tr46/
 // To switch to transitional processing, change the value of this flag
 // and kTransitionalProcessing in netwerk/test/unit/test_idna2008.js to true
 // (revert bug 1218179).
 const bool kIDNA2008_TransitionalProcessing = false;
 
 #include "ICUUtils.h"
 #include "unicode/uscript.h"
 
 using namespace mozilla::unicode;
+using mozilla::Preferences;
 
 //-----------------------------------------------------------------------------
 // RFC 1034 - 3.1. Name space specifications and terminology
 static const uint32_t kMaxDNSNodeLen = 63;
 // RFC 3490 - 5.   ACE prefix
 static const char kACEPrefix[] = "xn--";
 #define kACEPrefixLen 4
 
@@ -52,84 +52,70 @@ inline bool isOnlySafeChars(const nsStri
 
 //-----------------------------------------------------------------------------
 // nsIDNService
 //-----------------------------------------------------------------------------
 
 /* Implementation file */
 NS_IMPL_ISUPPORTS(nsIDNService,
                   nsIIDNService,
-                  nsIObserver,
                   nsISupportsWeakReference)
 
+static const char* gCallbackPrefs[] = {
+  NS_NET_PREF_IDNBLACKLIST,
+  NS_NET_PREF_SHOWPUNYCODE,
+  NS_NET_PREF_IDNRESTRICTION,
+  NS_NET_PREF_IDNUSEWHITELIST,
+  nullptr,
+};
+
 nsresult nsIDNService::Init()
 {
   MOZ_ASSERT(NS_IsMainThread());
   MutexAutoLock lock(mLock);
 
   nsCOMPtr<nsIPrefService> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
   if (prefs)
     prefs->GetBranch(NS_NET_PREF_IDNWHITELIST, getter_AddRefs(mIDNWhitelistPrefBranch));
 
-  nsCOMPtr<nsIPrefBranch> prefInternal(do_QueryInterface(prefs));
-  if (prefInternal) {
-    prefInternal->AddObserver(NS_NET_PREF_IDNBLACKLIST, this, true);
-    prefInternal->AddObserver(NS_NET_PREF_SHOWPUNYCODE, this, true);
-    prefInternal->AddObserver(NS_NET_PREF_IDNRESTRICTION, this, true);
-    prefInternal->AddObserver(NS_NET_PREF_IDNUSEWHITELIST, this, true);
-    prefsChanged(prefInternal, nullptr);
-  }
+  Preferences::RegisterPrefixCallbacks(PrefChanged, gCallbackPrefs, this);
+  prefsChanged(nullptr);
 
   return NS_OK;
 }
 
-NS_IMETHODIMP nsIDNService::Observe(nsISupports *aSubject,
-                                    const char *aTopic,
-                                    const char16_t *aData)
-{
-  MOZ_ASSERT(NS_IsMainThread());
-  MutexAutoLock lock(mLock);
-
-  if (!strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) {
-    nsCOMPtr<nsIPrefBranch> prefBranch( do_QueryInterface(aSubject) );
-    if (prefBranch)
-      prefsChanged(prefBranch, aData);
-  }
-  return NS_OK;
-}
-
-void nsIDNService::prefsChanged(nsIPrefBranch *prefBranch, const char16_t *pref)
+void nsIDNService::prefsChanged(const char *pref)
 {
   MOZ_ASSERT(NS_IsMainThread());
   mLock.AssertCurrentThreadOwns();
 
-  if (!pref || NS_LITERAL_STRING(NS_NET_PREF_IDNBLACKLIST).Equals(pref)) {
+  if (!pref || NS_LITERAL_CSTRING(NS_NET_PREF_IDNBLACKLIST).Equals(pref)) {
     nsAutoCString blacklist;
-    nsresult rv = prefBranch->GetStringPref(NS_NET_PREF_IDNBLACKLIST,
-                                            EmptyCString(), 0, blacklist);
+    nsresult rv = Preferences::GetCString(NS_NET_PREF_IDNBLACKLIST,
+                                          blacklist);
     if (NS_SUCCEEDED(rv)) {
       CopyUTF8toUTF16(blacklist, mIDNBlacklist);
     } else {
       mIDNBlacklist.Truncate();
     }
   }
-  if (!pref || NS_LITERAL_STRING(NS_NET_PREF_SHOWPUNYCODE).Equals(pref)) {
+  if (!pref || NS_LITERAL_CSTRING(NS_NET_PREF_SHOWPUNYCODE).Equals(pref)) {
     bool val;
-    if (NS_SUCCEEDED(prefBranch->GetBoolPref(NS_NET_PREF_SHOWPUNYCODE, &val)))
+    if (NS_SUCCEEDED(Preferences::GetBool(NS_NET_PREF_SHOWPUNYCODE, &val)))
       mShowPunycode = val;
   }
-  if (!pref || NS_LITERAL_STRING(NS_NET_PREF_IDNUSEWHITELIST).Equals(pref)) {
+  if (!pref || NS_LITERAL_CSTRING(NS_NET_PREF_IDNUSEWHITELIST).Equals(pref)) {
     bool val;
-    if (NS_SUCCEEDED(prefBranch->GetBoolPref(NS_NET_PREF_IDNUSEWHITELIST,
+    if (NS_SUCCEEDED(Preferences::GetBool(NS_NET_PREF_IDNUSEWHITELIST,
                                              &val)))
       mIDNUseWhitelist = val;
   }
-  if (!pref || NS_LITERAL_STRING(NS_NET_PREF_IDNRESTRICTION).Equals(pref)) {
+  if (!pref || NS_LITERAL_CSTRING(NS_NET_PREF_IDNRESTRICTION).Equals(pref)) {
     nsAutoCString profile;
-    if (NS_FAILED(prefBranch->GetCharPref(NS_NET_PREF_IDNRESTRICTION,
+    if (NS_FAILED(Preferences::GetCString(NS_NET_PREF_IDNRESTRICTION,
                                           profile))) {
       profile.Truncate();
     }
     if (profile.EqualsLiteral("moderate")) {
       mRestrictionProfile = eModeratelyRestrictiveProfile;
     } else if (profile.EqualsLiteral("high")) {
       mRestrictionProfile = eHighlyRestrictiveProfile;
     } else {
@@ -153,16 +139,18 @@ nsIDNService::nsIDNService()
   UErrorCode errorCode = U_ZERO_ERROR;
   mIDNA = uidna_openUTS46(IDNAOptions, &errorCode);
 }
 
 nsIDNService::~nsIDNService()
 {
   MOZ_ASSERT(NS_IsMainThread());
 
+  Preferences::UnregisterPrefixCallbacks(PrefChanged, gCallbackPrefs, this);
+
   uidna_close(mIDNA);
 }
 
 nsresult
 nsIDNService::IDNA2008ToUnicode(const nsACString& input, nsAString& output)
 {
   NS_ConvertUTF8toUTF16 inputStr(input);
   UIDNAInfo info = UIDNA_INFO_INITIALIZER;
--- a/netwerk/dns/nsIDNService.h
+++ b/netwerk/dns/nsIDNService.h
@@ -18,23 +18,21 @@
 
 class nsIPrefBranch;
 
 //-----------------------------------------------------------------------------
 // nsIDNService
 //-----------------------------------------------------------------------------
 
 class nsIDNService final : public nsIIDNService,
-                           public nsIObserver,
                            public nsSupportsWeakReference
 {
 public:
   NS_DECL_THREADSAFE_ISUPPORTS
   NS_DECL_NSIIDNSERVICE
-  NS_DECL_NSIOBSERVER
 
   nsIDNService();
 
   nsresult Init();
 
 protected:
   virtual ~nsIDNService();
 
@@ -93,17 +91,23 @@ private:
    *  punycode and some in UTF-8
    */
   nsresult UTF8toACE(const nsACString& input, nsACString& ace,
                      stringPrepFlag flag);
   nsresult ACEtoUTF8(const nsACString& input, nsACString& _retval,
                      stringPrepFlag flag);
 
   bool isInWhitelist(const nsACString &host);
-  void prefsChanged(nsIPrefBranch *prefBranch, const char16_t *pref);
+  void prefsChanged(const char *pref);
+
+  static void PrefChanged(const char* aPref, nsIDNService* aSelf)
+  {
+    mozilla::MutexAutoLock lock(aSelf->mLock);
+    aSelf->prefsChanged(aPref);
+  }
 
   /**
    * Determine whether a label is considered safe to display to the user
    * according to the algorithm defined in UTR 39 and the profile
    * selected in mRestrictionProfile.
    *
    * For the ASCII-only profile, returns false for all labels containing
    * non-ASCII characters.