FSTFirestoreComponent.mm 5.1 KB

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