Bug 1337905 - Update WebCompat Go Faster addon to version 1.1. r?felipe draft
authorDennis Schubert <dschubert@mozilla.com>
Fri, 10 Feb 2017 11:15:56 +0100
changeset 481688 8d04b89f627029859b91f9dd2e511bdfea53210f
parent 481595 25a94c1047e793ef096d8556fa3c26dd72bd37d7
child 545287 36b466efad5718ed9c4a2eafc7069a185d0bc09f
push id44920
push userdschubert@mozilla.com
push dateFri, 10 Feb 2017 13:13:47 +0000
reviewersfelipe
bugs1337905
milestone54.0a1
Bug 1337905 - Update WebCompat Go Faster addon to version 1.1. r?felipe MozReview-Commit-ID: FMgf5E9Obzk
browser/extensions/webcompat/content/lib/ua_overrider.jsm
browser/extensions/webcompat/install.rdf.in
browser/extensions/webcompat/moz.build
browser/extensions/webcompat/test/browser_overrider.js
--- a/browser/extensions/webcompat/content/lib/ua_overrider.jsm
+++ b/browser/extensions/webcompat/content/lib/ua_overrider.jsm
@@ -11,17 +11,16 @@ const DefaultUA = Cc["@mozilla.org/netwo
 const NS_HTTP_ON_USERAGENT_REQUEST_TOPIC = "http-on-useragent-request";
 
 XPCOMUtils.defineLazyModuleGetter(this, "Services", "resource://gre/modules/Services.jsm");
 XPCOMUtils.defineLazyServiceGetter(this, "eTLDService", "@mozilla.org/network/effective-tld-service;1", "nsIEffectiveTLDService");
 
 class UAOverrider {
   constructor(overrides) {
     this._overrides = {};
-    this._overrideForURICache = new Map();
 
     this.initOverrides(overrides);
   }
 
   initOverrides(overrides) {
     for (let override of overrides) {
       if (!this._overrides[override.baseDomain]) {
         this._overrides[override.baseDomain] = [];
@@ -44,54 +43,38 @@ class UAOverrider {
   }
 
   observe(subject, topic) {
     if (topic !== NS_HTTP_ON_USERAGENT_REQUEST_TOPIC) {
       return;
     }
 
     let channel = subject.QueryInterface(Components.interfaces.nsIHttpChannel);
-    let uaOverride = this.getUAForURI(channel.URI);
+    let uaOverride = this.lookupUAOverride(channel.URI);
 
     if (uaOverride) {
       console.log("The user agent has been overridden for compatibility reasons.");
       channel.setRequestHeader("User-Agent", uaOverride, false);
     }
   }
 
-  getUAForURI(uri) {
-    let bareUri = uri.specIgnoringRef;
-    if (this._overrideForURICache.has(bareUri)) {
-      // Although the cache could have an entry to a bareUri, `false` is also
-      // a value that could be cached. A `false` cache entry means that there
-      // is no override for this URI.
-      // We cache these to avoid having to walk through all overrides to see
-      // if a domain matches.
-      return this._overrideForURICache.get(bareUri);
+  /**
+   * Try to use the eTLDService to get the base domain (will return example.com
+   * for http://foo.bar.example.com/foo/bar).
+   *
+   * However, the eTLDService is a bit picky and throws whenever we pass a
+   * blank host name or an IP into it, see bug 1337785. Since we do not plan on
+   * override UAs for such cases, we simply catch everything and return false.
+   */
+  getBaseDomainFromURI(uri) {
+    try {
+      return eTLDService.getBaseDomain(uri);
+    } catch (_) {
+      return false;
     }
-
-    let finalUA = this.lookupUAOverride(uri);
-    this._overrideForURICache.set(bareUri, finalUA);
-
-    return finalUA;
-  }
-
-  /**
-   * This function gets called from within the embedded webextension to check
-   * if the current site has been overriden or not. We only check the cached
-   * URI list here, but that's safe in our case since the tabUpdateHandler will
-   * always run after our message observer.
-   */
-  hasUAForURIInCache(uri) {
-    let bareUri = uri.specIgnoringRef;
-    if (this._overrideForURICache.has(bareUri)) {
-      return !!this._overrideForURICache.get(bareUri);
-    }
-
-    return false;
   }
 
   /**
    * This function returns a User Agent based on the URI passed into. All
    * override rules are defined in data/ua_overrides.jsm and the required format
    * is explained there.
    *
    * Since it is expected and designed to have more than one override per base
@@ -101,18 +84,18 @@ class UAOverrider {
    * If the uriMatcher function returns true, the uaTransformer function gets
    * called and its result will be used as the Use Agent for the current
    * request.
    *
    * If there are more than one possible overrides, that is if two or more
    * uriMatchers would return true, the first one gets applied.
    */
   lookupUAOverride(uri) {
-    let baseDomain = eTLDService.getBaseDomain(uri);
-    if (this._overrides[baseDomain]) {
+    let baseDomain = this.getBaseDomainFromURI(uri);
+    if (baseDomain && this._overrides[baseDomain]) {
       for (let uaOverride of this._overrides[baseDomain]) {
         if (uaOverride.uriMatcher(uri.specIgnoringRef)) {
           return uaOverride.uaTransformer(DefaultUA);
         }
       }
     }
 
     return false;
--- a/browser/extensions/webcompat/install.rdf.in
+++ b/browser/extensions/webcompat/install.rdf.in
@@ -5,17 +5,17 @@
 
 #filter substitution
 
 <RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
      xmlns:em="http://www.mozilla.org/2004/em-rdf#">
 
   <Description about="urn:mozilla:install-manifest">
     <em:id>webcompat@mozilla.org</em:id>
-    <em:version>1.0</em:version>
+    <em:version>1.1</em:version>
     <em:type>2</em:type>
     <em:bootstrap>true</em:bootstrap>
     <em:multiprocessCompatible>true</em:multiprocessCompatible>
 
     <!-- Target Application this extension can install into,
         with minimum and maximum supported versions. -->
     <em:targetApplication>
       <Description>
--- a/browser/extensions/webcompat/moz.build
+++ b/browser/extensions/webcompat/moz.build
@@ -12,8 +12,11 @@ FINAL_TARGET_FILES.features['webcompat@m
 ]
 
 FINAL_TARGET_PP_FILES.features['webcompat@mozilla.org'] += [
   'install.rdf.in'
 ]
 
 BROWSER_CHROME_MANIFESTS += ['test/browser.ini']
 JAR_MANIFESTS += ['jar.mn']
+
+with Files('**'):
+  BUG_COMPONENT = ('Web Compatibility', 'Go Faster')
--- a/browser/extensions/webcompat/test/browser_overrider.js
+++ b/browser/extensions/webcompat/test/browser_overrider.js
@@ -17,24 +17,24 @@ function getnsIURI(uri) {
 add_task(function test() {
   let overrider = new UAOverrider([
     {
       baseDomain: "example.org",
       uaTransformer: () => "Test UA"
     }
   ]);
 
-  let finalUA = overrider.getUAForURI(getnsIURI("http://www.example.org/foobar/"));
+  let finalUA = overrider.lookupUAOverride(getnsIURI("http://www.example.org/foobar/"));
   is(finalUA, "Test UA", "Overrides the UA without a matcher function");
 });
 
 add_task(function test() {
   let overrider = new UAOverrider([
     {
       baseDomain: "example.org",
       uriMatcher: () => false,
       uaTransformer: () => "Test UA"
     }
   ]);
 
-  let finalUA = overrider.getUAForURI(getnsIURI("http://www.example.org/foobar/"));
+  let finalUA = overrider.lookupUAOverride(getnsIURI("http://www.example.org/foobar/"));
   isnot(finalUA, "Test UA", "Does not override the UA with the matcher returning false");
 });