diff --git a/netwerk/system/mac/nsNetworkLinkService.h b/netwerk/system/mac/nsNetworkLinkService.h index 57c0df963ffb..76a128b6757e 100644 --- a/netwerk/system/mac/nsNetworkLinkService.h +++ b/netwerk/system/mac/nsNetworkLinkService.h @@ -7,6 +7,7 @@ #include "nsINetworkLinkService.h" #include "nsIObserver.h" +#include "mozilla/Mutex.h" #include #include @@ -44,6 +45,8 @@ class nsNetworkLinkService : public nsINetworkLinkService, public nsIObserver { static void IPConfigChanged(SCDynamicStoreRef store, CFArrayRef changedKeys, void* info); void calculateNetworkId(void); + + mozilla::Mutex mMutex; nsCString mNetworkId; }; diff --git a/netwerk/system/mac/nsNetworkLinkService.mm b/netwerk/system/mac/nsNetworkLinkService.mm index 221f340600ec..ebc1ecbebb00 100644 --- a/netwerk/system/mac/nsNetworkLinkService.mm +++ b/netwerk/system/mac/nsNetworkLinkService.mm @@ -26,6 +26,7 @@ #include "mozilla/Base64.h" #include "mozilla/Telemetry.h" #include "nsNetworkLinkService.h" +#include "MainThreadUtils.h" #include "../../base/IPv6Utils.h" #import @@ -74,7 +75,8 @@ nsNetworkLinkService::nsNetworkLinkService() mReachability(nullptr), mCFRunLoop(nullptr), mRunLoopSource(nullptr), - mStoreRef(nullptr) {} + mStoreRef(nullptr), + mMutex("nsNetworkLinkService::mMutex") {} nsNetworkLinkService::~nsNetworkLinkService() = default; @@ -99,6 +101,13 @@ nsNetworkLinkService::GetLinkType(uint32_t* aLinkType) { return NS_OK; } +NS_IMETHODIMP +nsNetworkLinkService::GetNetworkID(nsACString& aNetworkID) { + MutexAutoLock lock(mMutex); + aNetworkID = mNetworkId; + return NS_OK; +} + #ifndef SA_SIZE # define SA_SIZE(sa) \ ((!(sa) || ((struct sockaddr*)(sa))->sa_len == 0) \ @@ -315,6 +324,7 @@ static bool ipv6NetworkId(SHA1Sum* sha1) { } void nsNetworkLinkService::calculateNetworkId(void) { + MOZ_ASSERT(!NS_IsMainThread(), "Should not be called on the main thread"); SHA1Sum sha1; bool found4 = ipv4NetworkId(&sha1); bool found6 = ipv6NetworkId(&sha1); @@ -331,6 +341,7 @@ void nsNetworkLinkService::calculateNetworkId(void) { nsresult rv = Base64Encode(newString, output); MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv)); LOG(("networkid: id %s\n", output.get())); + MutexAutoLock lock(mMutex); if (mNetworkId != output) { // new id if (found4 && !found6) { @@ -343,10 +354,14 @@ void nsNetworkLinkService::calculateNetworkId(void) { mNetworkId = output; } else { // same id + LOG(("Same network id")); Telemetry::Accumulate(Telemetry::NETWORK_ID2, 2); } } else { // no id + LOG(("No network id")); + MutexAutoLock lock(mMutex); + mNetworkId.Truncate(); Telemetry::Accumulate(Telemetry::NETWORK_ID2, 0); } }