Skip to content

Commit

Permalink
Merge pull request brave#12116 from petemill/feature/debug-tab-events
Browse files Browse the repository at this point in the history
Create a flag which will output to the console a line for each event all tabs emit
  • Loading branch information
cezaraugusto authored Jan 19, 2018
2 parents 1ffd6cc + f0840da commit 9c0b63f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
29 changes: 29 additions & 0 deletions app/browser/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const windowActions = require('../../js/actions/windowActions')
const tabActions = require('../common/actions/tabActions')
const config = require('../../js/constants/config')
const Immutable = require('immutable')
const { shouldDebugTabEvents } = require('../cmdLine')
const tabState = require('../common/state/tabState')
const {app, BrowserWindow, extensions, session, ipcMain} = require('electron')
const {makeImmutable, makeJS} = require('../common/state/immutableUtil')
Expand Down Expand Up @@ -71,6 +72,9 @@ const getTabValue = function (tabId) {

const updateTab = (tabId, changeInfo = {}) => {
let tabValue = getTabValue(tabId)
if (shouldDebugTabEvents) {
console.log('tab updated from muon', { tabId, changeIndex: changeInfo.index, changeActive: changeInfo.active, newIndex: tabValue && tabValue.get('index'), newActive: tabValue && tabValue.get('active') })
}
if (tabValue) {
appActions.tabUpdated(tabValue, makeImmutable(changeInfo))
}
Expand Down Expand Up @@ -505,18 +509,43 @@ const api = {
})

process.on('chrome-tabs-created', (tabId) => {
if (shouldDebugTabEvents) {
console.log(`tab [${tabId} via process] chrome-tabs-created`)
}
updateTab(tabId)
})

process.on('chrome-tabs-updated', (tabId, changeInfo) => {
if (shouldDebugTabEvents) {
console.log(`tab [${tabId} via process] chrome-tabs-updated`)
}
updateTab(tabId, changeInfo)
})

process.on('chrome-tabs-removed', (tabId, changeInfo) => {
if (shouldDebugTabEvents) {
console.log(`tab [${tabId} via process] - chrome-tabs-removed`)
}
})

app.on('web-contents-created', function (event, tab) {
if (tab.isBackgroundPage() || !tab.isGuest()) {
return
}
const tabId = tab.getId()

// command-line flag --debug-tab-events
if (shouldDebugTabEvents) {
console.log(`Tab [${tabId}] created in window ${tab.tabValue().windowId}`)
// output console log for each event the tab receives
const oldEmit = tab.emit
tab.emit = function () {
const eventTabId = tab && !tab.isDestroyed() ? tab.getId() : `probably ${tabId}`
console.log(`Tab [${eventTabId}] event '${arguments[0]}'`)
oldEmit.apply(tab, arguments)
}
}

tab.on('did-start-navigation', (e, navigationHandle) => {
if (!tab.isDestroyed() && navigationHandle.isValid() && navigationHandle.isInMainFrame()) {
const controller = tab.controller()
Expand Down
3 changes: 3 additions & 0 deletions app/cmdLine.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const fs = require('fs')
const path = require('path')
let appInitialized = false
let newWindowURL
const debugTabEventsFlagName = '--debug-tab-events'

const focusOrOpenWindow = function (url) {
// don't try to do anything if the app hasn't been initialized
Expand Down Expand Up @@ -136,3 +137,5 @@ module.exports.newWindowURL = () => {
}
return newWindowURL
}

module.exports.shouldDebugTabEvents = process.argv.includes(debugTabEventsFlagName)

0 comments on commit 9c0b63f

Please sign in to comment.