Bug 788422 - Use UserAgentOverrides.jsm as designed to fix YouTube on tablets. r=bnicholson

This commit is contained in:
Dão Gottwald 2013-07-27 16:18:25 +02:00
Родитель 7565581ba1
Коммит 500ec2e26f
4 изменённых файлов: 23 добавлений и 29 удалений

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

@ -771,3 +771,5 @@ pref("security.csp.speccompliant", true);
// Enable hardware-accelerated Skia canvas // Enable hardware-accelerated Skia canvas
pref("gfx.canvas.azure.backends", "skia"); pref("gfx.canvas.azure.backends", "skia");
pref("gfx.canvas.azure.accelerated", true); pref("gfx.canvas.azure.accelerated", true);
pref("general.useragent.override.youtube.com", "Android; Tablet;#Android; Mobile;");

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

@ -334,7 +334,8 @@ var BrowserApp = {
WebappsUI.init(); WebappsUI.init();
RemoteDebugger.init(); RemoteDebugger.init();
Reader.init(); Reader.init();
UserAgent.init(); UserAgentOverrides.init();
DesktopUserAgent.init();
ExternalApps.init(); ExternalApps.init();
Distribution.init(); Distribution.init();
Tabs.init(); Tabs.init();
@ -656,7 +657,8 @@ var BrowserApp = {
WebappsUI.uninit(); WebappsUI.uninit();
RemoteDebugger.uninit(); RemoteDebugger.uninit();
Reader.uninit(); Reader.uninit();
UserAgent.uninit(); UserAgentOverrides.uninit();
DesktopUserAgent.uninit();
ExternalApps.uninit(); ExternalApps.uninit();
Distribution.uninit(); Distribution.uninit();
Tabs.uninit(); Tabs.uninit();
@ -2354,13 +2356,11 @@ var LightWeightThemeWebInstaller = {
} }
}; };
var UserAgent = { var DesktopUserAgent = {
DESKTOP_UA: null, DESKTOP_UA: null,
YOUTUBE_DOMAIN: /(^|\.)youtube\.com$/,
init: function ua_init() { init: function ua_init() {
Services.obs.addObserver(this, "DesktopMode:Change", false); Services.obs.addObserver(this, "DesktopMode:Change", false);
UserAgentOverrides.init();
UserAgentOverrides.addComplexOverride(this.onRequest.bind(this)); UserAgentOverrides.addComplexOverride(this.onRequest.bind(this));
// See https://developer.mozilla.org/en/Gecko_user_agent_string_reference // See https://developer.mozilla.org/en/Gecko_user_agent_string_reference
@ -2372,42 +2372,31 @@ var UserAgent = {
uninit: function ua_uninit() { uninit: function ua_uninit() {
Services.obs.removeObserver(this, "DesktopMode:Change"); Services.obs.removeObserver(this, "DesktopMode:Change");
UserAgentOverrides.uninit();
}, },
onRequest: function(channel, defaultUA) { onRequest: function(channel, defaultUA) {
let channelWindow = this._getWindowForRequest(channel); let channelWindow = this._getWindowForRequest(channel);
let tab = BrowserApp.getTabForWindow(channelWindow); let tab = BrowserApp.getTabForWindow(channelWindow);
if (tab == null) if (tab == null)
return; return null;
let ua = this.getUserAgentForUriAndTab(channel.URI, tab, defaultUA); return this.getUserAgentForTab(tab);
if (ua)
channel.setRequestHeader("User-Agent", ua, false);
}, },
getUserAgentForWindow: function ua_getUserAgentForWindow(aWindow, defaultUA) { getUserAgentForWindow: function ua_getUserAgentForWindow(aWindow) {
let tab = BrowserApp.getTabForWindow(aWindow.top); let tab = BrowserApp.getTabForWindow(aWindow.top);
if (tab) if (tab)
return this.getUserAgentForUriAndTab(tab.browser.currentURI, tab, defaultUA); return this.getUserAgentForTab(tab);
return defaultUA;
return null;
}, },
getUserAgentForUriAndTab: function ua_getUserAgentForUriAndTab(aUri, aTab, defaultUA) { getUserAgentForTab: function ua_getUserAgentForTab(aTab) {
// Send desktop UA if "Request Desktop Site" is enabled. // Send desktop UA if "Request Desktop Site" is enabled.
if (aTab.desktopMode) if (aTab.desktopMode)
return this.DESKTOP_UA; return this.DESKTOP_UA;
// Not all schemes have a host member. return null;
if (aUri.schemeIs("http") || aUri.schemeIs("https")) {
if (this.YOUTUBE_DOMAIN.test(aUri.host)) {
// Send the phone UA to Youtube if this is a tablet.
if (!defaultUA.contains("Android; Mobile;"))
return defaultUA.replace("Android;", "Android; Mobile;");
}
}
return defaultUA;
}, },
_getRequestLoadContext: function ua_getRequestLoadContext(aRequest) { _getRequestLoadContext: function ua_getRequestLoadContext(aRequest) {

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

@ -18,11 +18,12 @@ function SiteSpecificUserAgent() {}
SiteSpecificUserAgent.prototype = { SiteSpecificUserAgent.prototype = {
getUserAgentForURIAndWindow: function ssua_getUserAgentForURIAndWindow(aURI, aWindow) { getUserAgentForURIAndWindow: function ssua_getUserAgentForURIAndWindow(aURI, aWindow) {
let win = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator).getMostRecentWindow("navigator:browser"); let UA;
if (win && win.UserAgent) { let win = Services.wm.getMostRecentWindow("navigator:browser");
return win.UserAgent.getUserAgentForWindow(aWindow, DEFAULT_UA); if (win && win.DesktopUserAgent) {
UA = win.DesktopUserAgent.getUserAgentForWindow(aWindow);
} }
return DEFAULT_UA; return UA || UserAgentOverrides.getOverrideForURI(aURI) || DEFAULT_UA;
}, },
classID: Components.ID("{d5234c9d-0ee2-4b3c-9da3-18be9e5cf7e6}"), classID: Components.ID("{d5234c9d-0ee2-4b3c-9da3-18be9e5cf7e6}"),

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

@ -116,7 +116,9 @@ function buildOverrides() {
builtUAs.set(override, userAgent); builtUAs.set(override, userAgent);
} }
gOverrides.set(domain, userAgent); if (userAgent != DEFAULT_UA) {
gOverrides.set(domain, userAgent);
}
} }
} }