FSTFirestoreComponent.mm 5.5 KB

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