From 9a9e444c24c5d388a0c64fad16aaf878b9b22ad8 Mon Sep 17 00:00:00 2001 From: Taylor R Campbell Date: Tue, 24 Oct 2017 21:05:24 +0000 Subject: [PATCH] Draft implementation of chrome.tabs.duplicate, #7458. --- atom/common/api/resources/tabs_bindings.js | 12 ++++++++ lib/browser/api/extensions.js | 33 ++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/atom/common/api/resources/tabs_bindings.js b/atom/common/api/resources/tabs_bindings.js index 1edbd38e44..c73922ca1a 100644 --- a/atom/common/api/resources/tabs_bindings.js +++ b/atom/common/api/resources/tabs_bindings.js @@ -213,6 +213,18 @@ binding.registerCustomHook(function(bindingsAPI, extensionId) { ipc.send('chrome-tabs-create', responseId, createProperties) }) + apiFunctions.setHandleRequest('duplicate', function (tabId, cb) { + var responseId = ipc.guid() + cb && ipc.once('chrome-tabs-duplicate-response-' + responseId, function (evt, dtab, error) { + if (error) { + lastError.run('tabs.duplicate', error, '', () => { cb(null) }) + } else { + cb(dtab) + } + }) + ipc.send('chrome-tabs-duplicate', responseId, tabId) + }) + apiFunctions.setHandleRequest('executeScript', function (tabId, details, cb) { var responseId = ipc.guid() tabId = tabId || -2 diff --git a/lib/browser/api/extensions.js b/lib/browser/api/extensions.js index e4624619e8..14ca11c79f 100644 --- a/lib/browser/api/extensions.js +++ b/lib/browser/api/extensions.js @@ -259,6 +259,25 @@ const createTab = function (createProperties, cb) { } } +const duplicateTab = function (tabId, cb) { + try { + let tab = getWebContentsForTab(tabId) + // XXX What to do if not there? + tab._clone( + (dtab) => { + if (dtab) { + cb(dtab) + } else { + cb(null, 'An unexpected error occurred') + } + } + ) + } catch (e) { + console.error(e) + cb(null, 'An unexpected error occurred: ' + e.message) + } +} + const removeTabs = function (tabIds) { for (let tabId of tabIds) { var tabContents = getWebContentsForTab(tabId) @@ -475,6 +494,20 @@ ipcMain.on('chrome-tabs-create', function (evt, responseId, createProperties) { } }) +ipcMain.on('chrome-tabs-duplicate', function (evt, responseId, tabId) { + const cb = (dtab, error) => { + if (!evt.sender.isDestroyed()) { + let dtabValue = error ? null : dtab.tabValue() + evt.sender.send('chrome-tabs-duplicate-response-' + responseId, dtabValue, error) + } + } + try { + duplicateTab(tabId, cb) + } catch (e) { + cb(null, e.message) + } +}) + ipcMain.on('chrome-tabs-remove', function (evt, responseId, tabIds) { let senderTabId = evt.sender.getId() tabIds = Array.isArray(tabIds) ? tabIds : [tabIds]