Bug 800157 - Make User Agent overrides affect navigator.userAgent in addition to the HTTP header. r=bz

--HG--
extra : rebase_source : dd856c03a45e9d95866fb5a95e568ae3cce67521
This commit is contained in:
Dão Gottwald 2012-10-12 00:56:13 +02:00
parent 8c57e31df0
commit 65e7938e9c
10 changed files with 113 additions and 13 deletions

View File

@ -359,6 +359,8 @@
@BINPATH@/components/nsLoginInfo.js
@BINPATH@/components/nsLoginManager.js
@BINPATH@/components/nsLoginManagerPrompter.js
@BINPATH@/components/SiteSpecificUserAgent.js
@BINPATH@/components/SiteSpecificUserAgent.manifest
@BINPATH@/components/storage-Legacy.js
@BINPATH@/components/storage-mozStorage.js
@BINPATH@/components/crypto-SDR.js

View File

@ -345,6 +345,8 @@
@BINPATH@/components/BrowserPageThumbs.manifest
@BINPATH@/components/nsPrivateBrowsingService.manifest
@BINPATH@/components/nsPrivateBrowsingService.js
@BINPATH@/components/SiteSpecificUserAgent.js
@BINPATH@/components/SiteSpecificUserAgent.manifest
@BINPATH@/components/toolkitsearch.manifest
@BINPATH@/components/nsSearchService.js
@BINPATH@/components/nsSearchSuggestions.js

View File

@ -20,6 +20,11 @@ DIRS = \
test \
$(NULL)
EXTRA_COMPONENTS = \
SiteSpecificUserAgent.js \
SiteSpecificUserAgent.manifest \
$(NULL)
EXTRA_PP_COMPONENTS = \
ConsoleAPI.js \
ConsoleAPI.manifest \
@ -39,6 +44,7 @@ XPIDLSRCS = \
nsIDOMDOMRequest.idl \
nsIEntropyCollector.idl \
nsIScriptChannel.idl \
nsISiteSpecificUserAgent.idl \
$(NULL)
EXPORTS = \

View File

@ -33,6 +33,7 @@
#include "nsISmsService.h"
#include "mozilla/Hal.h"
#include "nsIWebNavigation.h"
#include "nsISiteSpecificUserAgent.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/StaticPtr.h"
#include "Connection.h"
@ -234,7 +235,30 @@ Navigator::GetWindow()
NS_IMETHODIMP
Navigator::GetUserAgent(nsAString& aUserAgent)
{
return NS_GetNavigatorUserAgent(aUserAgent);
nsresult rv = NS_GetNavigatorUserAgent(aUserAgent);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsPIDOMWindow> win(do_QueryReferent(mWindow));
if (!win || !win->GetDocShell()) {
return NS_OK;
}
nsIDocument* doc = win->GetExtantDoc();
if (!doc) {
return NS_OK;
}
nsCOMPtr<nsIURI> codebaseURI;
doc->NodePrincipal()->GetURI(getter_AddRefs(codebaseURI));
if (!codebaseURI) {
return NS_OK;
}
nsCOMPtr<nsISiteSpecificUserAgent> siteSpecificUA =
do_GetService("@mozilla.org/dom/site-specific-user-agent;1");
NS_ENSURE_TRUE(siteSpecificUA, NS_OK);
return siteSpecificUA->GetUserAgentForURI(codebaseURI, aUserAgent);
}
NS_IMETHODIMP

View File

@ -0,0 +1,27 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const Cu = Components.utils;
const Cc = Components.classes;
const Ci = Components.interfaces;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/UserAgentOverrides.jsm");
const DEFAULT_UA = Cc["@mozilla.org/network/protocol;1?name=http"]
.getService(Ci.nsIHttpProtocolHandler)
.userAgent;
function SiteSpecificUserAgent() {}
SiteSpecificUserAgent.prototype = {
getUserAgentForURI: function ssua_getUserAgentForURI(aURI) {
return UserAgentOverrides.getOverrideForURI(aURI) || DEFAULT_UA;
},
classID: Components.ID("{506c680f-3d1c-4954-b351-2c80afbc37d3}"),
QueryInterface: XPCOMUtils.generateQI([Ci.nsISiteSpecificUserAgent])
};
let NSGetFactory = XPCOMUtils.generateNSGetFactory([SiteSpecificUserAgent]);

