Bug 891968 - Only have one copy of each overriding UA string. f=hurley r=jduell
authorDão Gottwald <dao@mozilla.com>
Fri, 26 Jul 2013 09:39:01 +0200
changeset 140004 37567e5bbe9f5432c245f8d82822cd361f644658
parent 140003 46d73e889cb47c1497c9a967d055ab5123e05514
child 140005 8aa1d0e0025d65f5bc17e6d5ec89ab1f29bc2482
push id1937
push userdgottwald@mozilla.com
push dateFri, 26 Jul 2013 07:39:13 +0000
treeherderfx-team@37567e5bbe9f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjduell
bugs891968
milestone25.0a1
Bug 891968 - Only have one copy of each overriding UA string. f=hurley r=jduell
netwerk/protocol/http/UserAgentOverrides.jsm
--- a/netwerk/protocol/http/UserAgentOverrides.jsm
+++ b/netwerk/protocol/http/UserAgentOverrides.jsm
@@ -13,17 +13,17 @@ Components.utils.import("resource://gre/
 
 const PREF_OVERRIDES_ENABLED = "general.useragent.site_specific_overrides";
 const DEFAULT_UA = Cc["@mozilla.org/network/protocol;1?name=http"]
                      .getService(Ci.nsIHttpProtocolHandler)
                      .userAgent;
 const MAX_OVERRIDE_FOR_HOST_CACHE_SIZE = 250;
 
 var gPrefBranch;
-var gOverrides;
+var gOverrides = new Map;
 var gOverrideForHostCache = new Map;
 var gInitialized = false;
 var gOverrideFunctions = [
   function (aHttpChannel) UserAgentOverrides.getOverrideForURI(aHttpChannel.URI)
 ];
 
 this.UserAgentOverrides = {
   init: function uao_init() {
@@ -58,20 +58,20 @@ this.UserAgentOverrides = {
     let host = aURI.asciiHost;
 
     let override = gOverrideForHostCache.get(host);
     if (override !== undefined)
       return override;
 
     override = null;
 
-    for (let domain in gOverrides) {
+    for (let [domain, userAgent] of gOverrides) {
       if (host == domain ||
           host.endsWith("." + domain)) {
-        override = gOverrides[domain];
+        override = userAgent;
         break;
       }
     }
 
     if (gOverrideForHostCache.size >= MAX_OVERRIDE_FOR_HOST_CACHE_SIZE) {
       gOverrideForHostCache.clear();
     }
     gOverrideForHostCache.set(host, override);
@@ -88,33 +88,40 @@ this.UserAgentOverrides = {
 
     Services.prefs.removeObserver(PREF_OVERRIDES_ENABLED, buildOverrides);
 
     Services.obs.removeObserver(HTTP_on_modify_request, "http-on-modify-request");
   }
 };
 
 function buildOverrides() {
-  gOverrides = {};
+  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);
 
-    let [search, replace] = override.split("#", 2);
-    if (search && replace) {
-      gOverrides[domain] = DEFAULT_UA.replace(new RegExp(search, "g"), replace);
-    } else {
-      gOverrides[domain] = 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);
     }
+
+    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);