FIRRemoteConfigComponent.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "FirebaseRemoteConfig/Sources/FIRRemoteConfigComponent.h"
  17. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  18. #import "FirebaseRemoteConfig/Sources/Private/FIRRemoteConfig_Private.h"
  19. #import "FirebaseRemoteConfig/Sources/RCNConfigContent.h"
  20. #import "FirebaseRemoteConfig/Sources/RCNConfigDBManager.h"
  21. #import "Interop/Analytics/Public/FIRAnalyticsInterop.h"
  22. @implementation FIRRemoteConfigComponent
  23. // Because Component now need to register two protocols (provider and interop), we need a way to
  24. // return the same component instance for both registered protocol, this singleton pattern allow us
  25. // to return the same component object for both registration callback.
  26. static NSMutableDictionary<NSString *, FIRRemoteConfigComponent *> *_componentInstances = nil;
  27. + (FIRRemoteConfigComponent *)getComponentForApp:(FIRApp *)app {
  28. @synchronized(_componentInstances) {
  29. // need to init the dictionary first
  30. if (!_componentInstances) {
  31. _componentInstances = [[NSMutableDictionary alloc] init];
  32. }
  33. if (![_componentInstances objectForKey:app.name]) {
  34. _componentInstances[app.name] = [[self alloc] initWithApp:app];
  35. }
  36. return _componentInstances[app.name];
  37. }
  38. return nil;
  39. }
  40. + (void)clearAllComponentInstances {
  41. @synchronized(_componentInstances) {
  42. [_componentInstances removeAllObjects];
  43. }
  44. }
  45. /// Default method for retrieving a Remote Config instance, or creating one if it doesn't exist.
  46. - (FIRRemoteConfig *)remoteConfigForNamespace:(NSString *)remoteConfigNamespace {
  47. if (!remoteConfigNamespace) {
  48. // TODO: Throw an error? Return nil? What do we want to do?
  49. return nil;
  50. }
  51. // Validate the required information is available.
  52. FIROptions *options = self.app.options;
  53. NSString *errorPropertyName;
  54. if (options.googleAppID.length == 0) {
  55. errorPropertyName = @"googleAppID";
  56. } else if (options.GCMSenderID.length == 0) {
  57. errorPropertyName = @"GCMSenderID";
  58. } else if (options.projectID.length == 0) {
  59. errorPropertyName = @"projectID";
  60. }
  61. if (errorPropertyName) {
  62. NSString *const kFirebaseConfigErrorDomain = @"com.firebase.config";
  63. [NSException
  64. raise:kFirebaseConfigErrorDomain
  65. format:@"%@",
  66. [NSString
  67. stringWithFormat:
  68. @"Firebase Remote Config is missing the required %@ property from the "
  69. @"configured FirebaseApp and will not be able to function properly. Please "
  70. @"fix this issue to ensure that Firebase is correctly configured.",
  71. errorPropertyName]];
  72. }
  73. FIRRemoteConfig *instance = self.instances[remoteConfigNamespace];
  74. if (!instance) {
  75. FIRApp *app = self.app;
  76. id<FIRAnalyticsInterop> analytics =
  77. app.isDefaultApp ? FIR_COMPONENT(FIRAnalyticsInterop, app.container) : nil;
  78. instance = [[FIRRemoteConfig alloc] initWithAppName:app.name
  79. FIROptions:app.options
  80. namespace:remoteConfigNamespace
  81. DBManager:[RCNConfigDBManager sharedInstance]
  82. configContent:[RCNConfigContent sharedInstance]
  83. analytics:analytics];
  84. self.instances[remoteConfigNamespace] = instance;
  85. }
  86. return instance;
  87. }
  88. /// Default initializer.
  89. - (instancetype)initWithApp:(FIRApp *)app {
  90. self = [super init];
  91. if (self) {
  92. _app = app;
  93. _instances = [[NSMutableDictionary alloc] initWithCapacity:1];
  94. }
  95. return self;
  96. }
  97. #pragma mark - Lifecycle
  98. + (void)load {
  99. // Register as an internal library to be part of the initialization process. The name comes from
  100. // go/firebase-sdk-platform-info.
  101. [FIRApp registerInternalLibrary:self withName:@"fire-rc"];
  102. }
  103. #pragma mark - Interoperability
  104. + (NSArray<FIRComponent *> *)componentsToRegister {
  105. FIRComponent *rcProvider = [FIRComponent
  106. componentWithProtocol:@protocol(FIRRemoteConfigProvider)
  107. instantiationTiming:FIRInstantiationTimingAlwaysEager
  108. creationBlock:^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
  109. // Cache the component so instances of Remote Config are cached.
  110. *isCacheable = YES;
  111. return [FIRRemoteConfigComponent getComponentForApp:container.app];
  112. }];
  113. // Unlike provider needs to setup a hard dependency on remote config, interop allows an optional
  114. // dependency on RC
  115. FIRComponent *rcInterop = [FIRComponent
  116. componentWithProtocol:@protocol(FIRRemoteConfigInterop)
  117. instantiationTiming:FIRInstantiationTimingAlwaysEager
  118. creationBlock:^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
  119. // Cache the component so instances of Remote Config are cached.
  120. *isCacheable = YES;
  121. return [FIRRemoteConfigComponent getComponentForApp:container.app];
  122. }];
  123. return @[ rcProvider, rcInterop ];
  124. }
  125. #pragma mark - Remote Config Interop Protocol
  126. - (void)registerRolloutsStateSubscriber:(id<FIRRolloutsStateSubscriber>)subscriber
  127. for:(NSString * _Nonnull)namespace {
  128. FIRRemoteConfig *instance = [self remoteConfigForNamespace:namespace];
  129. [instance addRemoteConfigInteropSubscriber:subscriber];
  130. }
  131. @end