Bug 1602832 - P5: Make sure ChildDNSService is created on main thread r=dragana

Differential Revision: https://phabricator.services.mozilla.com/D72611
This commit is contained in:
Kershaw Chang 2020-05-26 08:35:47 +00:00
parent 4b58c9ca0d
commit 3412834247

View File

@ -13,6 +13,7 @@
#include "nsQueryObject.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/SyncRunnable.h"
#include "mozilla/net/NeckoChild.h"
#include "mozilla/net/DNSListenerProxy.h"
#include "mozilla/net/TRRServiceParent.h"
@ -35,8 +36,27 @@ already_AddRefed<ChildDNSService> ChildDNSService::GetSingleton() {
XRE_IsContentProcess() || XRE_IsSocketProcess());
if (!gChildDNSService) {
gChildDNSService = new ChildDNSService();
ClearOnShutdown(&gChildDNSService);
auto initTask = []() {
gChildDNSService = new ChildDNSService();
ClearOnShutdown(&gChildDNSService);
};
// For normal cases, DNS service should be initialized in nsHttpHandler. For
// some xpcshell tests, nsHttpHandler is not used at all, so the best we use
// SyncRunnable to make sure DNS service is initialized on main thread.
if (!NS_IsMainThread()) {
// Forward to the main thread synchronously.
RefPtr<nsIThread> mainThread = do_GetMainThread();
if (!mainThread) {
return nullptr;
}
SyncRunnable::DispatchToThread(
mainThread, new SyncRunnable(NS_NewRunnableFunction(
"ChildDNSService::GetSingleton", initTask)));
} else {
initTask();
}
}
return do_AddRef(gChildDNSService);