Bug 1529258 - Part 1: Slightly modernise HelperApps.jsm r=geckoview-reviewers,agi
authorJan Henning <jh+bugzilla@buttercookie.de>
Mon, 25 Feb 2019 18:50:11 +0000
changeset 519074 66d9e6a13efc5a05b57ee83be94d9194983fb442
parent 519073 fec419c7471bcd3d7c2a5cbb4670106323f3252c
child 519075 13ac435c29366bfe5688ea06fbc580e7dd96d24d
push id10862
push userffxbld-merge
push dateMon, 11 Mar 2019 13:01:11 +0000
treeherdermozilla-beta@a2e7f5c935da [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgeckoview-reviewers, agi
bugs1529258
milestone67.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1529258 - Part 1: Slightly modernise HelperApps.jsm r=geckoview-reviewers,agi Use the more concise way of defining functions and more arrow functions. Also use a class to define "App" objects. Differential Revision: https://phabricator.services.mozilla.com/D20859
mobile/android/modules/HelperApps.jsm
--- a/mobile/android/modules/HelperApps.jsm
+++ b/mobile/android/modules/HelperApps.jsm
@@ -17,31 +17,31 @@ ChromeUtils.defineModuleGetter(this, "Ev
 XPCOMUtils.defineLazyGetter(this, "ContentAreaUtils", function() {
   let ContentAreaUtils = {};
   Services.scriptloader.loadSubScript("chrome://global/content/contentAreaUtils.js", ContentAreaUtils);
   return ContentAreaUtils;
 });
 
 var EXPORTED_SYMBOLS = ["App", "HelperApps"];
 
-function App(data) {
-  this.name = data.name;
-  this.isDefault = data.isDefault;
-  this.packageName = data.packageName;
-  this.activityName = data.activityName;
-  this.iconUri = "-moz-icon://" + data.packageName;
-}
+class App {
+  constructor(data) {
+    this.name = data.name;
+    this.isDefault = data.isDefault;
+    this.packageName = data.packageName;
+    this.activityName = data.activityName;
+    this.iconUri = "-moz-icon://" + data.packageName;
+  }
 
-App.prototype = {
   // callback will be null if a result is not requested
-  launch: function(uri, callback) {
+  launch(uri, callback) {
     HelperApps._launchApp(this, uri, callback);
     return false;
-  },
-};
+  }
+}
 
 var HelperApps =  {
   get defaultBrowsers() {
     delete this.defaultBrowsers;
     this.defaultBrowsers = this._getHandlers("http://www.example.com", {
       filterBrowsers: false,
       filterHtml: false,
     });
@@ -54,60 +54,60 @@ var HelperApps =  {
   get defaultHtmlHandlers() {
     delete this.defaultHtmlHandlers;
     return this.defaultHtmlHandlers = this._getHandlers("http://www.example.com/index.html", {
       filterBrowsers: false,
       filterHtml: false,
     });
   },
 
-  _getHandlers: function(url, options) {
+  _getHandlers(url, options) {
     let values = {};
 
     let handlers = this.getAppsForUri(Services.io.newURI(url), options);
-    handlers.forEach(function(app) {
+    handlers.forEach(app => {
       values[app.name] = app;
-    }, this);
+    });
 
     return values;
   },
 
   get protoSvc() {
     delete this.protoSvc;
     return this.protoSvc = Cc["@mozilla.org/uriloader/external-protocol-service;1"].getService(Ci.nsIExternalProtocolService);
   },
 
   get urlHandlerService() {
     delete this.urlHandlerService;
     return this.urlHandlerService = Cc["@mozilla.org/uriloader/external-url-handler-service;1"].getService(Ci.nsIExternalURLHandlerService);
   },
 
-  prompt: function showPicker(apps, promptOptions, callback) {
+  prompt(apps, promptOptions, callback) {
     let p = new Prompt(promptOptions).addIconGrid({ items: apps });
     p.show(callback);
   },
 
-  getAppsForProtocol: function getAppsForProtocol(scheme) {
+  getAppsForProtocol(scheme) {
     let protoHandlers = this.protoSvc.getProtocolHandlerInfoFromOS(scheme, {}).possibleApplicationHandlers;
 
     let results = {};
     for (let i = 0; i < protoHandlers.length; i++) {
       try {
         let protoApp = protoHandlers.queryElementAt(i, Ci.nsIHandlerApp);
         results[protoApp.name] = new App({
           name: protoApp.name,
           description: protoApp.detailedDescription,
         });
       } catch (e) {}
     }
 
     return results;
   },
 
-  getAppsForUri: function getAppsForUri(uri, flags = { }, callback) {
+  getAppsForUri(uri, flags = { }, callback) {
     // Return early for well-known internal schemes
     if (!uri || uri.schemeIs("about") || uri.schemeIs("chrome")) {
       if (callback) {
         callback([]);
       }
       return [];
     }
 
@@ -119,92 +119,90 @@ var HelperApps =  {
     let parseData = (apps) => {
       if (!apps) {
         return [];
       }
 
       apps = this._parseApps(apps);
 
       if (flags.filterBrowsers) {
-        apps = apps.filter((app) => {
-          return app.name && !this.defaultBrowsers[app.name];
-        });
+        apps = apps.filter(app =>
+          app.name && !this.defaultBrowsers[app.name]);
       }
 
       // Some apps will register for html files (the Samsung Video player) but should be shown
       // for non-HTML files (like videos). This filters them only if the page has an htm of html
       // file extension.
       if (flags.filterHtml) {
         // Matches from the first '.' to the end of the string, '?', or '#'
         let ext = /\.([^\?#]*)/.exec(uri.pathQueryRef);
         if (ext && (ext[1] === "html" || ext[1] === "htm")) {
-          apps = apps.filter(function(app) {
-            return app.name && !this.defaultHtmlHandlers[app.name];
-          }, this);
+          apps = apps.filter(app =>
+            app.name && !this.defaultHtmlHandlers[app.name]);
         }
       }
 
       return apps;
     };
 
     if (!callback) {
       let data = null;
       // Use dispatch to enable synchronous callback for Gecko thread event.
       EventDispatcher.instance.dispatch(msg.type, msg, {
-        onSuccess: (result) => { data = result; },
+        onSuccess: result => { data = result; },
         onError: () => { throw new Error("Intent:GetHandler callback failed"); },
       });
       if (data === null) {
         throw new Error("Intent:GetHandler did not return data");
       }
       return parseData(data);
     }
-      EventDispatcher.instance.sendRequestForResult(msg).then(function(data) {
+      EventDispatcher.instance.sendRequestForResult(msg).then(data => {
         callback(parseData(data));
       });
   },
 
-  launchUri: function launchUri(uri) {
+  launchUri(uri) {
     let msg = this._getMessage("Intent:Open", uri);
     EventDispatcher.instance.sendRequest(msg);
   },
 
-  _parseApps: function _parseApps(appInfo) {
+  _parseApps(appInfo) {
     // appInfo -> {apps: [app1Label, app1Default, app1PackageName, app1ActivityName, app2Label, app2Defaut, ...]}
     // see GeckoAppShell.java getHandlersForIntent function for details
     const numAttr = 4; // 4 elements per ResolveInfo: label, default, package name, activity name.
 
     let apps = [];
     for (let i = 0; i < appInfo.length; i += numAttr) {
       apps.push(new App({"name": appInfo[i],
                  "isDefault": appInfo[i + 1],
                  "packageName": appInfo[i + 2],
                  "activityName": appInfo[i + 3]}));
     }
 
     return apps;
   },
 
-  _getMessage: function(type, uri, options = {}) {
+  _getMessage(type, uri, options = {}) {
     let mimeType = options.mimeType;
     if (uri && mimeType == undefined) {
       mimeType = ContentAreaUtils.getMIMETypeForURI(uri) || "";
     }
 
     return {
       type: type,
       mime: mimeType,
       action: options.action || "", // empty action string defaults to android.intent.action.VIEW
       url: uri ? uri.displaySpec : "",
       packageName: options.packageName || "",
       className: options.className || "",
     };
   },
 
-  _launchApp: function launchApp(app, uri, callback) {
+  _launchApp(app, uri, callback) {
     if (callback) {
         let msg = this._getMessage("Intent:OpenForResult", uri, {
             packageName: app.packageName,
             className: app.activityName,
         });
 
         EventDispatcher.instance.sendRequestForResult(msg).then(callback);
     } else {