GDTCORTransformer.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2018 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 "GDTCORLibrary/Private/GDTCORTransformer.h"
  17. #import "GDTCORLibrary/Private/GDTCORTransformer_Private.h"
  18. #import <GoogleDataTransport/GDTCORAssert.h>
  19. #import <GoogleDataTransport/GDTCORConsoleLogger.h>
  20. #import <GoogleDataTransport/GDTCOREvent.h>
  21. #import <GoogleDataTransport/GDTCOREventTransformer.h>
  22. #import <GoogleDataTransport/GDTCORLifecycle.h>
  23. #import <GoogleDataTransport/GDTCORStorageProtocol.h>
  24. #import "GDTCORLibrary/Private/GDTCOREvent_Private.h"
  25. #import "GDTCORLibrary/Private/GDTCORRegistrar_Private.h"
  26. @implementation GDTCORTransformer
  27. + (instancetype)sharedInstance {
  28. static GDTCORTransformer *eventTransformer;
  29. static dispatch_once_t onceToken;
  30. dispatch_once(&onceToken, ^{
  31. eventTransformer = [[self alloc] init];
  32. });
  33. return eventTransformer;
  34. }
  35. - (instancetype)init {
  36. self = [super init];
  37. if (self) {
  38. _eventWritingQueue =
  39. dispatch_queue_create("com.google.GDTCORTransformer", DISPATCH_QUEUE_SERIAL);
  40. }
  41. return self;
  42. }
  43. - (void)transformEvent:(GDTCOREvent *)event
  44. withTransformers:(NSArray<id<GDTCOREventTransformer>> *)transformers
  45. onComplete:(void (^_Nullable)(BOOL wasWritten, NSError *_Nullable error))completion {
  46. GDTCORAssert(event, @"You can't write a nil event");
  47. BOOL hadOriginalCompletion = completion != nil;
  48. if (!completion) {
  49. completion = ^(BOOL wasWritten, NSError *_Nullable error) {
  50. };
  51. }
  52. __block GDTCORBackgroundIdentifier bgID = GDTCORBackgroundIdentifierInvalid;
  53. bgID = [[GDTCORApplication sharedApplication]
  54. beginBackgroundTaskWithName:@"GDTTransformer"
  55. expirationHandler:^{
  56. [[GDTCORApplication sharedApplication] endBackgroundTask:bgID];
  57. bgID = GDTCORBackgroundIdentifierInvalid;
  58. }];
  59. dispatch_async(_eventWritingQueue, ^{
  60. GDTCOREvent *transformedEvent = event;
  61. for (id<GDTCOREventTransformer> transformer in transformers) {
  62. if ([transformer respondsToSelector:@selector(transform:)]) {
  63. GDTCORLogDebug(@"Applying a transformer to event %@", event);
  64. transformedEvent = [transformer transform:transformedEvent];
  65. if (!transformedEvent) {
  66. completion(NO, nil);
  67. return;
  68. }
  69. } else {
  70. GDTCORLogError(GDTCORMCETransformerDoesntImplementTransform,
  71. @"Transformer doesn't implement transform: %@", transformer);
  72. completion(NO, nil);
  73. return;
  74. }
  75. }
  76. id<GDTCORStorageProtocol> storage =
  77. [GDTCORRegistrar sharedInstance].targetToStorage[@(event.target)];
  78. [storage storeEvent:transformedEvent onComplete:hadOriginalCompletion ? completion : nil];
  79. // The work is done, cancel the background task if it's valid.
  80. [[GDTCORApplication sharedApplication] endBackgroundTask:bgID];
  81. bgID = GDTCORBackgroundIdentifierInvalid;
  82. });
  83. }
  84. #pragma mark - GDTCORLifecycleProtocol
  85. - (void)appWillTerminate:(GDTCORApplication *)application {
  86. // Flush the queue immediately.
  87. dispatch_sync(_eventWritingQueue, ^{
  88. });
  89. }
  90. @end