FIRRemoteConfigComponent.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. [NSException
  45. raise:kFirebaseConfigErrorDomain
  46. format:@"%@",
  47. [NSString
  48. stringWithFormat:
  49. @"Firebase Remote Config is missing the required %@ property from the "
  50. @"configured FirebaseApp and will not be able to function properly. Please "
  51. @"fix this issue to ensure that Firebase is correctly configured.",
  52. errorPropertyName]];
  53. }
  54. FIRRemoteConfig *instance = self.instances[remoteConfigNamespace];
  55. if (!instance) {
  56. FIRApp *app = self.app;
  57. id<FIRAnalyticsInterop> analytics =
  58. app.isDefaultApp ? FIR_COMPONENT(FIRAnalyticsInterop, app.container) : nil;
  59. instance = [[FIRRemoteConfig alloc] initWithAppName:app.name
  60. FIROptions:app.options
  61. namespace:remoteConfigNamespace
  62. DBManager:[RCNConfigDBManager sharedInstance]
  63. configContent:[RCNConfigContent sharedInstance]
  64. analytics:analytics];
  65. self.instances[remoteConfigNamespace] = instance;
  66. }
  67. return instance;
  68. }
  69. /// Default initializer.
  70. - (instancetype)initWithApp:(FIRApp *)app {
  71. self = [super init];
  72. if (self) {
  73. _app = app;
  74. _instances = [[NSMutableDictionary alloc] initWithCapacity:1];
  75. }
  76. return self;
  77. }
  78. #pragma mark - Lifecycle
  79. + (void)load {
  80. // Register as an internal library to be part of the initialization process. The name comes from
  81. // go/firebase-sdk-platform-info.
  82. [FIRApp registerInternalLibrary:self
  83. withName:@"fire-rc"
  84. withVersion:[NSString stringWithUTF8String:STR(FIRRemoteConfig_VERSION)]];
  85. }
  86. #pragma mark - Interoperability
  87. + (NSArray<FIRComponent *> *)componentsToRegister {
  88. FIRDependency *analyticsDep = [FIRDependency dependencyWithProtocol:@protocol(FIRAnalyticsInterop)
  89. isRequired:NO];
  90. FIRComponent *rcProvider = [FIRComponent
  91. componentWithProtocol:@protocol(FIRRemoteConfigProvider)
  92. instantiationTiming:FIRInstantiationTimingAlwaysEager
  93. dependencies:@[ analyticsDep ]
  94. creationBlock:^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
  95. // Cache the component so instances of Remote Config are cached.
  96. *isCacheable = YES;
  97. return [[FIRRemoteConfigComponent alloc] initWithApp:container.app];
  98. }];
  99. return @[ rcProvider ];
  100. }
  101. @end