FIRDatabaseComponent.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright 2018 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FIRDatabaseComponent.h"
  17. #import "FIRDatabaseConfig_Private.h"
  18. #import "FIRDatabase_Private.h"
  19. #import "FRepoManager.h"
  20. #import <FirebaseAuthInterop/FIRAuthInterop.h>
  21. #import <FirebaseCore/FIRAppInternal.h>
  22. #import <FirebaseCore/FIRComponent.h>
  23. #import <FirebaseCore/FIRComponentContainer.h>
  24. #import <FirebaseCore/FIRDependency.h>
  25. #import <FirebaseCore/FIRLibrary.h>
  26. #import <FirebaseCore/FIROptions.h>
  27. NS_ASSUME_NONNULL_BEGIN
  28. /** A NSMutableDictionary of FirebaseApp name and FRepoInfo to FirebaseDatabase
  29. * instance. */
  30. typedef NSMutableDictionary<NSString *, FIRDatabase *> FIRDatabaseDictionary;
  31. @interface FIRDatabaseComponent () <FIRComponentLifecycleMaintainer, FIRLibrary>
  32. @property(nonatomic) FIRDatabaseDictionary *instances;
  33. /// Internal intializer.
  34. - (instancetype)initWithApp:(FIRApp *)app;
  35. @end
  36. @implementation FIRDatabaseComponent
  37. #pragma mark - Initialization
  38. - (instancetype)initWithApp:(FIRApp *)app {
  39. self = [super init];
  40. if (self) {
  41. _app = app;
  42. _instances = [NSMutableDictionary dictionary];
  43. }
  44. return self;
  45. }
  46. #pragma mark - Lifecycle
  47. + (void)load {
  48. [FIRApp registerInternalLibrary:(Class<FIRLibrary>)self
  49. withName:@"fire-db"
  50. withVersion:[FIRDatabase sdkVersion]];
  51. }
  52. #pragma mark - FIRComponentRegistrant
  53. + (NSArray<FIRComponent *> *)componentsToRegister {
  54. FIRDependency *authDep =
  55. [FIRDependency dependencyWithProtocol:@protocol(FIRAuthInterop)
  56. isRequired:NO];
  57. FIRComponentCreationBlock creationBlock =
  58. ^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
  59. *isCacheable = YES;
  60. return [[FIRDatabaseComponent alloc] initWithApp:container.app];
  61. };
  62. FIRComponent *databaseProvider =
  63. [FIRComponent componentWithProtocol:@protocol(FIRDatabaseProvider)
  64. instantiationTiming:FIRInstantiationTimingLazy
  65. dependencies:@[ authDep ]
  66. creationBlock:creationBlock];
  67. return @[ databaseProvider ];
  68. }
  69. #pragma mark - Instance management.
  70. - (void)appWillBeDeleted:(FIRApp *)app {
  71. NSString *appName = app.name;
  72. if (appName == nil) {
  73. return;
  74. }
  75. FIRDatabaseDictionary *instances = [self instances];
  76. @synchronized(instances) {
  77. // Clean up the deleted instance in an effort to remove any resources
  78. // still in use. Note: Any leftover instances of this exact database
  79. // will be invalid.
  80. for (FIRDatabase *database in [instances allValues]) {
  81. [FRepoManager disposeRepos:database.config];
  82. }
  83. [instances removeAllObjects];
  84. }
  85. }
  86. #pragma mark - FIRDatabaseProvider Conformance
  87. - (FIRDatabase *)databaseForApp:(FIRApp *)app URL:(NSString *)url {
  88. if (app == nil) {
  89. [NSException raise:@"InvalidFIRApp"
  90. format:@"nil FIRApp instance passed to databaseForApp."];
  91. }
  92. if (url == nil) {
  93. [NSException raise:@"MissingDatabaseURL"
  94. format:@"Failed to get FirebaseDatabase instance: "
  95. "Specify DatabaseURL within FIRApp or from your "
  96. "databaseForApp:URL: call."];
  97. }
  98. NSURL *databaseUrl = [NSURL URLWithString:url];
  99. if (databaseUrl == nil) {
  100. [NSException raise:@"InvalidDatabaseURL"
  101. format:@"The Database URL '%@' cannot be parsed. "
  102. "Specify a valid DatabaseURL within FIRApp or from "
  103. "your databaseForApp:URL: call.",
  104. databaseUrl];
  105. } else if (![databaseUrl.path isEqualToString:@""] &&
  106. ![databaseUrl.path isEqualToString:@"/"]) {
  107. [NSException
  108. raise:@"InvalidDatabaseURL"
  109. format:@"Configured Database URL '%@' is invalid. It should point "
  110. "to the root of a Firebase Database but it includes a "
  111. "path: %@",
  112. databaseUrl, databaseUrl.path];
  113. }
  114. FIRDatabaseDictionary *instances = [self instances];
  115. @synchronized(instances) {
  116. FParsedUrl *parsedUrl =
  117. [FUtilities parseUrl:databaseUrl.absoluteString];
  118. NSString *urlIndex =
  119. [NSString stringWithFormat:@"%@:%@", parsedUrl.repoInfo.host,
  120. [parsedUrl.path toString]];
  121. FIRDatabase *database = instances[urlIndex];
  122. if (!database) {
  123. id<FAuthTokenProvider> authTokenProvider = [FAuthTokenProvider
  124. authTokenProviderWithAuth:FIR_COMPONENT(FIRAuthInterop,
  125. app.container)];
  126. // If this is the default app, don't set the session persistence key
  127. // so that we use our default ("default") instead of the FIRApp
  128. // default ("[DEFAULT]") so that we preserve the default location
  129. // used by the legacy Firebase SDK.
  130. NSString *sessionIdentifier = @"default";
  131. if (![FIRApp isDefaultAppConfigured] ||
  132. app != [FIRApp defaultApp]) {
  133. sessionIdentifier = app.name;
  134. }
  135. FIRDatabaseConfig *config = [[FIRDatabaseConfig alloc]
  136. initWithSessionIdentifier:sessionIdentifier
  137. authTokenProvider:authTokenProvider];
  138. database = [[FIRDatabase alloc] initWithApp:app
  139. repoInfo:parsedUrl.repoInfo
  140. config:config];
  141. instances[urlIndex] = database;
  142. }
  143. return database;
  144. }
  145. }
  146. @end
  147. NS_ASSUME_NONNULL_END