FIRRemoteConfigComponent.m 4.2 KB

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