FIRRemoteConfigComponent.m 4.1 KB

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