Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement chrome.runtime.onMessage response callback #7974

Merged
merged 2 commits into from
Nov 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions lib/browser/chrome-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,22 @@ ipcMain.on('CHROME_I18N_MANIFEST', function (event, extensionId) {
event.returnValue = manifestMap[extensionId]
})

ipcMain.on('CHROME_RUNTIME_SENDMESSAGE', function (event, extensionId, message) {
let resultID = 1
ipcMain.on('CHROME_RUNTIME_SENDMESSAGE', function (event, extensionId, message, originResultID) {
const page = backgroundPages[extensionId]
if (!page) {
console.error(`Connect to unknown extension ${extensionId}`)
return
}

page.webContents.sendToAll(`CHROME_RUNTIME_ONMESSAGE_${extensionId}`, event.sender.id, message)
page.webContents.sendToAll(`CHROME_RUNTIME_ONMESSAGE_${extensionId}`, event.sender.id, message, resultID)
ipcMain.once(`CHROME_RUNTIME_ONMESSAGE_RESULT_${resultID}`, (event, result) => {
event.sender.send(`CHROME_RUNTIME_SENDMESSAGE_RESULT_${originResultID}`, result)
})
resultID++
})

ipcMain.on('CHROME_TABS_SEND_MESSAGE', function (event, tabId, extensionId, isBackgroundPage, message) {
ipcMain.on('CHROME_TABS_SEND_MESSAGE', function (event, tabId, extensionId, isBackgroundPage, message, originResultID) {
const contents = webContents.fromId(tabId)
if (!contents) {
console.error(`Sending message to unknown tab ${tabId}`)
Expand All @@ -189,7 +194,11 @@ ipcMain.on('CHROME_TABS_SEND_MESSAGE', function (event, tabId, extensionId, isBa

const senderTabId = isBackgroundPage ? null : event.sender.id

contents.sendToAll(`CHROME_RUNTIME_ONMESSAGE_${extensionId}`, senderTabId, message)
contents.sendToAll(`CHROME_RUNTIME_ONMESSAGE_${extensionId}`, senderTabId, message, resultID)
ipcMain.once(`CHROME_RUNTIME_ONMESSAGE_RESULT_${resultID}`, (event, result) => {
event.sender.send(`CHROME_TABS_SEND_MESSAGE_RESULT_${originResultID}`, result)
})
resultID++
})

ipcMain.on('CHROME_TABS_EXECUTESCRIPT', function (event, requestId, tabId, extensionId, details) {
Expand Down
20 changes: 13 additions & 7 deletions lib/renderer/chrome-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,16 @@ class Port {
// Inject chrome API to the |context|
exports.injectTo = function (extensionId, isBackgroundPage, context) {
const chrome = context.chrome = context.chrome || {}
let originResultID = 1

ipcRenderer.on(`CHROME_RUNTIME_ONCONNECT_${extensionId}`, (event, tabId, portId, connectInfo) => {
chrome.runtime.onConnect.emit(new Port(tabId, portId, extensionId, connectInfo.name))
})

ipcRenderer.on(`CHROME_RUNTIME_ONMESSAGE_${extensionId}`, (event, tabId, message) => {
chrome.runtime.onMessage.emit(message, new MessageSender(tabId, extensionId))
ipcRenderer.on(`CHROME_RUNTIME_ONMESSAGE_${extensionId}`, (event, tabId, message, resultID) => {
chrome.runtime.onMessage.emit(message, new MessageSender(tabId, extensionId), (messageResult) => {
ipcRenderer.send(`CHROME_RUNTIME_ONMESSAGE_RESULT_${resultID}`, messageResult)
})
})

ipcRenderer.on('CHROME_TABS_ONCREATED', (event, tabId) => {
Expand Down Expand Up @@ -121,16 +124,18 @@ exports.injectTo = function (extensionId, isBackgroundPage, context) {
} else if (args.length === 2) {
// A case of not provide extension-id: (message, responseCallback)
if (typeof args[1] === 'function') {
console.error('responseCallback is not supported')
ipcRenderer.on(`CHROME_RUNTIME_SENDMESSAGE_RESULT_${originResultID}`, (event, result) => args[1](result))
message = args[0]
} else {
[targetExtensionId, message] = args
}
} else {
console.error('options and responseCallback are not supported')
console.error('options is not supported')
ipcRenderer.on(`CHROME_RUNTIME_SENDMESSAGE_RESULT_${originResultID}`, (event, result) => args[2](result))
}

ipcRenderer.send('CHROME_RUNTIME_SENDMESSAGE', targetExtensionId, message)
ipcRenderer.send('CHROME_RUNTIME_SENDMESSAGE', targetExtensionId, message, originResultID)
originResultID++
},

onConnect: new Event(),
Expand All @@ -149,9 +154,10 @@ exports.injectTo = function (extensionId, isBackgroundPage, context) {

sendMessage (tabId, message, options, responseCallback) {
if (responseCallback) {
console.error('responseCallback is not supported')
ipcRenderer.on(`CHROME_TABS_SEND_MESSAGE_RESULT_${originResultID}`, (event, result) => responseCallback(result))
}
ipcRenderer.send('CHROME_TABS_SEND_MESSAGE', tabId, extensionId, isBackgroundPage, message)
ipcRenderer.send('CHROME_TABS_SEND_MESSAGE', tabId, extensionId, isBackgroundPage, message, originResultID)
originResultID++
},

onUpdated: new Event(),
Expand Down