Skip to content

Commit

Permalink
rename async methods + locking tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
jamie-mh committed Aug 25, 2023
1 parent a2d5bfc commit 3a4d30e
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 89 deletions.
21 changes: 17 additions & 4 deletions AuthenticatorPro.Droid/src/Activity/AsyncActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,15 @@ protected override async void OnResume()
base.OnResume();

await _onResumeLock.WaitAsync();
await OnResumeAsync();
_onResumeLock.Release();

try
{
await OnResumeAsync();
}
finally
{
_onResumeLock.Release();
}
}

protected abstract Task OnResumeAsync();
Expand All @@ -63,9 +70,15 @@ protected override async void OnActivityResult(int requestCode, [GeneratedEnum]
base.OnActivityResult(requestCode, resultCode, intent);

await _onResumeLock.WaitAsync();
_onResumeLock.Release();

await OnActivityResultAsync(requestCode, resultCode, intent);
try
{
await OnActivityResultAsync(requestCode, resultCode, intent);
}
finally
{
_onResumeLock.Release();
}
}

protected abstract Task
Expand Down
10 changes: 5 additions & 5 deletions AuthenticatorPro.Droid/src/Activity/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ protected override async Task OnResumeAsync()
_authenticatorList.Visibility = ViewStates.Invisible;
});

switch (await _database.IsOpen(Database.Origin.Activity))
switch (await _database.IsOpenAsync(Database.Origin.Activity))
{
// Unlocked, no need to do anything
case true:
Expand All @@ -266,7 +266,7 @@ protected override async Task OnResumeAsync()
{
_unlockFragmentOpen = false;

if (!await _database.IsOpen(Database.Origin.Activity))
if (!await _database.IsOpenAsync(Database.Origin.Activity))
{
Finish();
}
Expand All @@ -283,7 +283,7 @@ protected override async Task OnResumeAsync()
{
try
{
await _database.Open(null, Database.Origin.Activity);
await _database.OpenAsync(null, Database.Origin.Activity);
}
catch (Exception e)
{
Expand Down Expand Up @@ -670,7 +670,7 @@ private async void OnUnlockAttempted(object sender, string password)

try
{
await _database.Open(password, Database.Origin.Activity);
await _database.OpenAsync(password, Database.Origin.Activity);
}
catch (Exception e)
{
Expand Down Expand Up @@ -752,7 +752,7 @@ private void ShowDatabaseErrorDialog(Exception exception)

builder.SetPositiveButton(Resource.String.retry, async delegate
{
await _database.Close(Database.Origin.Activity);
await _database.CloseAsync(Database.Origin.Activity);
Recreate();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protected override async void OnResume()
base.OnResume();
var database = Dependencies.Resolve<Database>();

if (!await database.IsOpen(Database.Origin.Activity))
if (!await database.IsOpenAsync(Database.Origin.Activity))
{
Finish();
}
Expand Down
4 changes: 2 additions & 2 deletions AuthenticatorPro.Droid/src/AutoBackupWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ public AutoBackupWorker(Context context, WorkerParameters workerParams) : base(c
private async Task OpenDatabase()
{
var password = _secureStorageWrapper.GetDatabasePassword();
await _database.Open(password, Database.Origin.AutoBackup);
await _database.OpenAsync(password, Database.Origin.AutoBackup);
}

private async Task CloseDatabase()
{
await _database.Close(Database.Origin.AutoBackup);
await _database.CloseAsync(Database.Origin.AutoBackup);
}

private bool HasPersistentPermissionsAtUri(Uri uri)
Expand Down
12 changes: 6 additions & 6 deletions AuthenticatorPro.Droid/src/BaseApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ internal class BaseApplication : Application, ILifecycleObserver

public BaseApplication(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
Dependencies.Register();
_database = new Database();

Dependencies.Register(_database);
Dependencies.RegisterApplicationContext(this);

AutoLockEnabled = false;
PreventNextAutoLock = false;

_database = Dependencies.Resolve<Database>();
}

public override void OnCreate()
Expand Down Expand Up @@ -100,15 +100,15 @@ public async void OnStopped()

if (!_preferences.PasswordProtected || _preferences.Timeout == 0)
{
await _database.Close(Database.Origin.Application);
await _database.CloseAsync(Database.Origin.Application);
}
else
{
_timeoutTimer = new Timer(_preferences.Timeout * 1000) { AutoReset = false };

_timeoutTimer.Elapsed += async delegate
{
await _database.Close(Database.Origin.Application);
await _database.CloseAsync(Database.Origin.Application);
};

_timeoutTimer.Start();
Expand All @@ -126,7 +126,7 @@ public void OnStarted()
[Export]
public async void OnDestroyed()
{
await _database.Close(Database.Origin.Application);
await _database.CloseAsync(Database.Origin.Application);
}
}
}
103 changes: 62 additions & 41 deletions AuthenticatorPro.Droid/src/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,43 +25,54 @@ public enum Origin
Application, Activity, Wear, AutoBackup, Gc, Other
}

public async Task<SQLiteAsyncConnection> GetConnection()
public async Task<SQLiteAsyncConnection> GetConnectionAsync()
{
await _lock.WaitAsync();

if (_connection == null)
try
{
if (_connection == null)
{
throw new InvalidOperationException("Connection not open");
}

return _connection;
}
finally
{
_lock.Release();
throw new InvalidOperationException("Connection not open");
}

_lock.Release();
return _connection;
}

public async Task<bool> IsOpen(Origin origin)
{
await _lock.WaitAsync();
var isOpen = _connection != null;
Logger.Debug($"Is database open from {origin}? {isOpen}");
_lock.Release();
return isOpen;
}

public async Task Close(Origin origin)
public async Task<bool> IsOpenAsync(Origin origin)
{
await _lock.WaitAsync();

if (_connection == null)
try
{
var isOpen = _connection != null;
Logger.Debug($"Is database open from {origin}? {isOpen}");
return isOpen;
}
finally
{
_lock.Release();
return;
}
}

Logger.Debug($"Closing database from {origin}");
public async Task CloseAsync(Origin origin)
{
await _lock.WaitAsync();

try
{
if (_connection == null)
{
return;
}

Logger.Debug($"Closing database from {origin}");

await _connection.CloseAsync();
_connection = null;
}
Expand All @@ -71,9 +82,8 @@ public async Task Close(Origin origin)
}
}

public async Task Open(string password, Origin origin)
public async Task OpenAsync(string password, Origin origin)
{
await Close(origin);
Logger.Debug($"Opening database from {origin}");

var path = GetPath();
Expand All @@ -95,26 +105,37 @@ public async Task Open(string password, Origin origin)
});

await _lock.WaitAsync();
_connection = new SQLiteAsyncConnection(connStr);

try
{
await MigrateAsync(firstLaunch);
}
catch
{
_lock.Release();
await Close(origin);
throw;
}
if (_connection != null)
{
await _connection.CloseAsync();
}

_connection = new SQLiteAsyncConnection(connStr);

try
{
await MigrateAsync(firstLaunch);
}
catch
{
await _connection.CloseAsync();
_connection = null;
throw;
}

#if DEBUG
_connection.Trace = true;
_connection.Tracer = Logger.Debug;
_connection.TimeExecution = true;
#endif

_lock.Release();
}
finally
{
_lock.Release();
}
}

private async Task MigrateAsync(bool firstLaunch)
Expand All @@ -140,7 +161,7 @@ private static string GetPath()
);
}

