added a cache to the IdentityManager, changed its signature, modified when and where the author is stored

made the author creation single-threaded again in the LifecycleManager, removed redundant code
This commit is contained in:
Ernir Erlingsson
2016-10-13 11:01:25 +02:00
parent 47d6fc526f
commit eaa393a7ed
45 changed files with 299 additions and 552 deletions

View File

@@ -6,7 +6,7 @@ public interface ConfigController {
String getEncryptedDatabaseKey();
void setEncryptedDatabaseKey(String hex);
void storeEncryptedDatabaseKey(String hex);
void deleteAccount(Context ctx);

View File

@@ -29,7 +29,7 @@ public class ConfigControllerImpl implements ConfigController {
}
@Override
public void setEncryptedDatabaseKey(String hex) {
public void storeEncryptedDatabaseKey(String hex) {
SharedPreferences.Editor editor = briarPrefs.edit();
editor.putString(PREF_DB_KEY, hex);
editor.apply();

View File

@@ -1,15 +1,9 @@
package org.briarproject.android.controller;
import org.briarproject.android.controller.handler.ResultHandler;
import org.briarproject.api.TransportId;
import org.briarproject.api.identity.LocalAuthor;
public interface NavDrawerController extends ActivityLifecycleController {
boolean isTransportRunning(TransportId transportId);
void storeLocalAuthor(LocalAuthor author,
ResultHandler<Void> resultHandler);
LocalAuthor removeAuthorHandle(long handle);
}

View File

@@ -2,18 +2,14 @@ package org.briarproject.android.controller;
import android.app.Activity;
import org.briarproject.android.api.ReferenceManager;
import org.briarproject.android.controller.handler.ResultHandler;
import org.briarproject.api.TransportId;
import org.briarproject.api.db.DatabaseExecutor;
import org.briarproject.api.db.DbException;
import org.briarproject.api.event.Event;
import org.briarproject.api.event.EventBus;
import org.briarproject.api.event.EventListener;
import org.briarproject.api.event.TransportDisabledEvent;
import org.briarproject.api.event.TransportEnabledEvent;
import org.briarproject.api.identity.IdentityManager;
import org.briarproject.api.identity.LocalAuthor;
import org.briarproject.api.lifecycle.LifecycleManager;
import org.briarproject.api.plugins.Plugin;
import org.briarproject.api.plugins.PluginManager;
@@ -24,7 +20,6 @@ import java.util.logging.Logger;
import javax.inject.Inject;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
public class NavDrawerControllerImpl extends DbControllerImpl
implements NavDrawerController, EventListener {
@@ -32,7 +27,6 @@ public class NavDrawerControllerImpl extends DbControllerImpl
private static final Logger LOG =
Logger.getLogger(NavDrawerControllerImpl.class.getName());
private final ReferenceManager referenceManager;
private final PluginManager pluginManager;
private final EventBus eventBus;
private final IdentityManager identityManager;
@@ -42,10 +36,9 @@ public class NavDrawerControllerImpl extends DbControllerImpl
@Inject
NavDrawerControllerImpl(@DatabaseExecutor Executor dbExecutor,
LifecycleManager lifecycleManager,
ReferenceManager referenceManager, PluginManager pluginManager,
EventBus eventBus, IdentityManager identityManager) {
PluginManager pluginManager, EventBus eventBus,
IdentityManager identityManager) {
super(dbExecutor, lifecycleManager);
this.referenceManager = referenceManager;
this.pluginManager = pluginManager;
this.eventBus = eventBus;
this.identityManager = identityManager;
@@ -101,33 +94,8 @@ public class NavDrawerControllerImpl extends DbControllerImpl
@Override
public boolean isTransportRunning(TransportId transportId) {
Plugin plugin = pluginManager.getPlugin(transportId);
return plugin != null && plugin.isRunning();
}
@Override
public void storeLocalAuthor(final LocalAuthor author,
final ResultHandler<Void> resultHandler) {
runOnDbThread(new Runnable() {
@Override
public void run() {
try {
long now = System.currentTimeMillis();
identityManager.addLocalAuthor(author);
long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO))
LOG.info("Storing author took " + duration + " ms");
resultHandler.onResult(null);
} catch (final DbException e) {
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
}
}
});
}
@Override
public LocalAuthor removeAuthorHandle(long handle) {
return referenceManager.removeReference(handle, LocalAuthor.class);
}
}

View File

@@ -65,7 +65,7 @@ public class PasswordControllerImpl extends ConfigControllerImpl
} else {
String hex =
encryptDatabaseKey(new SecretKey(key), newPassword);
setEncryptedDatabaseKey(hex);
storeEncryptedDatabaseKey(hex);
resultHandler.onResult(true);
}
}

