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