2018-07-25 13:19:53 -04:00
|
|
|
/*
|
|
|
|
|
* Copyright 2018 Google
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#import "Firestore/Source/API/FSTFirestoreComponent.h"
|
|
|
|
|
|
2023-10-11 18:39:18 -04:00
|
|
|
#import <FirebaseAppCheckInterop/FirebaseAppCheckInterop.h>
|
|
|
|
|
|
2018-07-25 13:19:53 -04:00
|
|
|
#include <memory>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
2024-02-20 17:24:46 -08:00
|
|
|
#import "FirebaseAuth/Interop/Public/FirebaseAuthInterop/FIRAuthInterop.h"
|
2022-03-22 13:36:33 -04:00
|
|
|
#import "FirebaseCore/Extension/FIRAppInternal.h"
|
|
|
|
|
#import "FirebaseCore/Extension/FIRComponent.h"
|
|
|
|
|
#import "FirebaseCore/Extension/FIRComponentContainer.h"
|
|
|
|
|
#import "FirebaseCore/Extension/FIRComponentType.h"
|
|
|
|
|
#import "FirebaseCore/Extension/FIRLibrary.h"
|
|
|
|
|
#import "FirebaseCore/Extension/FIROptionsInternal.h"
|
2018-07-25 13:19:53 -04:00
|
|
|
#import "Firestore/Source/API/FIRFirestore+Internal.h"
|
2019-10-18 12:58:45 -07:00
|
|
|
|
2018-12-13 08:36:08 -08:00
|
|
|
#include "Firestore/core/include/firebase/firestore/firestore_version.h"
|
2020-04-20 14:57:34 -07:00
|
|
|
#include "Firestore/core/src/api/firestore.h"
|
2021-10-07 14:16:58 -06:00
|
|
|
#include "Firestore/core/src/credentials/credentials_provider.h"
|
2021-10-11 12:00:23 -06:00
|
|
|
#include "Firestore/core/src/credentials/firebase_app_check_credentials_provider_apple.h"
|
2021-10-08 08:58:08 -06:00
|
|
|
#include "Firestore/core/src/credentials/firebase_auth_credentials_provider_apple.h"
|
2020-09-25 15:47:20 -04:00
|
|
|
#include "Firestore/core/src/remote/firebase_metadata_provider.h"
|
|
|
|
|
#include "Firestore/core/src/remote/firebase_metadata_provider_apple.h"
|
2020-04-20 14:57:34 -07:00
|
|
|
#include "Firestore/core/src/util/async_queue.h"
|
|
|
|
|
#include "Firestore/core/src/util/exception.h"
|
|
|
|
|
#include "Firestore/core/src/util/executor.h"
|
|
|
|
|
#include "Firestore/core/src/util/hard_assert.h"
|
2018-07-25 13:19:53 -04:00
|
|
|
#include "absl/memory/memory.h"
|
|
|
|
|
|
2021-10-11 12:00:23 -06:00
|
|
|
using firebase::firestore::credentials::FirebaseAppCheckCredentialsProvider;
|
2021-10-08 08:58:08 -06:00
|
|
|
using firebase::firestore::credentials::FirebaseAuthCredentialsProvider;
|
2020-09-25 15:47:20 -04:00
|
|
|
using firebase::firestore::remote::FirebaseMetadataProviderApple;
|
2019-10-18 12:58:45 -07:00
|
|
|
using firebase::firestore::util::AsyncQueue;
|
2019-11-15 09:34:44 -08:00
|
|
|
using firebase::firestore::util::Executor;
|
2021-09-24 11:11:41 -04:00
|
|
|
using firebase::firestore::util::MakeString;
|
2019-10-18 12:58:45 -07:00
|
|
|
using firebase::firestore::util::ThrowInvalidArgument;
|
2018-07-25 13:19:53 -04:00
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
2018-12-13 08:36:08 -08:00
|
|
|
@interface FSTFirestoreComponent () <FIRComponentLifecycleMaintainer, FIRLibrary>
|
2018-07-25 13:19:53 -04:00
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation FSTFirestoreComponent
|
|
|
|
|
|
|
|
|
|
// Explicitly @synthesize because instances is part of the FSTInstanceProvider protocol.
|
|
|
|
|
@synthesize instances = _instances;
|
|
|
|
|
|
|
|
|
|
#pragma mark - Initialization
|
|
|
|
|
|
|
|
|
|
- (instancetype)initWithApp:(FIRApp *)app {
|
|
|
|
|
self = [super init];
|
|
|
|
|
if (self) {
|
|
|
|
|
_instances = [[NSMutableDictionary alloc] init];
|
|
|
|
|
|
|
|
|
|
HARD_ASSERT(app, "Cannot initialize Firestore with a nil FIRApp.");
|
|
|
|
|
_app = app;
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-06 20:33:08 -04:00
|
|
|
- (NSString *)keyForDatabase:(NSString *)database {
|
|
|
|
|
return [NSString stringWithFormat:@"%@|%@", self.app.name, database];
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-25 13:19:53 -04:00
|
|
|
#pragma mark - FSTInstanceProvider Conformance
|
|
|
|
|
|
|
|
|
|
- (FIRFirestore *)firestoreForDatabase:(NSString *)database {
|
|
|
|
|
if (!database) {
|
2019-03-28 13:19:28 -07:00
|
|
|
ThrowInvalidArgument("Database identifier may not be nil.");
|
2018-07-25 13:19:53 -04:00
|
|
|
}
|
|
|
|
|
|
2019-08-16 07:43:13 -07:00
|
|
|
NSString *projectID = self.app.options.projectID;
|
|
|
|
|
if (!projectID) {
|
|
|
|
|
ThrowInvalidArgument("FIROptions.projectID must be set to a valid project ID.");
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-06 20:33:08 -04:00
|
|
|
NSString *key = [self keyForDatabase:database];
|
2018-07-25 13:19:53 -04:00
|
|
|
|
|
|
|
|
// Get the component from the container.
|
|
|
|
|
@synchronized(self.instances) {
|
|
|
|
|
FIRFirestore *firestore = _instances[key];
|
|
|
|
|
if (!firestore) {
|
|
|
|
|
std::string queue_name{"com.google.firebase.firestore"};
|
|
|
|
|
if (!self.app.isDefaultApp) {
|
2021-09-24 11:11:41 -04:00
|
|
|
absl::StrAppend(&queue_name, ".", MakeString(self.app.name));
|
2018-07-25 13:19:53 -04:00
|
|
|
}
|
2018-11-18 17:49:32 -05:00
|
|
|
|
2019-11-15 09:34:44 -08:00
|
|
|
auto executor = Executor::CreateSerial(queue_name.c_str());
|
2020-02-04 13:38:59 -08:00
|
|
|
auto workerQueue = AsyncQueue::Create(std::move(executor));
|
2018-07-25 13:19:53 -04:00
|
|
|
|
|
|
|
|
id<FIRAuthInterop> auth = FIR_COMPONENT(FIRAuthInterop, self.app.container);
|
2021-10-11 12:00:23 -06:00
|
|
|
id<FIRAppCheckInterop> app_check = FIR_COMPONENT(FIRAppCheckInterop, self.app.container);
|
|
|
|
|
auto authCredentialsProvider =
|
|
|
|
|
std::make_shared<FirebaseAuthCredentialsProvider>(self.app, auth);
|
|
|
|
|
auto appCheckCredentialsProvider =
|
|
|
|
|
std::make_shared<FirebaseAppCheckCredentialsProvider>(self.app, app_check);
|
2018-07-25 13:19:53 -04:00
|
|
|
|
2020-09-25 15:47:20 -04:00
|
|
|
auto firebaseMetadataProvider = absl::make_unique<FirebaseMetadataProviderApple>(self.app);
|
|
|
|
|
|
2021-09-24 11:11:41 -04:00
|
|
|
model::DatabaseId databaseID{MakeString(projectID), MakeString(database)};
|
|
|
|
|
std::string persistenceKey = MakeString(self.app.name);
|
2019-05-21 20:28:51 -07:00
|
|
|
firestore = [[FIRFirestore alloc] initWithDatabaseID:std::move(databaseID)
|
|
|
|
|
persistenceKey:std::move(persistenceKey)
|
2021-10-11 12:00:23 -06:00
|
|
|
authCredentialsProvider:std::move(authCredentialsProvider)
|
|
|
|
|
appCheckCredentialsProvider:std::move(appCheckCredentialsProvider)
|
2019-05-21 20:28:51 -07:00
|
|
|
workerQueue:std::move(workerQueue)
|
2020-09-25 15:47:20 -04:00
|
|
|
firebaseMetadataProvider:std::move(firebaseMetadataProvider)
|
2019-08-06 20:33:08 -04:00
|
|
|
firebaseApp:self.app
|
|
|
|
|
instanceRegistry:self];
|
2018-07-25 13:19:53 -04:00
|
|
|
_instances[key] = firestore;
|
|
|
|
|
}
|
|
|
|
|
return firestore;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-06 20:33:08 -04:00
|
|
|
- (void)removeInstanceWithDatabase:(NSString *)database {
|
|
|
|
|
@synchronized(_instances) {
|
|
|
|
|
NSString *key = [self keyForDatabase:database];
|
|
|
|
|
[_instances removeObjectForKey:key];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-25 13:19:53 -04:00
|
|
|
#pragma mark - FIRComponentLifecycleMaintainer
|
|
|
|
|
|
2020-04-20 14:23:36 -07:00
|
|
|
- (void)appWillBeDeleted:(__unused FIRApp *)app {
|
2019-08-06 20:33:08 -04:00
|
|
|
NSDictionary<NSString *, FIRFirestore *> *instances;
|
|
|
|
|
@synchronized(_instances) {
|
|
|
|
|
instances = [_instances copy];
|
|
|
|
|
[_instances removeAllObjects];
|
|
|
|
|
}
|
|
|
|
|
for (NSString *key in instances) {
|
2019-08-27 11:51:01 -04:00
|
|
|
[instances[key] terminateInternalWithCompletion:nil];
|
2019-08-06 20:33:08 -04:00
|
|
|
}
|
2018-07-25 13:19:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark - Object Lifecycle
|
|
|
|
|
|
|
|
|
|
+ (void)load {
|
2020-10-02 12:26:22 -07:00
|
|
|
[FIRApp registerInternalLibrary:(Class<FIRLibrary>)self withName:@"fire-fst"];
|
2018-07-25 13:19:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark - Interoperability
|
|
|
|
|
|
|
|
|
|
+ (NSArray<FIRComponent *> *)componentsToRegister {
|
|
|
|
|
FIRComponent *firestoreProvider = [FIRComponent
|
|
|
|
|
componentWithProtocol:@protocol(FSTFirestoreMultiDBProvider)
|
|
|
|
|
instantiationTiming:FIRInstantiationTimingLazy
|
|
|
|
|
creationBlock:^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
|
|
|
|
|
FSTFirestoreComponent *multiDBComponent =
|
|
|
|
|
[[FSTFirestoreComponent alloc] initWithApp:container.app];
|
|
|
|
|
*isCacheable = YES;
|
|
|
|
|
return multiDBComponent;
|
|
|
|
|
}];
|
|
|
|
|
return @[ firestoreProvider ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
2023-03-10 12:48:44 -05:00
|
|
|
/// This function forces the linker to include `FSTFirestoreComponent`. See `+[FIRFirestore
|
|
|
|
|
/// notCalled]`.
|
|
|
|
|
void FSTIncludeFSTFirestoreComponent(void) {
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-25 13:19:53 -04:00
|
|
|
NS_ASSUME_NONNULL_END
|