diff options
Diffstat (limited to 'platform/platform-impl/src/com/intellij/ide/passwordSafe/impl/providers/masterKey/MasterKeyPasswordSafe.java')
-rw-r--r-- | platform/platform-impl/src/com/intellij/ide/passwordSafe/impl/providers/masterKey/MasterKeyPasswordSafe.java | 65 |
1 files changed, 36 insertions, 29 deletions
diff --git a/platform/platform-impl/src/com/intellij/ide/passwordSafe/impl/providers/masterKey/MasterKeyPasswordSafe.java b/platform/platform-impl/src/com/intellij/ide/passwordSafe/impl/providers/masterKey/MasterKeyPasswordSafe.java index 6b0b9814698b..de5d67b5e2d8 100644 --- a/platform/platform-impl/src/com/intellij/ide/passwordSafe/impl/providers/masterKey/MasterKeyPasswordSafe.java +++ b/platform/platform-impl/src/com/intellij/ide/passwordSafe/impl/providers/masterKey/MasterKeyPasswordSafe.java @@ -15,6 +15,8 @@ */ package com.intellij.ide.passwordSafe.impl.providers.masterKey; +import com.intellij.concurrency.AsyncFutureFactory; +import com.intellij.concurrency.AsyncFutureResult; import com.intellij.ide.passwordSafe.MasterPasswordUnavailableException; import com.intellij.ide.passwordSafe.PasswordSafeException; import com.intellij.ide.passwordSafe.impl.PasswordSafeTimed; @@ -23,14 +25,11 @@ import com.intellij.ide.passwordSafe.impl.providers.ByteArrayWrapper; import com.intellij.ide.passwordSafe.impl.providers.EncryptionUtil; import com.intellij.ide.passwordSafe.impl.providers.masterKey.windows.WindowsCryptUtils; import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.application.ModalityState; import com.intellij.openapi.progress.ProcessCanceledException; import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.Condition; -import com.intellij.openapi.util.Ref; -import com.intellij.openapi.util.SystemInfo; -import com.intellij.openapi.util.ThrowableComputable; +import com.intellij.openapi.util.*; import com.intellij.openapi.util.registry.Registry; +import com.intellij.openapi.wm.IdeFocusManager; import com.intellij.util.ArrayUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -38,6 +37,7 @@ import org.jetbrains.annotations.Nullable; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ExecutionException; /** * The password safe that stores information in configuration file encrypted by master password @@ -170,12 +170,6 @@ public class MasterKeyPasswordSafe extends BasePasswordSafeProvider { throw new MasterPasswordUnavailableException("The provider is not available in headless environment"); } - if (myDatabase.isEmpty()) { - if (!MasterPasswordDialog.resetMasterPasswordDialog(project, this, requestor).showAndGet()) { - throw new MasterPasswordUnavailableException("Master password is required to store passwords in the database."); - } - } - key = invokeAndWait(new ThrowableComputable<Object, PasswordSafeException>() { @Override public Object compute() throws PasswordSafeException { @@ -184,7 +178,14 @@ public class MasterKeyPasswordSafe extends BasePasswordSafeProvider { return key; } try { - MasterPasswordDialog.askPassword(project, MasterKeyPasswordSafe.this, requestor); + if (myDatabase.isEmpty()) { + if (!MasterPasswordDialog.resetMasterPasswordDialog(project, MasterKeyPasswordSafe.this, requestor).showAndGet()) { + throw new MasterPasswordUnavailableException("Master password is required to store passwords in the database."); + } + } + else { + MasterPasswordDialog.askPassword(project, MasterKeyPasswordSafe.this, requestor); + } } catch (PasswordSafeException e) { myKey.get().set(e); @@ -200,35 +201,41 @@ public class MasterKeyPasswordSafe extends BasePasswordSafeProvider { } private static final Object ourEDTLock = new Object(); - public <T, E extends Throwable> T invokeAndWait(@NotNull final ThrowableComputable<T, E> computable, @NotNull final Condition expired) throws E { + public <T, E extends Throwable> T invokeAndWait(@NotNull final ThrowableComputable<T, E> computable, @NotNull final Condition<?> expired) throws E { if (ApplicationManager.getApplication().isDispatchThread()) { return computable.compute(); } - final Ref<Throwable> exRef = Ref.create(); - final Ref<T> ref = Ref.create(); + + final AsyncFutureResult<Object> future = AsyncFutureFactory.getInstance().createAsyncFutureResult(); synchronized (ourEDTLock) { - if (expired.value(null)) { - throw new ProcessCanceledException(); - } - ApplicationManager.getApplication().invokeAndWait(new Runnable() { + IdeFocusManager.getGlobalInstance().doWhenFocusSettlesDown(new ExpirableRunnable() { @Override - public void run() { - if (expired.value(null)) { - exRef.set(new ProcessCanceledException()); - return; - } + public boolean isExpired() { + boolean b = expired.value(null); + if (b) future.setException(new ProcessCanceledException()); + return b; + } + @Override + public void run() { try { - ref.set(computable.compute()); + future.set(computable.compute()); } catch (Throwable e) { - exRef.set(e); + future.setException(e); } } - }, ModalityState.any()); + }); + } + try { + return (T)future.get(); + } + catch (InterruptedException e) { + throw new ProcessCanceledException(e); + } + catch (ExecutionException e) { + throw (E) e.getCause(); } - if (!exRef.isNull()) throw (E)exRef.get(); - return ref.get(); } @Override |