mirror of
https://github.com/bitwarden/android.git
synced 2024-10-31 23:25:45 +03:00
service container
This commit is contained in:
parent
e1080f9be4
commit
b9838ecc4e
4 changed files with 98 additions and 3 deletions
|
@ -1,6 +1,13 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.IO;
|
||||||
using Android.App;
|
using Android.App;
|
||||||
using Android.Runtime;
|
using Android.Runtime;
|
||||||
|
using Bit.App.Abstractions;
|
||||||
|
using Bit.App.Services;
|
||||||
|
using Bit.Core.Abstractions;
|
||||||
|
using Bit.Core.Services;
|
||||||
|
using Bit.Core.Utilities;
|
||||||
|
using Bit.Droid.Services;
|
||||||
|
|
||||||
namespace Bit.Droid
|
namespace Bit.Droid
|
||||||
{
|
{
|
||||||
|
@ -14,12 +21,34 @@ namespace Bit.Droid
|
||||||
{
|
{
|
||||||
public MainApplication(IntPtr handle, JniHandleOwnership transer)
|
public MainApplication(IntPtr handle, JniHandleOwnership transer)
|
||||||
: base(handle, transer)
|
: base(handle, transer)
|
||||||
{ }
|
{
|
||||||
|
if(ServiceContainer.RegisteredServices.Count == 0)
|
||||||
|
{
|
||||||
|
RegisterLocalServices();
|
||||||
|
ServiceContainer.Init();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public override void OnCreate()
|
public override void OnCreate()
|
||||||
{
|
{
|
||||||
base.OnCreate();
|
base.OnCreate();
|
||||||
Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this);
|
Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void RegisterLocalServices()
|
||||||
|
{
|
||||||
|
var preferencesStorage = new PreferencesStorageService(null);
|
||||||
|
var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
|
||||||
|
var liteDbStorage = new LiteDbStorageService(Path.Combine(documentsPath, "bitwarden.db"));
|
||||||
|
var deviceActionService = new DeviceActionService();
|
||||||
|
|
||||||
|
ServiceContainer.Register<ICryptoPrimitiveService>("cryptoPrimitiveService", new CryptoPrimitiveService());
|
||||||
|
ServiceContainer.Register<IStorageService>("storageService",
|
||||||
|
new MobileStorageService(preferencesStorage, liteDbStorage));
|
||||||
|
ServiceContainer.Register<IStorageService>("secureStorageService", new SecureStorageService());
|
||||||
|
ServiceContainer.Register<IDeviceActionService>("deviceActionService", deviceActionService);
|
||||||
|
ServiceContainer.Register<IPlatformUtilsService>("platformUtilsService",
|
||||||
|
new MobilePlatformUtilsService(deviceActionService));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -4,7 +4,7 @@ using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Bit.Core.Services
|
namespace Bit.Core.Services
|
||||||
{
|
{
|
||||||
public class AppIdService
|
public class AppIdService : IAppIdService
|
||||||
{
|
{
|
||||||
private readonly IStorageService _storageService;
|
private readonly IStorageService _storageService;
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ using static PCLCrypto.WinRTCrypto;
|
||||||
|
|
||||||
namespace Bit.Core.Services
|
namespace Bit.Core.Services
|
||||||
{
|
{
|
||||||
public class PclCryptoFunctionService
|
public class PclCryptoFunctionService : ICryptoFunctionService
|
||||||
{
|
{
|
||||||
private readonly ICryptoPrimitiveService _cryptoPrimitiveService;
|
private readonly ICryptoPrimitiveService _cryptoPrimitiveService;
|
||||||
|
|
||||||
|
|
66
src/Core/Utilities/ServiceContainer.cs
Normal file
66
src/Core/Utilities/ServiceContainer.cs
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
using Bit.Core.Abstractions;
|
||||||
|
using Bit.Core.Services;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Bit.Core.Utilities
|
||||||
|
{
|
||||||
|
public static class ServiceContainer
|
||||||
|
{
|
||||||
|
public static Dictionary<string, object> RegisteredServices { get; set; } = new Dictionary<string, object>();
|
||||||
|
public static bool Inited { get; set; }
|
||||||
|
|
||||||
|
public static void Init()
|
||||||
|
{
|
||||||
|
if(Inited)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Inited = true;
|
||||||
|
|
||||||
|
var platformUtilsService = Resolve<IPlatformUtilsService>("platformUtilsService");
|
||||||
|
var storageService = Resolve<IStorageService>("storageService");
|
||||||
|
var secureStorageService = Resolve<IStorageService>("secureStorageService");
|
||||||
|
var cryptoPrimitiveService = Resolve<ICryptoPrimitiveService>("cryptoPrimitiveService");
|
||||||
|
|
||||||
|
var stateService = new StateService();
|
||||||
|
var cryptoFunctionService = new PclCryptoFunctionService(cryptoPrimitiveService);
|
||||||
|
var cryptoService = new CryptoService(storageService, secureStorageService, cryptoFunctionService);
|
||||||
|
var tokenService = new TokenService(storageService);
|
||||||
|
var apiService = new ApiService(tokenService, platformUtilsService, (bool expired) => Task.FromResult(0));
|
||||||
|
var appIdService = new AppIdService(storageService);
|
||||||
|
var userService = new UserService(storageService, tokenService);
|
||||||
|
|
||||||
|
Register<IStateService>("stateService", stateService);
|
||||||
|
Register<ICryptoFunctionService>("cryptoFunctionService", cryptoFunctionService);
|
||||||
|
Register<ICryptoService>("cryptoService", cryptoService);
|
||||||
|
Register<ITokenService>("tokenService", tokenService);
|
||||||
|
Register<ApiService>("apiService", apiService); // TODO: interface
|
||||||
|
Register<IAppIdService>("appIdService", appIdService);
|
||||||
|
Register<IUserService>("userService", userService);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Register<T>(string serviceName, T obj)
|
||||||
|
{
|
||||||
|
if(RegisteredServices.ContainsKey(serviceName))
|
||||||
|
{
|
||||||
|
throw new Exception($"Service {serviceName} has already been registered.");
|
||||||
|
}
|
||||||
|
RegisteredServices.Add(serviceName, obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static T Resolve<T>(string serviceName, bool dontThrow = false)
|
||||||
|
{
|
||||||
|
if(RegisteredServices.ContainsKey(serviceName))
|
||||||
|
{
|
||||||
|
return (T)RegisteredServices[serviceName];
|
||||||
|
}
|
||||||
|
if(dontThrow)
|
||||||
|
{
|
||||||
|
return (T)(object)null;
|
||||||
|
}
|
||||||
|
throw new Exception($"Service {serviceName} is not registered.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue