FIRCLSApplication.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Copyright 2019 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "Crashlytics/Crashlytics/Components/FIRCLSApplication.h"
  15. #import "Crashlytics/Crashlytics/Components/FIRCLSHost.h"
  16. #import "Crashlytics/Crashlytics/Helpers/FIRCLSUtility.h"
  17. #import <GoogleUtilities/GULAppEnvironmentUtil.h>
  18. #if CLS_TARGET_OS_OSX
  19. #import <AppKit/AppKit.h>
  20. #endif
  21. #if CLS_TARGET_OS_HAS_UIKIT
  22. #import <UIKit/UIKit.h>
  23. #endif
  24. NSString* FIRCLSApplicationGetBundleIdentifier(void) {
  25. return [[[NSBundle mainBundle] bundleIdentifier] stringByReplacingOccurrencesOfString:@"/"
  26. withString:@"_"];
  27. }
  28. NSString* FIRCLSApplicationGetSDKBundleID(void) {
  29. return
  30. [@"com.google.firebase.crashlytics." stringByAppendingString:FIRCLSApplicationGetPlatform()];
  31. }
  32. NSString* FIRCLSApplicationGetPlatform(void) {
  33. #if defined(TARGET_OS_MACCATALYST) && TARGET_OS_MACCATALYST
  34. return @"mac";
  35. #elif TARGET_OS_IOS
  36. return @"ios";
  37. #elif TARGET_OS_OSX
  38. return @"mac";
  39. #elif TARGET_OS_TV
  40. return @"tvos";
  41. #elif TARGET_OS_WATCH
  42. return @"ios"; // TODO: temporarily use iOS until Firebase can add watchos to the backend
  43. #endif
  44. }
  45. NSString* FIRCLSApplicationGetFirebasePlatform(void) {
  46. NSString* firebasePlatform = [GULAppEnvironmentUtil applePlatform];
  47. #if TARGET_OS_IOS
  48. // This check is necessary because iOS-only apps running on iPad
  49. // will report UIUserInterfaceIdiomPhone via UI_USER_INTERFACE_IDIOM().
  50. if ([firebasePlatform isEqualToString:@"ios"] &&
  51. ([[UIDevice currentDevice].model.lowercaseString containsString:@"ipad"] ||
  52. [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)) {
  53. return @"ipados";
  54. }
  55. #endif
  56. return firebasePlatform;
  57. }
  58. // these defaults match the FIRCLSInfoPlist helper in FIRCLSIDEFoundation
  59. NSString* FIRCLSApplicationGetBundleVersion(void) {
  60. return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
  61. }
  62. NSString* FIRCLSApplicationGetShortBundleVersion(void) {
  63. return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
  64. }
  65. NSString* FIRCLSApplicationGetName(void) {
  66. NSString* name;
  67. NSBundle* mainBundle;
  68. mainBundle = [NSBundle mainBundle];
  69. name = [mainBundle objectForInfoDictionaryKey:@"CFBundleDisplayName"];
  70. if (name) {
  71. return name;
  72. }
  73. name = [mainBundle objectForInfoDictionaryKey:@"CFBundleName"];
  74. if (name) {
  75. return name;
  76. }
  77. return FIRCLSApplicationGetBundleVersion();
  78. }
  79. BOOL FIRCLSApplicationHasAppStoreReceipt(void) {
  80. NSURL* url = NSBundle.mainBundle.appStoreReceiptURL;
  81. return [NSFileManager.defaultManager fileExistsAtPath:[url path]];
  82. }
  83. FIRCLSApplicationInstallationSourceType FIRCLSApplicationInstallationSource(void) {
  84. if (FIRCLSApplicationHasAppStoreReceipt()) {
  85. return FIRCLSApplicationInstallationSourceTypeAppStore;
  86. }
  87. return FIRCLSApplicationInstallationSourceTypeDeveloperInstall;
  88. }
  89. BOOL FIRCLSApplicationIsExtension(void) {
  90. return FIRCLSApplicationExtensionPointIdentifier() != nil;
  91. }
  92. NSString* FIRCLSApplicationExtensionPointIdentifier(void) {
  93. id extensionDict = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"NSExtension"];
  94. if (!extensionDict) {
  95. return nil;
  96. }
  97. if (![extensionDict isKindOfClass:[NSDictionary class]]) {
  98. FIRCLSSDKLog("Error: NSExtension Info.plist entry is mal-formed\n");
  99. return nil;
  100. }
  101. id typeValue = [(NSDictionary*)extensionDict objectForKey:@"NSExtensionPointIdentifier"];
  102. if (![typeValue isKindOfClass:[NSString class]]) {
  103. FIRCLSSDKLog("Error: NSExtensionPointIdentifier Info.plist entry is mal-formed\n");
  104. return nil;
  105. }
  106. return typeValue;
  107. }
  108. #if CLS_TARGET_OS_HAS_UIKIT
  109. UIApplication* FIRCLSApplicationSharedInstance(void) {
  110. if (FIRCLSApplicationIsExtension()) {
  111. return nil;
  112. }
  113. return [[UIApplication class] performSelector:@selector(sharedApplication)];
  114. }
  115. #elif CLS_TARGET_OS_OSX
  116. id FIRCLSApplicationSharedInstance(void) {
  117. return [NSClassFromString(@"NSApplication") sharedApplication];
  118. }
  119. #else
  120. id FIRCLSApplicationSharedInstance(void) {
  121. return nil; // FIXME: what do we actually return for watch?
  122. }
  123. #endif
  124. void FIRCLSApplicationOpenURL(NSURL* url,
  125. NSExtensionContext* extensionContext,
  126. void (^completionBlock)(BOOL success)) {
  127. if (extensionContext) {
  128. [extensionContext openURL:url completionHandler:completionBlock];
  129. return;
  130. }
  131. BOOL result = NO;
  132. #if TARGET_OS_IOS
  133. // What's going on here is the value returned is a scalar, but we really need an object to
  134. // call this dynamically. Hoops must be jumped.
  135. NSInvocationOperation* op =
  136. [[NSInvocationOperation alloc] initWithTarget:FIRCLSApplicationSharedInstance()
  137. selector:@selector(openURL:)
  138. object:url];
  139. [op start];
  140. [op.result getValue:&result];
  141. #elif CLS_TARGET_OS_OSX
  142. result = [[NSClassFromString(@"NSWorkspace") sharedWorkspace] openURL:url];
  143. #endif
  144. completionBlock(result);
  145. }
  146. id<NSObject> FIRCLSApplicationBeginActivity(NSActivityOptions options, NSString* reason) {
  147. if ([[NSProcessInfo processInfo] respondsToSelector:@selector(beginActivityWithOptions:
  148. reason:)]) {
  149. return [[NSProcessInfo processInfo] beginActivityWithOptions:options reason:reason];
  150. }
  151. #if CLS_TARGET_OS_OSX
  152. if (options & NSActivitySuddenTerminationDisabled) {
  153. [[NSProcessInfo processInfo] disableSuddenTermination];
  154. }
  155. if (options & NSActivityAutomaticTerminationDisabled) {
  156. [[NSProcessInfo processInfo] disableAutomaticTermination:reason];
  157. }
  158. #endif
  159. // encode the options, so we can undo our work later
  160. return @{@"options" : @(options), @"reason" : reason};
  161. }
  162. void FIRCLSApplicationEndActivity(id<NSObject> activity) {
  163. if (!activity) {
  164. return;
  165. }
  166. if ([[NSProcessInfo processInfo] respondsToSelector:@selector(endActivity:)]) {
  167. [[NSProcessInfo processInfo] endActivity:activity];
  168. return;
  169. }
  170. #if CLS_TARGET_OS_OSX
  171. NSInteger options = [[(NSDictionary*)activity objectForKey:@"options"] integerValue];
  172. if (options & NSActivitySuddenTerminationDisabled) {
  173. [[NSProcessInfo processInfo] enableSuddenTermination];
  174. }
  175. if (options & NSActivityAutomaticTerminationDisabled) {
  176. [[NSProcessInfo processInfo]
  177. enableAutomaticTermination:[(NSDictionary*)activity objectForKey:@"reason"]];
  178. }
  179. #endif
  180. }
  181. void FIRCLSApplicationActivity(NSActivityOptions options, NSString* reason, void (^block)(void)) {
  182. id<NSObject> activity = FIRCLSApplicationBeginActivity(options, reason);
  183. block();
  184. FIRCLSApplicationEndActivity(activity);
  185. }