FIRRemoteConfigComponent.m 4.6 KB

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