Bug 1513574 - Remove UAOverride use from mobile/.../browser.js r=snorp

This patch removes the use of UserAgentOverrides from browser.js
The UA change when in desktop mode now uses nsIDocShell.customUserAgent, while the feature landed in bug 838332 that is only performed for t.co URLs is removed, as it landed 4 years ago and was limited to Nightly.

Differential Revision: https://phabricator.services.mozilla.com/D14749

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Valentin Gosu 2019-09-13 17:19:13 +00:00
Родитель 1b33a49317
Коммит eb1e7e00fc
2 изменённых файлов: 87 добавлений и 109 удалений

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

@ -68,12 +68,6 @@ ChromeUtils.defineModuleGetter(
"resource://gre/modules/Downloads.jsm"
);
ChromeUtils.defineModuleGetter(
this,
"UserAgentOverrides",
"resource://gre/modules/UserAgentOverrides.jsm"
);
ChromeUtils.defineModuleGetter(this, "OS", "resource://gre/modules/osfile.jsm");
ChromeUtils.defineModuleGetter(
@ -125,6 +119,19 @@ XPCOMUtils.defineLazyServiceGetter(
"nsIUUIDGenerator"
);
XPCOMUtils.defineLazyGetter(this, "DEFAULT_UA", function() {
return Cc["@mozilla.org/network/protocol;1?name=http"].getService(
Ci.nsIHttpProtocolHandler
).userAgent;
});
XPCOMUtils.defineLazyGetter(this, "DESKTOP_UA", function() {
return DEFAULT_UA.replace(
/Android \d.+?; [a-zA-Z]+/,
"X11; Linux x86_64"
).replace(/Gecko\/[0-9\.]+/, "Gecko/20100101");
});
if (AppConstants.MOZ_ENABLE_PROFILER_SPS) {
XPCOMUtils.defineLazyServiceGetter(
this,
@ -595,6 +602,7 @@ var BrowserApp = {
GlobalEventDispatcher.registerListener(this, [
"Browser:LoadManifest",
"Browser:Quit",
"DesktopMode:Change",
"Fonts:Reload",
"FormHistory:Init",
"FullScreen:Exit",
@ -2326,6 +2334,13 @@ var BrowserApp = {
FontEnumerator.updateFontList();
break;
case "DesktopMode:Change":
let tab = this.getTabForId(data.tabId);
if (tab) {
tab.reloadWithMode(data.desktopMode);
}
break;
case "FormHistory:Init": {
// Force creation/upgrade of formhistory.sqlite
FormHistory.count(
@ -4003,94 +4018,6 @@ var LightWeightThemeStuff = {
},
};
var DesktopUserAgent = {
DESKTOP_UA: null,
TCO_DOMAIN: "t.co",
TCO_REPLACE: / Gecko.*/,
init: function ua_init() {
GlobalEventDispatcher.registerListener(this, "DesktopMode:Change");
UserAgentOverrides.addComplexOverride(this.onRequest.bind(this));
// See https://developer.mozilla.org/en/Gecko_user_agent_string_reference
this.DESKTOP_UA = Cc["@mozilla.org/network/protocol;1?name=http"]
.getService(Ci.nsIHttpProtocolHandler)
.userAgent.replace(/Android \d.+?; [a-zA-Z]+/, "X11; Linux x86_64")
.replace(/Gecko\/[0-9\.]+/, "Gecko/20100101");
},
onRequest: function(channel, defaultUA) {
if (AppConstants.NIGHTLY_BUILD && this.TCO_DOMAIN == channel.URI.host) {
// Force the referrer
channel.referrer = channel.URI;
// Send a bot-like UA to t.co to get a real redirect. We strip off the
// "Gecko/x.y Firefox/x.y" part
return defaultUA.replace(this.TCO_REPLACE, "");
}
let channelWindow = this._getWindowForRequest(channel);
let tab = BrowserApp.getTabForWindow(channelWindow);
if (tab) {
return this.getUserAgentForTab(tab);
}
return null;
},
getUserAgentForTab: function ua_getUserAgentForTab(aTab) {
// Send desktop UA if "Request Desktop Site" is enabled.
if (aTab.desktopMode) {
return this.DESKTOP_UA;
}
return null;
},
_getRequestLoadContext: function ua_getRequestLoadContext(aRequest) {
if (aRequest && aRequest.notificationCallbacks) {
try {
return aRequest.notificationCallbacks.getInterface(Ci.nsILoadContext);
} catch (ex) {}
}
if (
aRequest &&
aRequest.loadGroup &&
aRequest.loadGroup.notificationCallbacks
) {
try {
return aRequest.loadGroup.notificationCallbacks.getInterface(
Ci.nsILoadContext
);
} catch (ex) {}
}
return null;
},
_getWindowForRequest: function ua_getWindowForRequest(aRequest) {
let loadContext = this._getRequestLoadContext(aRequest);
if (loadContext) {
try {
return loadContext.associatedWindow;
} catch (e) {
// loadContext.associatedWindow can throw when there's no window
}
}
return null;
},
onEvent: function ua_onEvent(event, data, callback) {
if (event === "DesktopMode:Change") {
let tab = BrowserApp.getTabForId(data.tabId);
if (tab) {
tab.reloadWithMode(data.desktopMode);
}
}
},
};
function nsBrowserAccess() {}
nsBrowserAccess.prototype = {
@ -4392,6 +4319,13 @@ Tab.prototype = {
this.browser.docShell.setOriginAttributes(attrs);
let desktopMode = "desktopMode" in aParams ? aParams.desktopMode : false;
if (desktopMode) {
this.browser.docShell.customUserAgent = DESKTOP_UA;
} else {
this.browser.docShell.customUserAgent = "";
}
// Set the new docShell load flags based on network state.
if (Tabs.useCache) {
this.browser.docShell.defaultLoadFlags |= Ci.nsIRequest.LOAD_FROM_CACHE;
@ -4611,6 +4545,14 @@ Tab.prototype = {
// We were redirected; reload the original URL
url = this.originalURI.spec;
}
if (aDesktopMode) {
this.browser.docShell.customUserAgent = DESKTOP_UA;
} else {
// Clear custom UA
this.browser.docShell.customUserAgent = "";
}
let loadURIOptions = {
triggeringPrincipal: this.browser.contentPrincipal,
loadFlags,

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

@ -24,21 +24,6 @@ Migrated from Robocop: https://bugzilla.mozilla.org/show_bug.cgi?id=1184186
let chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
let BrowserApp = chromeWin.BrowserApp;
// Add a new 'desktop mode' tab with our test page
let desktopTab = BrowserApp.addTab(TestURI.spec, { selected: true, parentId: BrowserApp.selectedTab.id, desktopMode: true });
let desktopBrowser = desktopTab.browser;
await promiseBrowserEvent(desktopBrowser, "load");
// Some debugging
info("desktop: " + desktopBrowser.contentWindow.navigator.userAgent);
info("desktop: " + desktopBrowser.contentDocument.body.innerHTML);
// Check the server UA and the navigator UA for 'desktop'
ok(desktopBrowser.contentWindow.navigator.userAgent.includes("Linux x86_64"), "window.navigator.userAgent has 'Linux' in it");
ok(desktopBrowser.contentDocument.body.innerHTML.includes("Linux x86_64"), "HTTP header 'User-Agent' has 'Linux' in it");
BrowserApp.closeTab(desktopTab);
// Add a new 'mobile mode' tab with our test page
let mobileTab = BrowserApp.addTab(TestURI.spec, { selected: true, parentId: BrowserApp.selectedTab.id });
let mobileBrowser = mobileTab.browser;
@ -53,7 +38,58 @@ Migrated from Robocop: https://bugzilla.mozilla.org/show_bug.cgi?id=1184186
ok(mobileBrowser.contentWindow.navigator.userAgent.includes("Android"), "window.navigator.userAgent has 'Android' in it");
ok(mobileBrowser.contentDocument.body.innerHTML.includes("Android"), "HTTP header 'User-Agent' has 'Android' in it");
mobileTab.reloadWithMode(true);
await promiseBrowserEvent(mobileBrowser, "load");
// Some debugging
info("desktop: " + mobileBrowser.contentWindow.navigator.userAgent);
info("desktop: " + mobileBrowser.contentDocument.body.innerHTML);
// Check the server UA and the navigator UA for 'desktop'
ok(mobileBrowser.contentWindow.navigator.userAgent.includes("Linux x86_64"), "window.navigator.userAgent has 'Linux' in it");
ok(mobileBrowser.contentDocument.body.innerHTML.includes("Linux x86_64"), "HTTP header 'User-Agent' has 'Linux' in it");
BrowserApp.closeTab(mobileTab);
// Add a new 'desktop mode' tab with our test page
let desktopTab = BrowserApp.addTab(TestURI.spec, { selected: true, parentId: BrowserApp.selectedTab.id, desktopMode: true });
let desktopBrowser = desktopTab.browser;
await promiseBrowserEvent(desktopBrowser, "load");
// Some debugging
info("desktop: " + desktopBrowser.contentWindow.navigator.userAgent);
info("desktop: " + desktopBrowser.contentDocument.body.innerHTML);
// Check the server UA and the navigator UA for 'desktop'
ok(desktopBrowser.contentWindow.navigator.userAgent.includes("Linux x86_64"), "window.navigator.userAgent has 'Linux' in it");
ok(desktopBrowser.contentDocument.body.innerHTML.includes("Linux x86_64"), "HTTP header 'User-Agent' has 'Linux' in it");
// should reload, and keep desktop mode
desktopTab.reloadWithMode(true);
await promiseBrowserEvent(desktopBrowser, "load");
// Some debugging
info("desktop: " + desktopBrowser.contentWindow.navigator.userAgent);
info("desktop: " + desktopBrowser.contentDocument.body.innerHTML);
// Check the server UA and the navigator UA for 'desktop'
ok(desktopBrowser.contentWindow.navigator.userAgent.includes("Linux x86_64"), "window.navigator.userAgent has 'Linux' in it");
ok(desktopBrowser.contentDocument.body.innerHTML.includes("Linux x86_64"), "HTTP header 'User-Agent' has 'Linux' in it");
// Now reload in mobile mode
desktopTab.reloadWithMode(false);
await promiseBrowserEvent(desktopBrowser, "load");
// Some debugging
info("mobile: " + desktopBrowser.contentWindow.navigator.userAgent);
info("mobile: " + desktopBrowser.contentDocument.body.innerHTML);
// Check the server UA and the navigator UA for 'mobile'
// We only check for 'Android' because we don't know the version or if it's phone or tablet
ok(desktopBrowser.contentWindow.navigator.userAgent.includes("Android"), "window.navigator.userAgent has 'Android' in it");
ok(desktopBrowser.contentDocument.body.innerHTML.includes("Android"), "HTTP header 'User-Agent' has 'Android' in it");
BrowserApp.closeTab(desktopTab);
});
</script>