GDTCORTransformer.m 3.4 KB

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