From a34f73bd67d4213487858c6ab2b82f523236b055 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Fri, 29 Aug 2014 01:11:00 -0400 Subject: [PATCH] Bug 939318 - Introduce the "network.notify.changed" prefs. r=mcmanus Makes it possible to disable 'changed' events. Disable the events in test_ping_aboutnetworking.js and in test_proxy-failover_passing.js --- modules/libpref/init/all.js | 4 ++++ netwerk/base/src/nsIOService.cpp | 13 ++++++++++++- netwerk/base/src/nsIOService.h | 1 + netwerk/system/win32/nsNotifyAddrListener.cpp | 15 +++++++++++++-- netwerk/system/win32/nsNotifyAddrListener.h | 3 +++ netwerk/test/unit/test_ping_aboutnetworking.js | 6 ++++++ netwerk/test/unit/test_proxy-failover_passing.js | 3 +++ 7 files changed, 42 insertions(+), 3 deletions(-) diff --git a/modules/libpref/init/all.js b/modules/libpref/init/all.js index 7e1bd3a4366e..2c8f68b3fa93 100644 --- a/modules/libpref/init/all.js +++ b/modules/libpref/init/all.js @@ -990,6 +990,10 @@ pref("security.fileuri.strict_origin_policy", true); // the results pref("network.allow-experiments", true); +// Allow the network changed event to get sent when a network topology or +// setup change is noticed while running. +pref("network.notify.changed", true); + // Transmit UDP busy-work to the LAN when anticipating low latency // network reads and on wifi to mitigate 802.11 Power Save Polling delays pref("network.tickle-wifi.enabled", false); diff --git a/netwerk/base/src/nsIOService.cpp b/netwerk/base/src/nsIOService.cpp index 6151aac6300e..35f4976fa586 100644 --- a/netwerk/base/src/nsIOService.cpp +++ b/netwerk/base/src/nsIOService.cpp @@ -55,6 +55,7 @@ using namespace mozilla; // but the old names are still used to preserve backward compatibility. #define NECKO_BUFFER_CACHE_COUNT_PREF "network.buffer.cache.count" #define NECKO_BUFFER_CACHE_SIZE_PREF "network.buffer.cache.size" +#define NETWORK_NOTIFY_CHANGED_PREF "network.notify.changed" #define MAX_RECURSION_COUNT 50 @@ -149,6 +150,7 @@ nsIOService::nsIOService() , mNetworkLinkServiceInitialized(false) , mChannelEventSinks(NS_CHANNEL_EVENT_SINK_CATEGORY) , mAutoDialEnabled(false) + , mNetworkNotifyChanged(true) { } @@ -188,6 +190,7 @@ nsIOService::Init() prefBranch->AddObserver(MANAGE_OFFLINE_STATUS_PREF, this, true); prefBranch->AddObserver(NECKO_BUFFER_CACHE_COUNT_PREF, this, true); prefBranch->AddObserver(NECKO_BUFFER_CACHE_SIZE_PREF, this, true); + prefBranch->AddObserver(NETWORK_NOTIFY_CHANGED_PREF, this, true); PrefsChanged(prefBranch); } @@ -852,6 +855,14 @@ nsIOService::PrefsChanged(nsIPrefBranch *prefs, const char *pref) gDefaultSegmentSize = size; NS_WARN_IF_FALSE( (!(size & (size - 1))) , "network segment size is not a power of 2!"); } + + if (!pref || strcmp(pref, NETWORK_NOTIFY_CHANGED_PREF) == 0) { + bool allow; + nsresult rv = prefs->GetBoolPref(NETWORK_NOTIFY_CHANGED_PREF, &allow); + if (NS_SUCCEEDED(rv)) { + mNetworkNotifyChanged = allow; + } + } } void @@ -958,7 +969,7 @@ nsIOService::Observe(nsISupports *subject, NS_ASSERTION(observerService, "The observer service should not be null"); - if (observerService) { + if (observerService && mNetworkNotifyChanged) { (void)observerService-> NotifyObservers(nullptr, NS_NETWORK_LINK_TOPIC, diff --git a/netwerk/base/src/nsIOService.h b/netwerk/base/src/nsIOService.h index f442fb52a203..2839d9b057d5 100644 --- a/netwerk/base/src/nsIOService.h +++ b/netwerk/base/src/nsIOService.h @@ -129,6 +129,7 @@ private: nsTArray mRestrictedPortList; bool mAutoDialEnabled; + bool mNetworkNotifyChanged; public: // Used for all default buffer sizes that necko allocates. static uint32_t gDefaultSegmentSize; diff --git a/netwerk/system/win32/nsNotifyAddrListener.cpp b/netwerk/system/win32/nsNotifyAddrListener.cpp index feef9fb93678..f0e465fab090 100644 --- a/netwerk/system/win32/nsNotifyAddrListener.cpp +++ b/netwerk/system/win32/nsNotifyAddrListener.cpp @@ -27,10 +27,13 @@ #include "nsAutoPtr.h" #include "mozilla/Services.h" #include "nsCRT.h" +#include "mozilla/Preferences.h" #include #include +using namespace mozilla; + static HMODULE sNetshell; static decltype(NcFreeNetconProperties)* sNcFreeNetconProperties; @@ -38,6 +41,8 @@ static HMODULE sIphlpapi; static decltype(NotifyIpInterfaceChange)* sNotifyIpInterfaceChange; static decltype(CancelMibChangeNotify2)* sCancelMibChangeNotify2; +#define NETWORK_NOTIFY_CHANGED_PREF "network.notify.changed" + static void InitIphlpapi(void) { if (!sIphlpapi) { @@ -90,6 +95,7 @@ nsNotifyAddrListener::nsNotifyAddrListener() , mStatusKnown(false) , mCheckAttempted(false) , mShutdownEvent(nullptr) + , mAllowChangedEvent(true) { InitIphlpapi(); } @@ -211,6 +217,9 @@ nsNotifyAddrListener::Init(void) false); NS_ENSURE_SUCCESS(rv, rv); + Preferences::AddBoolVarCache(&mAllowChangedEvent, + NETWORK_NOTIFY_CHANGED_PREF, true); + mShutdownEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr); NS_ENSURE_TRUE(mShutdownEvent, NS_ERROR_OUT_OF_MEMORY); @@ -494,8 +503,10 @@ nsNotifyAddrListener::CheckLinkStatus(void) if (mLinkUp && (prevCsum != mIPInterfaceChecksum)) { // Network is online. Topology has changed. Always send CHANGED - // before UP. - SendEvent(NS_NETWORK_LINK_DATA_CHANGED); + // before UP - if allowed to. + if (mAllowChangedEvent) { + SendEvent(NS_NETWORK_LINK_DATA_CHANGED); + } } if (prevLinkUp != mLinkUp) { // UP/DOWN status changed, send appropriate UP/DOWN event diff --git a/netwerk/system/win32/nsNotifyAddrListener.h b/netwerk/system/win32/nsNotifyAddrListener.h index 22feb5354083..660b01398751 100644 --- a/netwerk/system/win32/nsNotifyAddrListener.h +++ b/netwerk/system/win32/nsNotifyAddrListener.h @@ -65,6 +65,9 @@ private: // This is a checksum of various meta data for all network interfaces // considered UP at last check. ULONG mIPInterfaceChecksum; + + // Network changed events are enabled + bool mAllowChangedEvent; }; #endif /* NSNOTIFYADDRLISTENER_H_ */ diff --git a/netwerk/test/unit/test_ping_aboutnetworking.js b/netwerk/test/unit/test_ping_aboutnetworking.js index 903136889082..69bdee8b65b4 100644 --- a/netwerk/test/unit/test_ping_aboutnetworking.js +++ b/netwerk/test/unit/test_ping_aboutnetworking.js @@ -42,6 +42,12 @@ function test_sockets(serverSocket) { } function run_test() { + var ps = Cc["@mozilla.org/preferences-service;1"] + .getService(Ci.nsIPrefBranch); + // disable network changed events to avoid the the risk of having the dns + // cache getting flushed behind our back + ps.setBoolPref("network.notify.changed", false); + let serverSocket = Components.classes["@mozilla.org/network/server-socket;1"] .createInstance(Ci.nsIServerSocket); serverSocket.init(-1, true, -1); diff --git a/netwerk/test/unit/test_proxy-failover_passing.js b/netwerk/test/unit/test_proxy-failover_passing.js index 26c4cbf6cefb..c07f0959c105 100644 --- a/netwerk/test/unit/test_proxy-failover_passing.js +++ b/netwerk/test/unit/test_proxy-failover_passing.js @@ -36,6 +36,9 @@ function run_test() "function FindProxyForURL(url, host) {return 'PROXY a_non_existent_domain_x7x6c572v:80; PROXY localhost:" + httpServer.identity.primaryPort + "';}" ); + // disable network changed events to avoid the the risk of having the + // proxyservice reset in the middle + prefserv.setBoolPref("network.notify.changed", false); var chan = make_channel("http://localhost:" + httpServer.identity.primaryPort + "/content");