introduce named states in MessageCryptoHelper

This commit is contained in:
Vincent Breitmoser 2017-08-27 23:14:20 +02:00
parent 0fe68498e0
commit 05361adfde

View file

@ -81,9 +81,7 @@ public class MessageCryptoHelper {
private CryptoPart currentCryptoPart; private CryptoPart currentCryptoPart;
private Intent currentCryptoResult; private Intent currentCryptoResult;
private Intent userInteractionResultIntent; private Intent userInteractionResultIntent;
private boolean firstPassStarted; private State state;
private boolean secondPassStarted;
private boolean thirdPassStarted;
private CancelableBackgroundOperation cancelableBackgroundOperation; private CancelableBackgroundOperation cancelableBackgroundOperation;
private boolean isCancelled; private boolean isCancelled;
@ -116,6 +114,7 @@ public class MessageCryptoHelper {
} }
this.messageAnnotations = new MessageCryptoAnnotations(); this.messageAnnotations = new MessageCryptoAnnotations();
this.state = State.START;
this.currentMessage = message; this.currentMessage = message;
this.cachedDecryptionResult = cachedDecryptionResult; this.cachedDecryptionResult = cachedDecryptionResult;
this.callback = callback; this.callback = callback;
@ -123,28 +122,26 @@ public class MessageCryptoHelper {
nextStep(); nextStep();
} }
private void findPartsForFirstPass() { private void findPartsForEncryptionPass() {
firstPassStarted = true;
List<Part> encryptedParts = MessageDecryptVerifier.findEncryptedParts(currentMessage); List<Part> encryptedParts = MessageDecryptVerifier.findEncryptedParts(currentMessage);
processFoundEncryptedParts(encryptedParts); processFoundEncryptedParts(encryptedParts);
} }
private void findPartsForSecondPass() { private void findPartsForSignaturePass() {
secondPassStarted = true;
List<Part> signedParts = MessageDecryptVerifier.findSignedParts(currentMessage, messageAnnotations); List<Part> signedParts = MessageDecryptVerifier.findSignedParts(currentMessage, messageAnnotations);
processFoundSignedParts(signedParts); processFoundSignedParts(signedParts);
List<Part> inlineParts = MessageDecryptVerifier.findPgpInlineParts(currentMessage); List<Part> inlineParts = MessageDecryptVerifier.findPgpInlineParts(currentMessage);
addFoundInlinePgpParts(inlineParts); processFoundInlinePgpParts(inlineParts);
} }
private void findPartsForThirdPass() { private void findPartsForAutocryptPass() {
thirdPassStarted = true; boolean otherCryptoPerformed = !messageAnnotations.isEmpty();
if (otherCryptoPerformed) {
return;
}
boolean noOtherCryptoPerformed = messageAnnotations.isEmpty(); if (autocryptOperations.hasAutocryptHeader(currentMessage)) {
if (noOtherCryptoPerformed && autocryptOperations.hasAutocryptHeader(currentMessage)) {
CryptoPart cryptoPart = new CryptoPart(CryptoPartType.PLAIN_AUTOCRYPT, currentMessage); CryptoPart cryptoPart = new CryptoPart(CryptoPartType.PLAIN_AUTOCRYPT, currentMessage);
partsToProcess.add(cryptoPart); partsToProcess.add(cryptoPart);
} }
@ -187,7 +184,7 @@ public class MessageCryptoHelper {
messageAnnotations.put(part, annotation); messageAnnotations.put(part, annotation);
} }
private void addFoundInlinePgpParts(List<Part> foundParts) { private void processFoundInlinePgpParts(List<Part> foundParts) {
for (Part part : foundParts) { for (Part part : foundParts) {
if (!currentMessage.getFlags().contains(Flag.X_DOWNLOADED_FULL)) { if (!currentMessage.getFlags().contains(Flag.X_DOWNLOADED_FULL)) {
if (MessageDecryptVerifier.isPartPgpInlineEncrypted(part)) { if (MessageDecryptVerifier.isPartPgpInlineEncrypted(part)) {
@ -209,13 +206,13 @@ public class MessageCryptoHelper {
return; return;
} }
while (partsToProcess.isEmpty()) { while (state != State.FINISHED && partsToProcess.isEmpty()) {
boolean hadNextPass = findPartsForNextPass(); findPartsForNextPass();
}
if (!hadNextPass) { if (state == State.FINISHED) {
callbackReturnResult(); callbackReturnResult();
return; return;
}
} }
if (!isBoundToCryptoProviderService()) { if (!isBoundToCryptoProviderService()) {
@ -642,21 +639,37 @@ public class MessageCryptoHelper {
nextStep(); nextStep();
} }
private boolean findPartsForNextPass() { private void findPartsForNextPass() {
if (!firstPassStarted) { switch (state) {
findPartsForFirstPass(); case START: {
return true; state = State.ENCRYPTION;
}
if (!secondPassStarted) {
findPartsForSecondPass();
return true;
}
if (!thirdPassStarted) {
findPartsForThirdPass();
return true;
}
return false; findPartsForEncryptionPass();
return;
}
case ENCRYPTION: {
state = State.SIGNATURES;
findPartsForSignaturePass();
return;
}
case SIGNATURES: {
state = State.AUTOCRYPT;
findPartsForAutocryptPass();
return;
}
case AUTOCRYPT: {
state = State.FINISHED;
return;
}
default: {
throw new IllegalStateException("unhandled state");
}
}
} }
private void cleanupAfterProcessingFinished() { private void cleanupAfterProcessingFinished() {
@ -780,4 +793,8 @@ public class MessageCryptoHelper {
return NO_REPLACEMENT_PART; return NO_REPLACEMENT_PART;
} }
} }
private enum State {
START, ENCRYPTION, SIGNATURES, AUTOCRYPT, FINISHED
}
} }