Bug 1473631: Part 2 - Replace pref observers with callbacks in STS. r?njn draft
authorKris Maglione <maglione.k@gmail.com>
Thu, 05 Jul 2018 13:15:52 -0700
changeset 818474 d2476f28372589bddc939ba5718a90907eeb7d33
parent 818473 1e8404ea2dbd50a05258589a005f39ccddbc9a89
child 818475 73aa1856bc7fa338b7ac49cf5f01c4477710fbde
push id116269
push usermaglione.k@gmail.com
push dateSat, 14 Jul 2018 02:18:47 +0000
reviewersnjn
bugs1473631
milestone63.0a1
Bug 1473631: Part 2 - Replace pref observers with callbacks in STS. r?njn MozReview-Commit-ID: BUiQqMA8eVs
netwerk/base/nsSocketTransportService2.cpp
netwerk/base/nsSocketTransportService2.h
--- a/netwerk/base/nsSocketTransportService2.cpp
+++ b/netwerk/base/nsSocketTransportService2.cpp
@@ -582,16 +582,35 @@ NS_IMPL_ISUPPORTS(nsSocketTransportServi
                   nsISocketTransportService,
                   nsIRoutedSocketTransportService,
                   nsIEventTarget,
                   nsIThreadObserver,
                   nsIRunnable,
                   nsPISocketTransportService,
                   nsIObserver)
 
+static const char* gCallbackPrefs[] = {
+    SEND_BUFFER_PREF,
+    KEEPALIVE_ENABLED_PREF,
+    KEEPALIVE_IDLE_TIME_PREF,
+    KEEPALIVE_RETRY_INTERVAL_PREF,
+    KEEPALIVE_PROBE_COUNT_PREF,
+    MAX_TIME_BETWEEN_TWO_POLLS,
+    TELEMETRY_PREF,
+    MAX_TIME_FOR_PR_CLOSE_DURING_SHUTDOWN,
+    POLLABLE_EVENT_TIMEOUT,
+    nullptr,
+};
+
+/* static */ void
+nsSocketTransportService::PrefCallback(const char* aPref, nsSocketTransportService* aSelf)
+{
+    aSelf->UpdatePrefs();
+}
+
 // called from main thread only
 NS_IMETHODIMP
 nsSocketTransportService::Init()
 {
     if (!NS_IsMainThread()) {
         NS_ERROR("wrong thread");
         return NS_ERROR_UNEXPECTED;
     }
@@ -607,28 +626,17 @@ nsSocketTransportService::Init()
     if (NS_FAILED(rv)) return rv;
 
     {
         MutexAutoLock lock(mLock);
         // Install our mThread, protecting against concurrent readers
         thread.swap(mThread);
     }
 
-    nsCOMPtr<nsIPrefBranch> tmpPrefService = do_GetService(NS_PREFSERVICE_CONTRACTID);
-    if (tmpPrefService) {
-        tmpPrefService->AddObserver(SEND_BUFFER_PREF, this, false);
-        tmpPrefService->AddObserver(KEEPALIVE_ENABLED_PREF, this, false);
-        tmpPrefService->AddObserver(KEEPALIVE_IDLE_TIME_PREF, this, false);
-        tmpPrefService->AddObserver(KEEPALIVE_RETRY_INTERVAL_PREF, this, false);
-        tmpPrefService->AddObserver(KEEPALIVE_PROBE_COUNT_PREF, this, false);
-        tmpPrefService->AddObserver(MAX_TIME_BETWEEN_TWO_POLLS, this, false);
-        tmpPrefService->AddObserver(TELEMETRY_PREF, this, false);
-        tmpPrefService->AddObserver(MAX_TIME_FOR_PR_CLOSE_DURING_SHUTDOWN, this, false);
-        tmpPrefService->AddObserver(POLLABLE_EVENT_TIMEOUT, this, false);
-    }
+    Preferences::RegisterCallbacks(PrefCallback, gCallbackPrefs, this);
     UpdatePrefs();
 
     nsCOMPtr<nsIObserverService> obsSvc = services::GetObserverService();
     if (obsSvc) {
         obsSvc->AddObserver(this, "profile-initial-state", false);
         obsSvc->AddObserver(this, "last-pb-context-exited", false);
         obsSvc->AddObserver(this, NS_WIDGET_SLEEP_OBSERVER_TOPIC, true);
         obsSvc->AddObserver(this, NS_WIDGET_WAKE_OBSERVER_TOPIC, true);
@@ -686,19 +694,17 @@ nsSocketTransportService::ShutdownThread
     mThread->Shutdown();
     {
         MutexAutoLock lock(mLock);
         // Drop our reference to mThread and make sure that any concurrent
         // readers are excluded
         mThread = nullptr;
     }
 
-    nsCOMPtr<nsIPrefBranch> tmpPrefService = do_GetService(NS_PREFSERVICE_CONTRACTID);
-    if (tmpPrefService)
-        tmpPrefService->RemoveObserver(SEND_BUFFER_PREF, this);
+    Preferences::UnregisterCallbacks(PrefCallback, gCallbackPrefs, this);
 
     nsCOMPtr<nsIObserverService> obsSvc = services::GetObserverService();
     if (obsSvc) {
         obsSvc->RemoveObserver(this, "profile-initial-state");
         obsSvc->RemoveObserver(this, "last-pb-context-exited");
         obsSvc->RemoveObserver(this, NS_WIDGET_SLEEP_OBSERVER_TOPIC);
         obsSvc->RemoveObserver(this, NS_WIDGET_WAKE_OBSERVER_TOPIC);
         obsSvc->RemoveObserver(this, "xpcom-shutdown-threads");
@@ -1434,21 +1440,16 @@ nsSocketTransportService::NotifyKeepaliv
 
 NS_IMETHODIMP
 nsSocketTransportService::Observe(nsISupports *subject,
                                   const char *topic,
                                   const char16_t *data)
 {
     SOCKET_LOG(("nsSocketTransportService::Observe topic=%s", topic));
 
-    if (!strcmp(topic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID)) {
-        UpdatePrefs();
-        return NS_OK;
-    }
-
     if (!strcmp(topic, "profile-initial-state")) {
         if (!Preferences::GetBool(IO_ACTIVITY_ENABLED_PREF, false)) {
           return NS_OK;
         }
         return net::IOActivityMonitor::Init();
     }
 
     if (!strcmp(topic, "last-pb-context-exited")) {
--- a/netwerk/base/nsSocketTransportService2.h
+++ b/netwerk/base/nsSocketTransportService2.h
@@ -232,16 +232,17 @@ private:
 
     //-------------------------------------------------------------------------
     // pending socket queue - see NotifyWhenCanAttachSocket
     //-------------------------------------------------------------------------
     AutoCleanLinkedList<LinkedRunnableEvent> mPendingSocketQueue;
 
     // Preference Monitor for SendBufferSize and Keepalive prefs.
     nsresult    UpdatePrefs();
+    static void PrefCallback(const char* aPref, nsSocketTransportService* aSelf);
     void        UpdateSendBufferPref(nsIPrefBranch *);
     int32_t     mSendBufferSize;
     // Number of seconds of connection is idle before first keepalive ping.
     int32_t     mKeepaliveIdleTimeS;
     // Number of seconds between retries should keepalive pings fail.
     int32_t     mKeepaliveRetryIntervalS;
     // Number of keepalive probes to send.
     int32_t     mKeepaliveProbeCount;