Bug 788422 - Use UserAgentOverrides.jsm as designed to fix YouTube on tablets. r=bnicholson
authorDão Gottwald <dao@mozilla.com>
Sat, 27 Jul 2013 16:18:25 +0200
changeset 140256 d12b8094728eea922062ee5e62e8ad2d481bb96b
parent 140255 36326783bd82f53c39e92e96d98b3f2f151574a4
child 140257 75e2498668dfb5868e35dfa00d5b453662e050fa
push id1951
push userryanvm@gmail.com
push dateSun, 28 Jul 2013 01:55:51 +0000
treeherderfx-team@73b69c146ca6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbnicholson
bugs788422
milestone25.0a1
Bug 788422 - Use UserAgentOverrides.jsm as designed to fix YouTube on tablets. r=bnicholson
mobile/android/app/mobile.js
mobile/android/chrome/content/browser.js
mobile/android/components/SiteSpecificUserAgent.js
netwerk/protocol/http/UserAgentOverrides.jsm
--- a/mobile/android/app/mobile.js
+++ b/mobile/android/app/mobile.js
@@ -766,8 +766,10 @@ pref("dom.phonenumber.substringmatching.
 pref("media.useAudioChannelService", false);
 
 // Turn on the CSP 1.0 parser for Content Security Policy headers
 pref("security.csp.speccompliant", true);
 
 // Enable hardware-accelerated Skia canvas
 pref("gfx.canvas.azure.backends", "skia");
 pref("gfx.canvas.azure.accelerated", true);
+
+pref("general.useragent.override.youtube.com", "Android; Tablet;#Android; Mobile;");
--- a/mobile/android/chrome/content/browser.js
+++ b/mobile/android/chrome/content/browser.js
@@ -329,17 +329,18 @@ var BrowserApp = {
     HealthReportStatusListener.init();
     XPInstallObserver.init();
     ClipboardHelper.init();
     CharacterEncoding.init();
     ActivityObserver.init();
     WebappsUI.init();
     RemoteDebugger.init();
     Reader.init();
-    UserAgent.init();
+    UserAgentOverrides.init();
+    DesktopUserAgent.init();
     ExternalApps.init();
     Distribution.init();
     Tabs.init();
 #ifdef MOZ_TELEMETRY_REPORTING
     Telemetry.init();
 #endif
 #ifdef ACCESSIBILITY
     AccessFu.attach(window);
@@ -651,17 +652,18 @@ var BrowserApp = {
     ViewportHandler.uninit();
     XPInstallObserver.uninit();
     HealthReportStatusListener.uninit();
     CharacterEncoding.uninit();
     SearchEngines.uninit();
     WebappsUI.uninit();
     RemoteDebugger.uninit();
     Reader.uninit();
-    UserAgent.uninit();
+    UserAgentOverrides.uninit();
+    DesktopUserAgent.uninit();
     ExternalApps.uninit();
     Distribution.uninit();
     Tabs.uninit();
 #ifdef MOZ_TELEMETRY_REPORTING
     Telemetry.uninit();
 #endif
   },
 
@@ -2349,70 +2351,57 @@ var LightWeightThemeWebInstaller = {
     return pm.testPermission(uri, "install") == pm.ALLOW_ACTION;
   },
 
   _getThemeFromNode: function (node) {
     return this._manager.parseTheme(node.getAttribute("data-browsertheme"), node.baseURI);
   }
 };
 
-var UserAgent = {
+var DesktopUserAgent = {
   DESKTOP_UA: null,
-  YOUTUBE_DOMAIN: /(^|\.)youtube\.com$/,
 
   init: function ua_init() {
     Services.obs.addObserver(this, "DesktopMode:Change", false);
-    UserAgentOverrides.init();
     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; [a-zA-Z]+/, "X11; Linux x86_64")
                         .replace(/Gecko\/[0-9\.]+/, "Gecko/20100101");
   },
 
   uninit: function ua_uninit() {
     Services.obs.removeObserver(this, "DesktopMode:Change");
-    UserAgentOverrides.uninit();
   },
 
   onRequest: function(channel, defaultUA) {
     let channelWindow = this._getWindowForRequest(channel);
     let tab = BrowserApp.getTabForWindow(channelWindow);
     if (tab == null)
-      return;
-
-    let ua = this.getUserAgentForUriAndTab(channel.URI, tab, defaultUA);
-    if (ua)
-      channel.setRequestHeader("User-Agent", ua, false);
-  },
-
-  getUserAgentForWindow: function ua_getUserAgentForWindow(aWindow, defaultUA) {
+      return null;
+
+    return this.getUserAgentForTab(tab);
+  },
+
+  getUserAgentForWindow: function ua_getUserAgentForWindow(aWindow) {
     let tab = BrowserApp.getTabForWindow(aWindow.top);
     if (tab)
-      return this.getUserAgentForUriAndTab(tab.browser.currentURI, tab, defaultUA);
-    return defaultUA;
-  },
-
-  getUserAgentForUriAndTab: function ua_getUserAgentForUriAndTab(aUri, aTab, defaultUA) {
+      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;
 
-    // Not all schemes have a host member.
-    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;
+    return null;
   },
 
   _getRequestLoadContext: function ua_getRequestLoadContext(aRequest) {
     if (aRequest && aRequest.notificationCallbacks) {
       try {
         return aRequest.notificationCallbacks.getInterface(Ci.nsILoadContext);
       } catch (ex) { }
     }
--- a/mobile/android/components/SiteSpecificUserAgent.js
+++ b/mobile/android/components/SiteSpecificUserAgent.js
@@ -13,20 +13,21 @@ Cu.import("resource://gre/modules/Servic
 const DEFAULT_UA = Cc["@mozilla.org/network/protocol;1?name=http"]
                      .getService(Ci.nsIHttpProtocolHandler)
                      .userAgent;
 
 function SiteSpecificUserAgent() {}
 
 SiteSpecificUserAgent.prototype = {
   getUserAgentForURIAndWindow: function ssua_getUserAgentForURIAndWindow(aURI, aWindow) {
-    let win = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator).getMostRecentWindow("navigator:browser");
-    if (win && win.UserAgent) {
-      return win.UserAgent.getUserAgentForWindow(aWindow, DEFAULT_UA);
+    let UA;
+    let win = Services.wm.getMostRecentWindow("navigator:browser");
+    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}"),
   QueryInterface: XPCOMUtils.generateQI([Ci.nsISiteSpecificUserAgent])
 };
 
 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([SiteSpecificUserAgent]);
--- a/netwerk/protocol/http/UserAgentOverrides.jsm
+++ b/netwerk/protocol/http/UserAgentOverrides.jsm
@@ -111,17 +111,19 @@ function buildOverrides() {
       if (search && replace) {
         userAgent = DEFAULT_UA.replace(new RegExp(search, "g"), replace);
       } else {
         userAgent = override;
       }
       builtUAs.set(override, userAgent);
     }
 
-    gOverrides.set(domain, userAgent);
+    if (userAgent != DEFAULT_UA) {
+      gOverrides.set(domain, userAgent);
+    }
   }
 }
 
 function HTTP_on_modify_request(aSubject, aTopic, aData) {
   let channel = aSubject.QueryInterface(Ci.nsIHttpChannel);
 
   for (let callback of gOverrideFunctions) {
     let modifiedUA = callback(channel, DEFAULT_UA);