FIRRemoteConfigComponent.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/Sources/Private/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. #ifndef FIRRemoteConfig_VERSION
  23. #error "FIRRemoteConfig_VERSION is not defined: \
  24. add -DFIRRemoteConfig_VERSION=... to the build invocation"
  25. #endif
  26. #define STR(x) STR_EXPAND(x)
  27. #define STR_EXPAND(x) #x
  28. @implementation FIRRemoteConfigComponent
  29. /// Default method for retrieving a Remote Config instance, or creating one if it doesn't exist.
  30. - (FIRRemoteConfig *)remoteConfigForNamespace:(NSString *)remoteConfigNamespace {
  31. if (!remoteConfigNamespace) {
  32. // TODO: Throw an error? Return nil? What do we want to do?
  33. return nil;
  34. }
  35. // Validate the required information is available.
  36. FIROptions *options = self.app.options;
  37. NSString *errorPropertyName;
  38. if (options.googleAppID.length == 0) {
  39. errorPropertyName = @"googleAppID";
  40. } else if (options.GCMSenderID.length == 0) {
  41. errorPropertyName = @"GCMSenderID";
  42. }
  43. if (errorPropertyName) {
  44. NSString *const kFirebaseConfigErrorDomain = @"com.firebase.config";
  45. [NSException
  46. raise:kFirebaseConfigErrorDomain
  47. format:@"%@",
  48. [NSString
  49. stringWithFormat:
  50. @"Firebase Remote Config is missing the required %@ property from the "
  51. @"configured FirebaseApp and will not be able to function properly. Please "
  52. @"fix this issue to ensure that Firebase is correctly configured.",
  53. errorPropertyName]];
  54. }
  55. FIRRemoteConfig *instance = self.instances[remoteConfigNamespace];
  56. if (!instance) {
  57. FIRApp *app = self.app;
  58. id<FIRAnalyticsInterop> analytics =
  59. app.isDefaultApp ? FIR_COMPONENT(FIRAnalyticsInterop, app.container) : nil;
  60. instance = [[FIRRemoteConfig alloc] initWithAppName:app.name
  61. FIROptions:app.options
  62. namespace:remoteConfigNamespace
  63. DBManager:[RCNConfigDBManager sharedInstance]
  64. configContent:[RCNConfigContent sharedInstance]
  65. analytics:analytics];
  66. self.instances[remoteConfigNamespace] = instance;
  67. }
  68. return instance;
  69. }
  70. /// Default initializer.
  71. - (instancetype)initWithApp:(FIRApp *)app {
  72. self = [super init];
  73. if (self) {
  74. _app = app;
  75. _instances = [[NSMutableDictionary alloc] initWithCapacity:1];
  76. }
  77. return self;
  78. }
  79. #pragma mark - Lifecycle
  80. + (void)load {
  81. // Register as an internal library to be part of the initialization process. The name comes from
  82. // go/firebase-sdk-platform-info.
  83. [FIRApp registerInternalLibrary:self
  84. withName:@"fire-rc"
  85. withVersion:[NSString stringWithUTF8String:STR(FIRRemoteConfig_VERSION)]];
  86. }
  87. #pragma mark - Interoperability
  88. + (NSArray<FIRComponent *> *)componentsToRegister {
  89. FIRDependency *analyticsDep = [FIRDependency dependencyWithProtocol:@protocol(FIRAnalyticsInterop)
  90. isRequired:NO];
  91. FIRComponent *rcProvider = [FIRComponent
  92. componentWithProtocol:@protocol(FIRRemoteConfigProvider)
  93. instantiationTiming:FIRInstantiationTimingAlwaysEager
  94. dependencies:@[ analyticsDep ]
  95. creationBlock:^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
  96. // Cache the component so instances of Remote Config are cached.
  97. *isCacheable = YES;
  98. return [[FIRRemoteConfigComponent alloc] initWithApp:container.app];
  99. }];
  100. return @[ rcProvider ];
  101. }
  102. @end