FIRComponentContainer.m 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 "FirebaseCore/Extension/FIRComponentContainer.h"
  17. #import "FirebaseCore/Extension/FIRAppInternal.h"
  18. #import "FirebaseCore/Extension/FIRComponent.h"
  19. #import "FirebaseCore/Extension/FIRLibrary.h"
  20. #import "FirebaseCore/Extension/FIRLogger.h"
  21. #import "FirebaseCore/Sources/FIROptionsInternal.h"
  22. #import "FirebaseCore/Sources/Public/FirebaseCore/FIROptions.h"
  23. NS_ASSUME_NONNULL_BEGIN
  24. @interface FIRComponentContainer ()
  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. /// Protocols of components that have requested to be eagerly instantiated.
  31. @property(nonatomic, strong, nullable) NSMutableArray<Protocol *> *eagerProtocolsToInstantiate;
  32. @end
  33. @implementation FIRComponentContainer
  34. // Collection of all classes that register to provide components.
  35. static NSMutableSet<Class> *sFIRComponentRegistrants;
  36. #pragma mark - Public Registration
  37. + (void)registerAsComponentRegistrant:(Class<FIRLibrary>)klass {
  38. static dispatch_once_t onceToken;
  39. dispatch_once(&onceToken, ^{
  40. sFIRComponentRegistrants = [[NSMutableSet<Class> alloc] init];
  41. });
  42. [self registerAsComponentRegistrant:klass inSet:sFIRComponentRegistrants];
  43. }
  44. + (void)registerAsComponentRegistrant:(Class<FIRLibrary>)klass
  45. inSet:(NSMutableSet<Class> *)allRegistrants {
  46. [allRegistrants addObject:klass];
  47. }
  48. #pragma mark - Internal Initialization
  49. - (instancetype)initWithApp:(FIRApp *)app {
  50. NSMutableSet<Class> *componentRegistrants = sFIRComponentRegistrants;
  51. // If the app being created is for the ARCore SDK, remove the App Check
  52. // component (if it exists) since it does not support App Check.
  53. if ([self isAppForARCore:app]) {
  54. Class klass = NSClassFromString(@"FIRAppCheckComponent");
  55. if (klass && [sFIRComponentRegistrants containsObject:klass]) {
  56. componentRegistrants = [componentRegistrants mutableCopy];
  57. [componentRegistrants removeObject:klass];
  58. }
  59. }
  60. return [self initWithApp:app registrants:componentRegistrants];
  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. // Keep track of any components that need to eagerly instantiate after all components are added.
  74. self.eagerProtocolsToInstantiate = [[NSMutableArray alloc] init];
  75. // Loop through the verified component registrants and populate the components array.
  76. for (Class<FIRLibrary> klass in classes) {
  77. // Loop through all the components being registered and store them as appropriate.
  78. // Classes which do not provide functionality should use a dummy FIRComponentRegistrant
  79. // protocol.
  80. for (FIRComponent *component in [klass componentsToRegister]) {
  81. // Check if the component has been registered before, and error out if so.
  82. NSString *protocolName = NSStringFromProtocol(component.protocol);
  83. if (self.components[protocolName]) {
  84. FIRLogError(kFIRLoggerCore, @"I-COR000029",
  85. @"Attempted to register protocol %@, but it already has an implementation.",
  86. protocolName);
  87. continue;
  88. }
  89. // Store the creation block for later usage.
  90. self.components[protocolName] = component.creationBlock;
  91. // Queue any protocols that should be eagerly instantiated. Don't instantiate them yet
  92. // because they could depend on other components that haven't been added to the components
  93. // array yet.
  94. BOOL shouldInstantiateEager =
  95. (component.instantiationTiming == FIRInstantiationTimingAlwaysEager);
  96. BOOL shouldInstantiateDefaultEager =
  97. (component.instantiationTiming == FIRInstantiationTimingEagerInDefaultApp &&
  98. [app isDefaultApp]);
  99. if (shouldInstantiateEager || shouldInstantiateDefaultEager) {
  100. [self.eagerProtocolsToInstantiate addObject:component.protocol];
  101. }
  102. }
  103. }
  104. }
  105. #pragma mark - Instance Creation
  106. - (void)instantiateEagerComponents {
  107. // After all components are registered, instantiate the ones that are requesting eager
  108. // instantiation.
  109. @synchronized(self) {
  110. for (Protocol *protocol in self.eagerProtocolsToInstantiate) {
  111. // Get an instance for the protocol, which will instantiate it since it couldn't have been
  112. // cached yet. Ignore the instance coming back since we don't need it.
  113. __unused id unusedInstance = [self instanceForProtocol:protocol];
  114. }
  115. // All eager instantiation is complete, clear the stored property now.
  116. self.eagerProtocolsToInstantiate = nil;
  117. }
  118. }
  119. /// Instantiate an instance of a class that conforms to the specified protocol.
  120. /// This will:
  121. /// - Call the block to create an instance if possible,
  122. /// - Validate that the instance returned conforms to the protocol it claims to,
  123. /// - Cache the instance if the block requests it
  124. ///
  125. /// Note that this method assumes the caller already has @synchronized on self.
  126. - (nullable id)instantiateInstanceForProtocol:(Protocol *)protocol
  127. withBlock:(FIRComponentCreationBlock)creationBlock {
  128. if (!creationBlock) {
  129. return nil;
  130. }
  131. // Create an instance using the creation block.
  132. BOOL shouldCache = NO;
  133. id instance = creationBlock(self, &shouldCache);
  134. if (!instance) {
  135. return nil;
  136. }
  137. // An instance was created, validate that it conforms to the protocol it claims to.
  138. NSString *protocolName = NSStringFromProtocol(protocol);
  139. if (![instance conformsToProtocol:protocol]) {
  140. FIRLogError(kFIRLoggerCore, @"I-COR000030",
  141. @"An instance conforming to %@ was requested, but the instance provided does not "
  142. @"conform to the protocol",
  143. protocolName);
  144. }
  145. // The instance is ready to be returned, but check if it should be cached first before returning.
  146. if (shouldCache) {
  147. self.cachedInstances[protocolName] = instance;
  148. }
  149. return instance;
  150. }
  151. #pragma mark - Internal Retrieval
  152. // Redirected for Swift users.
  153. - (nullable id)__instanceForProtocol:(Protocol *)protocol {
  154. return [self instanceForProtocol:protocol];
  155. }
  156. - (nullable id)instanceForProtocol:(Protocol *)protocol {
  157. // Check if there is a cached instance, and return it if so.
  158. NSString *protocolName = NSStringFromProtocol(protocol);
  159. id cachedInstance;
  160. @synchronized(self) {
  161. cachedInstance = self.cachedInstances[protocolName];
  162. if (!cachedInstance) {
  163. // Use the creation block to instantiate an instance and return it.
  164. FIRComponentCreationBlock creationBlock = self.components[protocolName];
  165. cachedInstance = [self instantiateInstanceForProtocol:protocol withBlock:creationBlock];
  166. }
  167. }
  168. return cachedInstance;
  169. }
  170. #pragma mark - Lifecycle
  171. - (void)removeAllCachedInstances {
  172. @synchronized(self) {
  173. // Loop through the cache and notify each instance that is a maintainer to clean up after
  174. // itself.
  175. for (id instance in self.cachedInstances.allValues) {
  176. if ([instance conformsToProtocol:@protocol(FIRComponentLifecycleMaintainer)] &&
  177. [instance respondsToSelector:@selector(appWillBeDeleted:)]) {
  178. [instance appWillBeDeleted:self.app];
  179. }
  180. }
  181. // Empty the cache.
  182. [self.cachedInstances removeAllObjects];
  183. }
  184. }
  185. - (void)removeAllComponents {
  186. @synchronized(self) {
  187. [self.components removeAllObjects];
  188. }
  189. }
  190. #pragma mark - Helpers
  191. - (BOOL)isAppForARCore:(FIRApp *)app {
  192. // First, check if the app name matches that of the one used by ARCore.
  193. if ([app.name isEqualToString:@"ARCoreFIRApp"]) {
  194. // Second, check if the app's gcmSenderID matches that of ARCore. This
  195. // prevents false positives in the unlikely event a 3P Firebase app is
  196. // named `ARCoreFIRApp`.
  197. const char *p1 = "406756";
  198. const char *p2 = "893798";
  199. const char gcmSenderIDKey[27] = {p1[0], p2[0], p1[1], p2[1], p1[2], p2[2], p1[3],
  200. p2[3], p1[4], p2[4], p1[5], p2[5], p1[6], p2[6],
  201. p1[7], p2[7], p1[8], p2[8], p1[9], p2[9], p1[10],
  202. p2[10], p1[11], p2[11], p1[12], p2[12], '\0'};
  203. NSString *gcmSenderID = [NSString stringWithUTF8String:gcmSenderIDKey];
  204. return [app.options.GCMSenderID isEqualToString:gcmSenderID];
  205. }
  206. return NO;
  207. }
  208. @end
  209. NS_ASSUME_NONNULL_END