GDTCORPlatform.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. #elif TARGET_OS_WATCH
  25. #import <WatchKit/WatchKit.h>
  26. #endif // TARGET_OS_IOS || TARGET_OS_TV
  27. #if TARGET_OS_IOS
  28. #import <CoreTelephony/CTTelephonyNetworkInfo.h>
  29. #endif
  30. NS_ASSUME_NONNULL_BEGIN
  31. /** The GoogleDataTransport library version. */
  32. FOUNDATION_EXPORT NSString *const kGDTCORVersion;
  33. /** A notification sent out if the app is backgrounding. */
  34. FOUNDATION_EXPORT NSString *const kGDTCORApplicationDidEnterBackgroundNotification;
  35. /** A notification sent out if the app is foregrounding. */
  36. FOUNDATION_EXPORT NSString *const kGDTCORApplicationWillEnterForegroundNotification;
  37. /** A notification sent out if the app is terminating. */
  38. FOUNDATION_EXPORT NSString *const kGDTCORApplicationWillTerminateNotification;
  39. /** The different possible network connection type. */
  40. typedef NS_ENUM(NSInteger, GDTCORNetworkType) {
  41. GDTCORNetworkTypeUNKNOWN = 0,
  42. GDTCORNetworkTypeWIFI = 1,
  43. GDTCORNetworkTypeMobile = 2,
  44. };
  45. /** The different possible network connection mobile subtype. */
  46. typedef NS_ENUM(NSInteger, GDTCORNetworkMobileSubtype) {
  47. GDTCORNetworkMobileSubtypeUNKNOWN = 0,
  48. GDTCORNetworkMobileSubtypeGPRS = 1,
  49. GDTCORNetworkMobileSubtypeEdge = 2,
  50. GDTCORNetworkMobileSubtypeWCDMA = 3,
  51. GDTCORNetworkMobileSubtypeHSDPA = 4,
  52. GDTCORNetworkMobileSubtypeHSUPA = 5,
  53. GDTCORNetworkMobileSubtypeCDMA1x = 6,
  54. GDTCORNetworkMobileSubtypeCDMAEVDORev0 = 7,
  55. GDTCORNetworkMobileSubtypeCDMAEVDORevA = 8,
  56. GDTCORNetworkMobileSubtypeCDMAEVDORevB = 9,
  57. GDTCORNetworkMobileSubtypeHRPD = 10,
  58. GDTCORNetworkMobileSubtypeLTE = 11,
  59. };
  60. #if !TARGET_OS_WATCH
  61. /** Define SCNetworkReachabilityFlags as GDTCORNetworkReachabilityFlags on non-watchOS. */
  62. typedef SCNetworkReachabilityFlags GDTCORNetworkReachabilityFlags;
  63. /** Define SCNetworkReachabilityRef as GDTCORNetworkReachabilityRef on non-watchOS. */
  64. typedef SCNetworkReachabilityRef GDTCORNetworkReachabilityRef;
  65. #else
  66. /** The different possible reachabilityFlags option on watchOS. */
  67. typedef NS_OPTIONS(uint32_t, GDTCORNetworkReachabilityFlags) {
  68. kGDTCORNetworkReachabilityFlagsReachable = 1 << 1,
  69. // TODO(doudounan): Add more options on watchOS if watchOS network connection information relative
  70. // APIs available in the future.
  71. };
  72. /** Define a struct as GDTCORNetworkReachabilityRef on watchOS to store network connection
  73. * information. */
  74. typedef struct {
  75. // TODO(doudounan): Store network connection information on watchOS if watchOS network connection
  76. // information relative APIs available in the future.
  77. } GDTCORNetworkReachabilityRef;
  78. #endif
  79. /** Returns a URL to the root directory under which all GDT-associated data must be saved.
  80. *
  81. * @return A URL to the root directory under which all GDT-associated data must be saved.
  82. */
  83. NSURL *GDTCORRootDirectory(void);
  84. /** Compares flags with the reachable flag (on non-watchos with both reachable and
  85. * connectionRequired flags), if available, and returns YES if network reachable.
  86. *
  87. * @param flags The set of reachability flags.
  88. * @return YES if the network is reachable, NO otherwise.
  89. */
  90. BOOL GDTCORReachabilityFlagsReachable(GDTCORNetworkReachabilityFlags flags);
  91. /** Compares flags with the WWAN reachability flag, if available, and returns YES if present.
  92. *
  93. * @param flags The set of reachability flags.
  94. * @return YES if the WWAN flag is set, NO otherwise.
  95. */
  96. BOOL GDTCORReachabilityFlagsContainWWAN(GDTCORNetworkReachabilityFlags flags);
  97. /** Generates an enum message GDTCORNetworkType representing network connection type.
  98. *
  99. * @return A GDTCORNetworkType representing network connection type.
  100. */
  101. GDTCORNetworkType GDTCORNetworkTypeMessage(void);
  102. /** Generates an enum message GDTCORNetworkMobileSubtype representing network connection mobile
  103. * subtype.
  104. *
  105. * @return A GDTCORNetworkMobileSubtype representing network connection mobile subtype.
  106. */
  107. GDTCORNetworkMobileSubtype GDTCORNetworkMobileSubTypeMessage(void);
  108. /** Identifies the model of the device on which the library is currently working on.
  109. *
  110. * @return A NSString representing the device model.
  111. */
  112. NSString *_Nonnull GDTCORDeviceModel(void);
  113. /** Writes the given object to the given fileURL and populates the given error if it fails.
  114. *
  115. * @param obj The object to encode.
  116. * @param filePath The path to write the object to. Can be nil if you just need the data.
  117. * @param error The error to populate if something goes wrong.
  118. * @return The data of the archive. If error is nil, it's been written to disk.
  119. */
  120. NSData *_Nullable GDTCOREncodeArchive(id<NSSecureCoding> obj,
  121. NSString *_Nullable filePath,
  122. NSError *_Nullable *error);
  123. /** Decodes an object of the given class from the given archive path or data and populates the given
  124. * error if it fails.
  125. *
  126. * @param archiveClass The class of the archive's root object.
  127. * @param archivePath The path to the archived data. Don't use with the archiveData param.
  128. * @param archiveData The data to decode. Don't use with the archivePath param.
  129. * @param error The error to populate if something goes wrong.
  130. */
  131. id<NSSecureCoding> _Nullable GDTCORDecodeArchive(Class archiveClass,
  132. NSString *_Nullable archivePath,
  133. NSData *_Nullable archiveData,
  134. NSError *_Nullable *error);
  135. /** Writes the provided data to a file at the provided path. Intermediate directories will be
  136. * created as needed.
  137. * @param data The file content.
  138. * @param filePath The path to the file to write the provided data.
  139. * @param outError The error to populate if something goes wrong.
  140. * @return `YES` in the case of success, `NO` otherwise.
  141. */
  142. BOOL GDTCORWriteDataToFile(NSData *data, NSString *filePath, NSError *_Nullable *outError);
  143. /** A typedef identify background identifiers. */
  144. typedef volatile NSUInteger GDTCORBackgroundIdentifier;
  145. /** A background task's invalid sentinel value. */
  146. FOUNDATION_EXPORT const GDTCORBackgroundIdentifier GDTCORBackgroundIdentifierInvalid;
  147. #if TARGET_OS_IOS || TARGET_OS_TV
  148. /** A protocol that wraps UIApplicationDelegate, WKExtensionDelegate or NSObject protocol, depending
  149. * on the platform.
  150. */
  151. @protocol GDTCORApplicationDelegate <UIApplicationDelegate>
  152. #elif TARGET_OS_OSX
  153. @protocol GDTCORApplicationDelegate <NSApplicationDelegate>
  154. #elif TARGET_OS_WATCH
  155. @protocol GDTCORApplicationDelegate <WKExtensionDelegate>
  156. #else
  157. @protocol GDTCORApplicationDelegate <NSObject>
  158. #endif // TARGET_OS_IOS || TARGET_OS_TV
  159. @end
  160. @protocol GDTCORApplicationProtocol <NSObject>
  161. @required
  162. /** Flag to determine if the application is running in the background. */
  163. @property(atomic, readonly) BOOL isRunningInBackground;
  164. /** Creates a background task with the returned identifier if on a suitable platform.
  165. *
  166. * @name name The name of the task, useful for debugging which background tasks are running.
  167. * @param handler The handler block that is called if the background task expires.
  168. * @return An identifier for the background task, or GDTCORBackgroundIdentifierInvalid if one
  169. * couldn't be created.
  170. */
  171. - (GDTCORBackgroundIdentifier)beginBackgroundTaskWithName:(NSString *)name
  172. expirationHandler:(void (^__nullable)(void))handler;
  173. /** Ends the background task if the identifier is valid.
  174. *
  175. * @param bgID The background task to end.
  176. */
  177. - (void)endBackgroundTask:(GDTCORBackgroundIdentifier)bgID;
  178. @end
  179. /** A cross-platform application class. */
  180. @interface GDTCORApplication : NSObject <GDTCORApplicationProtocol, GDTCORApplicationDelegate>
  181. /** Creates and/or returns the shared application instance.
  182. *
  183. * @return The shared application instance.
  184. */
  185. + (nullable GDTCORApplication *)sharedApplication;
  186. @end
  187. NS_ASSUME_NONNULL_END