FSTFirestoreComponent.mm 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 "Firestore/Source/API/FSTFirestoreComponent.h"
  17. #include <memory>
  18. #include <string>
  19. #include <utility>
  20. #import "FirebaseAppCheck/Sources/Interop/FIRAppCheckInterop.h"
  21. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  22. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  23. #import "Interop/Auth/Public/FIRAuthInterop.h"
  24. #include "Firestore/core/include/firebase/firestore/firestore_version.h"
  25. #include "Firestore/core/src/api/firestore.h"
  26. #include "Firestore/core/src/credentials/credentials_provider.h"
  27. #include "Firestore/core/src/credentials/firebase_app_check_credentials_provider_apple.h"
  28. #include "Firestore/core/src/credentials/firebase_auth_credentials_provider_apple.h"
  29. #include "Firestore/core/src/remote/firebase_metadata_provider.h"
  30. #include "Firestore/core/src/remote/firebase_metadata_provider_apple.h"
  31. #include "Firestore/core/src/util/async_queue.h"
  32. #include "Firestore/core/src/util/exception.h"
  33. #include "Firestore/core/src/util/executor.h"
  34. #include "Firestore/core/src/util/hard_assert.h"
  35. #include "absl/memory/memory.h"
  36. using firebase::firestore::credentials::CredentialsProvider;
  37. using firebase::firestore::credentials::FirebaseAppCheckCredentialsProvider;
  38. using firebase::firestore::credentials::FirebaseAuthCredentialsProvider;
  39. using firebase::firestore::remote::FirebaseMetadataProviderApple;
  40. using firebase::firestore::util::AsyncQueue;
  41. using firebase::firestore::util::Executor;
  42. using firebase::firestore::util::MakeString;
  43. using firebase::firestore::util::ThrowInvalidArgument;
  44. NS_ASSUME_NONNULL_BEGIN
  45. @interface FSTFirestoreComponent () <FIRComponentLifecycleMaintainer, FIRLibrary>
  46. @end
  47. @implementation FSTFirestoreComponent
  48. // Explicitly @synthesize because instances is part of the FSTInstanceProvider protocol.
  49. @synthesize instances = _instances;
  50. #pragma mark - Initialization
  51. - (instancetype)initWithApp:(FIRApp *)app {
  52. self = [super init];
  53. if (self) {
  54. _instances = [[NSMutableDictionary alloc] init];
  55. HARD_ASSERT(app, "Cannot initialize Firestore with a nil FIRApp.");
  56. _app = app;
  57. }
  58. return self;
  59. }
  60. - (NSString *)keyForDatabase:(NSString *)database {
  61. return [NSString stringWithFormat:@"%@|%@", self.app.name, database];
  62. }
  63. #pragma mark - FSTInstanceProvider Conformance
  64. - (FIRFirestore *)firestoreForDatabase:(NSString *)database {
  65. if (!database) {
  66. ThrowInvalidArgument("Database identifier may not be nil.");
  67. }
  68. NSString *projectID = self.app.options.projectID;
  69. if (!projectID) {
  70. ThrowInvalidArgument("FIROptions.projectID must be set to a valid project ID.");
  71. }
  72. NSString *key = [self keyForDatabase:database];
  73. // Get the component from the container.
  74. @synchronized(self.instances) {
  75. FIRFirestore *firestore = _instances[key];
  76. if (!firestore) {
  77. std::string queue_name{"com.google.firebase.firestore"};
  78. if (!self.app.isDefaultApp) {
  79. absl::StrAppend(&queue_name, ".", MakeString(self.app.name));
  80. }
  81. auto executor = Executor::CreateSerial(queue_name.c_str());
  82. auto workerQueue = AsyncQueue::Create(std::move(executor));
  83. id<FIRAuthInterop> auth = FIR_COMPONENT(FIRAuthInterop, self.app.container);
  84. id<FIRAppCheckInterop> app_check = FIR_COMPONENT(FIRAppCheckInterop, self.app.container);
  85. auto authCredentialsProvider =
  86. std::make_shared<FirebaseAuthCredentialsProvider>(self.app, auth);
  87. auto appCheckCredentialsProvider =
  88. std::make_shared<FirebaseAppCheckCredentialsProvider>(self.app, app_check);
  89. auto firebaseMetadataProvider = absl::make_unique<FirebaseMetadataProviderApple>(self.app);
  90. model::DatabaseId databaseID{MakeString(projectID), MakeString(database)};
  91. std::string persistenceKey = MakeString(self.app.name);
  92. firestore = [[FIRFirestore alloc] initWithDatabaseID:std::move(databaseID)
  93. persistenceKey:std::move(persistenceKey)
  94. authCredentialsProvider:std::move(authCredentialsProvider)
  95. appCheckCredentialsProvider:std::move(appCheckCredentialsProvider)
  96. workerQueue:std::move(workerQueue)
  97. firebaseMetadataProvider:std::move(firebaseMetadataProvider)
  98. firebaseApp:self.app
  99. instanceRegistry:self];
  100. _instances[key] = firestore;
  101. }
  102. return firestore;
  103. }
  104. }
  105. - (void)removeInstanceWithDatabase:(NSString *)database {
  106. @synchronized(_instances) {
  107. NSString *key = [self keyForDatabase:database];
  108. [_instances removeObjectForKey:key];
  109. }
  110. }
  111. #pragma mark - FIRComponentLifecycleMaintainer
  112. - (void)appWillBeDeleted:(__unused FIRApp *)app {
  113. NSDictionary<NSString *, FIRFirestore *> *instances;
  114. @synchronized(_instances) {
  115. instances = [_instances copy];
  116. [_instances removeAllObjects];
  117. }
  118. for (NSString *key in instances) {
  119. [instances[key] terminateInternalWithCompletion:nil];
  120. }
  121. }
  122. #pragma mark - Object Lifecycle
  123. + (void)load {
  124. [FIRApp registerInternalLibrary:(Class<FIRLibrary>)self withName:@"fire-fst"];
  125. }
  126. #pragma mark - Interoperability
  127. + (NSArray<FIRComponent *> *)componentsToRegister {
  128. FIRDependency *auth = [FIRDependency dependencyWithProtocol:@protocol(FIRAuthInterop)
  129. isRequired:NO];
  130. FIRComponent *firestoreProvider = [FIRComponent
  131. componentWithProtocol:@protocol(FSTFirestoreMultiDBProvider)
  132. instantiationTiming:FIRInstantiationTimingLazy
  133. dependencies:@[ auth ]
  134. creationBlock:^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
  135. FSTFirestoreComponent *multiDBComponent =
  136. [[FSTFirestoreComponent alloc] initWithApp:container.app];
  137. *isCacheable = YES;
  138. return multiDBComponent;
  139. }];
  140. return @[ firestoreProvider ];
  141. }
  142. @end
  143. NS_ASSUME_NONNULL_END