Avoid blocking UI during vault open and unlock
This commit is contained in:
@@ -321,6 +321,14 @@ type ui struct {
|
||||
apiHost *api.Host
|
||||
auditLog *apiaudit.Log
|
||||
grpcAddress string
|
||||
backgroundResults chan backgroundActionResult
|
||||
invalidate func()
|
||||
}
|
||||
|
||||
type backgroundActionResult struct {
|
||||
label string
|
||||
apply func() error
|
||||
err error
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -431,6 +439,7 @@ func newUIWithState(mode string, sess appstate.CurrentSession, paths statePaths)
|
||||
syncSourceMode: syncSourceLocal,
|
||||
syncDirection: syncDirectionPull,
|
||||
apiPolicyGroupScope: true,
|
||||
backgroundResults: make(chan backgroundActionResult, 8),
|
||||
}
|
||||
u.apiPolicyAllow.Value = true
|
||||
u.apiPolicyGroupScopeW.Value = true
|
||||
@@ -731,6 +740,42 @@ func (u *ui) openVaultAction() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *ui) startOpenVaultAction() {
|
||||
manager, ok := u.state.Session.(*session.Manager)
|
||||
if !ok {
|
||||
u.runAction("open vault", u.openVaultAction)
|
||||
return
|
||||
}
|
||||
key, err := u.currentMasterKey()
|
||||
u.clearMasterPassword()
|
||||
if err != nil {
|
||||
u.state.ErrorMessage = u.describeActionError("open vault", err)
|
||||
return
|
||||
}
|
||||
path := strings.TrimSpace(u.vaultPath.Text())
|
||||
if path == "" {
|
||||
u.state.ErrorMessage = u.describeActionError("open vault", errors.New(errVaultPathRequired))
|
||||
return
|
||||
}
|
||||
u.runBackgroundAction("open vault", func() (func() error, error) {
|
||||
prepared, err := session.PrepareLocalOpen(path, key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return func() error {
|
||||
manager.ApplyPreparedLocalOpen(prepared)
|
||||
u.noteRecentVault(path)
|
||||
u.resetPasswordPeek()
|
||||
u.currentPath = append([]string(nil), u.state.CurrentPath...)
|
||||
u.restoreRecentVaultGroup(path)
|
||||
u.loadSecuritySettingsFromSession()
|
||||
u.editingEntry = false
|
||||
u.filter()
|
||||
return nil
|
||||
}, nil
|
||||
})
|
||||
}
|
||||
|
||||
func (u *ui) saveAction() error {
|
||||
if err := u.state.Save(); err != nil {
|
||||
return err
|
||||
@@ -779,6 +824,48 @@ func (u *ui) openRemoteAction() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *ui) startOpenRemoteAction() {
|
||||
manager, ok := u.state.Session.(*session.Manager)
|
||||
if !ok {
|
||||
u.runAction("open remote vault", u.openRemoteAction)
|
||||
return
|
||||
}
|
||||
key, err := u.currentMasterKey()
|
||||
u.clearMasterPassword()
|
||||
if err != nil {
|
||||
u.state.ErrorMessage = u.describeActionError("open remote vault", err)
|
||||
return
|
||||
}
|
||||
client := webdav.Client{
|
||||
BaseURL: strings.TrimSpace(u.remoteBaseURL.Text()),
|
||||
Username: strings.TrimSpace(u.remoteUsername.Text()),
|
||||
Password: u.remotePassword.Text(),
|
||||
}
|
||||
remotePath := strings.TrimSpace(u.remotePath.Text())
|
||||
u.runBackgroundAction("open remote vault", func() (func() error, error) {
|
||||
prepared, err := session.PrepareRemoteOpen(client, remotePath, key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return func() error {
|
||||
manager.ApplyPreparedRemoteOpen(prepared)
|
||||
u.noteRecentRemote(
|
||||
strings.TrimSpace(u.remoteBaseURL.Text()),
|
||||
remotePath,
|
||||
strings.TrimSpace(u.remoteUsername.Text()),
|
||||
u.remotePassword.Text(),
|
||||
u.rememberRemoteAuth.Value,
|
||||
)
|
||||
u.resetPasswordPeek()
|
||||
u.restoreRecentRemoteGroup(strings.TrimSpace(u.remoteBaseURL.Text()), remotePath)
|
||||
u.loadSecuritySettingsFromSession()
|
||||
u.editingEntry = false
|
||||
u.filter()
|
||||
return nil
|
||||
}, nil
|
||||
})
|
||||
}
|
||||
|
||||
func (u *ui) lockAction() error {
|
||||
u.clearMasterPassword()
|
||||
if err := u.state.Lock(); err != nil {
|
||||
@@ -808,6 +895,36 @@ func (u *ui) unlockAction() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *ui) startUnlockAction() {
|
||||
manager, ok := u.state.Session.(*session.Manager)
|
||||
if !ok {
|
||||
u.runAction("unlock vault", u.unlockAction)
|
||||
return
|
||||
}
|
||||
key, err := u.currentMasterKey()
|
||||
u.clearMasterPassword()
|
||||
if err != nil {
|
||||
u.state.ErrorMessage = u.describeActionError("unlock vault", err)
|
||||
return
|
||||
}
|
||||
encoded := append([]byte(nil), manager.EncodedBytes()...)
|
||||
u.runBackgroundAction("unlock vault", func() (func() error, error) {
|
||||
prepared, err := session.PrepareUnlock(encoded, key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return func() error {
|
||||
manager.ApplyPreparedUnlock(prepared)
|
||||
u.resetPasswordPeek()
|
||||
u.currentPath = append([]string(nil), u.state.CurrentPath...)
|
||||
u.loadSecuritySettingsFromSession()
|
||||
u.editingEntry = false
|
||||
u.filter()
|
||||
return nil
|
||||
}, nil
|
||||
})
|
||||
}
|
||||
|
||||
func (u *ui) changeMasterKeyAction() error {
|
||||
key, err := u.currentMasterKey()
|
||||
defer u.clearMasterPassword()
|
||||
@@ -1446,6 +1563,9 @@ func (u *ui) armDeleteCurrentGroupAction() {
|
||||
}
|
||||
|
||||
func (u *ui) runAction(label string, action func() error) {
|
||||
if strings.TrimSpace(u.loadingMessage) != "" {
|
||||
return
|
||||
}
|
||||
u.loadingMessage = actionLoadingLabel(label)
|
||||
if err := action(); err != nil {
|
||||
u.loadingMessage = ""
|
||||
@@ -1466,6 +1586,61 @@ func (u *ui) runAction(label string, action func() error) {
|
||||
u.statusExpiresAt = u.now().Add(statusBannerDuration)
|
||||
}
|
||||
|
||||
func (u *ui) runBackgroundAction(label string, prepare func() (func() error, error)) {
|
||||
if strings.TrimSpace(u.loadingMessage) != "" {
|
||||
return
|
||||
}
|
||||
u.loadingMessage = actionLoadingLabel(label)
|
||||
u.state.ErrorMessage = ""
|
||||
u.state.StatusMessage = ""
|
||||
u.statusExpiresAt = time.Time{}
|
||||
go func() {
|
||||
apply, err := prepare()
|
||||
u.backgroundResults <- backgroundActionResult{label: label, apply: apply, err: err}
|
||||
if u.invalidate != nil {
|
||||
u.invalidate()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (u *ui) applyBackgroundResult(result backgroundActionResult) {
|
||||
u.loadingMessage = ""
|
||||
if result.err != nil {
|
||||
u.state.ErrorMessage = u.describeActionError(result.label, result.err)
|
||||
u.state.StatusMessage = ""
|
||||
u.statusExpiresAt = time.Time{}
|
||||
return
|
||||
}
|
||||
if result.apply != nil {
|
||||
if err := result.apply(); err != nil {
|
||||
u.state.ErrorMessage = u.describeActionError(result.label, err)
|
||||
u.state.StatusMessage = ""
|
||||
u.statusExpiresAt = time.Time{}
|
||||
return
|
||||
}
|
||||
}
|
||||
u.syncAutofillCache()
|
||||
u.state.ErrorMessage = ""
|
||||
if suppressStatusMessage(result.label) {
|
||||
u.state.StatusMessage = ""
|
||||
u.statusExpiresAt = time.Time{}
|
||||
return
|
||||
}
|
||||
u.state.StatusMessage = result.label + " complete"
|
||||
u.statusExpiresAt = u.now().Add(statusBannerDuration)
|
||||
}
|
||||
|
||||
func (u *ui) processBackgroundActions() {
|
||||
for {
|
||||
select {
|
||||
case result := <-u.backgroundResults:
|
||||
u.applyBackgroundResult(result)
|
||||
default:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (u *ui) syncAutofillCache() {
|
||||
if strings.TrimSpace(u.autofillCachePath) == "" {
|
||||
return
|
||||
@@ -1719,7 +1894,7 @@ func (u *ui) layout(gtx layout.Context) layout.Dimensions {
|
||||
u.runAction("create vault", u.createVaultAction)
|
||||
}
|
||||
for u.openVault.Clicked(gtx) {
|
||||
u.runAction("open vault", u.openVaultAction)
|
||||
u.startOpenVaultAction()
|
||||
}
|
||||
for u.saveVault.Clicked(gtx) {
|
||||
u.runAction("save vault", u.saveAction)
|
||||
@@ -1728,7 +1903,7 @@ func (u *ui) layout(gtx layout.Context) layout.Dimensions {
|
||||
u.runAction("save-as vault", u.saveAsAction)
|
||||
}
|
||||
for u.openRemote.Clicked(gtx) {
|
||||
u.runAction("open remote vault", u.openRemoteAction)
|
||||
u.startOpenRemoteAction()
|
||||
}
|
||||
for u.changeMasterKey.Clicked(gtx) {
|
||||
u.runAction("change master key", u.changeMasterKeyAction)
|
||||
@@ -1760,7 +1935,7 @@ func (u *ui) layout(gtx layout.Context) layout.Dimensions {
|
||||
u.runAction("save security settings", u.saveSecuritySettingsAction)
|
||||
}
|
||||
for u.unlockVault.Clicked(gtx) {
|
||||
u.runAction("unlock vault", u.unlockAction)
|
||||
u.startUnlockAction()
|
||||
}
|
||||
for u.showEntries.Clicked(gtx) {
|
||||
u.clearDeleteGroupConfirmation()
|
||||
@@ -3661,6 +3836,7 @@ func run(w *app.Window, mode string, paths statePaths, grpcAddr string) error {
|
||||
var ops op.Ops
|
||||
manager := &session.Manager{}
|
||||
ui := newUIWithSession(mode, manager, paths)
|
||||
ui.invalidate = w.Invalidate
|
||||
host, err := api.StartHost(grpcAddr, manager, passwords.DefaultProfiles(), ui.clipboardWriter, func() bool { return ui.state.Dirty })
|
||||
if err != nil {
|
||||
ui.state.ErrorMessage = fmt.Sprintf("start gRPC API: %v", err)
|
||||
@@ -3678,6 +3854,7 @@ func run(w *app.Window, mode string, paths statePaths, grpcAddr string) error {
|
||||
return e.Err
|
||||
case app.FrameEvent:
|
||||
gtx := app.NewContext(&ops, e)
|
||||
ui.processBackgroundActions()
|
||||
ui.layout(gtx)
|
||||
e.Frame(gtx.Ops)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user