Bug 1620758 - Don't call AltSvcMapping::ProcessHeader OMT r=valentin
authorKershaw Chang <kershaw@mozilla.com>
Sun, 08 Mar 2020 13:52:07 +0000
changeset 517476 b3ad5d93c16edf253577df49111cce4e5125401b
parent 517475 25beb671c14a2cf8f3e4f7e7d74755353b268acf
child 517477 7aa696aefc462bd6a6b50170f04bdad47a0a7e27
push id37193
push usershindli@mozilla.com
push dateSun, 08 Mar 2020 21:41:29 +0000
treeherdermozilla-central@7aa696aefc46 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersvalentin
bugs1620758
milestone75.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1620758 - Don't call AltSvcMapping::ProcessHeader OMT r=valentin This patch contains two changes. 1. `AltSvcMapping::ProcessHeader` is not thread-safe, so dispatching an async task to do this. 2. `TRRService::Init` is called earlier before the user profile is loaded. If `network.trr.fetch_off_main_thread` is default to false and changed to true by users, TRR background thread would not be created. Differential Revision: https://phabricator.services.mozilla.com/D65890
netwerk/dns/TRRService.cpp
netwerk/protocol/http/TRRServiceChannel.cpp
--- a/netwerk/dns/TRRService.cpp
+++ b/netwerk/dns/TRRService.cpp
@@ -104,26 +104,23 @@ nsresult TRRService::Init() {
   ReadPrefs(nullptr);
 
   gTRRService = this;
 
   nsCOMPtr<nsINetworkLinkService> nls =
       do_GetService(NS_NETWORK_LINK_SERVICE_CONTRACTID);
   RebuildSuffixList(nls);
 
-  if (StaticPrefs::network_trr_fetch_off_main_thread()) {
-    nsCOMPtr<nsIThread> thread;
-    if (NS_FAILED(
-            NS_NewNamedThread("TRR Background", getter_AddRefs(thread)))) {
-      NS_WARNING("NS_NewNamedThread failed!");
-      return NS_ERROR_FAILURE;
-    }
+  nsCOMPtr<nsIThread> thread;
+  if (NS_FAILED(NS_NewNamedThread("TRR Background", getter_AddRefs(thread)))) {
+    NS_WARNING("NS_NewNamedThread failed!");
+    return NS_ERROR_FAILURE;
+  }
 
-    sTRRBackgroundThread = thread;
-  }
+  sTRRBackgroundThread = thread;
 
   LOG(("Initialized TRRService\n"));
   return NS_OK;
 }
 
 void TRRService::GetParentalControlEnabledInternal() {
   nsCOMPtr<nsIParentalControlsService> pc =
       do_CreateInstance("@mozilla.org/parental-controls-service;1");
--- a/netwerk/protocol/http/TRRServiceChannel.cpp
+++ b/netwerk/protocol/http/TRRServiceChannel.cpp
@@ -794,53 +794,68 @@ void TRRServiceChannel::ProcessAltServic
   if (!mAllowAltSvc) {  // per channel opt out
     return;
   }
 
   if (!gHttpHandler->AllowAltSvc() || (mCaps & NS_HTTP_DISALLOW_SPDY)) {
     return;
   }
 
-  nsAutoCString scheme;
+  nsCString scheme;
   mURI->GetScheme(scheme);
   bool isHttp = scheme.EqualsLiteral("http");
   if (!isHttp && !scheme.EqualsLiteral("https")) {
     return;
   }
 
-  nsAutoCString altSvc;
+  nsCString altSvc;
   Unused << mResponseHead->GetHeader(nsHttp::Alternate_Service, altSvc);
   if (altSvc.IsEmpty()) {
     return;
   }
 
   if (!nsHttp::IsReasonableHeaderValue(altSvc)) {
     LOG(("Alt-Svc Response Header seems unreasonable - skipping\n"));
     return;
   }
 
-  nsAutoCString originHost;
+  nsCString originHost;
   int32_t originPort = 80;
   mURI->GetPort(&originPort);
   if (NS_FAILED(mURI->GetAsciiHost(originHost))) {
     return;
   }
 
   nsCOMPtr<nsIInterfaceRequestor> callbacks;
   nsCOMPtr<nsProxyInfo> proxyInfo;
   NS_NewNotificationCallbacksAggregation(mCallbacks, mLoadGroup,
                                          getter_AddRefs(callbacks));
   if (mProxyInfo) {
     proxyInfo = do_QueryInterface(mProxyInfo);
   }
 
-  AltSvcMapping::ProcessHeader(
-      altSvc, scheme, originHost, originPort, mUsername, GetTopWindowOrigin(),
-      mPrivateBrowsing, IsIsolated(), callbacks, proxyInfo,
-      mCaps & NS_HTTP_DISALLOW_SPDY, OriginAttributes());
+  nsCString topWindowOrigin = GetTopWindowOrigin();
+  bool isIsolated = IsIsolated();
+  auto processHeaderTask = [altSvc, scheme, originHost, originPort,
+                            userName(mUsername), topWindowOrigin,
+                            privateBrowsing(mPrivateBrowsing), isIsolated,
+                            callbacks, proxyInfo, caps(mCaps)]() {
+    AltSvcMapping::ProcessHeader(
+        altSvc, scheme, originHost, originPort, userName, topWindowOrigin,
+        privateBrowsing, isIsolated, callbacks, proxyInfo,
+        caps & NS_HTTP_DISALLOW_SPDY, OriginAttributes());
+  };
+
+  if (NS_IsMainThread()) {
+    processHeaderTask();
+    return;
+  }
+
+  NS_DispatchToMainThread(NS_NewRunnableFunction(
+      "TRRServiceChannel::ProcessAltService", std::move(processHeaderTask)));
 }
 
 NS_IMETHODIMP
 TRRServiceChannel::OnStartRequest(nsIRequest* request) {
   LOG(("TRRServiceChannel::OnStartRequest [this=%p request=%p status=%" PRIx32
        "]\n",
        this, request, static_cast<uint32_t>(static_cast<nsresult>(mStatus))));