GDTCORPlatform.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 <Foundation/Foundation.h>
  17. #if !TARGET_OS_WATCH
  18. #import <SystemConfiguration/SystemConfiguration.h>
  19. #endif
  20. #if TARGET_OS_IOS || TARGET_OS_TV
  21. #import <UIKit/UIKit.h>
  22. #elif TARGET_OS_OSX
  23. #import <AppKit/AppKit.h>
  24. #endif // TARGET_OS_IOS || TARGET_OS_TV
  25. #if TARGET_OS_IOS
  26. #import <CoreTelephony/CTTelephonyNetworkInfo.h>
  27. #endif
  28. NS_ASSUME_NONNULL_BEGIN
  29. /** The GoogleDataTransport library version. */
  30. FOUNDATION_EXPORT NSString *const kGDTCORVersion;
  31. /** A notification sent out if the app is backgrounding. */
  32. FOUNDATION_EXPORT NSString *const kGDTCORApplicationDidEnterBackgroundNotification;
  33. /** A notification sent out if the app is foregrounding. */
  34. FOUNDATION_EXPORT NSString *const kGDTCORApplicationWillEnterForegroundNotification;
  35. /** A notification sent out if the app is terminating. */
  36. FOUNDATION_EXPORT NSString *const kGDTCORApplicationWillTerminateNotification;
  37. /** The different possible network connection type. */
  38. typedef NS_ENUM(NSInteger, GDTCORNetworkType) {
  39. GDTCORNetworkTypeUNKNOWN = 0,
  40. GDTCORNetworkTypeWIFI = 1,
  41. GDTCORNetworkTypeMobile = 2,
  42. };
  43. /** The different possible network connection mobile subtype. */
  44. typedef NS_ENUM(NSInteger, GDTCORNetworkMobileSubtype) {
  45. GDTCORNetworkMobileSubtypeUNKNOWN = 0,
  46. GDTCORNetworkMobileSubtypeGPRS = 1,
  47. GDTCORNetworkMobileSubtypeEdge = 2,
  48. GDTCORNetworkMobileSubtypeWCDMA = 3,
  49. GDTCORNetworkMobileSubtypeHSDPA = 4,
  50. GDTCORNetworkMobileSubtypeHSUPA = 5,
  51. GDTCORNetworkMobileSubtypeCDMA1x = 6,
  52. GDTCORNetworkMobileSubtypeCDMAEVDORev0 = 7,
  53. GDTCORNetworkMobileSubtypeCDMAEVDORevA = 8,
  54. GDTCORNetworkMobileSubtypeCDMAEVDORevB = 9,
  55. GDTCORNetworkMobileSubtypeHRPD = 10,
  56. GDTCORNetworkMobileSubtypeLTE = 11,
  57. };
  58. /** Returns a URL to the root directory under which all GDT-associated data must be saved.
  59. *
  60. * @return A URL to the root directory under which all GDT-associated data must be saved.
  61. */
  62. NSURL *GDTCORRootDirectory(void);
  63. #if !TARGET_OS_WATCH
  64. /** Compares flags with the WWAN reachability flag, if available, and returns YES if present.
  65. *
  66. * @param flags The set of reachability flags.
  67. * @return YES if the WWAN flag is set, NO otherwise.
  68. */
  69. BOOL GDTCORReachabilityFlagsContainWWAN(SCNetworkReachabilityFlags flags);
  70. #endif
  71. /** Generates an enum message GDTCORNetworkType representing network connection type.
  72. *
  73. * @return A GDTCORNetworkType representing network connection type.
  74. */
  75. GDTCORNetworkType GDTCORNetworkTypeMessage(void);
  76. /** Generates an enum message GDTCORNetworkMobileSubtype representing network connection mobile
  77. * subtype.
  78. *
  79. * @return A GDTCORNetworkMobileSubtype representing network connection mobile subtype.
  80. */
  81. GDTCORNetworkMobileSubtype GDTCORNetworkMobileSubTypeMessage(void);
  82. /** Writes the given object to the given fileURL and populates the given error if it fails.
  83. *
  84. * @param obj The object to encode.
  85. * @param filePath The path to write the object to. Can be nil if you just need the data.
  86. * @param error The error to populate if something goes wrong.
  87. * @return The data of the archive. If error is nil, it's been written to disk.
  88. */
  89. NSData *_Nullable GDTCOREncodeArchive(id<NSSecureCoding> obj,
  90. NSString *_Nullable filePath,
  91. NSError *_Nullable *error);
  92. /** Decodes an object of the given class from the given archive path or data and populates the given
  93. * error if it fails.
  94. *
  95. * @param archiveClass The class of the archive's root object.
  96. * @param archivePath The path to the archived data. Don't use with the archiveData param.
  97. * @param archiveData The data to decode. Don't use with the archivePath param.
  98. * @param error The error to populate if something goes wrong.
  99. */
  100. id<NSSecureCoding> _Nullable GDTCORDecodeArchive(Class archiveClass,
  101. NSString *_Nullable archivePath,
  102. NSData *_Nullable archiveData,
  103. NSError *_Nullable *error);
  104. /** A typedef identify background identifiers. */
  105. typedef volatile NSUInteger GDTCORBackgroundIdentifier;
  106. /** A background task's invalid sentinel value. */
  107. FOUNDATION_EXPORT const GDTCORBackgroundIdentifier GDTCORBackgroundIdentifierInvalid;
  108. #if TARGET_OS_IOS || TARGET_OS_TV
  109. /** A protocol that wraps UIApplicationDelegate or NSObject protocol, depending on the platform. */
  110. @protocol GDTCORApplicationDelegate <UIApplicationDelegate>
  111. #elif TARGET_OS_OSX
  112. @protocol GDTCORApplicationDelegate <NSApplicationDelegate>
  113. #else
  114. @protocol GDTCORApplicationDelegate <NSObject>
  115. #endif // TARGET_OS_IOS || TARGET_OS_TV
  116. @end
  117. /** A cross-platform application class. */
  118. @interface GDTCORApplication : NSObject <GDTCORApplicationDelegate>
  119. /** Flag to determine if the application is running in the background. */
  120. @property(atomic, readonly) BOOL isRunningInBackground;
  121. /** Creates and/or returns the shared application instance.
  122. *
  123. * @return The shared application instance.
  124. */
  125. + (nullable GDTCORApplication *)sharedApplication;
  126. /** Creates a background task with the returned identifier if on a suitable platform.
  127. *
  128. * @name name The name of the task, useful for debugging which background tasks are running.
  129. * @param handler The handler block that is called if the background task expires.
  130. * @return An identifier for the background task, or GDTCORBackgroundIdentifierInvalid if one
  131. * couldn't be created.
  132. */
  133. - (GDTCORBackgroundIdentifier)beginBackgroundTaskWithName:(NSString *)name
  134. expirationHandler:(void (^__nullable)(void))handler;
  135. /** Ends the background task if the identifier is valid.
  136. *
  137. * @param bgID The background task to end.
  138. */
  139. - (void)endBackgroundTask:(GDTCORBackgroundIdentifier)bgID;
  140. @end
  141. NS_ASSUME_NONNULL_END