Files
firebase-ios-sdk/FirebaseDatabase/Sources/Api/FIRDatabaseComponent.m
T

169 lines
6.2 KiB
Objective-C
Raw Normal View History

2018-11-22 06:44:42 -05:00
/*
* Copyright 2018 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2020-06-22 07:58:52 -07:00
#import "FirebaseDatabase/Sources/Api/FIRDatabaseComponent.h"
2018-11-22 06:44:42 -05:00
2020-06-22 07:58:52 -07:00
#import "FirebaseDatabase/Sources/Api/Private/FIRDatabase_Private.h"
#import "FirebaseDatabase/Sources/Core/FRepoManager.h"
#import "FirebaseDatabase/Sources/FIRDatabaseConfig_Private.h"
2018-11-22 06:44:42 -05:00
#import "FirebaseAuth/Interop/Public/FirebaseAuthInterop/FIRAuthInterop.h"
#import "FirebaseCore/Extension/FirebaseCoreInternal.h"
2018-11-22 06:44:42 -05:00
#import <FirebaseAppCheckInterop/FirebaseAppCheckInterop.h>
2023-09-15 11:35:51 -07:00
2018-11-22 06:44:42 -05:00
NS_ASSUME_NONNULL_BEGIN
2019-08-05 13:27:47 -07:00
/** A NSMutableDictionary of FirebaseApp name and FRepoInfo to FirebaseDatabase
* instance. */
2018-11-22 06:44:42 -05:00
typedef NSMutableDictionary<NSString *, FIRDatabase *> FIRDatabaseDictionary;
@interface FIRDatabaseComponent () <FIRComponentLifecycleMaintainer, FIRLibrary>
2019-08-05 13:27:47 -07:00
@property(nonatomic) FIRDatabaseDictionary *instances;
/// Internal initializer.
2018-11-22 06:44:42 -05:00
- (instancetype)initWithApp:(FIRApp *)app;
@end
@implementation FIRDatabaseComponent
#pragma mark - Initialization
- (instancetype)initWithApp:(FIRApp *)app {
2019-08-05 13:27:47 -07:00
self = [super init];
if (self) {
_app = app;
_instances = [NSMutableDictionary dictionary];
}
return self;
2018-11-22 06:44:42 -05:00
}
#pragma mark - Lifecycle
+ (void)load {
2019-08-05 13:27:47 -07:00
[FIRApp registerInternalLibrary:(Class<FIRLibrary>)self
2020-10-02 12:26:22 -07:00
withName:@"fire-db"];
2018-11-22 06:44:42 -05:00
}
#pragma mark - FIRComponentRegistrant
+ (NSArray<FIRComponent *> *)componentsToRegister {
2019-08-05 13:27:47 -07:00
FIRComponentCreationBlock creationBlock =
^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
2018-11-22 06:44:42 -05:00
*isCacheable = YES;
return [[FIRDatabaseComponent alloc] initWithApp:container.app];
2019-08-05 13:27:47 -07:00
};
FIRComponent *databaseProvider =
[FIRComponent componentWithProtocol:@protocol(FIRDatabaseProvider)
instantiationTiming:FIRInstantiationTimingLazy
creationBlock:creationBlock];
return @[ databaseProvider ];
2018-11-22 06:44:42 -05:00
}
#pragma mark - Instance management.
- (void)appWillBeDeleted:(FIRApp *)app {
2019-08-05 13:27:47 -07:00
NSString *appName = app.name;
if (appName == nil) {
return;
}
FIRDatabaseDictionary *instances = [self instances];
@synchronized(instances) {
// Clean up the deleted instance in an effort to remove any resources
// still in use. Note: Any leftover instances of this exact database
// will be invalid.
for (FIRDatabase *database in [instances allValues]) {
[FRepoManager disposeRepos:database.config];
}
[instances removeAllObjects];
2018-11-22 06:44:42 -05:00
}
}
#pragma mark - FIRDatabaseProvider Conformance
- (FIRDatabase *)databaseForApp:(FIRApp *)app URL:(NSString *)url {
2019-08-05 13:27:47 -07:00
if (app == nil) {
[NSException raise:@"InvalidFIRApp"
format:@"nil FIRApp instance passed to databaseForApp."];
2018-11-22 06:44:42 -05:00
}
2019-08-05 13:27:47 -07:00
if (url == nil) {
[NSException raise:@"MissingDatabaseURL"
format:@"Failed to get FirebaseDatabase instance: "
"Specify DatabaseURL within FIRApp or from your "
"databaseForApp:URL: call."];
}
NSURL *databaseUrl = [NSURL URLWithString:url];
if (databaseUrl == nil) {
[NSException raise:@"InvalidDatabaseURL"
format:@"The Database URL '%@' cannot be parsed. "
"Specify a valid DatabaseURL within FIRApp or from "
"your databaseForApp:URL: call.",
url];
2019-08-05 13:27:47 -07:00
} else if (![databaseUrl.path isEqualToString:@""] &&
![databaseUrl.path isEqualToString:@"/"]) {
[NSException
raise:@"InvalidDatabaseURL"
format:@"Configured Database URL '%@' is invalid. It should point "
"to the root of a Firebase Database but it includes a "
"path: %@",
databaseUrl, databaseUrl.path];
}
FIRDatabaseDictionary *instances = [self instances];
@synchronized(instances) {
FParsedUrl *parsedUrl =
[FUtilities parseUrl:databaseUrl.absoluteString];
NSString *urlIndex =
[NSString stringWithFormat:@"%@:%@", parsedUrl.repoInfo.host,
[parsedUrl.path toString]];
FIRDatabase *database = instances[urlIndex];
if (!database) {
id<FIRDatabaseConnectionContextProvider> contextProvider =
[FIRDatabaseConnectionContextProvider
contextProviderWithAuth:FIR_COMPONENT(FIRAuthInterop,
app.container)
appCheck:FIR_COMPONENT(FIRAppCheckInterop,
app.container)];
2019-08-05 13:27:47 -07:00
// If this is the default app, don't set the session persistence key
// so that we use our default ("default") instead of the FIRApp
// default ("[DEFAULT]") so that we preserve the default location
// used by the legacy Firebase SDK.
NSString *sessionIdentifier = @"default";
if (![FIRApp isDefaultAppConfigured] ||
app != [FIRApp defaultApp]) {
sessionIdentifier = app.name;
}
FIRDatabaseConfig *config = [[FIRDatabaseConfig alloc]
initWithSessionIdentifier:sessionIdentifier
2020-06-08 14:37:07 -07:00
googleAppID:app.options.googleAppID
contextProvider:contextProvider];
2019-08-05 13:27:47 -07:00
database = [[FIRDatabase alloc] initWithApp:app
repoInfo:parsedUrl.repoInfo
config:config];
instances[urlIndex] = database;
}
return database;
}
2018-11-22 06:44:42 -05:00
}
@end
NS_ASSUME_NONNULL_END