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;
|
|
|
|
|
|
|
|
Components.utils.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
|
|
|
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;
|
2012-08-30 04:49:05 -07:00
|
|
|
|
|
|
|
var gPrefBranch;
|
|
|
|
var gOverrides;
|
|
|
|
var gInitialized = false;
|
2012-10-11 15:56:13 -07:00
|
|
|
var gOverrideFunctions = [
|
|
|
|
function (aHttpChannel) UserAgentOverrides.getOverrideForURI(aHttpChannel.URI)
|
|
|
|
];
|
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);
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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) {
|
2012-10-11 15:56:13 -07:00
|
|
|
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;
|
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");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function buildOverrides() {
|
|
|
|
gOverrides = {};
|
|
|
|
|
|
|
|
if (!Services.prefs.getBoolPref(PREF_OVERRIDES_ENABLED))
|
|
|
|
return;
|
|
|
|
|
|
|
|
let domains = gPrefBranch.getChildList("");
|
|
|
|
|
|
|
|
for (let domain of domains) {
|
|
|
|
let override = gPrefBranch.getCharPref(domain);
|
|
|
|
|
|
|
|
let [search, replace] = override.split("#", 2);
|
|
|
|
if (search && replace) {
|
2012-10-10 05:53:11 -07:00
|
|
|
gOverrides[domain] = DEFAULT_UA.replace(new RegExp(search, "g"), replace);
|
2012-08-30 04:49:05 -07:00
|
|
|
} else {
|
|
|
|
gOverrides[domain] = override;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|