Bug 927633 - Move UA overrides to the child process. r=jchen

This commit is contained in:
Mason Chang 2013-10-31 10:09:06 -04:00
parent 4a86d7955a
commit acd30ed18f
4 changed files with 86 additions and 17 deletions

View File

@ -7,16 +7,78 @@ const Cc = Components.classes;
const Ci = Components.interfaces;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/UserAgentOverrides.jsm");
const MAX_CACHE_SIZE = 250;
const PREF_UPDATE = "general.useragent.updates.";
const PREF_OVERRIDE = "general.useragent.override.";
const XPCOM_SHUTDOWN = "xpcom-shutdown";
const HTTP_PROTO_HANDLER = Cc["@mozilla.org/network/protocol;1?name=http"]
.getService(Ci.nsIHttpProtocolHandler);
function SiteSpecificUserAgent() {}
XPCOMUtils.defineLazyServiceGetter(this, "cpmm",
"@mozilla.org/childprocessmessagemanager;1",
"nsISyncMessageSender");
function SiteSpecificUserAgent() {
this.inParent = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime)
.processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
if (this.inParent) {
Cu.import("resource://gre/modules/UserAgentOverrides.jsm");
} else {
Cu.import("resource://gre/modules/Services.jsm");
Services.prefs.addObserver(PREF_OVERRIDE, this, false);
Services.prefs.addObserver(PREF_UPDATE, this, false);
Services.obs.addObserver(this, XPCOM_SHUTDOWN, false);
this.userAgentCache = new Map;
}
}
SiteSpecificUserAgent.prototype = {
getUserAgentForURIAndWindow: function ssua_getUserAgentForURIAndWindow(aURI, aWindow) {
return UserAgentOverrides.getOverrideForURI(aURI) || HTTP_PROTO_HANDLER.userAgent;
if (this.inParent) {
return UserAgentOverrides.getOverrideForURI(aURI) || HTTP_PROTO_HANDLER.userAgent;
}
let host = aURI.asciiHost;
let cachedResult = this.userAgentCache.get(host);
if (cachedResult) {
return cachedResult;
}
let data = { uri: aURI };
let result = cpmm.sendSyncMessage("Useragent:GetOverride", data)[0] || HTTP_PROTO_HANDLER.userAgent;
if (this.userAgentCache.size >= MAX_CACHE_SIZE) {
this.userAgentCache.clear();
}
this.userAgentCache.set(host, result);
return result;
},
invalidateCache: function() {
this.userAgentCache.clear();
},
clean: function() {
this.userAgentCache.clear();
if (!this.inParent) {
Services.obs.removeObserver(this, XPCOM_SHUTDOWN);
Services.prefs.removeObserver(PREF_OVERRIDE, this);
Services.prefs.removeObserver(PREF_UPDATE, this);
}
},
observe: function(subject, topic, data) {
switch (topic) {
case "nsPref:changed":
this.invalidateCache();
break;
case XPCOM_SHUTDOWN:
this.clean();
break;
}
},
classID: Components.ID("{506c680f-3d1c-4954-b351-2c80afbc37d3}"),

View File

@ -30,10 +30,6 @@ const BrowserElementIsPreloaded = true;
Cu.import("resource://gre/modules/SettingsDB.jsm");
Cu.import("resource://gre/modules/SettingsQueue.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
if (Services.prefs.getBoolPref("general.useragent.enable_overrides")) {
Cu.import('resource://gre/modules/UserAgentOverrides.jsm');
UserAgentOverrides.init();
}
Cc["@mozilla.org/appshell/appShellService;1"].getService(Ci["nsIAppShellService"]);
Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci["nsIWindowMediator"]);

View File

@ -9,15 +9,21 @@ this.EXPORTED_SYMBOLS = [ "UserAgentOverrides" ];
const Ci = Components.interfaces;
const Cc = Components.classes;
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/UserAgentUpdates.jsm");
const OVERRIDE_MESSAGE = "Useragent:GetOverride";
const PREF_OVERRIDES_ENABLED = "general.useragent.site_specific_overrides";
const DEFAULT_UA = Cc["@mozilla.org/network/protocol;1?name=http"]
.getService(Ci.nsIHttpProtocolHandler)
.userAgent;
const MAX_OVERRIDE_FOR_HOST_CACHE_SIZE = 250;
XPCOMUtils.defineLazyServiceGetter(this, "ppmm",
"@mozilla.org/parentprocessmessagemanager;1",
"nsIMessageListenerManager"); // Might have to make this broadcast?
var gPrefBranch;
var gOverrides = new Map;
var gUpdatedOverrides;
@ -35,6 +41,7 @@ this.UserAgentOverrides = {
gPrefBranch = Services.prefs.getBranch("general.useragent.override.");
gPrefBranch.addObserver("", buildOverrides, false);
ppmm.addMessageListener(OVERRIDE_MESSAGE, this);
Services.prefs.addObserver(PREF_OVERRIDES_ENABLED, buildOverrides, false);
try {
@ -61,12 +68,12 @@ this.UserAgentOverrides = {
},
getOverrideForURI: function uao_getOverrideForURI(aURI) {
let host = aURI.asciiHost;
if (!gInitialized ||
(!gOverrides.size && !gUpdatedOverrides) ||
!(aURI instanceof Ci.nsIStandardURL))
!(host)) {
return null;
let host = aURI.asciiHost;
}
let override = gOverrideForHostCache.get(host);
if (override !== undefined)
@ -108,6 +115,17 @@ this.UserAgentOverrides = {
Services.prefs.removeObserver(PREF_OVERRIDES_ENABLED, buildOverrides);
Services.obs.removeObserver(HTTP_on_modify_request, "http-on-modify-request");
},
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);
}
}
};

View File

@ -118,13 +118,6 @@ this.UserAgentUpdates = {
let bytes = gEncoder.encode(JSON.stringify(update));
OS.File.writeAtomic(path, bytes, {tmpPath: path + ".tmp"}).then(
() => {
if (gApp.widgetToolkit === 'gonk') {
// B2G content processes run under different users;
// so we need to separately set more open permissions
file.permissions = OS.Constants.libc.S_IRUSR |
OS.Constants.libc.S_IWUSR | OS.Constants.libc.S_IRGRP |
OS.Constants.libc.S_IROTH;
}
this._lastUpdated = Date.now();
Services.prefs.setCharPref(
PREF_UPDATES_LASTUPDATED, this._lastUpdated.toString());