GDTCORTransformer.m 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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:(nonnull void (^)(BOOL wasWritten, NSError *error))completion {
  45. GDTCORAssert(event, @"You can't write a nil event");
  46. if (!completion) {
  47. completion = ^(BOOL wasWritten, NSError *_Nullable error) {
  48. };
  49. }
  50. __block GDTCORBackgroundIdentifier bgID = GDTCORBackgroundIdentifierInvalid;
  51. bgID = [[GDTCORApplication sharedApplication]
  52. beginBackgroundTaskWithName:@"GDTTransformer"
  53. expirationHandler:^{
  54. [[GDTCORApplication sharedApplication] endBackgroundTask:bgID];
  55. bgID = GDTCORBackgroundIdentifierInvalid;
  56. }];
  57. dispatch_async(_eventWritingQueue, ^{
  58. GDTCOREvent *transformedEvent = event;
  59. for (id<GDTCOREventTransformer> transformer in transformers) {
  60. if ([transformer respondsToSelector:@selector(transform:)]) {
  61. GDTCORLogDebug("Applying a transformer to event %@", event);
  62. transformedEvent = [transformer transform:transformedEvent];
  63. if (!transformedEvent) {
  64. completion(NO, nil);
  65. return;
  66. }
  67. } else {
  68. GDTCORLogError(GDTCORMCETransformerDoesntImplementTransform,
  69. @"Transformer doesn't implement transform: %@", transformer);
  70. completion(NO, nil);
  71. return;
  72. }
  73. }
  74. [self.storageInstance storeEvent:transformedEvent onComplete:completion];
  75. // The work is done, cancel the background task if it's valid.
  76. [[GDTCORApplication sharedApplication] endBackgroundTask:bgID];
  77. bgID = GDTCORBackgroundIdentifierInvalid;
  78. });
  79. }
  80. #pragma mark - GDTCORLifecycleProtocol
  81. - (void)appWillTerminate:(GDTCORApplication *)application {
  82. // Flush the queue immediately.
  83. dispatch_sync(_eventWritingQueue, ^{
  84. });
  85. }
  86. @end