Bug 1936996 - Use static pref getters instead of observer notification r=necko-reviewers,kershaw

Differential Revision: https://phabricator.services.mozilla.com/D232039
This commit is contained in:
Valentin Gosu
2024-12-20 11:27:36 +00:00
parent 30ddb074a0
commit d341105079
5 changed files with 13 additions and 72 deletions

View File

@@ -387,7 +387,7 @@ nsresult GetAddrInfo(const nsACString& aHost, uint16_t aAddressFamily,
host = aHost;
}
if (gNativeIsLocalhost) {
if (StaticPrefs::network_dns_native_is_localhost()) {
// pretend we use the given host but use IPv4 localhost instead!
host = "localhost"_ns;
aAddressFamily = PR_AF_INET;

View File

@@ -56,10 +56,8 @@ static const char kPrefDnsCacheExpiration[] = "network.dnsCacheExpiration";
static const char kPrefDnsCacheGrace[] =
"network.dnsCacheExpirationGracePeriod";
static const char kPrefIPv4OnlyDomains[] = "network.dns.ipv4OnlyDomains";
static const char kPrefBlockDotOnion[] = "network.dns.blockDotOnion";
static const char kPrefDnsLocalDomains[] = "network.dns.localDomains";
static const char kPrefDnsForceResolve[] = "network.dns.forceResolve";
static const char kPrefDnsOfflineLocalhost[] = "network.dns.offline-localhost";
static const char kPrefDnsNotifyResolution[] = "network.dns.notifyResolution";
static const char kPrefDnsMockHTTPSRRDomain[] =
"network.dns.mock_HTTPS_RR_domain";
@@ -779,17 +777,6 @@ void nsDNSService::ReadPrefs(const char* name) {
bool tmpbool;
// DNSservice prefs
if (!name || !strcmp(name, kPrefDnsOfflineLocalhost)) {
if (NS_SUCCEEDED(
Preferences::GetBool(kPrefDnsOfflineLocalhost, &tmpbool))) {
mOfflineLocalhost = tmpbool;
}
}
if (!name || !strcmp(name, kPrefBlockDotOnion)) {
if (NS_SUCCEEDED(Preferences::GetBool(kPrefBlockDotOnion, &tmpbool))) {
mBlockDotOnion = tmpbool;
}
}
if (!name || !strcmp(name, kPrefDnsNotifyResolution)) {
if (NS_SUCCEEDED(
Preferences::GetBool(kPrefDnsNotifyResolution, &tmpbool))) {
@@ -861,8 +848,6 @@ nsDNSService::Init() {
prefs->AddObserver(kPrefIPv4OnlyDomains, this, false);
prefs->AddObserver(kPrefDnsLocalDomains, this, false);
prefs->AddObserver(kPrefDnsForceResolve, this, false);
prefs->AddObserver(kPrefDnsOfflineLocalhost, this, false);
prefs->AddObserver(kPrefBlockDotOnion, this, false);
prefs->AddObserver(kPrefDnsNotifyResolution, this, false);
prefs->AddObserver(kPrefDnsMockHTTPSRRDomain, this, false);
AddPrefObserver(prefs);
@@ -940,7 +925,8 @@ nsresult nsDNSService::PreprocessHostname(bool aLocalDomain,
const nsACString& aInput,
nsACString& aACE) {
// Enforce RFC 7686
if (mBlockDotOnion && StringEndsWith(aInput, ".onion"_ns)) {
if (StaticPrefs::network_dns_blockDotOnion() &&
StringEndsWith(aInput, ".onion"_ns)) {
return NS_ERROR_UNKNOWN_HOST;
}
@@ -1026,8 +1012,8 @@ nsresult nsDNSService::AsyncResolveInternal(
return rv;
}
if (GetOffline() &&
(!mOfflineLocalhost || !hostname.LowerCaseEqualsASCII("localhost"))) {
if (GetOffline() && (!StaticPrefs::network_dns_offline_localhost() ||
!hostname.LowerCaseEqualsASCII("localhost"))) {
flags |= RESOLVE_OFFLINE;
}
@@ -1231,8 +1217,8 @@ nsresult nsDNSService::ResolveInternal(
return rv;
}
if (GetOffline() &&
(!mOfflineLocalhost || !hostname.LowerCaseEqualsASCII("localhost"))) {
if (GetOffline() && (!StaticPrefs::network_dns_offline_localhost() ||
!hostname.LowerCaseEqualsASCII("localhost"))) {
flags |= RESOLVE_OFFLINE;
}

View File

@@ -119,9 +119,7 @@ class nsDNSService final : public mozilla::net::DNSServiceBase,
nsCString mForceResolve;
nsCString mMockHTTPSRRDomain;
mozilla::Atomic<bool, mozilla::Relaxed> mHasMockHTTPSRRDomainSet{false};
bool mBlockDotOnion = false;
bool mNotifyResolution = false;
bool mOfflineLocalhost = false;
bool mForceResolveOn = false;
nsTHashSet<nsCString> mLocalDomains;
RefPtr<mozilla::net::TRRService> mTrrService;

View File

@@ -166,29 +166,8 @@ NS_IMPL_ISUPPORTS(DnsThreadListener, nsIThreadPoolListener)
//----------------------------------------------------------------------------
static const char kPrefGetTtl[] = "network.dns.get-ttl";
static const char kPrefNativeIsLocalhost[] = "network.dns.native-is-localhost";
static const char kPrefThreadIdleTime[] =
"network.dns.resolver-thread-extra-idle-time-seconds";
static bool sGetTtlEnabled = false;
mozilla::Atomic<bool, mozilla::Relaxed> gNativeIsLocalhost;
mozilla::Atomic<bool, mozilla::Relaxed> sNativeHTTPSSupported{false};
static void DnsPrefChanged(const char* aPref, void* aSelf) {
MOZ_ASSERT(NS_IsMainThread(),
"Should be getting pref changed notification on main thread!");
MOZ_ASSERT(aSelf);
if (!strcmp(aPref, kPrefGetTtl)) {
#ifdef DNSQUERY_AVAILABLE
sGetTtlEnabled = Preferences::GetBool(kPrefGetTtl);
#endif
} else if (!strcmp(aPref, kPrefNativeIsLocalhost)) {
gNativeIsLocalhost = Preferences::GetBool(kPrefNativeIsLocalhost);
}
}
NS_IMPL_ISUPPORTS0(nsHostResolver)
nsHostResolver::nsHostResolver()
@@ -212,21 +191,6 @@ nsresult nsHostResolver::Init() MOZ_NO_THREAD_SAFETY_ANALYSIS {
mShutdown = false;
mNCS = NetworkConnectivityService::GetSingleton();
// The preferences probably haven't been loaded from the disk yet, so we
// need to register a callback that will set up the experiment once they
// are. We also need to explicitly set a value for the props otherwise the
// callback won't be called.
{
DebugOnly<nsresult> rv = Preferences::RegisterCallbackAndCall(
&DnsPrefChanged, kPrefGetTtl, this);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"Could not register DNS TTL pref callback.");
rv = Preferences::RegisterCallbackAndCall(&DnsPrefChanged,
kPrefNativeIsLocalhost, this);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"Could not register DNS pref callback.");
}
#if defined(HAVE_RES_NINIT)
// We want to make sure the system is using the correct resolver settings,
// so we force it to reload those settings whenever we startup a subsequent
@@ -242,7 +206,8 @@ nsresult nsHostResolver::Init() MOZ_NO_THREAD_SAFETY_ANALYSIS {
// We can configure the threadpool to keep threads alive for a while after
// the last ThreadFunc task has been executed.
int32_t poolTimeoutSecs = Preferences::GetInt(kPrefThreadIdleTime, 60);
int32_t poolTimeoutSecs =
StaticPrefs::network_dns_resolver_thread_extra_idle_time_seconds();
uint32_t poolTimeoutMs;
if (poolTimeoutSecs < 0) {
// This means never shut down the idle threads
@@ -348,13 +313,6 @@ void nsHostResolver::FlushCache(bool aTrrToo) {
void nsHostResolver::Shutdown() {
LOG(("Shutting down host resolver.\n"));
{
DebugOnly<nsresult> rv =
Preferences::UnregisterCallback(&DnsPrefChanged, kPrefGetTtl, this);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"Could not unregister DNS TTL pref callback.");
}
LinkedList<RefPtr<nsHostRecord>> pendingQHigh, pendingQMed, pendingQLow,
evictionQ;
@@ -1330,7 +1288,8 @@ bool nsHostResolver::GetHostToLookup(nsHostRecord** result) {
// remove next record from Q; hand over owning reference. Check high, then
// med, then low
#define SET_GET_TTL(var, val) (var)->StoreGetTtl(sGetTtlEnabled && (val))
#define SET_GET_TTL(var, val) \
(var)->StoreGetTtl(StaticPrefs::network_dns_get_ttl() && (val))
RefPtr<nsHostRecord> rec = mQueue.Dequeue(true, lock);
if (rec) {
@@ -1400,7 +1359,7 @@ void nsHostResolver::PrepareRecordExpirationAddrRecord(
unsigned int grace = StaticPrefs::network_dnsCacheExpirationGracePeriod();
unsigned int ttl = StaticPrefs::network_dnsCacheExpiration();
if (sGetTtlEnabled || rec->addr_info->IsTRR()) {
if (StaticPrefs::network_dns_get_ttl() || rec->addr_info->IsTRR()) {
if (rec->addr_info && rec->addr_info->TTL() != AddrInfo::NO_TTL_DATA) {
ttl = rec->addr_info->TTL();
}
@@ -1733,7 +1692,7 @@ nsHostResolver::LookupStatus nsHostResolver::CompleteLookupLocked(
}
}
if (hasNativeResult && !mShutdown && !addrRec->LoadGetTtl() &&
!rec->mResolving && sGetTtlEnabled) {
!rec->mResolving && StaticPrefs::network_dns_get_ttl()) {
LOG(("Issuing second async lookup for TTL for host [%s].",
addrRec->host.get()));
addrRec->flags =

View File

@@ -53,8 +53,6 @@ static inline uint32_t MaxResolverThreads() {
(((x) == nsIDNSService::MODE_NATIVEONLY) || \
((x) == nsIDNSService::MODE_TRROFF))
extern mozilla::Atomic<bool, mozilla::Relaxed> gNativeIsLocalhost;
#define MAX_NON_PRIORITY_REQUESTS 150
class AHostResolver {