View File

@@ -6,6 +6,7 @@ public interface SetupController {
float estimatePasswordStrength(String password);
void createIdentity(String nickname, String password,
ResultHandler<Long> resultHandler);
void storeAuthorInfo(String password, String nickName,
ResultHandler<Void> resultHandler);
}

View File

@@ -2,24 +2,18 @@ package org.briarproject.android.controller;
import android.content.SharedPreferences;
import org.briarproject.android.api.ReferenceManager;
import org.briarproject.android.controller.handler.ResultHandler;
import org.briarproject.api.crypto.CryptoComponent;
import org.briarproject.api.crypto.CryptoExecutor;
import org.briarproject.api.crypto.KeyPair;
import org.briarproject.api.crypto.PasswordStrengthEstimator;
import org.briarproject.api.crypto.SecretKey;
import org.briarproject.api.db.DatabaseConfig;
import org.briarproject.api.identity.AuthorFactory;
import org.briarproject.api.identity.LocalAuthor;
import java.util.concurrent.Executor;
import java.util.logging.Logger;
import javax.inject.Inject;
import static java.util.logging.Level.INFO;
public class SetupControllerImpl extends PasswordControllerImpl
implements SetupController {
@@ -27,33 +21,14 @@ public class SetupControllerImpl extends PasswordControllerImpl
Logger.getLogger(SetupControllerImpl.class.getName());
private final PasswordStrengthEstimator strengthEstimator;
private final AuthorFactory authorFactory;
private final ReferenceManager referenceManager;
@Inject
SetupControllerImpl(SharedPreferences briarPrefs,
DatabaseConfig databaseConfig,
@CryptoExecutor Executor cryptoExecutor, CryptoComponent crypto,
PasswordStrengthEstimator strengthEstimator,
AuthorFactory authorFactory, ReferenceManager referenceManager) {
PasswordStrengthEstimator strengthEstimator) {
super(briarPrefs, databaseConfig, cryptoExecutor, crypto);
this.strengthEstimator = strengthEstimator;
this.authorFactory = authorFactory;
this.referenceManager = referenceManager;
}
private LocalAuthor createLocalAuthor(String nickname) {
long now = System.currentTimeMillis();
KeyPair keyPair = crypto.generateSignatureKeyPair();
byte[] publicKey = keyPair.getPublic().getEncoded();
byte[] privateKey = keyPair.getPrivate().getEncoded();
LocalAuthor localAuthor = authorFactory.createLocalAuthor(nickname,
publicKey, privateKey);
long duration = System.currentTimeMillis() - now;
if (LOG.isLoggable(INFO))
LOG.info("Identity creation took " + duration + " ms");
return localAuthor;
}
@Override
@@ -62,20 +37,19 @@ public class SetupControllerImpl extends PasswordControllerImpl
}
@Override
public void createIdentity(final String nickname, final String password,
final ResultHandler<Long> resultHandler) {
public void storeAuthorInfo(final String password, final String nickName,
final ResultHandler<Void> resultHandler) {
cryptoExecutor.execute(new Runnable() {
@Override
public void run() {
SecretKey key = crypto.generateSecretKey();
databaseConfig.setEncryptionKey(key);
String hex = encryptDatabaseKey(key, password);
setEncryptedDatabaseKey(hex);
LocalAuthor localAuthor = createLocalAuthor(nickname);
long handle = referenceManager.putReference(localAuthor,
LocalAuthor.class);
resultHandler.onResult(handle);
storeEncryptedDatabaseKey(hex);
databaseConfig.setAuthorNick(nickName);
resultHandler.onResult(null);
}
});
}
}