FIRFunctionsComponent.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #import <os/lock.h>
  15. #import "Functions/FirebaseFunctions/FIRFunctionsComponent.h"
  16. #import "Functions/FirebaseFunctions/FIRFunctions_Private.h"
  17. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  18. #import "FirebaseAppCheck/Sources/Interop/FIRAppCheckInterop.h"
  19. #import "FirebaseMessaging/Sources/Interop/FIRMessagingInterop.h"
  20. #import "Interop/Auth/Public/FIRAuthInterop.h"
  21. @interface FIRFunctionsComponent () <FIRLibrary, FIRFunctionsProvider>
  22. /// A map of active instances, grouped by app. Keys are FIRApp names and values are arrays
  23. /// containing all instances of FIRFunctions associated with the given app.
  24. @property(nonatomic) NSMutableDictionary<NSString *, NSMutableArray<FIRFunctions *> *> *instances;
  25. /// Internal intializer.
  26. - (instancetype)initWithApp:(FIRApp *)app;
  27. @end
  28. @implementation FIRFunctionsComponent {
  29. os_unfair_lock _instancesLock;
  30. }
  31. #pragma mark - Initialization
  32. - (instancetype)initWithApp:(FIRApp *)app {
  33. self = [super init];
  34. if (self) {
  35. _app = app;
  36. _instances = [NSMutableDictionary dictionary];
  37. _instancesLock = OS_UNFAIR_LOCK_INIT;
  38. }
  39. return self;
  40. }
  41. #pragma mark - Lifecycle
  42. + (void)load {
  43. [FIRApp registerInternalLibrary:(Class<FIRLibrary>)self withName:@"fire-fun"];
  44. }
  45. #pragma mark - FIRComponentRegistrant
  46. + (NSArray<FIRComponent *> *)componentsToRegister {
  47. FIRComponentCreationBlock creationBlock =
  48. ^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
  49. *isCacheable = YES;
  50. return [[self alloc] initWithApp:container.app];
  51. };
  52. FIRDependency *auth = [FIRDependency dependencyWithProtocol:@protocol(FIRAuthInterop)
  53. isRequired:NO];
  54. FIRComponent *internalProvider =
  55. [FIRComponent componentWithProtocol:@protocol(FIRFunctionsProvider)
  56. instantiationTiming:FIRInstantiationTimingLazy
  57. dependencies:@[ auth ]
  58. creationBlock:creationBlock];
  59. return @[ internalProvider ];
  60. }
  61. #pragma mark - Instance management
  62. - (void)appWillBeDeleted:(FIRApp *)app {
  63. NSString *appName = app.name;
  64. if (appName == nil) {
  65. return;
  66. }
  67. os_unfair_lock_lock(&_instancesLock);
  68. [self.instances removeObjectForKey:appName];
  69. os_unfair_lock_unlock(&_instancesLock);
  70. }
  71. #pragma mark - FIRFunctionsProvider Conformance
  72. - (FIRFunctions *)functionsForApp:(FIRApp *)app
  73. region:(NSString *)region
  74. customDomain:(NSString *_Nullable)customDomain
  75. type:(Class)cls {
  76. os_unfair_lock_lock(&_instancesLock);
  77. NSArray<FIRFunctions *> *associatedInstances = [self instances][app.name];
  78. if (associatedInstances.count > 0) {
  79. for (FIRFunctions *instance in associatedInstances) {
  80. // Domains may be nil, so handle with care
  81. BOOL equalDomains = NO;
  82. if (instance.customDomain != nil) {
  83. equalDomains = [customDomain isEqualToString:instance.customDomain];
  84. } else {
  85. equalDomains = customDomain == nil;
  86. }
  87. if ([instance.region isEqualToString:region] && equalDomains) {
  88. os_unfair_lock_unlock(&_instancesLock);
  89. return instance;
  90. }
  91. }
  92. }
  93. FIRFunctions *newInstance = [[cls alloc] initWithApp:app region:region customDomain:customDomain];
  94. if (self.instances[app.name] == nil) {
  95. NSMutableArray *array = [NSMutableArray arrayWithObject:newInstance];
  96. self.instances[app.name] = array;
  97. } else {
  98. [self.instances[app.name] addObject:newInstance];
  99. }
  100. os_unfair_lock_unlock(&_instancesLock);
  101. return newInstance;
  102. }
  103. @end