diff --git a/app/extensions.js b/app/extensions.js index f4806e7a8f6..eb785065e49 100644 --- a/app/extensions.js +++ b/app/extensions.js @@ -13,6 +13,9 @@ let generateBraveManifest = () => { name: 'brave', manifest_version: 2, version: '1.0', + background: { + scripts: [ 'content/scripts/idleHandler.js' ] + }, content_scripts: [ { run_at: 'document_start', @@ -90,7 +93,7 @@ let generateBraveManifest = () => { } ], permissions: [ - 'externally_connectable.all_urls', 'tabs', '', 'contentSettings' + 'externally_connectable.all_urls', 'tabs', '', 'contentSettings', 'idle' ], externally_connectable: { matches: [ diff --git a/app/extensions/brave/content/scripts/idleHandler.js b/app/extensions/brave/content/scripts/idleHandler.js new file mode 100644 index 00000000000..20e08949cbe --- /dev/null +++ b/app/extensions/brave/content/scripts/idleHandler.js @@ -0,0 +1,7 @@ +chrome.idle.setDetectionInterval(15*60) +chrome.idle.onStateChanged.addListener((idleState) => { + chrome.ipc.send('dispatch-action', JSON.stringify({ + actionType: 'app-idle-state-changed', + idleState + })) +}) diff --git a/docs/appActions.md b/docs/appActions.md index 03ba515e2b2..be99670f993 100644 --- a/docs/appActions.md +++ b/docs/appActions.md @@ -390,6 +390,16 @@ Remove credit card data +### windowBlurred(appWindowId) + +Dispatches a message when appWindowId loses focus + +**Parameters** + +**appWindowId**: `Number`, the unique id of the window + + + * * * diff --git a/js/actions/appActions.js b/js/actions/appActions.js index f075bc889bc..5b23338be2b 100644 --- a/js/actions/appActions.js +++ b/js/actions/appActions.js @@ -449,6 +449,18 @@ const appActions = { actionType: AppConstants.APP_REMOVE_AUTOFILL_CREDIT_CARD, detail }) + }, + + /** + * Dispatches a message when appWindowId loses focus + * + * @param {Number} appWindowId - the unique id of the window + */ + windowBlurred: function (appWindowId) { + AppDispatcher.dispatch({ + actionType: AppConstants.APP_WINDOW_BLURRED, + appWindowId: appWindowId + }) } } diff --git a/js/constants/appConstants.js b/js/constants/appConstants.js index a8f1e70d755..ae859514b66 100644 --- a/js/constants/appConstants.js +++ b/js/constants/appConstants.js @@ -41,7 +41,9 @@ const AppConstants = { APP_ADD_AUTOFILL_CREDIT_CARD: _, APP_REMOVE_AUTOFILL_CREDIT_CARD: _, APP_SET_LOGIN_REQUIRED_DETAIL: _, - APP_SET_LOGIN_RESPONSE_DETAIL: _ + APP_SET_LOGIN_RESPONSE_DETAIL: _, + APP_WINDOW_BLURRED: _, + APP_IDLE_STATE_CHANGED: _ } module.exports = mapValuesByKeys(AppConstants) diff --git a/js/stores/appStore.js b/js/stores/appStore.js index ca28e1e1146..122b6058234 100644 --- a/js/stores/appStore.js +++ b/js/stores/appStore.js @@ -151,6 +151,10 @@ const createWindow = (browserOpts, defaults, frameOpts, windowState) => { mainWindow.setFullScreen(true) } + mainWindow.on('blur', function () { + appActions.windowBlurred(mainWindow.id) + }) + mainWindow.on('focus', function () { mainWindow.webContents.send(messages.REQUEST_MENU_DATA_FOR_WINDOW) }) diff --git a/js/stores/eventStore.js b/js/stores/eventStore.js index 68b4fb4ddf2..b9e74837775 100644 --- a/js/stores/eventStore.js +++ b/js/stores/eventStore.js @@ -63,6 +63,13 @@ const addPageView = (url) => { lastActivePageUrl = url } +const windowBlurred = (windowId) => { + let windowCount = BrowserWindow.getAllWindows().filter((win) => win.isFocused()).length + if (windowCount === 0) { + addPageView(null) + } +} + const windowClosed = (windowId) => { let windowCount = BrowserWindow.getAllWindows().length let win = BrowserWindow.getFocusedWindow() @@ -101,6 +108,16 @@ const doAction = (action) => { addPageView(action.frameProps.get('location')) lastActiveTabId = action.frameProps.get('tabId') break + case AppConstants.APP_WINDOW_BLURRED: + windowBlurred(action.appWindowId) + break + case AppConstants.APP_IDLE_STATE_CHANGED: + if (action.idleState !== 'active') { + addPageView(null) + } else { + addPageView(lastActivePageUrl) + } + break case AppConstants.APP_CLOSE_WINDOW: AppDispatcher.waitFor([AppStore.dispatchToken], () => { windowClosed(action.appWindowId)