diff --git a/netwerk/protocol/http/HttpBaseChannel.cpp b/netwerk/protocol/http/HttpBaseChannel.cpp index 7d710a2f58b9..3cbda7ed2748 100644 --- a/netwerk/protocol/http/HttpBaseChannel.cpp +++ b/netwerk/protocol/http/HttpBaseChannel.cpp @@ -37,6 +37,7 @@ #include "nsIObserverService.h" #include "nsProxyRelease.h" #include "nsPIDOMWindow.h" +#include "nsIDocShell.h" #include "nsPerformance.h" #include "nsINetworkInterceptController.h" #include "mozIThirdPartyUtil.h" @@ -253,7 +254,7 @@ NS_IMETHODIMP HttpBaseChannel::SetLoadGroup(nsILoadGroup *aLoadGroup) { MOZ_ASSERT(NS_IsMainThread(), "Should only be called on the main thread."); - + if (!CanSetLoadGroup(aLoadGroup)) { return NS_ERROR_FAILURE; } @@ -295,6 +296,47 @@ HttpBaseChannel::SetLoadFlags(nsLoadFlags aLoadFlags) return NS_OK; } +NS_IMETHODIMP +HttpBaseChannel::SetDocshellUserAgentOverride() +{ + // This sets the docshell specific user agent override, it will be overwritten + // by UserAgentOverrides.jsm if site-specific user agent overrides are set. + nsresult rv; + nsCOMPtr loadContext; + NS_QueryNotificationCallbacks(this, loadContext); + if (!loadContext) { + return NS_OK; + } + + nsCOMPtr domWindow; + loadContext->GetAssociatedWindow(getter_AddRefs(domWindow)); + if (!domWindow) { + return NS_OK; + } + + nsCOMPtr pDomWindow = do_QueryInterface(domWindow); + if (!pDomWindow) { + return NS_OK; + } + + nsIDocShell* docshell = pDomWindow->GetDocShell(); + if (!docshell) { + return NS_OK; + } + + nsString customUserAgent; + docshell->GetCustomUserAgent(customUserAgent); + if (customUserAgent.IsEmpty()) { + return NS_OK; + } + + NS_ConvertUTF16toUTF8 utf8CustomUserAgent(customUserAgent); + rv = SetRequestHeader(NS_LITERAL_CSTRING("User-Agent"), utf8CustomUserAgent, false); + if (NS_FAILED(rv)) return rv; + + return NS_OK; +} + //----------------------------------------------------------------------------- // HttpBaseChannel::nsIChannel //----------------------------------------------------------------------------- @@ -369,7 +411,7 @@ NS_IMETHODIMP HttpBaseChannel::SetNotificationCallbacks(nsIInterfaceRequestor *aCallbacks) { MOZ_ASSERT(NS_IsMainThread(), "Should only be called on the main thread."); - + if (!CanSetCallbacks(aCallbacks)) { return NS_ERROR_FAILURE; } @@ -1379,7 +1421,7 @@ HttpBaseChannel::SetReferrerWithPolicy(nsIURI *referrer, if (eTLDService) { rv = eTLDService->GetBaseDomain(mURI, extraDomains, currentDomain); if (NS_FAILED(rv)) return rv; - rv = eTLDService->GetBaseDomain(clone, extraDomains, referrerDomain); + rv = eTLDService->GetBaseDomain(clone, extraDomains, referrerDomain); if (NS_FAILED(rv)) return rv; } @@ -2446,7 +2488,7 @@ void HttpBaseChannel::ReleaseListeners() { MOZ_ASSERT(NS_IsMainThread(), "Should only be called on the main thread."); - + mListener = nullptr; mListenerContext = nullptr; mCallbacks = nullptr; @@ -3113,4 +3155,3 @@ HttpBaseChannel::SetCorsPreflightParameters(const nsTArray& aUnsafeHe } // namespace net } // namespace mozilla - diff --git a/netwerk/protocol/http/HttpBaseChannel.h b/netwerk/protocol/http/HttpBaseChannel.h index 9c1029e51e8d..937a92d5c8e3 100644 --- a/netwerk/protocol/http/HttpBaseChannel.h +++ b/netwerk/protocol/http/HttpBaseChannel.h @@ -100,6 +100,7 @@ public: NS_IMETHOD SetLoadGroup(nsILoadGroup *aLoadGroup) override; NS_IMETHOD GetLoadFlags(nsLoadFlags *aLoadFlags) override; NS_IMETHOD SetLoadFlags(nsLoadFlags aLoadFlags) override; + NS_IMETHOD SetDocshellUserAgentOverride(); // nsIChannel NS_IMETHOD GetOriginalURI(nsIURI **aOriginalURI) override; diff --git a/netwerk/protocol/http/HttpChannelChild.cpp b/netwerk/protocol/http/HttpChannelChild.cpp index 51ec48458b82..0481bb734f13 100644 --- a/netwerk/protocol/http/HttpChannelChild.cpp +++ b/netwerk/protocol/http/HttpChannelChild.cpp @@ -1743,6 +1743,9 @@ HttpChannelChild::AsyncOpen(nsIStreamListener *listener, nsISupports *aContext) return NS_OK; } + // Set user agent override + HttpBaseChannel::SetDocshellUserAgentOverride(); + if (ShouldIntercept()) { mResponseCouldBeSynthesized = true; diff --git a/netwerk/protocol/http/nsHttpChannel.cpp b/netwerk/protocol/http/nsHttpChannel.cpp index 54d9a3d88a25..c7b3f619495b 100644 --- a/netwerk/protocol/http/nsHttpChannel.cpp +++ b/netwerk/protocol/http/nsHttpChannel.cpp @@ -5038,6 +5038,9 @@ nsHttpChannel::AsyncOpen(nsIStreamListener *listener, nsISupports *context) gHttpHandler->OnOpeningRequest(this); } + // Set user agent override + HttpBaseChannel::SetDocshellUserAgentOverride(); + mIsPending = true; mWasOpened = true;