diff --git a/atom/common/api/resources/context_menus_bindings.js b/atom/common/api/resources/context_menus_bindings.js index 275aee60e7..15bec8d718 100644 --- a/atom/common/api/resources/context_menus_bindings.js +++ b/atom/common/api/resources/context_menus_bindings.js @@ -1,15 +1,49 @@ -var ipc = require('ipc_utils'); -var process = requireNative('process'); -var extensionId = process.GetExtensionId(); +var ipc = require('ipc_utils') +var process = requireNative('process') +var extensionId = process.GetExtensionId() + +var id = 0 + +var createMenuItemId = () => { + return 'context-menus-' + extensionId + id++ +} + +var createResponseId = () => { + return extensionId + id++ +} var binding = { - removeAll: function () { - console.warn('chrome.contentMenus.removeAll is not supported yet') + onClicked: { + addListener: function (cb) { + ipc.on('chrome-context-menus-clicked', function(evt, info, tab) { + cb(info, tab) + }) + }, + removeListener: function (cb) { + // TODO + // ipc.off('chrome-context-menus-clicked', cb) + } + }, + removeAll: function (cb) { + var responseId = createResponseId() + cb && ipc.once('chrome-context-menus-remove-all-response-' + responseId, function(evt) { + cb() + }) + ipc.send('chrome-context-menus-remove-all', responseId, extensionId) }, create: function (properties, cb) { - console.warn('chrome.contentMenus.create is not supported yet') - cb && cb() + var responseId = createResponseId() + var menuItemId = properties.id || createMenuItemId() + cb && ipc.once('chrome-context-menus-create-response-' + responseId, function(evt) { + cb() + }) + properties.onclick && ipc.on('chrome-context-menus-clicked', (evt, info, tab) => { + if (info.menuItemId === menuItemId) { + properties.onclick(info, tab) + } + }) + ipc.send('chrome-context-menus-create', responseId, extensionId, menuItemId, properties) } } -exports.binding = binding; +exports.binding = binding diff --git a/lib/browser/api/extensions.js b/lib/browser/api/extensions.js index f1bba78938..73c1815508 100644 --- a/lib/browser/api/extensions.js +++ b/lib/browser/api/extensions.js @@ -32,6 +32,7 @@ process.on('EXTENSION_READY_INTERNAL', (installInfo) => { addBackgroundPageEvent(installInfo.id, 'chrome-browser-action-clicked') process.emit('chrome-browser-action-registered', installInfo.id, details) } + addBackgroundPageEvent(installInfo.id, 'chrome-context-menus-clicked') }) // TODO(bridiver) move these into modules @@ -611,3 +612,20 @@ ipcMain.on('autofill-popup-hidden', function (evt, tabId) { if (webContents) webContents.autofillPopupHidden() }) + +ipcMain.on('chrome-context-menus-remove-all', function (evt, responseId, extensionId) { + process.emit('chrome-context-menus-remove-all', extensionId) + evt.sender.send('chrome-context-menus-remove-all-response-' + responseId) +}) + +ipcMain.on('chrome-context-menus-create', function (evt, responseId, extensionId, menuItemId, properties) { + process.emit('chrome-context-menus-create', extensionId, menuItemId, properties) + evt.sender.send('chrome-context-menus-create-response-' + responseId) +}) + +process.on('chrome-context-menus-clicked', function (extensionId, tabId, info) { + let response = getTabValue(tabId) + if (response) { + sendToBackgroundPages(extensionId, getSessionForTab(tabId), 'chrome-context-menus-clicked', info, response) + } +})