No Description

FIROptions.m 16KB

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