FIROptions.m 17 KB

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