FSTFirestoreComponent.mm 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #import "Firestore/Source/Util/FSTUsageValidation.h"
  29. #include "Firestore/core/include/firebase/firestore/firestore_version.h"
  30. #include "Firestore/core/src/firebase/firestore/auth/credentials_provider.h"
  31. #include "Firestore/core/src/firebase/firestore/auth/firebase_credentials_provider_apple.h"
  32. #include "Firestore/core/src/firebase/firestore/util/async_queue.h"
  33. #include "Firestore/core/src/firebase/firestore/util/executor_libdispatch.h"
  34. #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
  35. #include "absl/memory/memory.h"
  36. namespace util = firebase::firestore::util;
  37. using firebase::firestore::auth::CredentialsProvider;
  38. using firebase::firestore::auth::FirebaseCredentialsProvider;
  39. using util::AsyncQueue;
  40. using util::ExecutorLibdispatch;
  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. #pragma mark - FSTInstanceProvider Conformance
  58. - (FIRFirestore *)firestoreForDatabase:(NSString *)database {
  59. if (!database) {
  60. FSTThrowInvalidArgument(@"database identifier may not be nil.");
  61. }
  62. NSString *key = [NSString stringWithFormat:@"%@|%@", self.app.name, database];
  63. // Get the component from the container.
  64. @synchronized(self.instances) {
  65. FIRFirestore *firestore = _instances[key];
  66. if (!firestore) {
  67. std::string queue_name{"com.google.firebase.firestore"};
  68. if (!self.app.isDefaultApp) {
  69. absl::StrAppend(&queue_name, ".", util::MakeString(self.app.name));
  70. }
  71. auto executor = absl::make_unique<ExecutorLibdispatch>(
  72. dispatch_queue_create(queue_name.c_str(), DISPATCH_QUEUE_SERIAL));
  73. auto workerQueue = absl::make_unique<AsyncQueue>(std::move(executor));
  74. id<FIRAuthInterop> auth = FIR_COMPONENT(FIRAuthInterop, self.app.container);
  75. std::unique_ptr<CredentialsProvider> credentials_provider =
  76. absl::make_unique<FirebaseCredentialsProvider>(self.app, auth);
  77. NSString *persistenceKey = self.app.name;
  78. NSString *projectID = self.app.options.projectID;
  79. firestore = [[FIRFirestore alloc] initWithProjectID:util::MakeString(projectID)
  80. database:util::MakeString(database)
  81. persistenceKey:persistenceKey
  82. credentialsProvider:std::move(credentials_provider)
  83. workerQueue:std::move(workerQueue)
  84. firebaseApp:self.app];
  85. _instances[key] = firestore;
  86. }
  87. return firestore;
  88. }
  89. }
  90. #pragma mark - FIRComponentLifecycleMaintainer
  91. - (void)appWillBeDeleted:(FIRApp *)app {
  92. // Stop any actions and clean up resources since instances of Firestore associated with this app
  93. // will be removed. Currently does not do anything.
  94. }
  95. #pragma mark - Object Lifecycle
  96. + (void)load {
  97. [FIRApp registerInternalLibrary:(Class<FIRLibrary>)self
  98. withName:@"fire-fst"
  99. withVersion:[NSString stringWithUTF8String:firebase::firestore::
  100. kFirestoreVersionString]];
  101. }
  102. #pragma mark - Interoperability
  103. + (NSArray<FIRComponent *> *)componentsToRegister {
  104. FIRDependency *auth = [FIRDependency dependencyWithProtocol:@protocol(FIRAuthInterop)
  105. isRequired:NO];
  106. FIRComponent *firestoreProvider = [FIRComponent
  107. componentWithProtocol:@protocol(FSTFirestoreMultiDBProvider)
  108. instantiationTiming:FIRInstantiationTimingLazy
  109. dependencies:@[ auth ]
  110. creationBlock:^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
  111. FSTFirestoreComponent *multiDBComponent =
  112. [[FSTFirestoreComponent alloc] initWithApp:container.app];
  113. *isCacheable = YES;
  114. return multiDBComponent;
  115. }];
  116. return @[ firestoreProvider ];
  117. }
  118. @end
  119. NS_ASSUME_NONNULL_END