Bug 1234020: Part 2g - [webext] Return promises from the browserAction API. r=rpl draft
authorKris Maglione <maglione.k@gmail.com>
Mon, 01 Feb 2016 18:14:05 -0800
changeset 328978 83846197d4562d443e2fda56b4f0ab3132e6dd2b
parent 328977 17c48c04f53eee3efea78d6f294530d77c3611e8
child 328979 bce3ef2ff299cc79e068eb81500086cbc8cd22fd
push id10445
push usermaglione.k@gmail.com
push dateThu, 04 Feb 2016 21:38:16 +0000
reviewersrpl
bugs1234020
milestone47.0a1
Bug 1234020: Part 2g - [webext] Return promises from the browserAction API. r=rpl
browser/components/extensions/ext-browserAction.js
browser/components/extensions/schemas/browser_action.json
browser/components/extensions/test/browser/browser_ext_browserAction_context.js
--- a/browser/components/extensions/ext-browserAction.js
+++ b/browser/components/extensions/ext-browserAction.js
@@ -5,17 +5,16 @@
 XPCOMUtils.defineLazyModuleGetter(this, "CustomizableUI",
                                   "resource:///modules/CustomizableUI.jsm");
 
 Cu.import("resource://devtools/shared/event-emitter.js");
 
 Cu.import("resource://gre/modules/ExtensionUtils.jsm");
 var {
   EventManager,
-  runSafe,
 } = ExtensionUtils;
 
 const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
 
 // WeakMap[Extension -> BrowserAction]
 var browserActionMap = new WeakMap();
 
 function browserActionOf(extension) {
@@ -256,61 +255,62 @@ extensions.registerSchemaAPI("browserAct
         let title = details.title;
         // Clear the tab-specific title when given a null string.
         if (tab && title == "") {
           title = null;
         }
         browserActionOf(extension).setProperty(tab, "title", title);
       },
 
-      getTitle: function(details, callback) {
+      getTitle: function(details) {
         let tab = details.tabId !== null ? TabManager.getTab(details.tabId) : null;
         let title = browserActionOf(extension).getProperty(tab, "title");
-        runSafe(context, callback, title);
+        return Promise.resolve(title);
       },
 
-      setIcon: function(details, callback) {
+      setIcon: function(details) {
         let tab = details.tabId !== null ? TabManager.getTab(details.tabId) : null;
         let icon = IconDetails.normalize(details, extension, context);
         browserActionOf(extension).setProperty(tab, "icon", icon);
+        return Promise.resolve();
       },
 
       setBadgeText: function(details) {
         let tab = details.tabId !== null ? TabManager.getTab(details.tabId) : null;
         browserActionOf(extension).setProperty(tab, "badgeText", details.text);
       },
 
-      getBadgeText: function(details, callback) {
+      getBadgeText: function(details) {
         let tab = details.tabId !== null ? TabManager.getTab(details.tabId) : null;
         let text = browserActionOf(extension).getProperty(tab, "badgeText");
-        runSafe(context, callback, text);
+        return Promise.resolve(text);
       },
 
       setPopup: function(details) {
         let tab = details.tabId !== null ? TabManager.getTab(details.tabId) : null;
         // Note: Chrome resolves arguments to setIcon relative to the calling
         // context, but resolves arguments to setPopup relative to the extension
         // root.
         // For internal consistency, we currently resolve both relative to the
         // calling context.
         let url = details.popup && context.uri.resolve(details.popup);
         browserActionOf(extension).setProperty(tab, "popup", url);
       },
 
-      getPopup: function(details, callback) {
+      getPopup: function(details) {
         let tab = details.tabId !== null ? TabManager.getTab(details.tabId) : null;
         let popup = browserActionOf(extension).getProperty(tab, "popup");
-        runSafe(context, callback, popup);
+        return Promise.resolve(popup);
       },
 
       setBadgeBackgroundColor: function(details) {
         let tab = details.tabId !== null ? TabManager.getTab(details.tabId) : null;
         browserActionOf(extension).setProperty(tab, "badgeBackgroundColor", details.color);
       },
 
       getBadgeBackgroundColor: function(details, callback) {
         let tab = details.tabId !== null ? TabManager.getTab(details.tabId) : null;
         let color = browserActionOf(extension).getProperty(tab, "badgeBackgroundColor");
-        runSafe(context, callback, color);
+        return Promise.resolve(color);
       },
     },
   };
 });
--- a/browser/components/extensions/schemas/browser_action.json
+++ b/browser/components/extensions/schemas/browser_action.json
@@ -67,16 +67,17 @@
             }
           }
         ]
       },
       {
         "name": "getTitle",
         "type": "function",
         "description": "Gets the title of the browser action.",
+        "async": "callback",
         "parameters": [
           {
             "name": "details",
             "type": "object",
             "properties": {
               "tabId": {
                 "type": "integer",
                 "optional": true,
@@ -95,16 +96,17 @@
             ]
           }
         ]
       },
       {
         "name": "setIcon",
         "type": "function",
         "description": "Sets the icon for the browser action. The icon can be specified either as the path to an image file or as the pixel data from a canvas element, or as dictionary of either one of those. Either the <b>path</b> or the <b>imageData</b> property must be specified.",
+        "async": "callback",
         "parameters": [
           {
             "name": "details",
             "type": "object",
             "properties": {
               "imageData": {
                 "choices": [
                   { "$ref": "ImageDataType" },
@@ -164,16 +166,17 @@
             }
           }
         ]
       },
       {
         "name": "getPopup",
         "type": "function",
         "description": "Gets the html document set as the popup for this browser action.",
+        "async": "callback",
         "parameters": [
           {
             "name": "details",
             "type": "object",
             "properties": {
               "tabId": {
                 "type": "integer",
                 "optional": true,
@@ -214,16 +217,17 @@
             }
           }
         ]
       },
       {
         "name": "getBadgeText",
         "type": "function",
         "description": "Gets the badge text of the browser action. If no tab is specified, the non-tab-specific badge text is returned.",
+        "async": "callback",
         "parameters": [
           {
             "name": "details",
             "type": "object",
             "properties": {
               "tabId": {
                 "type": "integer",
                 "optional": true,
@@ -267,16 +271,17 @@
             }
           }
         ]
       },
       {
         "name": "getBadgeBackgroundColor",
         "type": "function",
         "description": "Gets the background color of the browser action.",
+        "async": "callback",
         "parameters": [
           {
             "name": "details",
             "type": "object",
             "properties": {
               "tabId": {
                 "type": "integer",
                 "optional": true,
@@ -324,16 +329,17 @@
           }
         ]
       },
       {
         "name": "openPopup",
         "type": "function",
         "description": "Opens the extension popup window in the active window but does not grant tab permissions.",
         "unsupported": true,
+        "async": "callback",
         "parameters": [
           {
             "type": "function",
             "name": "callback",
             "parameters": [
               {
                 "name": "popupView",
                 "type": "object",
--- a/browser/components/extensions/test/browser/browser_ext_browserAction_context.js
+++ b/browser/components/extensions/test/browser/browser_ext_browserAction_context.js
@@ -3,20 +3,20 @@
 "use strict";
 
 function* runTests(options) {
   function background(getTests) {
     // Gets the current details of the browser action, and returns a
     // promise that resolves to an object containing them.
     function getDetails(tabId) {
       return Promise.all([
-        new Promise(resolve => browser.browserAction.getTitle({tabId}, resolve)),
-        new Promise(resolve => browser.browserAction.getPopup({tabId}, resolve)),
-        new Promise(resolve => browser.browserAction.getBadgeText({tabId}, resolve)),
-        new Promise(resolve => browser.browserAction.getBadgeBackgroundColor({tabId}, resolve))]
+        browser.browserAction.getTitle({tabId}),
+        browser.browserAction.getPopup({tabId}),
+        browser.browserAction.getBadgeText({tabId}),
+        browser.browserAction.getBadgeBackgroundColor({tabId})]
       ).then(details => {
         return Promise.resolve({ title: details[0],
                                  popup: details[1],
                                  badge: details[2],
                                  badgeBackgroundColor: details[3] });
       });
     }