|
| 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 | + |
| 15 | +#import "FIRCLSApplication.h" |
| 16 | + |
| 17 | +#import "FIRCLSHost.h" |
| 18 | +#import "FIRCLSUtility.h" |
| 19 | + |
| 20 | +#if CLS_TARGET_OS_OSX |
| 21 | +#import <AppKit/AppKit.h> |
| 22 | +#endif |
| 23 | + |
| 24 | +#if CLS_TARGET_OS_HAS_UIKIT |
| 25 | +#import <UIKit/UIKit.h> |
| 26 | +#endif |
| 27 | + |
| 28 | +NSString* FIRCLSApplicationGetBundleIdentifier(void) { |
| 29 | + return [[[NSBundle mainBundle] bundleIdentifier] stringByReplacingOccurrencesOfString:@"/" |
| 30 | + withString:@"_"]; |
| 31 | +} |
| 32 | + |
| 33 | +NSString* FIRCLSApplicationGetSDKBundleID(void) { |
| 34 | + return |
| 35 | + [@"com.google.firebase.crashlytics." stringByAppendingString:FIRCLSApplicationGetPlatform()]; |
| 36 | +} |
| 37 | + |
| 38 | +NSString* FIRCLSApplicationGetPlatform(void) { |
| 39 | +#if defined(TARGET_OS_MACCATALYST) && TARGET_OS_MACCATALYST |
| 40 | + return @"mac"; |
| 41 | +#elif TARGET_OS_IOS |
| 42 | + return @"ios"; |
| 43 | +#elif TARGET_OS_OSX |
| 44 | + return @"mac"; |
| 45 | +#elif TARGET_OS_TV |
| 46 | + return @"tvos"; |
| 47 | +#endif |
| 48 | +} |
| 49 | + |
| 50 | +// these defaults match the FIRCLSInfoPlist helper in FIRCLSIDEFoundation |
| 51 | +NSString* FIRCLSApplicationGetBundleVersion(void) { |
| 52 | + return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]; |
| 53 | +} |
| 54 | + |
| 55 | +NSString* FIRCLSApplicationGetShortBundleVersion(void) { |
| 56 | + return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]; |
| 57 | +} |
| 58 | + |
| 59 | +NSString* FIRCLSApplicationGetName(void) { |
| 60 | + NSString* name; |
| 61 | + NSBundle* mainBundle; |
| 62 | + |
| 63 | + mainBundle = [NSBundle mainBundle]; |
| 64 | + |
| 65 | + name = [mainBundle objectForInfoDictionaryKey:@"CFBundleDisplayName"]; |
| 66 | + if (name) { |
| 67 | + return name; |
| 68 | + } |
| 69 | + |
| 70 | + name = [mainBundle objectForInfoDictionaryKey:@"CFBundleName"]; |
| 71 | + if (name) { |
| 72 | + return name; |
| 73 | + } |
| 74 | + |
| 75 | + return FIRCLSApplicationGetBundleVersion(); |
| 76 | +} |
| 77 | + |
| 78 | +BOOL FIRCLSApplicationHasAppStoreReceipt(void) { |
| 79 | + NSURL* url = NSBundle.mainBundle.appStoreReceiptURL; |
| 80 | + return [NSFileManager.defaultManager fileExistsAtPath:[url path]]; |
| 81 | +} |
| 82 | + |
| 83 | +FIRCLSApplicationInstallationSourceType FIRCLSApplicationInstallationSource(void) { |
| 84 | + if (FIRCLSApplicationHasAppStoreReceipt()) { |
| 85 | + return FIRCLSApplicationInstallationSourceTypeAppStore; |
| 86 | + } |
| 87 | + |
| 88 | + return FIRCLSApplicationInstallationSourceTypeDeveloperInstall; |
| 89 | +} |
| 90 | + |
| 91 | +BOOL FIRCLSApplicationIsExtension(void) { |
| 92 | + return FIRCLSApplicationExtensionPointIdentifier() != nil; |
| 93 | +} |
| 94 | + |
| 95 | +NSString* FIRCLSApplicationExtensionPointIdentifier(void) { |
| 96 | + id extensionDict = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"NSExtension"]; |
| 97 | + |
| 98 | + if (!extensionDict) { |
| 99 | + return nil; |
| 100 | + } |
| 101 | + |
| 102 | + if (![extensionDict isKindOfClass:[NSDictionary class]]) { |
| 103 | + FIRCLSSDKLog("Error: NSExtension Info.plist entry is mal-formed\n"); |
| 104 | + return nil; |
| 105 | + } |
| 106 | + |
| 107 | + id typeValue = [(NSDictionary*)extensionDict objectForKey:@"NSExtensionPointIdentifier"]; |
| 108 | + |
| 109 | + if (![typeValue isKindOfClass:[NSString class]]) { |
| 110 | + FIRCLSSDKLog("Error: NSExtensionPointIdentifier Info.plist entry is mal-formed\n"); |
| 111 | + return nil; |
| 112 | + } |
| 113 | + |
| 114 | + return typeValue; |
| 115 | +} |
| 116 | + |
| 117 | +#if CLS_TARGET_OS_HAS_UIKIT |
| 118 | +UIApplication* FIRCLSApplicationSharedInstance(void) { |
| 119 | + if (FIRCLSApplicationIsExtension()) { |
| 120 | + return nil; |
| 121 | + } |
| 122 | + |
| 123 | + return [[UIApplication class] performSelector:@selector(sharedApplication)]; |
| 124 | +} |
| 125 | +#elif CLS_TARGET_OS_OSX |
| 126 | +id FIRCLSApplicationSharedInstance(void) { |
| 127 | + return [NSClassFromString(@"NSApplication") sharedApplication]; |
| 128 | +} |
| 129 | +#else |
| 130 | +id FIRCLSApplicationSharedInstance(void) { |
| 131 | + return nil; // FIXME: what do we actually return for watch? |
| 132 | +} |
| 133 | +#endif |
| 134 | + |
| 135 | +void FIRCLSApplicationOpenURL(NSURL* url, |
| 136 | + NSExtensionContext* extensionContext, |
| 137 | + void (^completionBlock)(BOOL success)) { |
| 138 | + if (extensionContext) { |
| 139 | + [extensionContext openURL:url completionHandler:completionBlock]; |
| 140 | + return; |
| 141 | + } |
| 142 | + |
| 143 | + BOOL result = NO; |
| 144 | + |
| 145 | +#if TARGET_OS_IOS |
| 146 | + // What's going on here is the value returned is a scalar, but we really need an object to |
| 147 | + // call this dynamically. Hoops must be jumped. |
| 148 | + NSInvocationOperation* op = |
| 149 | + [[NSInvocationOperation alloc] initWithTarget:FIRCLSApplicationSharedInstance() |
| 150 | + selector:@selector(openURL:) |
| 151 | + object:url]; |
| 152 | + [op start]; |
| 153 | + [op.result getValue:&result]; |
| 154 | +#elif CLS_TARGET_OS_OSX |
| 155 | + result = [[NSClassFromString(@"NSWorkspace") sharedWorkspace] openURL:url]; |
| 156 | +#endif |
| 157 | + |
| 158 | + completionBlock(result); |
| 159 | +} |
| 160 | + |
| 161 | +id<NSObject> FIRCLSApplicationBeginActivity(NSActivityOptions options, NSString* reason) { |
| 162 | + if ([[NSProcessInfo processInfo] respondsToSelector:@selector(beginActivityWithOptions: |
| 163 | + reason:)]) { |
| 164 | + return [[NSProcessInfo processInfo] beginActivityWithOptions:options reason:reason]; |
| 165 | + } |
| 166 | + |
| 167 | +#if CLS_TARGET_OS_OSX |
| 168 | + if (options & NSActivitySuddenTerminationDisabled) { |
| 169 | + [[NSProcessInfo processInfo] disableSuddenTermination]; |
| 170 | + } |
| 171 | + |
| 172 | + if (options & NSActivityAutomaticTerminationDisabled) { |
| 173 | + [[NSProcessInfo processInfo] disableAutomaticTermination:reason]; |
| 174 | + } |
| 175 | +#endif |
| 176 | + |
| 177 | + // encode the options, so we can undo our work later |
| 178 | + return @{@"options" : @(options), @"reason" : reason}; |
| 179 | +} |
| 180 | + |
| 181 | +void FIRCLSApplicationEndActivity(id<NSObject> activity) { |
| 182 | + if (!activity) { |
| 183 | + return; |
| 184 | + } |
| 185 | + |
| 186 | + if ([[NSProcessInfo processInfo] respondsToSelector:@selector(endActivity:)]) { |
| 187 | + [[NSProcessInfo processInfo] endActivity:activity]; |
| 188 | + return; |
| 189 | + } |
| 190 | + |
| 191 | +#if CLS_TARGET_OS_OSX |
| 192 | + NSInteger options = [[(NSDictionary*)activity objectForKey:@"options"] integerValue]; |
| 193 | + |
| 194 | + if (options & NSActivitySuddenTerminationDisabled) { |
| 195 | + [[NSProcessInfo processInfo] enableSuddenTermination]; |
| 196 | + } |
| 197 | + |
| 198 | + if (options & NSActivityAutomaticTerminationDisabled) { |
| 199 | + [[NSProcessInfo processInfo] |
| 200 | + enableAutomaticTermination:[(NSDictionary*)activity objectForKey:@"reason"]]; |
| 201 | + } |
| 202 | +#endif |
| 203 | +} |
| 204 | + |
| 205 | +void FIRCLSApplicationActivity(NSActivityOptions options, NSString* reason, void (^block)(void)) { |
| 206 | + id<NSObject> activity = FIRCLSApplicationBeginActivity(options, reason); |
| 207 | + |
| 208 | + block(); |
| 209 | + |
| 210 | + FIRCLSApplicationEndActivity(activity); |
| 211 | +} |
0 commit comments