FSTFirestoreComponent.mm 6.8 KB

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