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

This commit is contained in:
Tim Nguyen 2015-12-08 09:27:00 +01:00
Родитель cf7f672f1c
Коммит 8fe11e6a5d
4 изменённых файлов: 53 добавлений и 5 удалений

Просмотреть файл

@ -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<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
//-----------------------------------------------------------------------------
@ -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<nsCString>& aUnsafeHe
} // namespace net
} // namespace mozilla

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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