public async Task SetPassword(string currentPassword, string newPassword)
public async Task SetPasswordAsync(string currentPassword, string newPassword)
{
if (currentPassword == newPassword)
{
Expand Down Expand Up @@ -168,7 +189,7 @@ void RestoreBackup()

try
{
conn = await GetConnection();
conn = await GetConnectionAsync();
await conn.ExecuteScalarAsync<string>("PRAGMA wal_checkpoint(TRUNCATE)");
}
catch
Expand Down Expand Up @@ -209,17 +230,17 @@ void RestoreBackup()

try
{
await Close(Origin.Other);
await CloseAsync(Origin.Other);
DeleteDatabase();
File.Move(tempPath, dbPath);
await Open(newPassword, Origin.Other);
await OpenAsync(newPassword, Origin.Other);
}
catch
{
// Perhaps it wasn't moved correctly
File.Delete(tempPath);
RestoreBackup();
await Open(currentPassword, Origin.Other);
await OpenAsync(currentPassword, Origin.Other);
throw;
}
finally
Expand All @@ -237,13 +258,13 @@ void RestoreBackup()
{
await conn.ExecuteScalarAsync<string>($"PRAGMA rekey = {quoted}");

await Close(Origin.Other);
await Open(newPassword, Origin.Other);
await CloseAsync(Origin.Other);
await OpenAsync(newPassword, Origin.Other);
}
catch
{
RestoreBackup();
await Open(currentPassword, Origin.Other);
await OpenAsync(currentPassword, Origin.Other);
throw;
}
finally
Expand All @@ -255,7 +276,7 @@ void RestoreBackup()

public async ValueTask DisposeAsync()
{
await Close(Origin.Gc);
await CloseAsync(Origin.Gc);
}
}
}
4 changes: 2 additions & 2 deletions AuthenticatorPro.Droid/src/Dependencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ internal static class Dependencies
{
private static readonly TinyIoCContainer Container = TinyIoCContainer.Current;

public static void Register()
public static void Register(Database database)
{
Container.Register<Database>().AsSingleton();
Container.Register(database);
Container.RegisterMultiple<IBackupEncryption>(new []
{
typeof(StrongBackupEncryption), typeof(LegacyBackupEncryption), typeof(NoBackupEncryption)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private async void OnSetPasswordButtonClick(object sender, EventArgs args)
try
{
var currentPassword = _secureStorageWrapper.GetDatabasePassword();
await _database.SetPassword(currentPassword, newPassword);
await _database.SetPasswordAsync(currentPassword, newPassword);

try
{
Expand All @@ -116,7 +116,7 @@ private async void OnSetPasswordButtonClick(object sender, EventArgs args)
catch
{
// Revert changes
await _database.SetPassword(newPassword, currentPassword);
await _database.SetPasswordAsync(newPassword, currentPassword);
throw;
}
}
Expand Down
Loading

0 comments on commit 3a4d30e

Please sign in to comment.