FIRSegmentationComponent.m 3.3 KB

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