2012-08-30 04:49:05 -07:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.EXPORTED_SYMBOLS = [ "UserAgentOverrides" ];
|
2012-08-30 04:49:05 -07:00
|
|
|
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cc = Components.classes;
|
|
|
|
|
2013-10-31 07:09:06 -07:00
|
|
|
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
|
2012-08-30 04:49:05 -07:00
|
|
|
Components.utils.import("resource://gre/modules/Services.jsm");
|
2013-09-09 12:47:47 -07:00
|
|
|
Components.utils.import("resource://gre/modules/UserAgentUpdates.jsm");
|
2012-08-30 04:49:05 -07:00
|
|
|
|
2013-10-31 07:09:06 -07:00
|
|
|
const OVERRIDE_MESSAGE = "Useragent:GetOverride";
|
2012-08-30 04:49:05 -07:00
|
|
|
const PREF_OVERRIDES_ENABLED = "general.useragent.site_specific_overrides";
|
2012-10-10 05:53:11 -07:00
|
|
|
const DEFAULT_UA = Cc["@mozilla.org/network/protocol;1?name=http"]
|
|
|
|
.getService(Ci.nsIHttpProtocolHandler)
|
|
|
|
.userAgent;
|
2013-07-24 00:38:45 -07:00
|
|
|
const MAX_OVERRIDE_FOR_HOST_CACHE_SIZE = 250;
|
2012-08-30 04:49:05 -07:00
|
|
|
|
2013-10-31 07:09:06 -07:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "ppmm",
|
|
|
|
"@mozilla.org/parentprocessmessagemanager;1",
|
|
|
|
"nsIMessageListenerManager"); // Might have to make this broadcast?
|
|
|
|
|
2012-08-30 04:49:05 -07:00
|
|
|
var gPrefBranch;
|
2013-07-26 00:39:01 -07:00
|
|
|
var gOverrides = new Map;
|
2013-09-09 12:47:47 -07:00
|
|
|
var gUpdatedOverrides;
|
2013-07-24 00:38:45 -07:00
|
|
|
var gOverrideForHostCache = new Map;
|
2012-08-30 04:49:05 -07:00
|
|
|
var gInitialized = false;
|
2012-10-11 15:56:13 -07:00
|
|
|
var gOverrideFunctions = [
|
|
|
|
function (aHttpChannel) UserAgentOverrides.getOverrideForURI(aHttpChannel.URI)
|
|
|
|
];
|
2014-01-28 06:04:39 -08:00
|
|
|
var gBuiltUAs = new Map;
|
2012-08-30 04:49:05 -07:00
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.UserAgentOverrides = {
|
2012-08-30 04:49:05 -07:00
|
|
|
init: function uao_init() {
|
|
|
|
if (gInitialized)
|
|
|
|
return;
|
|
|
|
|
|
|
|
gPrefBranch = Services.prefs.getBranch("general.useragent.override.");
|
|
|
|
gPrefBranch.addObserver("", buildOverrides, false);
|
|
|
|
|
2013-10-31 07:09:06 -07:00
|
|
|
ppmm.addMessageListener(OVERRIDE_MESSAGE, this);
|
2012-08-30 04:49:05 -07:00
|
|
|
Services.prefs.addObserver(PREF_OVERRIDES_ENABLED, buildOverrides, false);
|
|
|
|
|
2013-01-07 08:47:01 -08:00
|
|
|
try {
|
|
|
|
Services.obs.addObserver(HTTP_on_modify_request, "http-on-modify-request", false);
|
|
|
|
} catch (x) {
|
|
|
|
// The http-on-modify-request notification is disallowed in content processes.
|
|
|
|
}
|
2012-08-30 04:49:05 -07:00
|
|
|
|
2013-09-09 12:47:47 -07:00
|
|
|
UserAgentUpdates.init(function(overrides) {
|
|
|
|
gOverrideForHostCache.clear();
|
|
|
|
if (overrides) {
|
2014-01-28 06:04:39 -08:00
|
|
|
for (let domain in overrides) {
|
|
|
|
overrides[domain] = getUserAgentFromOverride(overrides[domain]);
|
|
|
|
}
|
2013-09-09 12:47:47 -07:00
|
|
|
overrides.get = function(key) this[key];
|
|
|
|
}
|
|
|
|
gUpdatedOverrides = overrides;
|
|
|
|
});
|
|
|
|
|
2012-08-30 04:49:05 -07:00
|
|
|
buildOverrides();
|
2013-01-07 08:47:01 -08:00
|
|
|
gInitialized = true;
|
2012-08-30 04:49:05 -07:00
|
|
|
},
|
|
|
|
|
2012-10-10 05:53:11 -07:00
|
|
|
addComplexOverride: function uao_addComplexOverride(callback) {
|
2013-10-16 07:51:23 -07:00
|
|
|
// Add to front of array so complex overrides have precedence
|
|
|
|
gOverrideFunctions.unshift(callback);
|
2012-10-11 15:56:13 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
getOverrideForURI: function uao_getOverrideForURI(aURI) {
|
2013-10-31 07:09:06 -07:00
|
|
|
let host = aURI.asciiHost;
|
2013-07-30 13:51:25 -07:00
|
|
|
if (!gInitialized ||
|
2013-09-09 12:47:47 -07:00
|
|
|
(!gOverrides.size && !gUpdatedOverrides) ||
|
2013-10-31 07:09:06 -07:00
|
|
|
!(host)) {
|
2013-07-03 06:22:05 -07:00
|
|
|
return null;
|
2013-10-31 07:09:06 -07:00
|
|
|
}
|
2013-07-24 00:38:45 -07:00
|
|
|
|
|
|
|
let override = gOverrideForHostCache.get(host);
|
|
|
|
if (override !== undefined)
|
|
|
|
return override;
|
|
|
|
|
2013-09-09 12:47:47 -07:00
|
|
|
function findOverride(overrides) {
|
|
|
|
let searchHost = host;
|
|
|
|
let userAgent = overrides.get(searchHost);
|
|
|
|
|
|
|
|
while (!userAgent) {
|
|
|
|
let dot = searchHost.indexOf('.');
|
|
|
|
if (dot === -1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
searchHost = searchHost.slice(dot + 1);
|
|
|
|
userAgent = overrides.get(searchHost);
|
2012-10-11 15:56:13 -07:00
|
|
|
}
|
2013-09-09 12:47:47 -07:00
|
|
|
return userAgent;
|
2012-10-11 15:56:13 -07:00
|
|
|
}
|
|
|
|
|
2013-09-09 12:47:47 -07:00
|
|
|
override = (gOverrides.size && findOverride(gOverrides))
|
|
|
|
|| (gUpdatedOverrides && findOverride(gUpdatedOverrides));
|
|
|
|
|
2013-07-24 00:38:45 -07:00
|
|
|
if (gOverrideForHostCache.size >= MAX_OVERRIDE_FOR_HOST_CACHE_SIZE) {
|
|
|
|
gOverrideForHostCache.clear();
|
|
|
|
}
|
|
|
|
gOverrideForHostCache.set(host, override);
|
|
|
|
|
|
|
|
return override;
|
2012-10-10 05:53:11 -07:00
|
|
|
},
|
|
|
|
|
2012-08-30 04:49:05 -07:00
|
|
|
uninit: function uao_uninit() {
|
|
|
|
if (!gInitialized)
|
|
|
|
return;
|
|
|
|
gInitialized = false;
|
|
|
|
|
|
|
|
gPrefBranch.removeObserver("", buildOverrides);
|
|
|
|
|
|
|
|
Services.prefs.removeObserver(PREF_OVERRIDES_ENABLED, buildOverrides);
|
|
|
|
|
|
|
|
Services.obs.removeObserver(HTTP_on_modify_request, "http-on-modify-request");
|
2013-10-31 07:09:06 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
receiveMessage: function(aMessage) {
|
|
|
|
let name = aMessage.name;
|
|
|
|
switch (name) {
|
|
|
|
case OVERRIDE_MESSAGE:
|
|
|
|
let uri = aMessage.data.uri;
|
|
|
|
return this.getOverrideForURI(uri);
|
|
|
|
default:
|
|
|
|
throw("Wrong Message in UserAgentOverride: " + name);
|
|
|
|
}
|
2012-08-30 04:49:05 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-28 06:04:39 -08:00
|
|
|
function getUserAgentFromOverride(override)
|
|
|
|
{
|
|
|
|
let userAgent = gBuiltUAs.get(override);
|
|
|
|
if (userAgent !== undefined) {
|
|
|
|
return userAgent;
|
|
|
|
}
|
|
|
|
let [search, replace] = override.split("#", 2);
|
|
|
|
if (search && replace) {
|
|
|
|
userAgent = DEFAULT_UA.replace(new RegExp(search, "g"), replace);
|
|
|
|
} else {
|
|
|
|
userAgent = override;
|
|
|
|
}
|
|
|
|
gBuiltUAs.set(override, userAgent);
|
|
|
|
return userAgent;
|
|
|
|
}
|
|
|
|
|
2012-08-30 04:49:05 -07:00
|
|
|
function buildOverrides() {
|
2013-07-26 00:39:01 -07:00
|
|
|
gOverrides.clear();
|
2013-07-24 00:38:45 -07:00
|
|
|
gOverrideForHostCache.clear();
|
2012-08-30 04:49:05 -07:00
|
|
|
|
|
|
|
if (!Services.prefs.getBoolPref(PREF_OVERRIDES_ENABLED))
|
|
|
|
return;
|
|
|
|
|
2013-07-26 00:39:01 -07:00
|
|
|
let builtUAs = new Map;
|
2012-08-30 04:49:05 -07:00
|
|
|
let domains = gPrefBranch.getChildList("");
|
|
|
|
|
|
|
|
for (let domain of domains) {
|
|
|
|
let override = gPrefBranch.getCharPref(domain);
|
2014-01-28 06:04:39 -08:00
|
|
|
let userAgent = getUserAgentFromOverride(override);
|
2013-07-26 00:39:01 -07:00
|
|
|
|
2013-07-27 07:18:25 -07:00
|
|
|
if (userAgent != DEFAULT_UA) {
|
|
|
|
gOverrides.set(domain, userAgent);
|
|
|
|
}
|
2012-08-30 04:49:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function HTTP_on_modify_request(aSubject, aTopic, aData) {
|
|
|
|
let channel = aSubject.QueryInterface(Ci.nsIHttpChannel);
|
2012-10-10 05:53:11 -07:00
|
|
|
|
2012-10-11 15:56:13 -07:00
|
|
|
for (let callback of gOverrideFunctions) {
|
2012-10-10 05:53:11 -07:00
|
|
|
let modifiedUA = callback(channel, DEFAULT_UA);
|
|
|
|
if (modifiedUA) {
|
|
|
|
channel.setRequestHeader("User-Agent", modifiedUA, false);
|
|
|
|
return;
|
2012-08-30 04:49:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|