Skip to content

Commit

Permalink
Moves bookmark text calculation into the state
Browse files Browse the repository at this point in the history
Resolves brave#9517

Auditors:

Test Plan:
  • Loading branch information
NejcZdovc committed Sep 6, 2017
1 parent 2014e8c commit cfd62bb
Show file tree
Hide file tree
Showing 30 changed files with 1,752 additions and 288 deletions.
2 changes: 2 additions & 0 deletions app/browser/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,8 +701,10 @@ const doAction = (state, action) => {
}
case appConstants.APP_ADD_BOOKMARK:
case appConstants.APP_EDIT_BOOKMARK:
case appConstants.APP_MOVE_BOOKMARK:
case appConstants.APP_REMOVE_BOOKMARK:
case appConstants.APP_ADD_BOOKMARK_FOLDER:
case appConstants.APP_MOVE_BOOKMARK_FOLDER:
case appConstants.APP_EDIT_BOOKMARK_FOLDER:
case appConstants.APP_REMOVE_BOOKMARK_FOLDER:
createMenu(state)
Expand Down
48 changes: 41 additions & 7 deletions app/browser/reducers/bookmarkFoldersReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ const Immutable = require('immutable')

// State
const bookmarkFoldersState = require('../../common/state/bookmarkFoldersState')
const bookmarkToolbarState = require('../../common/state/bookmarkToolbarState')

// Constants
const appConstants = require('../../../js/constants/appConstants')
const siteTags = require('../../../js/constants/siteTags')
const {STATE_SITES} = require('../../../js/constants/stateConstants')

// Utils
const {makeImmutable} = require('../../common/state/immutableUtil')
const syncUtil = require('../../../js/state/syncUtil')
const textCalcUtil = require('../../common/lib/textCalcUtil')
const bookmarkFolderUtil = require('../../common/lib/bookmarkFoldersUtil')

const bookmarkFoldersReducer = (state, action, immutableAction) => {
action = immutableAction || makeImmutable(action)
Expand All @@ -29,12 +33,16 @@ const bookmarkFoldersReducer = (state, action, immutableAction) => {

if (Immutable.List.isList(folder)) {
action.get('folderDetails', Immutable.List()).forEach((folder) => {
state = bookmarkFoldersState.addFolder(state, folder, closestKey)
state = syncUtil.updateObjectCache(state, folder, STATE_SITES.BOOKMARK_FOLDERS)
const folderDetails = bookmarkFolderUtil.buildFolder(folder, bookmarkFoldersState.getFolders(state))
state = bookmarkFoldersState.addFolder(state, folderDetails, closestKey)
state = syncUtil.updateObjectCache(state, folderDetails, STATE_SITES.BOOKMARK_FOLDERS)
textCalcUtil.calcText(folderDetails, siteTags.BOOKMARK_FOLDER)
})
} else {
state = bookmarkFoldersState.addFolder(state, folder, closestKey)
state = syncUtil.updateObjectCache(state, folder, STATE_SITES.BOOKMARK_FOLDERS)
const folderDetails = bookmarkFolderUtil.buildFolder(folder, bookmarkFoldersState.getFolders(state))
state = bookmarkFoldersState.addFolder(state, folderDetails, closestKey)
state = syncUtil.updateObjectCache(state, folderDetails, STATE_SITES.BOOKMARK_FOLDERS)
textCalcUtil.calcText(folderDetails, siteTags.BOOKMARK_FOLDER)
}
break
}
Expand All @@ -49,7 +57,8 @@ const bookmarkFoldersReducer = (state, action, immutableAction) => {

state = bookmarkFoldersState.editFolder(state, key, folder)
state = syncUtil.updateObjectCache(state, folder, STATE_SITES.BOOKMARK_FOLDERS)

const folderDetails = bookmarkFoldersState.getFolder(state, key)
textCalcUtil.calcText(folderDetails, siteTags.BOOKMARK_FOLDER)
break
}
case appConstants.APP_MOVE_BOOKMARK_FOLDER:
Expand All @@ -70,6 +79,9 @@ const bookmarkFoldersReducer = (state, action, immutableAction) => {

const destinationDetail = bookmarkFoldersState.getFolder(state, action.get('destinationKey'))
state = syncUtil.updateObjectCache(state, destinationDetail, STATE_SITES.BOOKMARK_FOLDERS)
if (destinationDetail.get('parentFolderId') === 0) {
state = bookmarkToolbarState.setToolbars(state)
}
break
}
case appConstants.APP_REMOVE_BOOKMARK_FOLDER:
Expand All @@ -82,16 +94,38 @@ const bookmarkFoldersReducer = (state, action, immutableAction) => {

if (Immutable.List.isList(folderKey)) {
action.get('folderKey', Immutable.List()).forEach((key) => {
const folder = state.getIn([STATE_SITES.BOOKMARK_FOLDERS, key])
const folder = bookmarkFoldersState.getFolder(state, key)
state = bookmarkFoldersState.removeFolder(state, key)
state = syncUtil.updateObjectCache(state, folder, STATE_SITES.BOOKMARK_FOLDERS)
})
state = bookmarkToolbarState.setToolbars(state)
} else {
const folder = state.getIn([STATE_SITES.BOOKMARK_FOLDERS, folderKey])
const folder = bookmarkFoldersState.getFolder(state, folderKey)
state = bookmarkFoldersState.removeFolder(state, folderKey)
state = syncUtil.updateObjectCache(state, folder, STATE_SITES.BOOKMARK_FOLDERS)
if (folder.get('parentFolderId') === 0) {
state = bookmarkToolbarState.setToolbars(state)
}
}
break
}
case appConstants.APP_ON_BOOKMARK_FOLDER_WIDTH_CHANGED:
{
if (action.get('folderList', Immutable.List()).isEmpty()) {
break
}

let updateToolbar = false
action.get('folderList').forEach(item => {
state = bookmarkFoldersState.setWidth(state, item.get('key'), item.get('width'))
if (item.get('parentFolderId') === 0) {
updateToolbar = true
}
})

if (updateToolbar) {
state = bookmarkToolbarState.setToolbars(state)
}
break
}
}
Expand Down
37 changes: 37 additions & 0 deletions app/browser/reducers/bookmarkToolbarReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

