Bug 1386807 - Part 1: Allow platform-specific UA overrides draft
authorDennis Schubert <dschubert@mozilla.com>
Fri, 09 Feb 2018 18:44:56 +0100
changeset 785638 04a2082ecd0b698385abb4a534fc80606b0eb447
parent 785586 cc0d7de218cb0c260c8ba0cf6637845ad2222f49
child 785639 8a98e9d59292e9d03aa57f581335c7913b4178fa
push id107289
push userbmo:dschubert@mozilla.com
push dateFri, 20 Apr 2018 15:30:56 +0000
bugs1386807
milestone61.0a1
Bug 1386807 - Part 1: Allow platform-specific UA overrides MozReview-Commit-ID: 8OAVyZ3j9Rn
browser/extensions/webcompat/content/data/ua_overrides.jsm
browser/extensions/webcompat/content/lib/ua_overrider.jsm
browser/extensions/webcompat/install.rdf.in
--- a/browser/extensions/webcompat/content/data/ua_overrides.jsm
+++ b/browser/extensions/webcompat/content/data/ua_overrides.jsm
@@ -3,40 +3,47 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 /**
  * This is an array of objects that specify user agent overrides. Each object
  * can have three attributes:
  *
  *   * `baseDomain`, required: The base domain that further checks and user
  *     agents override are applied to. This does not include subdomains.
+ *   * `applications`: Array of applications this override is valid in.
+ *     Defaults to ["firefox"], can be one or more of:
+ *     * `firefox`: Firefox Desktop (regardless of the operating system)
+ *     * `fennec`: Firefox for Android
  *   * `uriMatcher`: Function that gets the requested URI passed in the first
  *     argument and needs to return boolean whether or not the override should
  *     be applied. If not provided, the user agent override will be applied
  *     every time.
  *   * `uaTransformer`, required: Function that gets the original Firefox user
  *     agent passed as its first argument and needs to return a string that
  *     will be used as the the user agent for this URI.
  *
  * Examples:
  *
- * Gets applied for all requests to mozilla.org and subdomains:
+ * Gets applied for all requests to mozilla.org and subdomains made on
+ * Firefox Desktop:
  *
  * ```
  *   {
  *     baseDomain: "mozilla.org",
+ *     uriMatcher: (uri) => uri.includes("/app/"),
  *     uaTransformer: (originalUA) => `Ohai Mozilla, it's me, ${originalUA}`
  *   }
  * ```
  *
- * Applies to *.example.com/app/*:
+ * Applies to *.example.com/app/* on Firefox for Android:
  *
  * ```
  *   {
  *     baseDomain: "example.com",
+ *     applications: ["fennec"],
  *     uriMatcher: (uri) => uri.includes("/app/"),
  *     uaTransformer: (originalUA) => originalUA.replace("Firefox", "Otherfox")
  *   }
  * ```
  */
 
 const UAOverrides = [
 
@@ -44,16 +51,17 @@ const UAOverrides = [
    * This is a dummy override that applies a Chrome UA to a dummy site that
    * blocks all browsers but Chrome.
    *
    * This was only put in place to allow QA to test this system addon on an
    * actual site, since we were not able to find a proper override in time.
    */
   {
     baseDomain: "schub.io",
+    applications: ["firefox", "fennec"],
     uriMatcher: (uri) => uri.includes("webcompat-ua-dummy.schub.io"),
     uaTransformer: (originalUA) => {
       let prefix = originalUA.substr(0, originalUA.indexOf(")") + 1);
       return `${prefix} AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36`;
     }
   }
 ];
 
--- a/browser/extensions/webcompat/content/lib/ua_overrider.jsm
+++ b/browser/extensions/webcompat/content/lib/ua_overrider.jsm
@@ -11,17 +11,36 @@ XPCOMUtils.defineLazyServiceGetter(this,
 class UAOverrider {
   constructor(overrides) {
     this._overrides = {};
 
     this.initOverrides(overrides);
   }
 
   initOverrides(overrides) {
+    // on xpcshell tests, there is no impleentation for nsIXULAppInfo, so this
+    // might fail there. To have all of our test cases running at all times,
+    // assume they are on Desktop for now.
+    let currentApplication = "firefox";
+    try {
+      currentApplication = Services.appinfo.name.toLowerCase();
+    } catch (_) {}
+
     for (let override of overrides) {
+      // Firefox for Desktop is the default application for all overrides.
+      if (!override.applications) {
+        override.applications = ["firefox"];
+      }
+
+      // If the current application is not targeted by the override in question,
+      // we can skip adding the override to our checks entirely.
+      if (!override.applications.includes(currentApplication)) {
+        continue;
+      }
+
       if (!this._overrides[override.baseDomain]) {
         this._overrides[override.baseDomain] = [];
       }
 
       if (!override.uriMatcher) {
         override.uriMatcher = () => true;
       }
 
--- a/browser/extensions/webcompat/install.rdf.in
+++ b/browser/extensions/webcompat/install.rdf.in
@@ -10,23 +10,31 @@
 
   <Description about="urn:mozilla:install-manifest">
     <em:id>webcompat@mozilla.org</em:id>
     <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. -->
+    <!-- Firefox Desktop -->
     <em:targetApplication>
       <Description>
         <em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
         <em:minVersion>@MOZ_APP_VERSION@</em:minVersion>
         <em:maxVersion>@MOZ_APP_MAXVERSION@</em:maxVersion>
       </Description>
     </em:targetApplication>
 
+    <!-- Firefox for Android -->
+    <em:targetApplication>
+      <Description>
+        <em:id>{aa3c5121-dab2-40e2-81ca-7ea25febc110}</em:id>
+        <em:minVersion>@MOZ_APP_VERSION@</em:minVersion>
+        <em:maxVersion>@MOZ_APP_MAXVERSION@</em:maxVersion>
+      </Description>
+    </em:targetApplication>
+
     <!-- Front End MetaData -->
     <em:name>Web Compat</em:name>
     <em:description>Urgent post-release fixes for web compatibility.</em:description>
   </Description>
 </RDF>