Implement chrome.tab.onCreated/onRemoved APIs
authorCheng Zhao <zcbenz@gmail.com>
Sun, 29 May 2016 02:50:14 +0000 (11:50 +0900)
committerCheng Zhao <zcbenz@gmail.com>
Sun, 29 May 2016 02:50:14 +0000 (11:50 +0900)
lib/browser/chrome-extension.js
lib/renderer/chrome-api.js

index e81f4811c1e5aa0875c954adf56cf032fe46fd30..233f7f1ce8b5f11c05f8c1cf54d64d6c107c191c 100644 (file)
@@ -68,6 +68,20 @@ const removeBackgroundPages = function (manifest) {
   delete backgroundPages[manifest.extensionId]
 }
 
+// Dispatch tabs events.
+const hookWindowForTabEvents = function (win) {
+  const tabId = win.webContents.id
+  for (const page of objectValues(backgroundPages)) {
+    page.webContents.sendToAll('CHROME_TABS_ONCREATED', tabId)
+  }
+
+  win.once('closed', () => {
+    for (const page of objectValues(backgroundPages)) {
+      page.webContents.sendToAll('CHROME_TABS_ONREMOVED', tabId)
+    }
+  })
+}
+
 // Handle the chrome.* API messages.
 let nextId = 0
 
@@ -290,6 +304,7 @@ app.once('ready', function () {
   const init = BrowserWindow.prototype._init
   BrowserWindow.prototype._init = function () {
     init.call(this)
+    hookWindowForTabEvents(this)
     this.webContents.on('devtools-opened', () => {
       loadDevToolsExtensions(this, objectValues(manifestMap))
     })
index 4633adfcdffac6c1433db7b9297aac7dbbb201c9..a7a216da2aa13b703c700713cdc4565adbda48f3 100644 (file)
@@ -90,6 +90,14 @@ exports.injectTo = function (extensionId, isBackgroundPage, context) {
     chrome.runtime.onMessage.emit(message, new MessageSender(tabId, extensionId))
   })
 
+  ipcRenderer.on('CHROME_TABS_ONCREATED', (event, tabId) => {
+    chrome.tabs.onCreated.emit(new Tab(tabId))
+  })
+
+  ipcRenderer.on('CHROME_TABS_ONREMOVED', (event, tabId) => {
+    chrome.tabs.onRemoved.emit(tabId)
+  })
+
   chrome.runtime = {
     getURL: function (path) {
       return url.format({
@@ -100,8 +108,6 @@ exports.injectTo = function (extensionId, isBackgroundPage, context) {
       })
     },
 
-    onConnect: new Event(),
-
     connect (...args) {
       if (isBackgroundPage) {
         console.error('chrome.runtime.connect is not supported in background page')
@@ -141,7 +147,9 @@ exports.injectTo = function (extensionId, isBackgroundPage, context) {
       ipcRenderer.send('CHROME_RUNTIME_SENDMESSAGE', targetExtensionId, message)
     },
 
-    onMessage: new Event()
+    onConnect: new Event(),
+    onMessage: new Event(),
+    onInstalled: new Event()
   }
 
   chrome.tabs = {
@@ -160,7 +168,9 @@ exports.injectTo = function (extensionId, isBackgroundPage, context) {
       ipcRenderer.send(`CHROME_TABS_SEND_MESSAGE`, tabId, extensionId, isBackgroundPage, message)
     },
 
-    onUpdated: new Event()
+    onUpdated: new Event(),
+    onCreated: new Event(),
+    onRemoved: new Event()
   }
 
   chrome.extension = {