Bug 1137681 - Make user agent docshell overrides affect network requests. r=jduell

This commit is contained in:
Tim Nguyen 2015-12-01 13:08:00 +01:00
parent f6642acc87
commit 23d4194266
4 changed files with 53 additions and 5 deletions

View File

@ -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"
@ -254,7 +255,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;
}
@ -296,6 +297,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<nsILoadContext> loadContext;
NS_QueryNotificationCallbacks(this, loadContext);
if (!loadContext) {
return NS_OK;
}
nsCOMPtr<nsIDOMWindow> domWindow;
loadContext->GetAssociatedWindow(getter_AddRefs(domWindow));
if (!domWindow) {
return NS_OK;
}
nsCOMPtr<nsPIDOMWindow> 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
//-----------------------------------------------------------------------------
@ -370,7 +412,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;
}
@ -1380,7 +1422,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;
}
@ -2445,7 +2487,7 @@ void
HttpBaseChannel::ReleaseListeners()
{
MOZ_ASSERT(NS_IsMainThread(), "Should only be called on the main thread.");
mListener = nullptr;
mListenerContext = nullptr;
mCallbacks = nullptr;
@ -3121,4 +3163,3 @@ HttpBaseChannel::SetCorsPreflightParameters(const nsTArray<nsCString>& aUnsafeHe
} // namespace net
} // namespace mozilla

View File

@ -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;

View File

@ -1740,6 +1740,9 @@ HttpChannelChild::AsyncOpen(nsIStreamListener *listener, nsISupports *aContext)
return NS_OK;
}
// Set user agent override
HttpBaseChannel::SetDocshellUserAgentOverride();
if (ShouldIntercept()) {
mResponseCouldBeSynthesized = true;

View File

@ -5040,6 +5040,9 @@ nsHttpChannel::AsyncOpen(nsIStreamListener *listener, nsISupports *context)
gHttpHandler->OnOpeningRequest(this);
}
// Set user agent override
HttpBaseChannel::SetDocshellUserAgentOverride();
mIsPending = true;
mWasOpened = true;