FSTFirestoreComponent.mm 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/auth/credentials_provider.h"
  26. #include "Firestore/core/src/auth/firebase_credentials_provider_apple.h"
  27. #include "Firestore/core/src/model/maybe_document.h"
  28. #include "Firestore/core/src/remote/firebase_metadata_provider.h"
  29. #include "Firestore/core/src/remote/firebase_metadata_provider_apple.h"
  30. #include "Firestore/core/src/util/async_queue.h"
  31. #include "Firestore/core/src/util/exception.h"
  32. #include "Firestore/core/src/util/executor.h"
  33. #include "Firestore/core/src/util/hard_assert.h"
  34. #include "absl/memory/memory.h"
  35. namespace util = firebase::firestore::util;
  36. using firebase::firestore::api::Firestore;
  37. using firebase::firestore::auth::CredentialsProvider;
  38. using firebase::firestore::auth::FirebaseCredentialsProvider;
  39. using firebase::firestore::remote::FirebaseMetadataProvider;
  40. using firebase::firestore::remote::FirebaseMetadataProviderApple;
  41. using firebase::firestore::util::AsyncQueue;
  42. using firebase::firestore::util::Executor;
  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, ".", util::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. auto credentialsProvider = std::make_shared<FirebaseCredentialsProvider>(self.app, auth);
  85. auto firebaseMetadataProvider = absl::make_unique<FirebaseMetadataProviderApple>(self.app);
  86. model::DatabaseId databaseID{util::MakeString(projectID), util::MakeString(database)};
  87. std::string persistenceKey = util::MakeString(self.app.name);
  88. firestore = [[FIRFirestore alloc] initWithDatabaseID:std::move(databaseID)
  89. persistenceKey:std::move(persistenceKey)
  90. credentialsProvider:std::move(credentialsProvider)
  91. workerQueue:std::move(workerQueue)
  92. firebaseMetadataProvider:std::move(firebaseMetadataProvider)
  93. firebaseApp:self.app
  94. instanceRegistry:self];
  95. _instances[key] = firestore;
  96. }
  97. return firestore;
  98. }
  99. }
  100. - (void)removeInstanceWithDatabase:(NSString *)database {
  101. @synchronized(_instances) {
  102. NSString *key = [self keyForDatabase:database];
  103. [_instances removeObjectForKey:key];
  104. }
  105. }
  106. #pragma mark - FIRComponentLifecycleMaintainer
  107. - (void)appWillBeDeleted:(__unused FIRApp *)app {
  108. NSDictionary<NSString *, FIRFirestore *> *instances;
  109. @synchronized(_instances) {
  110. instances = [_instances copy];
  111. [_instances removeAllObjects];
  112. }
  113. for (NSString *key in instances) {
  114. [instances[key] terminateInternalWithCompletion:nil];
  115. }
  116. }
  117. #pragma mark - Object Lifecycle
  118. + (void)load {
  119. [FIRApp registerInternalLibrary:(Class<FIRLibrary>)self withName:@"fire-fst"];
  120. }
  121. #pragma mark - Interoperability
  122. + (NSArray<FIRComponent *> *)componentsToRegister {
  123. FIRDependency *auth = [FIRDependency dependencyWithProtocol:@protocol(FIRAuthInterop)
  124. isRequired:NO];
  125. FIRComponent *firestoreProvider = [FIRComponent
  126. componentWithProtocol:@protocol(FSTFirestoreMultiDBProvider)
  127. instantiationTiming:FIRInstantiationTimingLazy
  128. dependencies:@[ auth ]
  129. creationBlock:^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
  130. FSTFirestoreComponent *multiDBComponent =
  131. [[FSTFirestoreComponent alloc] initWithApp:container.app];
  132. *isCacheable = YES;
  133. return multiDBComponent;
  134. }];
  135. return @[ firestoreProvider ];
  136. }
  137. @end
  138. NS_ASSUME_NONNULL_END