FIROptions.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. // Copyright 2017 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 "Private/FIRAppInternal.h"
  15. #import "Private/FIRBundleUtil.h"
  16. #import "Private/FIRErrors.h"
  17. #import "Private/FIRLogger.h"
  18. #import "Private/FIROptionsInternal.h"
  19. // Keys for the strings in the plist file.
  20. NSString *const kFIRAPIKey = @"API_KEY";
  21. NSString *const kFIRTrackingID = @"TRACKING_ID";
  22. NSString *const kFIRGoogleAppID = @"GOOGLE_APP_ID";
  23. NSString *const kFIRClientID = @"CLIENT_ID";
  24. NSString *const kFIRGCMSenderID = @"GCM_SENDER_ID";
  25. NSString *const kFIRAndroidClientID = @"ANDROID_CLIENT_ID";
  26. NSString *const kFIRDatabaseURL = @"DATABASE_URL";
  27. NSString *const kFIRStorageBucket = @"STORAGE_BUCKET";
  28. // The key to locate the expected bundle identifier in the plist file.
  29. NSString *const kFIRBundleID = @"BUNDLE_ID";
  30. // The key to locate the project identifier in the plist file.
  31. NSString *const kFIRProjectID = @"PROJECT_ID";
  32. NSString *const kFIRIsMeasurementEnabled = @"IS_MEASUREMENT_ENABLED";
  33. NSString *const kFIRIsAnalyticsCollectionEnabled = @"FIREBASE_ANALYTICS_COLLECTION_ENABLED";
  34. NSString *const kFIRIsAnalyticsCollectionDeactivated = @"FIREBASE_ANALYTICS_COLLECTION_DEACTIVATED";
  35. NSString *const kFIRIsAnalyticsEnabled = @"IS_ANALYTICS_ENABLED";
  36. NSString *const kFIRIsSignInEnabled = @"IS_SIGNIN_ENABLED";
  37. // Library version ID.
  38. NSString *const kFIRLibraryVersionID =
  39. @"5" // Major version (one or more digits)
  40. @"01" // Minor version (exactly 2 digits)
  41. @"07" // Build number (exactly 2 digits)
  42. @"000"; // Fixed "000"
  43. // Plist file name.
  44. NSString *const kServiceInfoFileName = @"GoogleService-Info";
  45. // Plist file type.
  46. NSString *const kServiceInfoFileType = @"plist";
  47. // Exception raised from attempting to modify a FIROptions after it's been copied to a FIRApp.
  48. NSString *const kFIRExceptionBadModification =
  49. @"Attempted to modify options after it's set on FIRApp. Please modify all properties before "
  50. @"initializing FIRApp.";
  51. @interface FIROptions ()
  52. /**
  53. * This property maintains the actual configuration key-value pairs.
  54. */
  55. @property(nonatomic, readwrite) NSMutableDictionary *optionsDictionary;
  56. /**
  57. * Calls `analyticsOptionsDictionaryWithInfoDictionary:` using [NSBundle mainBundle].infoDictionary.
  58. * It combines analytics options from both the infoDictionary and the GoogleService-Info.plist.
  59. * Values which are present in the main plist override values from the GoogleService-Info.plist.
  60. */
  61. @property(nonatomic, readonly) NSDictionary *analyticsOptionsDictionary;
  62. /**
  63. * Combination of analytics options from both the infoDictionary and the GoogleService-Info.plist.
  64. * Values which are present in the infoDictionary override values from the GoogleService-Info.plist.
  65. */
  66. - (NSDictionary *)analyticsOptionsDictionaryWithInfoDictionary:(NSDictionary *)infoDictionary;
  67. /**
  68. * Throw exception if editing is locked when attempting to modify an option.
  69. */
  70. - (void)checkEditingLocked;
  71. @end
  72. @implementation FIROptions {
  73. /// Backing variable for self.analyticsOptionsDictionary.
  74. NSDictionary *_analyticsOptionsDictionary;
  75. }
  76. static FIROptions *sDefaultOptions = nil;
  77. static NSDictionary *sDefaultOptionsDictionary = nil;
  78. #pragma mark - Public only for internal class methods
  79. + (FIROptions *)defaultOptions {
  80. if (sDefaultOptions != nil) {
  81. return sDefaultOptions;
  82. }
  83. NSDictionary *defaultOptionsDictionary = [self defaultOptionsDictionary];
  84. if (defaultOptionsDictionary == nil) {
  85. return nil;
  86. }
  87. sDefaultOptions = [[FIROptions alloc] initInternalWithOptionsDictionary:defaultOptionsDictionary];
  88. return sDefaultOptions;
  89. }
  90. #pragma mark - Private class methods
  91. + (void)initialize {
  92. // Report FirebaseCore version for useragent string
  93. NSRange major = NSMakeRange(0, 1);
  94. NSRange minor = NSMakeRange(1, 2);
  95. NSRange patch = NSMakeRange(3, 2);
  96. [FIRApp
  97. registerLibrary:@"fire-ios"
  98. withVersion:[NSString stringWithFormat:@"%@.%d.%d",
  99. [kFIRLibraryVersionID substringWithRange:major],
  100. [[kFIRLibraryVersionID substringWithRange:minor]
  101. intValue],
  102. [[kFIRLibraryVersionID substringWithRange:patch]
  103. intValue]]];
  104. NSDictionary<NSString *, id> *info = [[NSBundle mainBundle] infoDictionary];
  105. NSString *xcodeVersion = info[@"DTXcodeBuild"];
  106. NSString *sdkVersion = info[@"DTSDKBuild"];
  107. if (xcodeVersion) {
  108. [FIRApp registerLibrary:@"xcode" withVersion:xcodeVersion];
  109. }
  110. if (sdkVersion) {
  111. [FIRApp registerLibrary:@"apple-sdk" withVersion:sdkVersion];
  112. }
  113. }
  114. + (NSDictionary *)defaultOptionsDictionary {
  115. if (sDefaultOptionsDictionary != nil) {
  116. return sDefaultOptionsDictionary;
  117. }
  118. NSString *plistFilePath = [FIROptions plistFilePathWithName:kServiceInfoFileName];
  119. if (plistFilePath == nil) {
  120. return nil;
  121. }
  122. sDefaultOptionsDictionary = [NSDictionary dictionaryWithContentsOfFile:plistFilePath];
  123. if (sDefaultOptionsDictionary == nil) {
  124. FIRLogError(kFIRLoggerCore, @"I-COR000011",
  125. @"The configuration file is not a dictionary: "
  126. @"'%@.%@'.",
  127. kServiceInfoFileName, kServiceInfoFileType);
  128. }
  129. return sDefaultOptionsDictionary;
  130. }
  131. // Returns the path of the plist file with a given file name.
  132. + (NSString *)plistFilePathWithName:(NSString *)fileName {
  133. NSArray *bundles = [FIRBundleUtil relevantBundles];
  134. NSString *plistFilePath =
  135. [FIRBundleUtil optionsDictionaryPathWithResourceName:fileName
  136. andFileType:kServiceInfoFileType
  137. inBundles:bundles];
  138. if (plistFilePath == nil) {
  139. FIRLogError(kFIRLoggerCore, @"I-COR000012", @"Could not locate configuration file: '%@.%@'.",
  140. fileName, kServiceInfoFileType);
  141. }
  142. return plistFilePath;
  143. }
  144. + (void)resetDefaultOptions {
  145. sDefaultOptions = nil;
  146. sDefaultOptionsDictionary = nil;
  147. }
  148. #pragma mark - Private instance methods
  149. - (instancetype)initInternalWithOptionsDictionary:(NSDictionary *)optionsDictionary {
  150. self = [super init];
  151. if (self) {
  152. _optionsDictionary = [optionsDictionary mutableCopy];
  153. _usingOptionsFromDefaultPlist = YES;
  154. }
  155. return self;
  156. }
  157. - (id)copyWithZone:(NSZone *)zone {
  158. FIROptions *newOptions = [[[self class] allocWithZone:zone] init];
  159. if (newOptions) {
  160. newOptions.optionsDictionary = self.optionsDictionary;
  161. newOptions.deepLinkURLScheme = self.deepLinkURLScheme;
  162. newOptions.editingLocked = self.isEditingLocked;
  163. newOptions.usingOptionsFromDefaultPlist = self.usingOptionsFromDefaultPlist;
  164. }
  165. return newOptions;
  166. }
  167. #pragma mark - Public instance methods
  168. - (instancetype)initWithContentsOfFile:(NSString *)plistPath {
  169. self = [super init];
  170. if (self) {
  171. if (plistPath == nil) {
  172. FIRLogError(kFIRLoggerCore, @"I-COR000013", @"The plist file path is nil.");
  173. return nil;
  174. }
  175. _optionsDictionary = [[NSDictionary dictionaryWithContentsOfFile:plistPath] mutableCopy];
  176. if (_optionsDictionary == nil) {
  177. FIRLogError(kFIRLoggerCore, @"I-COR000014",
  178. @"The configuration file at %@ does not exist or "
  179. @"is not a well-formed plist file.",
  180. plistPath);
  181. return nil;
  182. }
  183. // TODO: Do we want to validate the dictionary here? It says we do that already in
  184. // the public header.
  185. }
  186. return self;
  187. }
  188. - (instancetype)initWithGoogleAppID:(NSString *)googleAppID GCMSenderID:(NSString *)GCMSenderID {
  189. self = [super init];
  190. if (self) {
  191. NSMutableDictionary *mutableOptionsDict = [NSMutableDictionary dictionary];
  192. [mutableOptionsDict setValue:googleAppID forKey:kFIRGoogleAppID];
  193. [mutableOptionsDict setValue:GCMSenderID forKey:kFIRGCMSenderID];
  194. [mutableOptionsDict setValue:[[NSBundle mainBundle] bundleIdentifier] forKey:kFIRBundleID];
  195. self.optionsDictionary = mutableOptionsDict;
  196. }
  197. return self;
  198. }
  199. - (NSString *)APIKey {
  200. return self.optionsDictionary[kFIRAPIKey];
  201. }
  202. - (void)checkEditingLocked {
  203. if (self.isEditingLocked) {
  204. [NSException raise:kFirebaseCoreErrorDomain format:kFIRExceptionBadModification];
  205. }
  206. }
  207. - (void)setAPIKey:(NSString *)APIKey {
  208. [self checkEditingLocked];
  209. _optionsDictionary[kFIRAPIKey] = [APIKey copy];
  210. }
  211. - (NSString *)clientID {
  212. return self.optionsDictionary[kFIRClientID];
  213. }
  214. - (void)setClientID:(NSString *)clientID {
  215. [self checkEditingLocked];
  216. _optionsDictionary[kFIRClientID] = [clientID copy];
  217. }
  218. - (NSString *)trackingID {
  219. return self.optionsDictionary[kFIRTrackingID];
  220. }
  221. - (void)setTrackingID:(NSString *)trackingID {
  222. [self checkEditingLocked];
  223. _optionsDictionary[kFIRTrackingID] = [trackingID copy];
  224. }
  225. - (NSString *)GCMSenderID {
  226. return self.optionsDictionary[kFIRGCMSenderID];
  227. }
  228. - (void)setGCMSenderID:(NSString *)GCMSenderID {
  229. [self checkEditingLocked];
  230. _optionsDictionary[kFIRGCMSenderID] = [GCMSenderID copy];
  231. }
  232. - (NSString *)projectID {
  233. return self.optionsDictionary[kFIRProjectID];
  234. }
  235. - (void)setProjectID:(NSString *)projectID {
  236. [self checkEditingLocked];
  237. _optionsDictionary[kFIRProjectID] = [projectID copy];
  238. }
  239. - (NSString *)androidClientID {
  240. return self.optionsDictionary[kFIRAndroidClientID];
  241. }
  242. - (void)setAndroidClientID:(NSString *)androidClientID {
  243. [self checkEditingLocked];
  244. _optionsDictionary[kFIRAndroidClientID] = [androidClientID copy];
  245. }
  246. - (NSString *)googleAppID {
  247. return self.optionsDictionary[kFIRGoogleAppID];
  248. }
  249. - (void)setGoogleAppID:(NSString *)googleAppID {
  250. [self checkEditingLocked];
  251. _optionsDictionary[kFIRGoogleAppID] = [googleAppID copy];
  252. }
  253. - (NSString *)libraryVersionID {
  254. return kFIRLibraryVersionID;
  255. }
  256. - (void)setLibraryVersionID:(NSString *)libraryVersionID {
  257. _optionsDictionary[kFIRLibraryVersionID] = [libraryVersionID copy];
  258. }
  259. - (NSString *)databaseURL {
  260. return self.optionsDictionary[kFIRDatabaseURL];
  261. }
  262. - (void)setDatabaseURL:(NSString *)databaseURL {
  263. [self checkEditingLocked];
  264. _optionsDictionary[kFIRDatabaseURL] = [databaseURL copy];
  265. }
  266. - (NSString *)storageBucket {
  267. return self.optionsDictionary[kFIRStorageBucket];
  268. }
  269. - (void)setStorageBucket:(NSString *)storageBucket {
  270. [self checkEditingLocked];
  271. _optionsDictionary[kFIRStorageBucket] = [storageBucket copy];
  272. }
  273. - (void)setDeepLinkURLScheme:(NSString *)deepLinkURLScheme {
  274. [self checkEditingLocked];
  275. _deepLinkURLScheme = [deepLinkURLScheme copy];
  276. }
  277. - (NSString *)bundleID {
  278. return self.optionsDictionary[kFIRBundleID];
  279. }
  280. - (void)setBundleID:(NSString *)bundleID {
  281. [self checkEditingLocked];
  282. _optionsDictionary[kFIRBundleID] = [bundleID copy];
  283. }
  284. #pragma mark - Internal instance methods
  285. - (NSDictionary *)analyticsOptionsDictionaryWithInfoDictionary:(NSDictionary *)infoDictionary {
  286. if (_analyticsOptionsDictionary == nil) {
  287. NSMutableDictionary *tempAnalyticsOptions = [[NSMutableDictionary alloc] init];
  288. NSArray *measurementKeys = @[
  289. kFIRIsMeasurementEnabled, kFIRIsAnalyticsCollectionEnabled,
  290. kFIRIsAnalyticsCollectionDeactivated
  291. ];
  292. for (NSString *key in measurementKeys) {
  293. id value = infoDictionary[key] ?: self.optionsDictionary[key] ?: nil;
  294. if (!value) {
  295. continue;
  296. }
  297. tempAnalyticsOptions[key] = value;
  298. }
  299. _analyticsOptionsDictionary = tempAnalyticsOptions;
  300. }
  301. return _analyticsOptionsDictionary;
  302. }
  303. - (NSDictionary *)analyticsOptionsDictionary {
  304. return [self analyticsOptionsDictionaryWithInfoDictionary:[NSBundle mainBundle].infoDictionary];
  305. }
  306. /**
  307. * Whether or not Measurement was enabled. Measurement is enabled unless explicitly disabled in
  308. * GoogleService-Info.plist. This uses the old plist flag IS_MEASUREMENT_ENABLED, which should still
  309. * be supported.
  310. */
  311. - (BOOL)isMeasurementEnabled {
  312. if (self.isAnalyticsCollectionDeactivated) {
  313. return NO;
  314. }
  315. NSNumber *value = self.analyticsOptionsDictionary[kFIRIsMeasurementEnabled];
  316. if (value == nil) {
  317. // TODO: This could probably be cleaned up since FIROptions shouldn't know about FIRApp or have
  318. // to check if it's the default app. The FIROptions instance can't be modified after
  319. // `+configure` is called, so it's not a good place to copy it either in case the flag is
  320. // changed at runtime.
  321. // If no values are set for Analytics, fall back to the global collection switch in FIRApp.
  322. // Analytics only supports the default FIRApp, so check that first.
  323. if (![FIRApp isDefaultAppConfigured]) {
  324. return NO;
  325. }
  326. // Fall back to the default app's collection switch when the key is not in the dictionary.
  327. return [FIRApp defaultApp].isDataCollectionDefaultEnabled;
  328. }
  329. return [value boolValue];
  330. }
  331. - (BOOL)isAnalyticsCollectionExpicitlySet {
  332. // If it's de-activated, it classifies as explicity set. If not, it's not a good enough indication
  333. // that the developer wants FirebaseAnalytics enabled so continue checking.
  334. if (self.isAnalyticsCollectionDeactivated) {
  335. return YES;
  336. }
  337. // Check if the current Analytics flag is set.
  338. id collectionEnabledObject = self.analyticsOptionsDictionary[kFIRIsAnalyticsCollectionEnabled];
  339. if (collectionEnabledObject && [collectionEnabledObject isKindOfClass:[NSNumber class]]) {
  340. // It doesn't matter what the value is, it's explicitly set.
  341. return YES;
  342. }
  343. // Check if the old measurement flag is set.
  344. id measurementEnabledObject = self.analyticsOptionsDictionary[kFIRIsMeasurementEnabled];
  345. if (measurementEnabledObject && [measurementEnabledObject isKindOfClass:[NSNumber class]]) {
  346. // It doesn't matter what the value is, it's explicitly set.
  347. return YES;
  348. }
  349. // No flags are set to explicitly enable or disable FirebaseAnalytics.
  350. return NO;
  351. }
  352. - (BOOL)isAnalyticsCollectionEnabled {
  353. if (self.isAnalyticsCollectionDeactivated) {
  354. return NO;
  355. }
  356. NSNumber *value = self.analyticsOptionsDictionary[kFIRIsAnalyticsCollectionEnabled];
  357. if (value == nil) {
  358. return self.isMeasurementEnabled; // Fall back to older plist flag.
  359. }
  360. return [value boolValue];
  361. }
  362. - (BOOL)isAnalyticsCollectionDeactivated {
  363. NSNumber *value = self.analyticsOptionsDictionary[kFIRIsAnalyticsCollectionDeactivated];
  364. if (value == nil) {
  365. return NO; // Analytics Collection is not deactivated when the key is not in the dictionary.
  366. }
  367. return [value boolValue];
  368. }
  369. - (BOOL)isAnalyticsEnabled {
  370. return [self.optionsDictionary[kFIRIsAnalyticsEnabled] boolValue];
  371. }
  372. - (BOOL)isSignInEnabled {
  373. return [self.optionsDictionary[kFIRIsSignInEnabled] boolValue];
  374. }
  375. @end