FSTFirestoreComponent.mm 6.6 KB

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