暂无描述

FIRComponentContainer.m 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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/FIRLibrary.h"
  20. #import "Private/FIRLogger.h"
  21. NS_ASSUME_NONNULL_BEGIN
  22. @interface FIRComponentContainer () {
  23. dispatch_queue_t _containerQueue;
  24. }
  25. /// The dictionary of components that are registered for a particular app. The key is an NSString
  26. /// of the protocol.
  27. @property(nonatomic, strong) NSMutableDictionary<NSString *, FIRComponentCreationBlock> *components;
  28. /// Cached instances of components that requested to be cached.
  29. @property(nonatomic, strong) NSMutableDictionary<NSString *, id> *cachedInstances;
  30. @end
  31. @implementation FIRComponentContainer
  32. // Collection of all classes that register to provide components.
  33. static NSMutableSet<Class> *sFIRComponentRegistrants;
  34. #pragma mark - Public Registration
  35. + (void)registerAsComponentRegistrant:(Class<FIRLibrary>)klass {
  36. static dispatch_once_t onceToken;
  37. dispatch_once(&onceToken, ^{
  38. sFIRComponentRegistrants = [[NSMutableSet<Class> alloc] init];
  39. });
  40. [self registerAsComponentRegistrant:klass inSet:sFIRComponentRegistrants];
  41. }
  42. + (void)registerAsComponentRegistrant:(Class<FIRLibrary>)klass
  43. inSet:(NSMutableSet<Class> *)allRegistrants {
  44. [allRegistrants addObject:klass];
  45. }
  46. #pragma mark - Internal Initialization
  47. - (instancetype)initWithApp:(FIRApp *)app {
  48. return [self initWithApp:app registrants:sFIRComponentRegistrants];
  49. }
  50. - (instancetype)initWithApp:(FIRApp *)app registrants:(NSMutableSet<Class> *)allRegistrants {
  51. self = [super init];
  52. if (self) {
  53. _app = app;
  54. _cachedInstances = [NSMutableDictionary<NSString *, id> dictionary];
  55. _components = [NSMutableDictionary<NSString *, FIRComponentCreationBlock> dictionary];
  56. _containerQueue =
  57. dispatch_queue_create("com.google.FirebaseComponentContainer", DISPATCH_QUEUE_SERIAL);
  58. [self populateComponentsFromRegisteredClasses:allRegistrants forApp:app];
  59. }
  60. return self;
  61. }
  62. - (void)populateComponentsFromRegisteredClasses:(NSSet<Class> *)classes forApp:(FIRApp *)app {
  63. // Loop through the verified component registrants and populate the components array.
  64. for (Class<FIRLibrary> klass in classes) {
  65. // Loop through all the components being registered and store them as appropriate.
  66. // Classes which do not provide functionality should use a dummy FIRComponentRegistrant
  67. // protocol.
  68. for (FIRComponent *component in [klass componentsToRegister]) {
  69. // Check if the component has been registered before, and error out if so.
  70. NSString *protocolName = NSStringFromProtocol(component.protocol);
  71. if (self.components[protocolName]) {
  72. FIRLogError(kFIRLoggerCore, @"I-COR000029",
  73. @"Attempted to register protocol %@, but it already has an implementation.",
  74. protocolName);
  75. continue;
  76. }
  77. // Store the creation block for later usage.
  78. self.components[protocolName] = component.creationBlock;
  79. // Instantiate the instance if it has requested to be instantiated.
  80. BOOL shouldInstantiateEager =
  81. (component.instantiationTiming == FIRInstantiationTimingAlwaysEager);
  82. BOOL shouldInstantiateDefaultEager =
  83. (component.instantiationTiming == FIRInstantiationTimingEagerInDefaultApp &&
  84. [app isDefaultApp]);
  85. if (shouldInstantiateEager || shouldInstantiateDefaultEager) {
  86. [self instantiateInstanceForProtocol:component.protocol withBlock:component.creationBlock];
  87. }
  88. }
  89. }
  90. }
  91. #pragma mark - Instance Creation
  92. /// Instantiate an instance of a class that conforms to the specified protocol.
  93. /// This will:
  94. /// - Call the block to create an instance if possible,
  95. /// - Validate that the instance returned conforms to the protocol it claims to,
  96. /// - Cache the instance if the block requests it
  97. - (nullable id)instantiateInstanceForProtocol:(Protocol *)protocol
  98. withBlock:(FIRComponentCreationBlock)creationBlock {
  99. if (!creationBlock) {
  100. return nil;
  101. }
  102. // Create an instance using the creation block.
  103. BOOL shouldCache = NO;
  104. id instance = creationBlock(self, &shouldCache);
  105. if (!instance) {
  106. return nil;
  107. }
  108. // An instance was created, validate that it conforms to the protocol it claims to.
  109. NSString *protocolName = NSStringFromProtocol(protocol);
  110. if (![instance conformsToProtocol:protocol]) {
  111. FIRLogError(kFIRLoggerCore, @"I-COR000030",
  112. @"An instance conforming to %@ was requested, but the instance provided does not "
  113. @"conform to the protocol",
  114. protocolName);
  115. }
  116. // The instance is ready to be returned, but check if it should be cached first before returning.
  117. if (shouldCache) {
  118. dispatch_sync(_containerQueue, ^{
  119. self.cachedInstances[protocolName] = instance;
  120. });
  121. }
  122. return instance;
  123. }
  124. #pragma mark - Internal Retrieval
  125. - (nullable id)instanceForProtocol:(Protocol *)protocol {
  126. // Check if there is a cached instance, and return it if so.
  127. NSString *protocolName = NSStringFromProtocol(protocol);
  128. __block id cachedInstance;
  129. dispatch_sync(_containerQueue, ^{
  130. cachedInstance = self.cachedInstances[protocolName];
  131. });
  132. if (cachedInstance) {
  133. return cachedInstance;
  134. }
  135. // Use the creation block to instantiate an instance and return it.
  136. FIRComponentCreationBlock creationBlock = self.components[protocolName];
  137. return [self instantiateInstanceForProtocol:protocol withBlock:creationBlock];
  138. }
  139. #pragma mark - Lifecycle
  140. - (void)removeAllCachedInstances {
  141. // Loop through the cache and notify each instance that is a maintainer to clean up after itself.
  142. // Design note: we're getting a copy here, unlocking the cached instances, iterating over the
  143. // copy, then locking and removing all cached instances. A race condition *could* exist where a
  144. // new cached instance is created between the copy and the removal, but the chances are slim and
  145. // side-effects are significantly smaller than including the entire loop in the `dispatch_sync`
  146. // block (access to the cache from inside the block would deadlock and crash).
  147. __block NSDictionary<NSString *, id> *instancesCopy;
  148. dispatch_sync(_containerQueue, ^{
  149. instancesCopy = [self.cachedInstances copy];
  150. });
  151. for (id instance in instancesCopy.allValues) {
  152. if ([instance conformsToProtocol:@protocol(FIRComponentLifecycleMaintainer)] &&
  153. [instance respondsToSelector:@selector(appWillBeDeleted:)]) {
  154. [instance appWillBeDeleted:self.app];
  155. }
  156. }
  157. instancesCopy = nil;
  158. // Empty the cache.
  159. dispatch_sync(_containerQueue, ^{
  160. [self.cachedInstances removeAllObjects];
  161. });
  162. }
  163. @end
  164. NS_ASSUME_NONNULL_END