FIRSegmentationComponent.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 "FirebaseSegmentation/Sources/Private/FIRSegmentationComponent.h"
  17. #import "FirebaseCore/Sources/Private/FIRAppInternal.h"
  18. #import "FirebaseCore/Sources/Private/FIRComponentContainer.h"
  19. #import "FirebaseCore/Sources/Private/FIROptionsInternal.h"
  20. #import "FirebaseSegmentation/Sources/Private/FIRSegmentationInternal.h"
  21. #import "FirebaseSegmentation/Sources/SEGSegmentationConstants.h"
  22. #ifndef FIRSegmentation_VERSION
  23. #error "FIRSegmentation_VERSION is not defined: \
  24. add -DFIRSegmentation_VERSION=... to the build invocation"
  25. #endif
  26. #define STR(x) STR_EXPAND(x)
  27. #define STR_EXPAND(x) #x
  28. @implementation FIRSegmentationComponent
  29. /// Default method for retrieving a Segmentation instance, or creating one if it doesn't exist.
  30. - (FIRSegmentation *)segmentation {
  31. // Validate the required information is available.
  32. FIROptions *options = self.app.options;
  33. NSString *errorPropertyName;
  34. if (options.googleAppID.length == 0) {
  35. errorPropertyName = @"googleAppID";
  36. } else if (options.GCMSenderID.length == 0) {
  37. errorPropertyName = @"GCMSenderID";
  38. }
  39. if (errorPropertyName) {
  40. [NSException
  41. raise:kFirebaseSegmentationErrorDomain
  42. format:@"%@",
  43. [NSString
  44. stringWithFormat:
  45. @"Firebase Segmentation is missing the required %@ property from the "
  46. @"configured FirebaseApp and will not be able to function properly. Please "
  47. @"fix this issue to ensure that Firebase is correctly configured.",
  48. errorPropertyName]];
  49. }
  50. FIRSegmentation *instance = self.segmentationInstance;
  51. if (!instance) {
  52. instance = [[FIRSegmentation alloc] initWithAppName:self.app.name FIROptions:self.app.options];
  53. self.segmentationInstance = instance;
  54. }
  55. return instance;
  56. }
  57. /// Default initializer.
  58. - (instancetype)initWithApp:(FIRApp *)app {
  59. self = [super init];
  60. if (self) {
  61. _app = app;
  62. if (!_segmentationInstance) {
  63. _segmentationInstance = [[FIRSegmentation alloc] initWithAppName:app.name
  64. FIROptions:app.options];
  65. }
  66. }
  67. return self;
  68. }
  69. #pragma mark - Lifecycle
  70. + (void)load {
  71. // Register as an internal library to be part of the initialization process. The name comes from
  72. // go/firebase-sdk-platform-info.
  73. [FIRApp registerInternalLibrary:self
  74. withName:@"fire-seg"
  75. withVersion:[NSString stringWithUTF8String:STR(FIRSegmentation_VERSION)]];
  76. }
  77. #pragma mark - Interoperability
  78. + (NSArray<FIRComponent *> *)componentsToRegister {
  79. FIRComponent *segProvider = [FIRComponent
  80. componentWithProtocol:@protocol(FIRSegmentationProvider)
  81. instantiationTiming:FIRInstantiationTimingAlwaysEager
  82. dependencies:@[]
  83. creationBlock:^id _Nullable(FIRComponentContainer *container, BOOL *isCacheable) {
  84. // Cache the component so instances of Segmentation are cached.
  85. *isCacheable = YES;
  86. return [[FIRSegmentationComponent alloc] initWithApp:container.app];
  87. }];
  88. return @[ segProvider ];
  89. }
  90. @synthesize instances;
  91. @end