Bug 963592 - Perform replacements for updated UA overrides. r=fabrice
authorJim Chen <nchen@mozilla.com>
Tue, 28 Jan 2014 09:04:39 -0500
changeset 165567 bcb7b4eed467ed05fdf8a1ba22ed31be2f0b7eb0
parent 165566 9441f9619749594ca288bc60f12f4664b0e33706
child 165568 151feb1e7b8b42411a62d38f2ae8207922acd165
push id4623
push userryanvm@gmail.com
push dateTue, 28 Jan 2014 21:48:39 +0000
treeherderfx-team@7e79536aca0a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfabrice
bugs963592
milestone29.0a1
Bug 963592 - Perform replacements for updated UA overrides. r=fabrice
netwerk/protocol/http/UserAgentOverrides.jsm
netwerk/test/mochitests/test_user_agent_updates.html
--- a/netwerk/protocol/http/UserAgentOverrides.jsm
+++ b/netwerk/protocol/http/UserAgentOverrides.jsm
@@ -27,16 +27,17 @@ XPCOMUtils.defineLazyServiceGetter(this,
 var gPrefBranch;
 var gOverrides = new Map;
 var gUpdatedOverrides;
 var gOverrideForHostCache = new Map;
 var gInitialized = false;
 var gOverrideFunctions = [
   function (aHttpChannel) UserAgentOverrides.getOverrideForURI(aHttpChannel.URI)
 ];
+var gBuiltUAs = new Map;
 
 this.UserAgentOverrides = {
   init: function uao_init() {
     if (gInitialized)
       return;
 
     gPrefBranch = Services.prefs.getBranch("general.useragent.override.");
     gPrefBranch.addObserver("", buildOverrides, false);
@@ -48,16 +49,19 @@ this.UserAgentOverrides = {
       Services.obs.addObserver(HTTP_on_modify_request, "http-on-modify-request", false);
     } catch (x) {
       // The http-on-modify-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]);
+        }
         overrides.get = function(key) this[key];
       }
       gUpdatedOverrides = overrides;
     });
 
     buildOverrides();
     gInitialized = true;
   },
@@ -124,39 +128,45 @@ this.UserAgentOverrides = {
         let uri = aMessage.data.uri;
         return this.getOverrideForURI(uri);
       default:
         throw("Wrong Message in UserAgentOverride: " + name);
     }
   }
 };
 
+function getUserAgentFromOverride(override)
+{
+  let userAgent = gBuiltUAs.get(override);
+  if (userAgent !== undefined) {
+    return userAgent;
+  }
+  let [search, replace] = override.split("#", 2);
+  if (search && replace) {
+    userAgent = DEFAULT_UA.replace(new RegExp(search, "g"), replace);
+  } else {
+    userAgent = override;
+  }
+  gBuiltUAs.set(override, userAgent);
+  return userAgent;
+}
+
 function buildOverrides() {
   gOverrides.clear();
   gOverrideForHostCache.clear();
 
   if (!Services.prefs.getBoolPref(PREF_OVERRIDES_ENABLED))
     return;
 
   let builtUAs = new Map;
   let domains = gPrefBranch.getChildList("");
 
   for (let domain of domains) {
     let override = gPrefBranch.getCharPref(domain);
-    let userAgent = builtUAs.get(override);
-
-    if (userAgent === undefined) {
-      let [search, replace] = override.split("#", 2);
-      if (search && replace) {
-        userAgent = DEFAULT_UA.replace(new RegExp(search, "g"), replace);
-      } else {
-        userAgent = override;
-      }
-      builtUAs.set(override, userAgent);
-    }
+    let userAgent = getUserAgentFromOverride(override);
 
     if (userAgent != DEFAULT_UA) {
       gOverrides.set(domain, userAgent);
     }
   }
 }
 
 function HTTP_on_modify_request(aSubject, aTopic, aData) {
--- a/netwerk/test/mochitests/test_user_agent_updates.html
+++ b/netwerk/test/mochitests/test_user_agent_updates.html
@@ -25,16 +25,22 @@ const PREF_UPDATES_TIMEOUT = PREF_UPDATE
 const KEY_PREFDIR = "PrefD";
 const KEY_APPDIR = "XCurProcD";
 const FILE_UPDATES = "ua-update.json";
 
 const DEFAULT_UA = navigator.userAgent;
 const UA_OVERRIDE = "DummyUserAgent";
 const UA_ALT_OVERRIDE = "AltUserAgent";
 
+const UA_PARTIAL_FROM = "\\wozilla"; // /\wozilla
+const UA_PARTIAL_SEP = "#";
+const UA_PARTIAL_TO = UA_OVERRIDE;
+const UA_PARTIAL_OVERRIDE = UA_PARTIAL_FROM + UA_PARTIAL_SEP + UA_PARTIAL_TO;
+const UA_PARTIAL_EXPECTED = DEFAULT_UA.replace(new RegExp(UA_PARTIAL_FROM, 'g'), UA_PARTIAL_TO);
+
 function getUA(host) {
   var url = location.pathname;
   url = host + url.slice(0, url.lastIndexOf('/')) + '/user_agent.sjs';
 
   var xhr = new XMLHttpRequest();
   xhr.open('GET', url, false); // sync request
   xhr.send();
   is(xhr.status, 200, 'request failed');
@@ -73,16 +79,22 @@ const OVERRIDES = [
     host: 'http://sub2.test1.example.org'
   },
   {
     domain: 'sub1.test2.example.org',
     override: '%OS%',
     expected: SpecialPowers.Services.appinfo.OS,
     host: 'http://sub1.test2.example.org'
   },
+  {
+    domain: 'sub2.test2.example.org',
+    override: UA_PARTIAL_OVERRIDE,
+    expected: UA_PARTIAL_EXPECTED,
+    host: 'http://sub2.test2.example.org'
+  },
 ];
 
 function getServerURL() {
   var url = location.pathname;
   return location.origin + url.slice(0, url.lastIndexOf('/')) + '/user_agent_update.sjs?';
 }
 
 function getUpdateURL() {