This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 974
move basic auth state to appStore #3390
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const electron = require('electron') | ||
const app = electron.app | ||
const appActions = require('../../js/actions/appActions') | ||
const appConstants = require('../../js/constants/appConstants') | ||
const appDispatcher = require('../../js/dispatcher/appDispatcher') | ||
const appStore = require('../../js/stores/appStore') | ||
|
||
// URLs to callback for auth. | ||
let authCallbacks = {} | ||
|
||
const cleanupAuthCallback = (tabId) => { | ||
delete authCallbacks[tabId] | ||
} | ||
|
||
const runAuthCallback = (tabId, detail) => { | ||
let cb = authCallbacks[tabId] | ||
if (cb) { | ||
delete authCallbacks[tabId] | ||
if (detail) { | ||
let username = detail.get('username') | ||
let password = detail.get('password') | ||
cb(username, password) | ||
} else { | ||
cb() | ||
} | ||
} | ||
} | ||
|
||
const doAction = (action) => { | ||
switch (action.actionType) { | ||
case appConstants.APP_SET_LOGIN_RESPONSE_DETAIL: | ||
appDispatcher.waitFor([appStore.dispatchToken], () => { | ||
runAuthCallback(action.tabId, action.detail) | ||
}) | ||
break | ||
default: | ||
} | ||
} | ||
|
||
const basicAuth = { | ||
init: () => { | ||
appDispatcher.register(doAction) | ||
app.on('login', (e, webContents, request, authInfo, cb) => { | ||
e.preventDefault() | ||
let tabId = webContents.getId() | ||
authCallbacks[tabId] = cb | ||
webContents.on('destroyed', () => { | ||
cleanupAuthCallback(tabId) | ||
}) | ||
webContents.on('crashed', () => { | ||
cleanupAuthCallback(tabId) | ||
}) | ||
appActions.setLoginRequiredDetail(tabId, { | ||
request, | ||
authInfo | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
module.exports = basicAuth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* 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/. */ | ||
|
||
const tabState = require('./tabState') | ||
const { makeImmutable } = require('./immutableUtil') | ||
|
||
const loginRequiredDetail = 'loginRequiredDetail' | ||
tabState.addTransientFields([loginRequiredDetail]) | ||
|
||
const basicAuthState = { | ||
setLoginRequiredDetail: (appState, tabId, detail) => { | ||
appState = makeImmutable(appState) | ||
detail = makeImmutable(detail) | ||
let tab = tabState.getOrCreateByTabId(appState, tabId) | ||
if (!detail || detail.size === 0) { | ||
tab = tab.delete(loginRequiredDetail) | ||
} else { | ||
tab = tab.set(loginRequiredDetail, detail) | ||
} | ||
return tabState.updateTab(appState, tabId, tab) | ||
}, | ||
|
||
getLoginRequiredDetail: (appState, tabId) => { | ||
appState = makeImmutable(appState) | ||
let tab = tabState.getByTabId(appState, tabId) | ||
return tab && tab.get(loginRequiredDetail) | ||
}, | ||
|
||
setLoginResponseDetail: (appState, tabId, detail) => { | ||
appState = makeImmutable(appState) | ||
let tab = tabState.getByTabId(appState, tabId) | ||
if (!tab) { | ||
return appState | ||
} | ||
tab = tab.delete(loginRequiredDetail) | ||
return tabState.updateTab(appState, tabId, tab) | ||
} | ||
} | ||
|
||
module.exports = basicAuthState |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* 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/. */ | ||
|
||
const Immutable = require('immutable') | ||
|
||
const immutableUtils = { | ||
makeImmutable: (obj) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely like this 👍 |
||
if (!obj) { | ||
return null | ||
} | ||
return obj.toJS ? obj : Immutable.fromJS(obj) | ||
} | ||
} | ||
|
||
module.exports = immutableUtils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* 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/. */ | ||
|
||
const { makeImmutable } = require('./immutableUtil') | ||
|
||
let transientFields = ['tabId', 'windowId'] | ||
|
||
const tabState = { | ||
defaultTabState: makeImmutable({ | ||
windowId: -1, | ||
frameKey: -1, | ||
tabId: -1 | ||
}), | ||
|
||
getTabIndexByTabId: (state, tabId) => { | ||
return state.get('tabs').findIndex((tab) => tab.get('tabId') === tabId) | ||
}, | ||
|
||
createTab: (props) => { | ||
props = makeImmutable(props) | ||
return tabState.defaultTabState.merge(props) | ||
}, | ||
|
||
getOrCreateByTabId: (state, tabId) => { | ||
let tab = tabState.getByTabId(state, tabId) | ||
return tab || tabState.createTab({tabId}) | ||
}, | ||
|
||
getByTabId: (state, tabId) => { | ||
return state.get('tabs').find((tab) => tab.get('tabId') === tabId) | ||
}, | ||
|
||
closeTab: (state, tabId) => { | ||
let index = tabState.getTabIndexByTabId(state, tabId) | ||
if (index === -1) { | ||
return state | ||
} | ||
|
||
let tabs = state.get('tabs').delete(index) | ||
state = state.set('tabs', tabs) | ||
return state | ||
}, | ||
|
||
updateTab: (state, tabId, tab) => { | ||
let tabs = state.get('tabs') | ||
let index = tabState.getTabIndexByTabId(state, tabId) | ||
tabs = tabs.delete(index).insert(index, tab) | ||
return state.set('tabs', tabs) | ||
}, | ||
|
||
addTransientFields: (fields) => { | ||
transientFields = transientFields.concat(fields) | ||
}, | ||
|
||
getTransientFields: () => { | ||
return transientFields | ||
}, | ||
|
||
getPersistentTabState: (tab) => { | ||
tab = makeImmutable(tab) | ||
tabState.getTransientFields().forEach((field) => { | ||
tab = tab.delete(field) | ||
}) | ||
return tab | ||
} | ||
} | ||
|
||
module.exports = tabState |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ const downloadStates = require('../js/constants/downloadStates') | |
const {tabFromFrame} = require('../js/state/frameStateUtil') | ||
const sessionStorageVersion = 1 | ||
const filtering = require('./filtering') | ||
// const tabState = require('./common/state/tabState') | ||
|
||
let suffix = '' | ||
if (process.env.NODE_ENV === 'development') { | ||
|
@@ -274,6 +275,11 @@ module.exports.cleanAppData = (data, isShutdown) => { | |
}) | ||
} | ||
} | ||
// all data in tabs is transient while we make the transition from window to app state | ||
delete data.tabs | ||
// if (data.tabs) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I left this in here so it was clear how it would work. I wanted to keep all the state management for tabs in on place |
||
// data.tabs = data.tabs.map((tab) => tabState.getPersistentTabState(tab).toJS()) | ||
// } | ||
} | ||
|
||
/** | ||
|
@@ -356,6 +362,7 @@ module.exports.loadAppState = () => { | |
module.exports.defaultAppState = () => { | ||
return { | ||
sites: [], | ||
tabs: [], | ||
visits: [], | ||
settings: {}, | ||
siteSettings: {}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure how I feel about this. I wanted to separate the electron code from the appState to make it more unit testable, but not sure if this is the best way to handle it or not. I also considered some kind of change listener, but I think that might need a larger change to make sure the notifications are efficient