FIRComponentContainer.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "Private/FIRComponentContainer.h"
  17. #import "Private/FIRAppInternal.h"
  18. #import "Private/FIRComponent.h"
  19. #import "Private/FIRComponentRegistrant.h"
  20. #import "Private/FIRLogger.h"
  21. NS_ASSUME_NONNULL_BEGIN
  22. @interface FIRComponentContainer ()
  23. /// The dictionary of components that are registered for a particular app. The key is an NSString
  24. /// of the protocol.
  25. @property(nonatomic, strong) NSMutableDictionary<NSString *, FIRComponentCreationBlock> *components;
  26. /// Cached instances of components that requested to be cached.
  27. @property(nonatomic, strong) NSMutableDictionary<NSString *, id> *cachedInstances;
  28. @end
  29. @implementation FIRComponentContainer
  30. // Collection of all classes that register to provide components.
  31. static NSMutableSet<Class> *gFIRComponentRegistrants;
  32. #pragma mark - Public Registration
  33. + (void)registerAsComponentRegistrant:(Class)klass {
  34. static dispatch_once_t onceToken;
  35. dispatch_once(&onceToken, ^{
  36. gFIRComponentRegistrants = [[NSMutableSet<Class> alloc] init];
  37. });
  38. [self registerAsComponentRegistrant:klass inSet:gFIRComponentRegistrants];
  39. }
  40. + (void)registerAsComponentRegistrant:(Class)klass inSet:(NSMutableSet<Class> *)allRegistrants {
  41. // Validate the array to store the components is initialized.
  42. if (!allRegistrants) {
  43. FIRLogWarning(kFIRLoggerCore, @"I-COR000025",
  44. @"Attempted to store registered components in an empty set.");
  45. return;
  46. }
  47. // Ensure the class given conforms to the proper protocol.
  48. if (![klass conformsToProtocol:@protocol(FIRComponentRegistrant)] ||
  49. ![klass respondsToSelector:@selector(componentsToRegister)]) {
  50. [NSException raise:NSInvalidArgumentException
  51. format:
  52. @"Class %@ attempted to register components, but it does not conform to "
  53. @"`FIRComponentRegistrant` or provide a `componentsToRegister:` method.",
  54. klass];
  55. }
  56. [allRegistrants addObject:klass];
  57. }
  58. #pragma mark - Internal Initialization
  59. - (instancetype)initWithApp:(FIRApp *)app {
  60. return [self initWithApp:app registrants:gFIRComponentRegistrants];
  61. }
  62. - (instancetype)initWithApp:(FIRApp *)app registrants:(NSMutableSet<Class> *)allRegistrants {
  63. self = [super init];
  64. if (self) {
  65. _app = app;
  66. _cachedInstances = [NSMutableDictionary<NSString *, id> dictionary];
  67. _components = [NSMutableDictionary<NSString *, FIRComponentCreationBlock> dictionary];
  68. [self populateComponentsFromRegisteredClasses:allRegistrants forApp:app];
  69. }
  70. return self;
  71. }
  72. - (void)populateComponentsFromRegisteredClasses:(NSSet<Class> *)classes forApp:(FIRApp *)app {
  73. // Loop through the verified component registrants and populate the components array.
  74. for (Class<FIRComponentRegistrant> klass in classes) {
  75. // Loop through all the components being registered and store them as appropriate.
  76. // Classes which do not provide functionality should use a dummy FIRComponentRegistrant
  77. // protocol.
  78. for (FIRComponent *component in [klass componentsToRegister]) {
  79. // Check if the component has been registered before, and error out if so.
  80. NSString *protocolName = NSStringFromProtocol(component.protocol);
  81. if (self.components[protocolName]) {
  82. FIRLogError(kFIRLoggerCore, @"I-COR000029",
  83. @"Attempted to register protocol %@, but it already has an implementation.",
  84. protocolName);
  85. continue;
  86. }
  87. // Store the creation block for later usage.
  88. self.components[protocolName] = component.creationBlock;
  89. // Instantiate the
  90. BOOL shouldInstantiateEager =
  91. (component.instantiationTiming == FIRInstantiationTimingAlwaysEager);
  92. BOOL shouldInstantiateDefaultEager =
  93. (component.instantiationTiming == FIRInstantiationTimingEagerInDefaultApp &&
  94. [app isDefaultApp]);
  95. if (shouldInstantiateEager || shouldInstantiateDefaultEager) {
  96. [self instantiateInstanceForProtocol:component.protocol withBlock:component.creationBlock];
  97. }
  98. }
  99. }
  100. }
  101. #pragma mark - Instance Creation
  102. /// Instantiate an instance of a class that conforms to the specified protocol.
  103. /// This will:
  104. /// - Call the block to create an instance if possible,
  105. /// - Validate that the instance returned conforms to the protocol it claims to,
  106. /// - Cache the instance if the block requests it
  107. - (nullable id)instantiateInstanceForProtocol:(Protocol *)protocol
  108. withBlock:(FIRComponentCreationBlock)creationBlock {
  109. if (!creationBlock) {
  110. return nil;
  111. }
  112. // Create an instance using the creation block.
  113. BOOL shouldCache = NO;
  114. id instance = creationBlock(self, &shouldCache);
  115. if (!instance) {
  116. return nil;
  117. }
  118. // An instance was created, validate that it conforms to the protocol it claims to.
  119. NSString *protocolName = NSStringFromProtocol(protocol);
  120. if (![instance conformsToProtocol:protocol]) {
  121. FIRLogError(kFIRLoggerCore, @"I-COR000030",
  122. @"An instance conforming to %@ was requested, but the instance provided does not "
  123. @"conform to the protocol",
  124. protocolName);
  125. }
  126. // The instance is ready to be returned, but check if it should be cached first before returning.
  127. if (shouldCache) {
  128. self.cachedInstances[protocolName] = instance;
  129. }
  130. return instance;
  131. }
  132. #pragma mark - Internal Retrieval
  133. - (nullable id)instanceForProtocol:(Protocol *)protocol {
  134. // Check if there is a cached instance, and return it if so.
  135. NSString *protocolName = NSStringFromProtocol(protocol);
  136. id cachedInstance = self.cachedInstances[protocolName];
  137. if (cachedInstance) {
  138. return cachedInstance;
  139. }
  140. // Use the creation block to instantiate an instance and return it.
  141. FIRComponentCreationBlock creationBlock = self.components[protocolName];
  142. return [self instantiateInstanceForProtocol:protocol withBlock:creationBlock];
  143. }
  144. #pragma mark - Lifecycle
  145. - (void)removeAllCachedInstances {
  146. // Loop through the cache and notify each instance that is a maintainer to clean up after itself.
  147. for (id instance in self.cachedInstances.allValues) {
  148. if ([instance conformsToProtocol:@protocol(FIRComponentLifecycleMaintainer)] &&
  149. [instance respondsToSelector:@selector(appWillBeDeleted:)]) {
  150. [instance appWillBeDeleted:self.app];
  151. }
  152. }
  153. [self.cachedInstances removeAllObjects];
  154. }
  155. #pragma mark - Testing Initializers
  156. // TODO(wilsonryan): Set up a testing flag so this only is compiled in with unit tests.
  157. /// Initialize an instance with an app and existing components.
  158. - (instancetype)initWithApp:(FIRApp *)app
  159. components:(NSDictionary<NSString *, FIRComponentCreationBlock> *)components {
  160. self = [self initWithApp:app registrants:[[NSMutableSet alloc] init]];
  161. if (self) {
  162. _components = [components mutableCopy];
  163. }
  164. return self;
  165. }
  166. @end
  167. NS_ASSUME_NONNULL_END