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 RegisteredServices { get; set; } = new Dictionary(); public static bool Inited { get; set; } public static void Init() { if(Inited) { return; } Inited = true; var platformUtilsService = Resolve("platformUtilsService"); var storageService = Resolve("storageService"); var secureStorageService = Resolve("secureStorageService"); var cryptoPrimitiveService = Resolve("cryptoPrimitiveService"); var i18nService = Resolve("i18nService"); var messagingService = Resolve("messagingService"); SearchService searchService = null; 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); var settingsService = new SettingsService(userService, storageService); var cipherService = new CipherService(cryptoService, userService, settingsService, apiService, storageService, i18nService, () => searchService); var folderService = new FolderService(cryptoService, userService, apiService, storageService, i18nService, cipherService); var collectionService = new CollectionService(cryptoService, userService, storageService, i18nService); searchService = new SearchService(cipherService); // TODO: lock service var syncService = new SyncService(userService, apiService, settingsService, folderService, cipherService, cryptoService, collectionService, storageService, messagingService); var passwordGenerationService = new PasswordGenerationService(cryptoService, storageService, cryptoFunctionService); var totpService = new TotpService(storageService, cryptoFunctionService); var authService = new AuthService(cryptoService, apiService, userService, tokenService, appIdService, i18nService, platformUtilsService, messagingService); // TODO: export service var auditService = new AuditService(cryptoFunctionService, apiService); var environmentService = new EnvironmentService(apiService, storageService); // TODO: notification service Register("stateService", stateService); Register("cryptoFunctionService", cryptoFunctionService); Register("cryptoService", cryptoService); Register("tokenService", tokenService); Register("apiService", apiService); Register("appIdService", appIdService); Register("userService", userService); Register("settingsService", settingsService); Register("cipherService", cipherService); Register("folderService", folderService); Register("collectionService", collectionService); Register("searchService", searchService); Register("syncService", syncService); Register("passwordGenerationService", passwordGenerationService); Register("totpService", totpService); Register("authService", authService); Register("auditService", auditService); Register("environmentService", environmentService); } public static void Register(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(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."); } } }