Bug 1363016 - Implement contextMenus.create WebExtension API method on android. draft
authorTushar Saini (:shatur) <tushar.saini1285@gmail.com>
Tue, 15 Aug 2017 11:39:45 +0530
changeset 646699 d538ff4b0b834fe8410db97403d316d8afb88b00
parent 646265 b95b1638db48fc3d450b95b98da6bcd2f9326d2f
child 726338 0e36ab2a84c2bada2fbf0267eeb402efa390f857
push id74223
push userbmo:tushar.saini1285@gmail.com
push dateTue, 15 Aug 2017 18:54:10 +0000
bugs1363016
milestone57.0a1
Bug 1363016 - Implement contextMenus.create WebExtension API method on android. MozReview-Commit-ID: F13ZFMuXviY
mobile/android/components/extensions/ext-android.js
mobile/android/components/extensions/ext-contextMenus.js
mobile/android/components/extensions/jar.mn
mobile/android/components/extensions/schemas/context_menus.json
mobile/android/components/extensions/schemas/jar.mn
--- a/mobile/android/components/extensions/ext-android.js
+++ b/mobile/android/components/extensions/ext-android.js
@@ -63,16 +63,25 @@ extensions.registerModules({
     url: "chrome://browser/content/ext-browsingData.js",
     schema: "chrome://browser/content/schemas/browsing_data.json",
     scopes: ["addon_parent"],
     manifest: ["browsing_data"],
     paths: [
       ["browsingData"],
     ],
   },
+  contextMenus: {
+    url: "chrome://browser/content/ext-contextMenus.js",
+    schema: "chrome://browser/content/schemas/context_menus.json",
+    scopes: ["addon_parent"],
+    manifest: ["context_menus"],
+    paths: [
+      ["contextMenus"],
+    ],
+  },
   pageAction: {
     url: "chrome://browser/content/ext-pageAction.js",
     schema: "chrome://browser/content/schemas/page_action.json",
     scopes: ["addon_parent"],
     manifest: ["page_action"],
     paths: [
       ["pageAction"],
     ],
new file mode 100644
--- /dev/null
+++ b/mobile/android/components/extensions/ext-contextMenus.js
@@ -0,0 +1,42 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+XPCOMUtils.defineLazyModuleGetter(this, "Services",
+                                  "resource://gre/modules/Services.jsm");
+
+let DEBUG = "Debug_contextMenu";
+let windows = Services.wm.getMostRecentWindow("navigator:browser");
+
+function addSelector(contextType) {
+  dump(DEBUG + "adSelector called. context: " + contextType);
+  switch (contextType[0]) {
+    case "all" :
+      return windows.NativeWindow.contextmenus.SelectorContext("*");
+    case "audio" :
+      return windows.NativeWindow.contextmenus.SelectorContext("audio");
+    case "image" :
+      return windows.NativeWindow.contextmenus.imageSaveableContext();
+    case "video" :
+      return windows.NativeWindow.contextmenus.videoContext();
+  }
+}
+
+this.contextMenus = class extends ExtensionAPI {
+  getAPI(context) {
+    return {
+      contextMenus: {
+        create(createProperties) {
+          dump(DEBUG + "create called.");
+
+          windows.NativeWindow.contextmenus.add(
+            createProperties.title,
+            addSelector(createProperties.contexts),
+            createProperties.onclick
+          );
+          return Promise.resolve("Added");
+        },
+      },
+    };
+  }
+};
--- a/mobile/android/components/extensions/jar.mn
+++ b/mobile/android/components/extensions/jar.mn
@@ -3,11 +3,12 @@
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 chrome.jar:
     content/ext-android.js
     content/ext-c-android.js
     content/ext-c-tabs.js
     content/ext-browserAction.js
     content/ext-browsingData.js
+    content/ext-contextMenus.js
     content/ext-pageAction.js
     content/ext-tabs.js
     content/ext-utils.js
copy from browser/components/extensions/schemas/menus.json
copy to mobile/android/components/extensions/schemas/context_menus.json
--- a/browser/components/extensions/schemas/menus.json
+++ b/mobile/android/components/extensions/schemas/context_menus.json
@@ -6,41 +6,26 @@
   {
     "namespace": "manifest",
     "types": [
       {
         "$extend": "Permission",
         "choices": [{
           "type": "string",
           "enum": [
-            "menus",
             "contextMenus"
           ]
         }]
       }
     ]
   },
   {
     "namespace": "contextMenus",
+    "description": "Use the browser.contextMenus API to add items to the browser's context menu. You can choose what types of objects your context menu additions apply to, such as images, hyperlinks, and pages.",
     "permissions": ["contextMenus"],
-    "description": "Use the browser.contextMenus API to add items to the browser's context menu. You can choose what types of objects your context menu additions apply to, such as images, hyperlinks, and pages.",
-    "$import": "menus",
-    "types": [
-      {
-        "id": "ContextType",
-        "type": "string",
-        "enum": ["all", "page", "frame", "selection", "link", "editable", "password", "image", "video", "audio", "launcher", "browser_action", "page_action", "tab"],
-        "description": "The different contexts a menu can appear in. Specifying 'all' is equivalent to the combination of all other contexts except for 'tab' and 'tools_menu'."
-      }
-    ]
-  },
-  {
-    "namespace": "menus",
-    "permissions": ["menus"],
-    "description": "Use the browser.menus API to add items to the browser's menus. You can choose what types of objects your context menu additions apply to, such as images, hyperlinks, and pages.",
     "properties": {
       "ACTION_MENU_TOP_LEVEL_LIMIT": {
         "value": 6,
         "description": "The maximum number of top level extension items that can be added to an extension action context menu. Any items beyond this limit will be ignored."
       }
     },
     "types": [
       {
@@ -118,40 +103,25 @@
             "type": "boolean",
             "optional": true,
             "description": "A flag indicating the state of a checkbox or radio item before it was clicked."
           },
           "checked": {
             "type": "boolean",
             "optional": true,
             "description": "A flag indicating the state of a checkbox or radio item after it is clicked."
-          },
-          "modifiers": {
-            "type": "array",
-            "items": {
-              "type": "string",
-              "enum": ["Shift", "Alt", "Command", "Ctrl", "MacCtrl"]
-            },
-            "description": "An array of keyboard modifiers that were held while the menu item was clicked."
           }
         }
       }
     ],
     "functions": [
       {
         "name": "create",
         "type": "function",
         "description": "Creates a new context menu item. Note that if an error occurs during creation, you may not find out until the creation callback fires (the details will be in $(ref:runtime.lastError)).",
-        "returns": {
-          "choices": [
-            { "type": "integer" },
-            { "type": "string" }
-          ],
-          "description": "The ID of the newly created item."
-        },
         "parameters": [
           {
             "type": "object",
             "name": "createProperties",
             "properties": {
               "type": {
                 "$ref": "ItemType",
                 "optional": true,
@@ -190,17 +160,17 @@
               },
               "onclick": {
                 "type": "function",
                 "optional": true,
                 "description": "A function that will be called back when the menu item is clicked. Event pages cannot use this; instead, they should register a listener for $(ref:contextMenus.onClicked).",
                 "parameters": [
                   {
                     "name": "info",
-                    "$ref": "contextMenusInternal.OnClickData",
+                    "$ref": "OnClickData",
                     "description": "Information about the item clicked and the context where the click happened."
                   },
                   {
                     "name": "tab",
                     "$ref": "tabs.Tab",
                     "description": "The details of the tab where the click took place. Note: this parameter only present for extensions."
                   }
                 ]
@@ -245,16 +215,17 @@
             "parameters": []
           }
         ]
       },
       {
         "name": "update",
         "type": "function",
         "description": "Updates a previously created context menu item.",
+        "unsupported": true,
         "async": "callback",
         "parameters": [
           {
             "choices": [
               { "type": "integer" },
               { "type": "string" }
             ],
             "name": "id",
@@ -332,16 +303,17 @@
             "description": "Called when the context menu has been updated."
           }
         ]
       },
       {
         "name": "remove",
         "type": "function",
         "description": "Removes a context menu item.",
+        "unsupported": true,
         "async": "callback",
         "parameters": [
           {
             "choices": [
               { "type": "integer" },
               { "type": "string" }
             ],
             "name": "menuItemId",
@@ -355,16 +327,17 @@
             "description": "Called when the context menu has been removed."
           }
         ]
       },
       {
         "name": "removeAll",
         "type": "function",
         "description": "Removes all context menu items added by this extension.",
+        "unsupported": true,
         "async": "callback",
         "parameters": [
           {
             "type": "function",
             "name": "callback",
             "optional": true,
             "parameters": [],
             "description": "Called when removal is complete."
@@ -372,16 +345,17 @@
         ]
       }
     ],
     "events": [
       {
         "name": "onClicked",
         "type": "function",
         "description": "Fired when a context menu item is clicked.",
+        "unsupported": true,
         "parameters": [
           {
             "name": "info",
             "$ref": "OnClickData",
             "description": "Information about the item clicked and the context where the click happened."
           },
           {
             "name": "tab",
--- a/mobile/android/components/extensions/schemas/jar.mn
+++ b/mobile/android/components/extensions/schemas/jar.mn
@@ -1,9 +1,10 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 chrome.jar:
     content/schemas/browser_action.json
     content/schemas/browsing_data.json
+    content/schemas/context_menus.json
     content/schemas/page_action.json
     content/schemas/tabs.json