FSTFirestoreComponent.mm 5.9 KB

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