View File

@ -0,0 +1,2 @@
component {506c680f-3d1c-4954-b351-2c80afbc37d3} SiteSpecificUserAgent.js
contract @mozilla.org/dom/site-specific-user-agent;1 {506c680f-3d1c-4954-b351-2c80afbc37d3}

View File

@ -0,0 +1,25 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsISupports.idl"
interface nsIURI;
/**
* nsISiteSpecificUserAgent provides you with site-specific User Agent strings.
*/
[scriptable, uuid(604a64af-9809-4c2f-a91d-f6ebfa21f6cb)]
interface nsISiteSpecificUserAgent : nsISupports
{
/**
* Get the User Agent string for a given URI.
*
* @param aURI is the URI of the page the UA string is used for.
*
* @returns the User Agent string for the given URI. If no override applies,
* the default User Agent string is used.
*/
AString getUserAgentForURI(in nsIURI aURI);
};

View File

@ -270,6 +270,8 @@
@BINPATH@/components/nsLoginInfo.js
@BINPATH@/components/nsLoginManager.js
@BINPATH@/components/nsLoginManagerPrompter.js
@BINPATH@/components/SiteSpecificUserAgent.js
@BINPATH@/components/SiteSpecificUserAgent.manifest
@BINPATH@/components/storage-Legacy.js
@BINPATH@/components/storage-mozStorage.js
@BINPATH@/components/crypto-SDR.js

View File

@ -329,6 +329,8 @@
@BINPATH@/components/nsLoginInfo.js
@BINPATH@/components/nsLoginManager.js
@BINPATH@/components/nsLoginManagerPrompter.js
@BINPATH@/components/SiteSpecificUserAgent.js
@BINPATH@/components/SiteSpecificUserAgent.manifest
@BINPATH@/components/storage-Legacy.js
@BINPATH@/components/storage-mozStorage.js
@BINPATH@/components/crypto-SDR.js

View File

@ -19,7 +19,9 @@ const DEFAULT_UA = Cc["@mozilla.org/network/protocol;1?name=http"]
var gPrefBranch;
var gOverrides;
var gInitialized = false;
var gComplexOverrides = [];
var gOverrideFunctions = [
function (aHttpChannel) UserAgentOverrides.getOverrideForURI(aHttpChannel.URI)
];
var UserAgentOverrides = {
init: function uao_init() {
@ -38,7 +40,22 @@ var UserAgentOverrides = {
},
addComplexOverride: function uao_addComplexOverride(callback) {
gComplexOverrides.push(callback);
gOverrideFunctions.push(callback);
},
getOverrideForURI: function uao_getOverrideForURI(aURI) {
if (!gInitialized)
return null;
let host = aURI.asciiHost;
for (let domain in gOverrides) {
if (host == domain ||
host.endsWith("." + domain)) {
return gOverrides[domain];
}
}
return null;
},
uninit: function uao_uninit() {
@ -76,17 +93,8 @@ function buildOverrides() {
function HTTP_on_modify_request(aSubject, aTopic, aData) {
let channel = aSubject.QueryInterface(Ci.nsIHttpChannel);
let host = channel.URI.asciiHost;
for (let domain in gOverrides) {
if (host == domain ||
host.endsWith("." + domain)) {
channel.setRequestHeader("User-Agent", gOverrides[domain], false);
return;
}
}
for (let callback of gComplexOverrides) {
for (let callback of gOverrideFunctions) {
let modifiedUA = callback(channel, DEFAULT_UA);
if (modifiedUA) {
channel.setRequestHeader("User-Agent", modifiedUA, false);