FSTFirestoreComponent.mm 4.8 KB

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