GULCCComponentContainer.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright 2019 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 "Public/GULCCComponentContainer.h"
  17. #import "Public/GULCCComponent.h"
  18. #import "Public/GULCCLibrary.h"
  19. #import <GoogleUtilities/GULLogger.h>
  20. static NSString *kGULComponentSubsystem = @"com.google.googleutilitiescomponents";
  21. static NSString *kGULComponentContainer = @"[GoogleUtilitiesComponents]";
  22. NS_ASSUME_NONNULL_BEGIN
  23. @interface GULCCComponentContainer ()
  24. /// The dictionary of components that are registered for a particular app. The key is an `NSString`
  25. /// of the protocol.
  26. @property(nonatomic, strong)
  27. NSMutableDictionary<NSString *, GULCCComponentCreationBlock> *components;
  28. /// Cached instances of components that requested to be cached.
  29. @property(nonatomic, strong) NSMutableDictionary<NSString *, id> *cachedInstances;
  30. /// Protocols of components that have requested to be eagerly instantiated.
  31. @property(nonatomic, strong, nullable) NSMutableArray<Protocol *> *eagerProtocolsToInstantiate;
  32. @end
  33. @implementation GULCCComponentContainer
  34. // Collection of all classes that register to provide components.
  35. static NSMutableSet<Class> *sGULComponentRegistrants;
  36. #pragma mark - Public Registration
  37. + (void)registerAsComponentRegistrant:(Class<GULCCLibrary>)klass {
  38. static dispatch_once_t onceToken;
  39. dispatch_once(&onceToken, ^{
  40. sGULComponentRegistrants = [[NSMutableSet<Class> alloc] init];
  41. });
  42. [self registerAsComponentRegistrant:klass inSet:sGULComponentRegistrants];
  43. }
  44. + (void)registerAsComponentRegistrant:(Class<GULCCLibrary>)klass
  45. inSet:(NSMutableSet<Class> *)allRegistrants {
  46. [allRegistrants addObject:klass];
  47. }
  48. #pragma mark - Internal Initialization
  49. - (instancetype)initWithContext:(nullable id)context {
  50. return [self initWithContext:context registrants:sGULComponentRegistrants];
  51. }
  52. - (instancetype)initWithContext:(nullable id)context
  53. registrants:(NSMutableSet<Class> *)allRegistrants {
  54. self = [super init];
  55. if (self) {
  56. _context = context;
  57. _cachedInstances = [NSMutableDictionary<NSString *, id> dictionary];
  58. _components = [NSMutableDictionary<NSString *, GULCCComponentCreationBlock> dictionary];
  59. [self populateComponentsFromRegisteredClasses:allRegistrants withContext:context];
  60. }
  61. return self;
  62. }
  63. - (void)populateComponentsFromRegisteredClasses:(NSSet<Class> *)classes withContext:(id)context {
  64. // Keep track of any components that need to eagerly instantiate after all components are added.
  65. self.eagerProtocolsToInstantiate = [[NSMutableArray alloc] init];
  66. // Loop through the verified component registrants and populate the components array.
  67. for (Class<GULCCLibrary> klass in classes) {
  68. // Loop through all the components being registered and store them as appropriate.
  69. // Classes which do not provide functionality should use a dummy GULCCComponentRegistrant
  70. // protocol.
  71. for (GULCCComponent *component in [klass componentsToRegister]) {
  72. // Check if the component has been registered before, and error out if so.
  73. NSString *protocolName = NSStringFromProtocol(component.protocol);
  74. if (self.components[protocolName]) {
  75. GULOSLogError(kGULComponentSubsystem, kGULComponentContainer, NO, @"I-COM000001",
  76. @"Attempted to register protocol %@, but it already has an implementation.",
  77. protocolName);
  78. continue;
  79. }
  80. // Store the creation block for later usage.
  81. self.components[protocolName] = component.creationBlock;
  82. // Queue any protocols that should be eagerly instantiated. Don't instantiate them yet
  83. // because they could depend on other components that haven't been added to the components
  84. // array yet.
  85. if (component.instantiationTiming == GULCCInstantiationTimingAlwaysEager) {
  86. [self.eagerProtocolsToInstantiate addObject:component.protocol];
  87. }
  88. }
  89. }
  90. }
  91. #pragma mark - Instance Creation
  92. - (void)instantiateEagerComponents {
  93. // After all components are registered, instantiate the ones that are requesting eager
  94. // instantiation.
  95. @synchronized(self) {
  96. for (Protocol *protocol in self.eagerProtocolsToInstantiate) {
  97. // Get an instance for the protocol, which will instantiate it since it couldn't have been
  98. // cached yet. Ignore the instance coming back since we don't need it.
  99. __unused id unusedInstance = [self instanceForProtocol:protocol];
  100. }
  101. // All eager instantiation is complete, clear the stored property now.
  102. self.eagerProtocolsToInstantiate = nil;
  103. }
  104. }
  105. /// Instantiate an instance of a class that conforms to the specified protocol.
  106. /// This will:
  107. /// - Call the block to create an instance if possible,
  108. /// - Validate that the instance returned conforms to the protocol it claims to,
  109. /// - Cache the instance if the block requests it
  110. ///
  111. /// Note that this method assumes the caller already has @sychronized on self.
  112. - (nullable id)instantiateInstanceForProtocol:(Protocol *)protocol
  113. withBlock:(GULCCComponentCreationBlock)creationBlock {
  114. if (!creationBlock) {
  115. return nil;
  116. }
  117. // Create an instance using the creation block.
  118. BOOL shouldCache = NO;
  119. id instance = creationBlock(self, &shouldCache);
  120. if (!instance) {
  121. return nil;
  122. }
  123. // An instance was created, validate that it conforms to the protocol it claims to.
  124. NSString *protocolName = NSStringFromProtocol(protocol);
  125. if (![instance conformsToProtocol:protocol]) {
  126. GULOSLogError(kGULComponentSubsystem, kGULComponentContainer, NO, @"I-COM000002",
  127. @"An instance conforming to %@ was requested, but the instance provided does not "
  128. @"conform to the protocol",
  129. protocolName);
  130. }
  131. // The instance is ready to be returned, but check if it should be cached first before returning.
  132. if (shouldCache) {
  133. self.cachedInstances[protocolName] = instance;
  134. }
  135. return instance;
  136. }
  137. #pragma mark - Internal Retrieval
  138. - (nullable id)instanceForProtocol:(Protocol *)protocol {
  139. // Check if there is a cached instance, and return it if so.
  140. NSString *protocolName = NSStringFromProtocol(protocol);
  141. id cachedInstance;
  142. @synchronized(self) {
  143. cachedInstance = self.cachedInstances[protocolName];
  144. if (!cachedInstance) {
  145. // Use the creation block to instantiate an instance and return it.
  146. GULCCComponentCreationBlock creationBlock = self.components[protocolName];
  147. cachedInstance = [self instantiateInstanceForProtocol:protocol withBlock:creationBlock];
  148. }
  149. }
  150. return cachedInstance;
  151. }
  152. #pragma mark - Lifecycle
  153. - (void)removeAllCachedInstances {
  154. @synchronized(self) {
  155. // Loop through the cache and notify each instance that is a maintainer to clean up after
  156. // itself.
  157. for (id instance in self.cachedInstances.allValues) {
  158. if ([instance conformsToProtocol:@protocol(GULCCComponentLifecycleMaintainer)] &&
  159. [instance respondsToSelector:@selector(containerWillBeEmptied:)]) {
  160. [instance containerWillBeEmptied:self];
  161. }
  162. }
  163. // Empty the cache.
  164. [self.cachedInstances removeAllObjects];
  165. }
  166. }
  167. @end
  168. NS_ASSUME_NONNULL_END