// Constants
const appConstants = require('../../../js/constants/appConstants')

// State
const bookmarksState = require('../../common/state/bookmarksState')
const bookmarkFoldersState = require('../../common/state/bookmarkFoldersState')

// Util
const {makeImmutable} = require('../../common/state/immutableUtil')
const textCalcUtil = require('../../common/lib/textCalcUtil')

const tabsReducer = (state, action, immutableAction) => {
action = immutableAction || makeImmutable(action)
switch (action.get('actionType')) {
case appConstants.APP_SET_STATE:
{
// update session for 0.21.x version
const bookmarks = bookmarksState.getBookmarks(state)
if (bookmarks.first() && !bookmarks.first().has('width')) {
textCalcUtil.calcTextList(bookmarks.toList())
}

const bookmarkFolders = bookmarkFoldersState.getFolders(state)
if (bookmarkFolders.first() && !bookmarkFolders.first().has('width')) {
textCalcUtil.calcTextList(bookmarkFolders.toList())
}
}
break
}
return state
}

module.exports = tabsReducer
52 changes: 47 additions & 5 deletions app/browser/reducers/bookmarksReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ const Immutable = require('immutable')

// State
const bookmarksState = require('../../common/state/bookmarksState')
const bookmarkToolbarState = require('../../common/state/bookmarkToolbarState')

// Constants
const appConstants = require('../../../js/constants/appConstants')
const siteTags = require('../../../js/constants/siteTags')
const {STATE_SITES} = require('../../../js/constants/stateConstants')

// Utils
const {makeImmutable} = require('../../common/state/immutableUtil')
const syncUtil = require('../../../js/state/syncUtil')
const bookmarkUtil = require('../../common/lib/bookmarkUtil')
const bookmarkLocationCache = require('../../common/cache/bookmarkLocationCache')
const textCalcUtil = require('../../common/lib/textCalcUtil')

