Bug 1363421 - Part 2, delay the initialization of UserAgentOverrides.jsm until first nsHttpChannel is created. r=mcmanus

UAOverridesBootstrapper.js is introduced to delay the initialization of
UserAgentOverrides.jsm until the creation of the first nsHttpChannel.
Uninit will be triggered at profile-change-net-teardown because no network
traffice after this point.


MozReview-Commit-ID: F8Lpn6RyZEm

--HG--
extra : rebase_source : 7c3649b50ad8594dc0968961fbbd2766d0d98b0a
This commit is contained in:
Shih-Chiang Chien 2017-05-16 12:11:12 +08:00
Родитель 7f847e5b32
Коммит 0131b8b831
10 изменённых файлов: 82 добавлений и 17 удалений

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

@ -37,7 +37,7 @@ XPCOMUtils.defineLazyGetter(this, "WeaveService", () =>
RemotePrompt:false, SessionStore:false,
ShellService:false, SimpleServiceDiscovery:false, TabCrashHandler:false,
Task:false, UITour:false, WebChannel:false,
WindowsRegistry:false, webrtcUI:false, UserAgentOverrides: false */
WindowsRegistry:false, webrtcUI:false */
/**
* IF YOU ADD OR REMOVE FROM THIS LIST, PLEASE UPDATE THE LIST ABOVE AS WELL.
@ -92,7 +92,6 @@ let initializedModules = {};
["WebChannel", "resource://gre/modules/WebChannel.jsm"],
["WindowsRegistry", "resource://gre/modules/WindowsRegistry.jsm"],
["webrtcUI", "resource:///modules/webrtcUI.jsm", "init"],
["UserAgentOverrides", "resource://gre/modules/UserAgentOverrides.jsm"],
].forEach(([name, resource, init]) => {
if (init) {
XPCOMUtils.defineLazyGetter(this, name, () => {
@ -516,8 +515,6 @@ BrowserGlue.prototype = {
// and e10s are active together.
E10SAccessibilityCheck.init();
}
UserAgentOverrides.init();
},
// cleanup (called on application shutdown)
@ -561,8 +558,6 @@ BrowserGlue.prototype = {
os.removeObserver(this, "browser-search-engine-modified");
os.removeObserver(this, "flash-plugin-hang");
os.removeObserver(this, "xpi-signature-changed");
UserAgentOverrides.uninit();
},
_onAppDefaults: function BG__onAppDefaults() {

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

@ -361,6 +361,8 @@
@RESPATH@/components/BrowserElementParent.js
@RESPATH@/components/FeedProcessor.manifest
@RESPATH@/components/FeedProcessor.js
@RESPATH@/components/UAOverridesBootstrapper.js
@RESPATH@/components/UAOverridesBootstrapper.manifest
@RESPATH@/components/WellKnownOpportunisticUtils.js
@RESPATH@/components/WellKnownOpportunisticUtils.manifest
#ifndef XP_MACOSX

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

@ -458,7 +458,6 @@ var BrowserApp = {
CharacterEncoding.init();
ActivityObserver.init();
RemoteDebugger.init();
UserAgentOverrides.init();
DesktopUserAgent.init();
Distribution.init();
Tabs.init();

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

@ -277,6 +277,8 @@
@BINPATH@/components/BrowserElementParent.js
@BINPATH@/components/FeedProcessor.manifest
@BINPATH@/components/FeedProcessor.js
@BINPATH@/components/UAOverridesBootstrapper.js
@BINPATH@/components/UAOverridesBootstrapper.manifest
@BINPATH@/components/WellKnownOpportunisticUtils.js
@BINPATH@/components/WellKnownOpportunisticUtils.manifest
@BINPATH@/components/mozProtocolHandler.js

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

@ -0,0 +1,36 @@
/* 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";
const Ci = Components.interfaces;
const Cu = Components.utils;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/UserAgentOverrides.jsm");
function UAOverridesBootstrapper() {
this.init();
}
UAOverridesBootstrapper.prototype = {
init: function uaob_init() {
Services.obs.addObserver(this, "profile-change-net-teardown", false);
UserAgentOverrides.init();
},
observe: function uaob_observe(aSubject, aTopic, aData) {
if (aTopic == "profile-change-net-teardown") {
Services.obs.removeObserver(this, "profile-change-net-teardown");
UserAgentOverrides.uninit();
}
},
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
classID: Components.ID("{965b0ca8-155b-11e7-93ae-92361f002671}")
};
const components = [UAOverridesBootstrapper];
this.NSGetFactory = XPCOMUtils.generateNSGetFactory(components);

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

@ -0,0 +1,3 @@
# UAOverridesBootstrapper.js
component {965b0ca8-155b-11e7-93ae-92361f002671} UAOverridesBootstrapper.js process=main
contract @mozilla.org/network/ua-overrides-bootstrapper;1 {965b0ca8-155b-11e7-93ae-92361f002671} process=main

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

@ -49,18 +49,24 @@ this.UserAgentOverrides = {
// The http-on-useragent-request notification is disallowed in content processes.
}
UserAgentUpdates.init(function(overrides) {
gOverrideForHostCache.clear();
if (overrides) {
for (let domain in overrides) {
overrides[domain] = getUserAgentFromOverride(overrides[domain]);
try {
UserAgentUpdates.init(function(overrides) {
gOverrideForHostCache.clear();
if (overrides) {
for (let domain in overrides) {
overrides[domain] = getUserAgentFromOverride(overrides[domain]);
}
overrides.get = function(key) { return this[key]; };
}
overrides.get = function(key) { return this[key]; };
}
gUpdatedOverrides = overrides;
});
gUpdatedOverrides = overrides;
});
buildOverrides();
} catch (e) {
// UserAgentOverrides is initialized before profile is ready.
// UA override might not work correctly.
}
buildOverrides();
gInitialized = true;
},

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

@ -119,6 +119,8 @@ LOCAL_INCLUDES += [
]
EXTRA_COMPONENTS += [
'UAOverridesBootstrapper.js',
'UAOverridesBootstrapper.manifest',
'WellKnownOpportunisticUtils.js',
'WellKnownOpportunisticUtils.manifest',
]

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

@ -333,6 +333,19 @@ nsHttpHandler::SetFastOpenOSSupport()
mFastOpenSupported ? "" : "not"));
}
void
nsHttpHandler::EnsureUAOverridesInit()
{
MOZ_ASSERT(XRE_IsParentProcess());
MOZ_ASSERT(NS_IsMainThread());
nsresult rv;
nsCOMPtr<nsISupports> bootstrapper
= do_GetService("@mozilla.org/network/ua-overrides-bootstrapper;1", &rv);
MOZ_ASSERT(bootstrapper);
MOZ_ASSERT(NS_SUCCEEDED(rv));
}
nsHttpHandler::~nsHttpHandler()
{
LOG(("Deleting nsHttpHandler [this=%p]\n", this));
@ -2103,6 +2116,11 @@ nsHttpHandler::NewProxiedChannel2(nsIURI *uri,
net_EnsurePSMInit();
}
if (XRE_IsParentProcess()) {
// Load UserAgentOverrides.jsm before any HTTP request is issued.
EnsureUAOverridesInit();
}
uint64_t channelId;
rv = NewChannelId(channelId);
NS_ENSURE_SUCCESS(rv, rv);

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

@ -403,6 +403,8 @@ private:
void NotifyObservers(nsIHttpChannel *chan, const char *event);
void SetFastOpenOSSupport();
void EnsureUAOverridesInit();
private:
// cached services