1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-06-28 10:54:59 +02:00
bitwarden-mobile/src/Core/Utilities/ServiceContainer.cs

136 lines
7.0 KiB
C#
Raw Normal View History

2019-04-11 20:14:34 +02:00
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(string customUserAgent = null, string clearCipherCacheKey = null,
string[] allClearCipherCacheKeys = null)
2019-04-11 20:14:34 +02:00
{
if (Inited)
2019-04-11 20:14:34 +02:00
{
return;
}
Inited = true;
var platformUtilsService = Resolve<IPlatformUtilsService>("platformUtilsService");
var storageService = Resolve<IStorageService>("storageService");
var secureStorageService = Resolve<IStorageService>("secureStorageService");
2019-04-18 16:40:57 +02:00
var i18nService = Resolve<II18nService>("i18nService");
var messagingService = Resolve<IMessagingService>("messagingService");
var cryptoFunctionService = Resolve<ICryptoFunctionService>("cryptoFunctionService");
var cryptoService = Resolve<ICryptoService>("cryptoService");
SearchService searchService = null;
2019-04-11 20:14:34 +02:00
var stateService = new StateService();
var tokenService = new TokenService(storageService);
2019-10-22 22:30:28 +02:00
var apiService = new ApiService(tokenService, platformUtilsService, (bool expired) =>
{
messagingService.Send("logout", expired);
return Task.FromResult(0);
}, customUserAgent);
2019-04-11 20:14:34 +02:00
var appIdService = new AppIdService(storageService);
var userService = new UserService(storageService, tokenService);
2019-04-18 16:40:57 +02:00
var settingsService = new SettingsService(userService, storageService);
var fileUploadService = new FileUploadService(apiService);
var cipherService = new CipherService(cryptoService, userService, settingsService, apiService, fileUploadService,
storageService, i18nService, () => searchService, clearCipherCacheKey, allClearCipherCacheKeys);
2019-04-18 16:40:57 +02:00
var folderService = new FolderService(cryptoService, userService, apiService, storageService,
i18nService, cipherService);
var collectionService = new CollectionService(cryptoService, userService, storageService, i18nService);
var sendService = new SendService(cryptoService, userService, apiService, fileUploadService, storageService,
i18nService, cryptoFunctionService);
searchService = new SearchService(cipherService, sendService);
2021-09-23 15:42:38 +02:00
var policyService = new PolicyService(storageService, userService);
var vaultTimeoutService = new VaultTimeoutService(cryptoService, userService, platformUtilsService,
[Auto Logout] Final review of feature (#932) * Initial commit of LockService name refactor (#831) * [Auto-Logout] Update Service layer logic (#835) * Initial commit of service logic update * Added default value for action * Updated ToggleTokensAsync conditional * Removed unused variables, updated action conditional * Initial commit: lockOption/lock refactor app layer (#840) * [Auto-Logout] Settings Refactor - Application Layer Part 2 (#844) * Initial commit of app layer part 2 * Updated biometrics position * Reverted resource name refactor * LockOptions refactor revert * Updated method casing :: Removed VaultTimeout prefix for timeouts * Fixed dupe string resource (#854) * Updated dependency to use VaultTimeoutService (#896) * [Auto Logout] Xamarin Forms in AutoFill flow (iOS) (#902) * fix typo in PINRequireMasterPasswordRestart (#900) * initial commit for xf usage in autofill * Fixed databinding for hint button * Updated Two Factor page launch - removed unused imports * First pass at broadcast/messenger implentation for autofill * setting theme in extension using theme manager * extension app resources * App resources from main app * fix ref to twoFactorPage * apply resources to page * load empty app for sytling in extension * move ios renderers to ios core * static ref to resources and GetResourceColor helper * fix method ref * move application.current.resources refs to helper * switch login page alerts to device action dialogs * run on main thread * showDialog with device action service * abstract action sheet to device action service * add support for yubikey * add yubikey iimages to extension * support close button action * add support to action extension * remove empty lines Co-authored-by: Jonas Kittner <54631600+theendlessriver13@users.noreply.github.com> Co-authored-by: Kyle Spearrin <kyle.spearrin@gmail.com> * [Auto Logout] Update lock option to be default value (#929) * Initial commit - make lock action default * Removed extra whitespace Co-authored-by: Jonas Kittner <54631600+theendlessriver13@users.noreply.github.com> Co-authored-by: Kyle Spearrin <kyle.spearrin@gmail.com> Co-authored-by: Kyle Spearrin <kspearrin@users.noreply.github.com>
2020-05-29 18:26:36 +02:00
storageService, folderService, cipherService, collectionService, searchService, messagingService, tokenService,
2021-09-23 15:42:38 +02:00
policyService, null, (expired) =>
[Auto Logout] Final review of feature (#932) * Initial commit of LockService name refactor (#831) * [Auto-Logout] Update Service layer logic (#835) * Initial commit of service logic update * Added default value for action * Updated ToggleTokensAsync conditional * Removed unused variables, updated action conditional * Initial commit: lockOption/lock refactor app layer (#840) * [Auto-Logout] Settings Refactor - Application Layer Part 2 (#844) * Initial commit of app layer part 2 * Updated biometrics position * Reverted resource name refactor * LockOptions refactor revert * Updated method casing :: Removed VaultTimeout prefix for timeouts * Fixed dupe string resource (#854) * Updated dependency to use VaultTimeoutService (#896) * [Auto Logout] Xamarin Forms in AutoFill flow (iOS) (#902) * fix typo in PINRequireMasterPasswordRestart (#900) * initial commit for xf usage in autofill * Fixed databinding for hint button * Updated Two Factor page launch - removed unused imports * First pass at broadcast/messenger implentation for autofill * setting theme in extension using theme manager * extension app resources * App resources from main app * fix ref to twoFactorPage * apply resources to page * load empty app for sytling in extension * move ios renderers to ios core * static ref to resources and GetResourceColor helper * fix method ref * move application.current.resources refs to helper * switch login page alerts to device action dialogs * run on main thread * showDialog with device action service * abstract action sheet to device action service * add support for yubikey * add yubikey iimages to extension * support close button action * add support to action extension * remove empty lines Co-authored-by: Jonas Kittner <54631600+theendlessriver13@users.noreply.github.com> Co-authored-by: Kyle Spearrin <kyle.spearrin@gmail.com> * [Auto Logout] Update lock option to be default value (#929) * Initial commit - make lock action default * Removed extra whitespace Co-authored-by: Jonas Kittner <54631600+theendlessriver13@users.noreply.github.com> Co-authored-by: Kyle Spearrin <kyle.spearrin@gmail.com> Co-authored-by: Kyle Spearrin <kspearrin@users.noreply.github.com>
2020-05-29 18:26:36 +02:00
{
messagingService.Send("logout", expired);
return Task.FromResult(0);
});
2019-04-18 16:40:57 +02:00
var syncService = new SyncService(userService, apiService, settingsService, folderService,
cipherService, cryptoService, collectionService, storageService, messagingService, policyService, sendService,
(bool expired) =>
2019-10-22 22:30:28 +02:00
{
messagingService.Send("logout", expired);
return Task.FromResult(0);
});
2019-04-18 16:40:57 +02:00
var passwordGenerationService = new PasswordGenerationService(cryptoService, storageService,
cryptoFunctionService, policyService);
2019-04-18 16:40:57 +02:00
var totpService = new TotpService(storageService, cryptoFunctionService);
var authService = new AuthService(cryptoService, apiService, userService, tokenService, appIdService,
[Auto Logout] Final review of feature (#932) * Initial commit of LockService name refactor (#831) * [Auto-Logout] Update Service layer logic (#835) * Initial commit of service logic update * Added default value for action * Updated ToggleTokensAsync conditional * Removed unused variables, updated action conditional * Initial commit: lockOption/lock refactor app layer (#840) * [Auto-Logout] Settings Refactor - Application Layer Part 2 (#844) * Initial commit of app layer part 2 * Updated biometrics position * Reverted resource name refactor * LockOptions refactor revert * Updated method casing :: Removed VaultTimeout prefix for timeouts * Fixed dupe string resource (#854) * Updated dependency to use VaultTimeoutService (#896) * [Auto Logout] Xamarin Forms in AutoFill flow (iOS) (#902) * fix typo in PINRequireMasterPasswordRestart (#900) * initial commit for xf usage in autofill * Fixed databinding for hint button * Updated Two Factor page launch - removed unused imports * First pass at broadcast/messenger implentation for autofill * setting theme in extension using theme manager * extension app resources * App resources from main app * fix ref to twoFactorPage * apply resources to page * load empty app for sytling in extension * move ios renderers to ios core * static ref to resources and GetResourceColor helper * fix method ref * move application.current.resources refs to helper * switch login page alerts to device action dialogs * run on main thread * showDialog with device action service * abstract action sheet to device action service * add support for yubikey * add yubikey iimages to extension * support close button action * add support to action extension * remove empty lines Co-authored-by: Jonas Kittner <54631600+theendlessriver13@users.noreply.github.com> Co-authored-by: Kyle Spearrin <kyle.spearrin@gmail.com> * [Auto Logout] Update lock option to be default value (#929) * Initial commit - make lock action default * Removed extra whitespace Co-authored-by: Jonas Kittner <54631600+theendlessriver13@users.noreply.github.com> Co-authored-by: Kyle Spearrin <kyle.spearrin@gmail.com> Co-authored-by: Kyle Spearrin <kspearrin@users.noreply.github.com>
2020-05-29 18:26:36 +02:00
i18nService, platformUtilsService, messagingService, vaultTimeoutService);
var exportService = new ExportService(folderService, cipherService, cryptoService);
2019-04-18 16:40:57 +02:00
var auditService = new AuditService(cryptoFunctionService, apiService);
2019-04-18 18:21:35 +02:00
var environmentService = new EnvironmentService(apiService, storageService);
2019-07-12 21:51:47 +02:00
var eventService = new EventService(storageService, apiService, userService, cipherService);
2019-04-11 20:14:34 +02:00
Register<IStateService>("stateService", stateService);
Register<ITokenService>("tokenService", tokenService);
2019-04-18 16:40:57 +02:00
Register<IApiService>("apiService", apiService);
2019-04-11 20:14:34 +02:00
Register<IAppIdService>("appIdService", appIdService);
Register<IUserService>("userService", userService);
2019-04-18 16:40:57 +02:00
Register<ISettingsService>("settingsService", settingsService);
Register<ICipherService>("cipherService", cipherService);
Register<IFolderService>("folderService", folderService);
Register<ICollectionService>("collectionService", collectionService);
Register<ISendService>("sendService", sendService);
Register<ISearchService>("searchService", searchService);
Register<IPolicyService>("policyService", policyService);
2019-04-18 16:40:57 +02:00
Register<ISyncService>("syncService", syncService);
[Auto Logout] Final review of feature (#932) * Initial commit of LockService name refactor (#831) * [Auto-Logout] Update Service layer logic (#835) * Initial commit of service logic update * Added default value for action * Updated ToggleTokensAsync conditional * Removed unused variables, updated action conditional * Initial commit: lockOption/lock refactor app layer (#840) * [Auto-Logout] Settings Refactor - Application Layer Part 2 (#844) * Initial commit of app layer part 2 * Updated biometrics position * Reverted resource name refactor * LockOptions refactor revert * Updated method casing :: Removed VaultTimeout prefix for timeouts * Fixed dupe string resource (#854) * Updated dependency to use VaultTimeoutService (#896) * [Auto Logout] Xamarin Forms in AutoFill flow (iOS) (#902) * fix typo in PINRequireMasterPasswordRestart (#900) * initial commit for xf usage in autofill * Fixed databinding for hint button * Updated Two Factor page launch - removed unused imports * First pass at broadcast/messenger implentation for autofill * setting theme in extension using theme manager * extension app resources * App resources from main app * fix ref to twoFactorPage * apply resources to page * load empty app for sytling in extension * move ios renderers to ios core * static ref to resources and GetResourceColor helper * fix method ref * move application.current.resources refs to helper * switch login page alerts to device action dialogs * run on main thread * showDialog with device action service * abstract action sheet to device action service * add support for yubikey * add yubikey iimages to extension * support close button action * add support to action extension * remove empty lines Co-authored-by: Jonas Kittner <54631600+theendlessriver13@users.noreply.github.com> Co-authored-by: Kyle Spearrin <kyle.spearrin@gmail.com> * [Auto Logout] Update lock option to be default value (#929) * Initial commit - make lock action default * Removed extra whitespace Co-authored-by: Jonas Kittner <54631600+theendlessriver13@users.noreply.github.com> Co-authored-by: Kyle Spearrin <kyle.spearrin@gmail.com> Co-authored-by: Kyle Spearrin <kspearrin@users.noreply.github.com>
2020-05-29 18:26:36 +02:00
Register<IVaultTimeoutService>("vaultTimeoutService", vaultTimeoutService);
2019-04-18 16:40:57 +02:00
Register<IPasswordGenerationService>("passwordGenerationService", passwordGenerationService);
Register<ITotpService>("totpService", totpService);
Register<IAuthService>("authService", authService);
Register<IExportService>("exportService", exportService);
2019-04-18 16:40:57 +02:00
Register<IAuditService>("auditService", auditService);
2019-04-18 18:21:35 +02:00
Register<IEnvironmentService>("environmentService", environmentService);
2019-07-12 21:51:47 +02:00
Register<IEventService>("eventService", eventService);
2019-04-11 20:14:34 +02:00
}
public static void Register<T>(string serviceName, T obj)
{
if (RegisteredServices.ContainsKey(serviceName))
2019-04-11 20:14:34 +02:00
{
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))
2019-04-11 20:14:34 +02:00
{
return (T)RegisteredServices[serviceName];
}
if (dontThrow)
2019-04-11 20:14:34 +02:00
{
return (T)(object)null;
}
throw new Exception($"Service {serviceName} is not registered.");
}
2019-06-28 17:47:04 +02:00
public static void Reset()
{
foreach (var service in RegisteredServices)
{
if (service.Value != null && service.Value is IDisposable disposableService)
{
disposableService.Dispose();
}
}
2019-06-28 17:47:04 +02:00
Inited = false;
RegisteredServices.Clear();
2019-07-03 22:49:47 +02:00
RegisteredServices = new Dictionary<string, object>();
2019-06-28 17:47:04 +02:00
}
2019-04-11 20:14:34 +02:00
}
}