const bookmarksReducer = (state, action, immutableAction) => {
action = immutableAction || makeImmutable(action)
Expand All @@ -34,12 +37,16 @@ const bookmarksReducer = (state, action, immutableAction) => {

if (Immutable.List.isList(bookmark)) {
action.get('siteDetail', Immutable.List()).forEach((bookmark) => {
state = bookmarksState.addBookmark(state, bookmark, closestKey)
state = syncUtil.updateObjectCache(state, bookmark, STATE_SITES.BOOKMARKS)
const bookmarkDetail = bookmarkUtil.buildBookmark(state, bookmark)
state = bookmarksState.addBookmark(state, bookmarkDetail, closestKey)
state = syncUtil.updateObjectCache(state, bookmarkDetail, STATE_SITES.BOOKMARKS)
textCalcUtil.calcText(bookmarkDetail, siteTags.BOOKMARK)
})
} else {
state = bookmarksState.addBookmark(state, bookmark, closestKey)
state = syncUtil.updateObjectCache(state, bookmark, STATE_SITES.BOOKMARKS)
const bookmarkDetail = bookmarkUtil.buildBookmark(state, bookmark)
state = bookmarksState.addBookmark(state, bookmarkDetail, closestKey)
state = syncUtil.updateObjectCache(state, bookmarkDetail, STATE_SITES.BOOKMARKS)
textCalcUtil.calcText(bookmarkDetail, siteTags.BOOKMARK)
}

state = bookmarkUtil.updateActiveTabBookmarked(state)
Expand All @@ -54,8 +61,15 @@ const bookmarksReducer = (state, action, immutableAction) => {
break
}

state = bookmarksState.editBookmark(state, key, bookmark)
const oldBookmark = bookmarksState.getBookmark(state, key)
if (oldBookmark.isEmpty()) {
break
}

const bookmarkDetail = bookmarkUtil.buildEditBookmark(oldBookmark, bookmark)
state = bookmarksState.editBookmark(state, oldBookmark, bookmarkDetail)
state = syncUtil.updateObjectCache(state, bookmark, STATE_SITES.BOOKMARKS)
textCalcUtil.calcText(bookmarkDetail, siteTags.BOOKMARK)

state = bookmarkUtil.updateActiveTabBookmarked(state)
break
Expand All @@ -78,6 +92,10 @@ const bookmarksReducer = (state, action, immutableAction) => {

const destinationDetail = bookmarksState.getBookmark(state, action.get('destinationKey'))
state = syncUtil.updateObjectCache(state, destinationDetail, STATE_SITES.BOOKMARKS)

if (destinationDetail.get('parentFolderId') === 0) {
state = bookmarkToolbarState.setToolbars(state)
}
break
}
case appConstants.APP_REMOVE_BOOKMARK:
Expand All @@ -91,12 +109,36 @@ const bookmarksReducer = (state, action, immutableAction) => {
action.get('bookmarkKey', Immutable.List()).forEach((key) => {
state = bookmarksState.removeBookmark(state, key)
})
state = bookmarkToolbarState.setToolbars(state)
} else {
const bookmark = bookmarksState.getBookmark(state, bookmarkKey)
state = bookmarksState.removeBookmark(state, bookmarkKey)
if (bookmark.get('parentFolderId') === 0) {
state = bookmarkToolbarState.setToolbars(state)
}
}
state = bookmarkUtil.updateActiveTabBookmarked(state)
break
}
case appConstants.APP_ON_BOOKMARK_WIDTH_CHANGED:
{
if (action.get('bookmarkList', Immutable.List()).isEmpty()) {
break
}

let updateToolbar = false
action.get('bookmarkList').forEach(item => {
state = bookmarksState.setWidth(state, item.get('key'), item.get('width'))
if (item.get('parentFolderId') === 0) {
updateToolbar = true
}
})

if (updateToolbar) {
state = bookmarkToolbarState.setToolbars(state)
}
break
}
}

return state
Expand Down
16 changes: 14 additions & 2 deletions app/browser/reducers/windowsReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@
const path = require('path')
const assert = require('assert')
const Immutable = require('immutable')

// Constants
const appConstants = require('../../../js/constants/appConstants')
const windowConstants = require('../../../js/constants/windowConstants')

// State
const windowState = require('../../common/state/windowState')

// Utils
const windows = require('../windows')
const sessionStoreShutdown = require('../../sessionStoreShutdown')
const {makeImmutable, isImmutable} = require('../../common/state/immutableUtil')
Expand Down Expand Up @@ -333,8 +339,14 @@ const windowsReducer = (state, action, immutableAction) => {
windows.cleanupWindow(windowId)
break
case appConstants.APP_WINDOW_CREATED:
state = windowState.maybeCreateWindow(state, action)
break
case appConstants.APP_WINDOW_RESIZED:
{
const bookmarkToolbarState = require('../../common/state/bookmarkToolbarState')
state = windowState.maybeCreateWindow(state, action)
const windowId = action.getIn(['windowValue', 'windowId'], windowState.WINDOW_ID_NONE)
state = bookmarkToolbarState.setToolbar(state, windowId)
break
}
case appConstants.APP_TAB_STRIP_EMPTY:
windows.closeWindow(action.get('windowId'))
break
Expand Down
46 changes: 33 additions & 13 deletions app/browser/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,20 @@ const createNavigationState = (navigationHandle, controller) => {
})
}

let backgroundProcess = null
let backgroundProcessTimer = null
const runScript = (win, debug, script, cb) => {
win.webContents.executeScriptInTab(config.braveExtensionId, script, {}, (err, url, result) => {
cb(err, url, result)
if (!debug) {
backgroundProcessTimer = setTimeout(() => {
win.close()
backgroundProcess = null
}, 2 * 60 * 1000) // 2 min
}
})
}

const api = {
init: (state, action) => {
process.on('open-url-from-tab', (e, source, targetUrl, disposition) => {
Expand Down Expand Up @@ -742,20 +756,26 @@ const api = {
})
},

executeScriptInBackground: (script, cb) => {
const win = new BrowserWindow({
show: false,
webPreferences: {
partition: 'default'
}
})
win.webContents.on('did-finish-load', (e) => {
win.webContents.executeScriptInTab(config.braveExtensionId, script, {}, (err, url, result) => {
cb(err, url, result)
setImmediate(() => win.close())
executeScriptInBackground: (script, cb, debug = false) => {
if (backgroundProcessTimer) {
clearTimeout(backgroundProcessTimer)
}

if (backgroundProcess === null) {
backgroundProcess = new BrowserWindow({
show: debug,
webPreferences: {
partition: 'default'
}
})
})
win.loadURL('about:blank')

backgroundProcess.webContents.on('did-finish-load', () => {
runScript(backgroundProcess, debug, script, cb)
})
backgroundProcess.loadURL('about:blank')
} else {
runScript(backgroundProcess, debug, script, cb)
}
},

moveTo: (state, tabId, frameOpts, browserOpts, toWindowId) => {
Expand Down
11 changes: 10 additions & 1 deletion app/browser/windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ const updateWindow = (windowId, updateDefault = false) => {
}
}

const onWindowResize = (windowId) => {
const windowValue = getWindowValue(windowId)
if (windowValue) {
appActions.onWindowResize(windowValue)
windowActions.onWindowUpdate(windowId, true)
}
}

const siteMatchesTab = (site, tab) => {
const matchesLocation = getLocationIfPDF(tab.get('url')) === site.get('location')
const matchesPartition = tab.get('partitionNumber', 0) === site.get('partitionNumber', 0)
Expand Down Expand Up @@ -107,6 +115,7 @@ const api = {
let windowId = -1
const updateWindowMove = debounce(updateWindow, 100)
const updateWindowDebounce = debounce(updateWindow, 5)
const onWindowResizeDebounce = debounce(onWindowResize, 5)

win.once('initialized', () => {
windowId = win.id
Expand Down Expand Up @@ -212,7 +221,7 @@ const api = {
updateWindowDebounce(windowId)
})
win.on('resize', () => {
updateWindowDebounce(windowId, true)
onWindowResizeDebounce(windowId)
})
win.on('move', () => {
updateWindowMove(windowId, true)
Expand Down
Loading

0 comments on commit cfd62bb

Please sign in to comment.