FSTFirestoreComponent.mm 5.9 KB

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