Skip to content
This repository has been archived by the owner on Jan 4, 2019. It is now read-only.

Commit

Permalink
Merge pull request #180 from brave/chrome-focus-changed
Browse files Browse the repository at this point in the history
add extension chrome.windows.onFocusChanged
  • Loading branch information
bridiver authored Apr 23, 2017
2 parents f88c0a7 + 20fbb84 commit 98371ce
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 24 deletions.
91 changes: 71 additions & 20 deletions atom/common/api/resources/windows_bindings.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,104 @@
var binding = require('binding').Binding.create('windows')

var ipc = require('ipc_utils')

var id = 1;

var binding = {
getCurrent: function () {
var cb, getInfo;
if (arguments.length == 1) {
binding.registerCustomHook(function (bindingsAPI, extensionId) {
var apiFunctions = bindingsAPI.apiFunctions
var windows = bindingsAPI.compiledApi

apiFunctions.setHandleRequest('getLastFocused', function (getInfo, cb) {
console.warn('chrome.windows.getLastFocused is not supported yet')
})

apiFunctions.setHandleRequest('remove', function (windowId, cb) {
console.warn('chrome.windows.remove is not supported yet')
})

apiFunctions.setHandleRequest('get', function (windowId, getInfo, cb) {
console.warn('chrome.windows.get is not supported yet')
})

apiFunctions.setHandleRequest('create', function (createData, cb) {
console.warn('chrome.windows.create is not supported yet')
})

apiFunctions.setHandleRequest('getCurrent', function () {
var cb, getInfo
if (arguments.length === 1) {
cb = arguments[0]
} else {
getInfo = arguments[0]
cb = arguments[1]
}

var responseId = ++id
ipc.once('chrome-windows-get-current-response-' + responseId, function(evt, win) {
ipc.once('chrome-windows-get-current-response-' + responseId, function (evt, win) {
cb(win)
})
ipc.send('chrome-windows-get-current', responseId, getInfo)
},
})

getAll: function (getInfo, cb) {
if (arguments.length == 1) {
apiFunctions.setHandleRequest('getAll', function (getInfo, cb) {
if (arguments.length === 1) {
cb = arguments[0]
} else {
getInfo = arguments[0]
cb = arguments[1]
}

var responseId = ++id
ipc.once('chrome-windows-get-all-response-' + responseId, function(evt, win) {
ipc.once('chrome-windows-get-all-response-' + responseId, function (evt, win) {
cb(win)
})
ipc.send('chrome-windows-get-all', responseId, getInfo)
},
})

create: function (createData, cb) {
console.warn('chrome.windows.create is not supported yet')
},

update: function (windowId, updateInfo, cb) {
apiFunctions.setHandleRequest('update', function (windowId, updateInfo, cb) {
var responseId = ++id
cb && ipc.once('chrome-windows-update-response-' + responseId, function (evt, win) {
cb(win)
})
ipc.send('chrome-windows-update', responseId, windowId, updateInfo)
},
})

windows.onCreated = {
addListener: function (callback) {
console.warn('chrome.windows.onCreated is not supported yet')
},

Filters: function () {
console.warn('chrome.windows.onCreated filters are not supported yet')
}
}

windows.onRemoved = {
addListener: function (callback) {
console.warn('chrome.windows.onRemoved is not supported yet')
},

Filters: function () {
console.warn('chrome.windows.onRemoved filters are not supported yet')
}
}

windows.onFocusChanged = {
addListener: function (cb) {
ipc.send('register-chrome-window-focus', extensionId)
ipc.on('chrome-window-focus', function (evt, windowId) {
cb(windowId)
})
},

Filters: function () {
console.warn('chrome.windows.onFocusChanged filters are not supported yet')
}
}

windows.WINDOW_ID_NONE = -1
windows.WINDOW_ID_CURRENT = -2
})

WINDOW_ID_NONE: -1,
WINDOW_ID_CURRENT: -2
};
exports.$set('binding', binding.generate())

exports.binding = binding;
38 changes: 34 additions & 4 deletions lib/browser/api/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ var addBackgroundPageEvent = function (extensionId, event) {
}

var sendToBackgroundPages = function (extensionId, session, event) {
if (!backgroundPageEvents[event] || !session)
return
if (!backgroundPageEvents[event] || !session) { return }

var pages = []
if (extensionId === 'all') {
Expand All @@ -79,8 +78,10 @@ var sendToBackgroundPages = function (extensionId, session, event) {
var args = [].slice.call(arguments, 2)
pages.forEach(function (backgroundPage) {
try {
// only send to background pages in the same browser context
if (backgroundPage.session.equal(session)) {
var sendToAll = (typeof (session) === 'string' && session === 'all')
// otherwise, only send to background pages in the same browser context

if (sendToAll || backgroundPage.session.equal(session)) {
backgroundPage.send.apply(backgroundPage, args)
}
} catch (e) {
Expand Down Expand Up @@ -480,6 +481,35 @@ tabEvents.forEach((event_name) => {
})
})

ipcMain.on('register-chrome-window-focus', function (evt, extensionId) {
addBackgroundPageEvent(extensionId, 'chrome-window-focus')
})

app.on('browser-window-focus', function (event, browserWindow) {
var forthcomingWindow = browserWindow
var forthcomingId = (forthcomingWindow == null) ? -1 : forthcomingWindow.id

// Only background pages have access to chrome.window, so this is enough
// We do need to send this to all sessions though
sendToBackgroundPages('all', 'all', 'chrome-window-focus', forthcomingId)
})

app.on('browser-window-blur', function (event, browserWindow) {
// see https://github.com/electron/electron/issues/4942
setImmediate(function () {
var forthcomingWindow = BrowserWindow.getFocusedWindow()

var forthcomingId = (forthcomingWindow == null) ? -1 : forthcomingWindow.id
var previousId = browserWindow.id

var allWindowsBlurred = (forthcomingId === -1 && previousId !== -1)
if (allWindowsBlurred) {
var blurredResponse = -1
sendToBackgroundPages('all', 'all', 'chrome-window-focus', blurredResponse)
}
})
})

// chrome.windows
var windowInfo = function (win, populateTabs) {
var bounds = win.getBounds()
Expand Down

0 comments on commit 98371ce

Please sign in to comment.