|
| 1 | +// Copyright 2021 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#import <os/lock.h> |
| 16 | + |
| 17 | +#import "Functions/FirebaseFunctions/FIRFunctionsComponent.h" |
| 18 | + |
| 19 | +#import "Functions/FirebaseFunctions/FIRFunctions_Private.h" |
| 20 | + |
| 21 | +#import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h" |
| 22 | + |
| 23 | +#import "FirebaseAppCheck/Sources/Interop/FIRAppCheckInterop.h" |
| 24 | +#import "FirebaseMessaging/Sources/Interop/FIRMessagingInterop.h" |
| 25 | +#import "Interop/Auth/Public/FIRAuthInterop.h" |
| 26 | + |
| 27 | +@interface FIRFunctionsComponent () <FIRLibrary, FIRFunctionsProvider> |
| 28 | + |
| 29 | +/// A map of active instances, grouped by app. Keys are FIRApp names and values are arrays |
| 30 | +/// containing all instances of FIRFunctions associated with the given app. |
| 31 | +@property(nonatomic) NSMutableDictionary<NSString *, NSMutableArray<FIRFunctions *> *> *instances; |
| 32 | + |
| 33 | +/// Internal intializer. |
| 34 | +- (instancetype)initWithApp:(FIRApp *)app; |
| 35 | + |
| 36 | +@end |
| 37 | + |
| 38 | +@implementation FIRFunctionsComponent { |
| 39 | + os_unfair_lock _instancesLock; |
| 40 | +} |
| 41 | + |
| 42 | +#pragma mark - Initialization |
| 43 | + |
| 44 | +- (instancetype)initWithApp:(FIRApp *)app { |
| 45 | + self = [super init]; |
| 46 | + if (self) { |
| 47 | + _app = app; |
| 48 | + _instances = [NSMutableDictionary dictionary]; |
| 49 | + _instancesLock = OS_UNFAIR_LOCK_INIT; |
| 50 | + } |
| 51 | + return self; |
| 52 | +} |
| 53 | + |
| 54 | +#pragma mark - Lifecycle |
| 55 | + |
| 56 | ++ (void)load { |
| 57 | + [FIRApp registerInternalLibrary:(Class<FIRLibrary>)self withName:@"fire-fun"]; |
| 58 | +} |
| 59 | + |
| 60 | +#pragma mark - FIRComponentRegistrant |
| 61 | + |
| 62 | ++ (NSArray<FIRComponent *> *)componentsToRegister { |
| 63 | + FIRComponentCreationBlock creationBlock = |
| 64 | + ^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) { |
| 65 | + *isCacheable = YES; |
| 66 | + return [[self alloc] initWithApp:container.app]; |
| 67 | + }; |
| 68 | + FIRDependency *auth = [FIRDependency dependencyWithProtocol:@protocol(FIRAuthInterop) |
| 69 | + isRequired:NO]; |
| 70 | + FIRComponent *internalProvider = |
| 71 | + [FIRComponent componentWithProtocol:@protocol(FIRFunctionsProvider) |
| 72 | + instantiationTiming:FIRInstantiationTimingLazy |
| 73 | + dependencies:@[ auth ] |
| 74 | + creationBlock:creationBlock]; |
| 75 | + return @[ internalProvider ]; |
| 76 | +} |
| 77 | + |
| 78 | +#pragma mark - Instance management |
| 79 | + |
| 80 | +- (void)appWillBeDeleted:(FIRApp *)app { |
| 81 | + NSString *appName = app.name; |
| 82 | + if (appName == nil) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + os_unfair_lock_lock(&_instancesLock); |
| 87 | + [self.instances removeObjectForKey:appName]; |
| 88 | + os_unfair_lock_unlock(&_instancesLock); |
| 89 | +} |
| 90 | + |
| 91 | +#pragma mark - FIRFunctionsProvider Conformance |
| 92 | + |
| 93 | +- (FIRFunctions *)functionsForApp:(FIRApp *)app |
| 94 | + region:(NSString *)region |
| 95 | + customDomain:(NSString *_Nullable)customDomain { |
| 96 | + os_unfair_lock_lock(&_instancesLock); |
| 97 | + NSArray<FIRFunctions *> *associatedInstances = [self instances][app.name]; |
| 98 | + if (associatedInstances.count > 0) { |
| 99 | + for (FIRFunctions *instance in associatedInstances) { |
| 100 | + // Domains may be nil, so handle with care |
| 101 | + BOOL equalDomains = NO; |
| 102 | + if (instance.customDomain != nil) { |
| 103 | + equalDomains = [customDomain isEqualToString:instance.customDomain]; |
| 104 | + } else { |
| 105 | + equalDomains = customDomain == nil; |
| 106 | + } |
| 107 | + if ([instance.region isEqualToString:region] && equalDomains) { |
| 108 | + os_unfair_lock_unlock(&_instancesLock); |
| 109 | + return instance; |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + FIRFunctions *newInstance = [[FIRFunctions alloc] initWithApp:app |
| 115 | + region:region |
| 116 | + customDomain:customDomain]; |
| 117 | + |
| 118 | + if (self.instances[app.name] == nil) { |
| 119 | + NSMutableArray *array = [NSMutableArray arrayWithObject:newInstance]; |
| 120 | + self.instances[app.name] = array; |
| 121 | + } else { |
| 122 | + [self.instances[app.name] addObject:newInstance]; |
| 123 | + } |
| 124 | + os_unfair_lock_unlock(&_instancesLock); |
| 125 | + return newInstance; |
| 126 | +} |
| 127 | + |
| 128 | +@end |
0 commit comments