FIROptions.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. + (void)initialize {
  93. // Report FirebaseCore version for useragent string
  94. [FIRApp registerLibrary:@"fire-ios"
  95. withVersion:[NSString stringWithUTF8String:FIRCoreVersionString]];
  96. NSDictionary<NSString *, id> *info = [[NSBundle mainBundle] infoDictionary];
  97. NSString *xcodeVersion = info[@"DTXcodeBuild"];
  98. NSString *sdkVersion = info[@"DTSDKBuild"];
  99. if (xcodeVersion) {
  100. [FIRApp registerLibrary:@"xcode" withVersion:xcodeVersion];
  101. }
  102. if (sdkVersion) {
  103. [FIRApp registerLibrary:@"apple-sdk" withVersion:sdkVersion];
  104. }
  105. }
  106. + (NSDictionary *)defaultOptionsDictionary {
  107. if (sDefaultOptionsDictionary != nil) {
  108. return sDefaultOptionsDictionary;
  109. }
  110. NSString *plistFilePath = [FIROptions plistFilePathWithName:kServiceInfoFileName];
  111. if (plistFilePath == nil) {
  112. return nil;
  113. }
  114. sDefaultOptionsDictionary = [NSDictionary dictionaryWithContentsOfFile:plistFilePath];
  115. if (sDefaultOptionsDictionary == nil) {
  116. FIRLogError(kFIRLoggerCore, @"I-COR000011",
  117. @"The configuration file is not a dictionary: "
  118. @"'%@.%@'.",
  119. kServiceInfoFileName, kServiceInfoFileType);
  120. }
  121. return sDefaultOptionsDictionary;
  122. }
  123. // Returns the path of the plist file with a given file name.
  124. + (NSString *)plistFilePathWithName:(NSString *)fileName {
  125. NSArray *bundles = [FIRBundleUtil relevantBundles];
  126. NSString *plistFilePath =
  127. [FIRBundleUtil optionsDictionaryPathWithResourceName:fileName
  128. andFileType:kServiceInfoFileType
  129. inBundles:bundles];
  130. if (plistFilePath == nil) {
  131. FIRLogError(kFIRLoggerCore, @"I-COR000012", @"Could not locate configuration file: '%@.%@'.",
  132. fileName, kServiceInfoFileType);
  133. }
  134. return plistFilePath;
  135. }
  136. + (void)resetDefaultOptions {
  137. sDefaultOptions = nil;
  138. sDefaultOptionsDictionary = nil;
  139. }
  140. #pragma mark - Private instance methods
  141. - (instancetype)initInternalWithOptionsDictionary:(NSDictionary *)optionsDictionary {
  142. self = [super init];
  143. if (self) {
  144. _optionsDictionary = [optionsDictionary mutableCopy];
  145. _usingOptionsFromDefaultPlist = YES;
  146. }
  147. return self;
  148. }
  149. - (id)copyWithZone:(NSZone *)zone {
  150. FIROptions *newOptions = [[[self class] allocWithZone:zone] init];
  151. if (newOptions) {
  152. newOptions.optionsDictionary = self.optionsDictionary;
  153. newOptions.deepLinkURLScheme = self.deepLinkURLScheme;
  154. newOptions.editingLocked = self.isEditingLocked;
  155. newOptions.usingOptionsFromDefaultPlist = self.usingOptionsFromDefaultPlist;
  156. }
  157. return newOptions;
  158. }
  159. #pragma mark - Public instance methods
  160. - (instancetype)initWithContentsOfFile:(NSString *)plistPath {
  161. self = [super init];
  162. if (self) {
  163. if (plistPath == nil) {
  164. FIRLogError(kFIRLoggerCore, @"I-COR000013", @"The plist file path is nil.");
  165. return nil;
  166. }
  167. _optionsDictionary = [[NSDictionary dictionaryWithContentsOfFile:plistPath] mutableCopy];
  168. if (_optionsDictionary == nil) {
  169. FIRLogError(kFIRLoggerCore, @"I-COR000014",
  170. @"The configuration file at %@ does not exist or "
  171. @"is not a well-formed plist file.",
  172. plistPath);
  173. return nil;
  174. }
  175. // TODO: Do we want to validate the dictionary here? It says we do that already in
  176. // the public header.
  177. }
  178. return self;
  179. }
  180. - (instancetype)initWithGoogleAppID:(NSString *)googleAppID GCMSenderID:(NSString *)GCMSenderID {
  181. self = [super init];
  182. if (self) {
  183. NSMutableDictionary *mutableOptionsDict = [NSMutableDictionary dictionary];
  184. [mutableOptionsDict setValue:googleAppID forKey:kFIRGoogleAppID];
  185. [mutableOptionsDict setValue:GCMSenderID forKey:kFIRGCMSenderID];
  186. [mutableOptionsDict setValue:[[NSBundle mainBundle] bundleIdentifier] forKey:kFIRBundleID];
  187. self.optionsDictionary = mutableOptionsDict;
  188. }
  189. return self;
  190. }
  191. - (NSString *)APIKey {
  192. return self.optionsDictionary[kFIRAPIKey];
  193. }
  194. - (void)checkEditingLocked {
  195. if (self.isEditingLocked) {
  196. [NSException raise:kFirebaseCoreErrorDomain format:kFIRExceptionBadModification];
  197. }
  198. }
  199. - (void)setAPIKey:(NSString *)APIKey {
  200. [self checkEditingLocked];
  201. _optionsDictionary[kFIRAPIKey] = [APIKey copy];
  202. }
  203. - (NSString *)clientID {
  204. return self.optionsDictionary[kFIRClientID];
  205. }
  206. - (void)setClientID:(NSString *)clientID {
  207. [self checkEditingLocked];
  208. _optionsDictionary[kFIRClientID] = [clientID copy];
  209. }
  210. - (NSString *)trackingID {
  211. return self.optionsDictionary[kFIRTrackingID];
  212. }
  213. - (void)setTrackingID:(NSString *)trackingID {
  214. [self checkEditingLocked];
  215. _optionsDictionary[kFIRTrackingID] = [trackingID copy];
  216. }
  217. - (NSString *)GCMSenderID {
  218. return self.optionsDictionary[kFIRGCMSenderID];
  219. }
  220. - (void)setGCMSenderID:(NSString *)GCMSenderID {
  221. [self checkEditingLocked];
  222. _optionsDictionary[kFIRGCMSenderID] = [GCMSenderID copy];
  223. }
  224. - (NSString *)projectID {
  225. return self.optionsDictionary[kFIRProjectID];
  226. }
  227. - (void)setProjectID:(NSString *)projectID {
  228. [self checkEditingLocked];
  229. _optionsDictionary[kFIRProjectID] = [projectID copy];
  230. }
  231. - (NSString *)androidClientID {
  232. return self.optionsDictionary[kFIRAndroidClientID];
  233. }
  234. - (void)setAndroidClientID:(NSString *)androidClientID {
  235. [self checkEditingLocked];
  236. _optionsDictionary[kFIRAndroidClientID] = [androidClientID copy];
  237. }
  238. - (NSString *)googleAppID {
  239. return self.optionsDictionary[kFIRGoogleAppID];
  240. }
  241. - (void)setGoogleAppID:(NSString *)googleAppID {
  242. [self checkEditingLocked];
  243. _optionsDictionary[kFIRGoogleAppID] = [googleAppID copy];
  244. }
  245. - (NSString *)libraryVersionID {
  246. static dispatch_once_t onceToken;
  247. dispatch_once(&onceToken, ^{
  248. // The unit tests are set up to catch anything that does not properly convert.
  249. NSString *version = [NSString stringWithUTF8String:FIRCoreVersionString];
  250. NSArray *components = [version componentsSeparatedByString:@"."];
  251. NSString *major = [components objectAtIndex:0];
  252. NSString *minor = [NSString stringWithFormat:@"%02d", [[components objectAtIndex:1] intValue]];
  253. NSString *patch = [NSString stringWithFormat:@"%02d", [[components objectAtIndex:2] intValue]];
  254. kFIRLibraryVersionID = [NSString stringWithFormat:@"%@%@%@000", major, minor, patch];
  255. });
  256. return kFIRLibraryVersionID;
  257. }
  258. - (void)setLibraryVersionID:(NSString *)libraryVersionID {
  259. _optionsDictionary[kFIRLibraryVersionID] = [libraryVersionID copy];
  260. }
  261. - (NSString *)databaseURL {
  262. return self.optionsDictionary[kFIRDatabaseURL];
  263. }
  264. - (void)setDatabaseURL:(NSString *)databaseURL {
  265. [self checkEditingLocked];
  266. _optionsDictionary[kFIRDatabaseURL] = [databaseURL copy];
  267. }
  268. - (NSString *)storageBucket {
  269. return self.optionsDictionary[kFIRStorageBucket];
  270. }
  271. - (void)setStorageBucket:(NSString *)storageBucket {
  272. [self checkEditingLocked];
  273. _optionsDictionary[kFIRStorageBucket] = [storageBucket copy];
  274. }
  275. - (void)setDeepLinkURLScheme:(NSString *)deepLinkURLScheme {
  276. [self checkEditingLocked];
  277. _deepLinkURLScheme = [deepLinkURLScheme copy];
  278. }
  279. - (NSString *)bundleID {
  280. return self.optionsDictionary[kFIRBundleID];
  281. }
  282. - (void)setBundleID:(NSString *)bundleID {
  283. [self checkEditingLocked];
  284. _optionsDictionary[kFIRBundleID] = [bundleID copy];
  285. }
  286. #pragma mark - Internal instance methods
  287. - (NSDictionary *)analyticsOptionsDictionaryWithInfoDictionary:(NSDictionary *)infoDictionary {
  288. if (_analyticsOptionsDictionary == nil) {
  289. NSMutableDictionary *tempAnalyticsOptions = [[NSMutableDictionary alloc] init];
  290. NSArray *measurementKeys = @[
  291. kFIRIsMeasurementEnabled, kFIRIsAnalyticsCollectionEnabled,
  292. kFIRIsAnalyticsCollectionDeactivated
  293. ];
  294. for (NSString *key in measurementKeys) {
  295. id value = infoDictionary[key] ?: self.optionsDictionary[key] ?: nil;
  296. if (!value) {
  297. continue;
  298. }
  299. tempAnalyticsOptions[key] = value;
  300. }
  301. _analyticsOptionsDictionary = tempAnalyticsOptions;
  302. }
  303. return _analyticsOptionsDictionary;
  304. }
  305. - (NSDictionary *)analyticsOptionsDictionary {
  306. return [self analyticsOptionsDictionaryWithInfoDictionary:[NSBundle mainBundle].infoDictionary];
  307. }
  308. /**
  309. * Whether or not Measurement was enabled. Measurement is enabled unless explicitly disabled in
  310. * GoogleService-Info.plist. This uses the old plist flag IS_MEASUREMENT_ENABLED, which should still
  311. * be supported.
  312. */
  313. - (BOOL)isMeasurementEnabled {
  314. if (self.isAnalyticsCollectionDeactivated) {
  315. return NO;
  316. }
  317. NSNumber *value = self.analyticsOptionsDictionary[kFIRIsMeasurementEnabled];
  318. if (value == nil) {
  319. // TODO: This could probably be cleaned up since FIROptions shouldn't know about FIRApp or have
  320. // to check if it's the default app. The FIROptions instance can't be modified after
  321. // `+configure` is called, so it's not a good place to copy it either in case the flag is
  322. // changed at runtime.
  323. // If no values are set for Analytics, fall back to the global collection switch in FIRApp.
  324. // Analytics only supports the default FIRApp, so check that first.
  325. if (![FIRApp isDefaultAppConfigured]) {
  326. return NO;
  327. }
  328. // Fall back to the default app's collection switch when the key is not in the dictionary.
  329. return [FIRApp defaultApp].isDataCollectionDefaultEnabled;
  330. }
  331. return [value boolValue];
  332. }
  333. - (BOOL)isAnalyticsCollectionExpicitlySet {
  334. // If it's de-activated, it classifies as explicity set. If not, it's not a good enough indication
  335. // that the developer wants FirebaseAnalytics enabled so continue checking.
  336. if (self.isAnalyticsCollectionDeactivated) {
  337. return YES;
  338. }
  339. // Check if the current Analytics flag is set.
  340. id collectionEnabledObject = self.analyticsOptionsDictionary[kFIRIsAnalyticsCollectionEnabled];
  341. if (collectionEnabledObject && [collectionEnabledObject isKindOfClass:[NSNumber class]]) {
  342. // It doesn't matter what the value is, it's explicitly set.
  343. return YES;
  344. }
  345. // Check if the old measurement flag is set.
  346. id measurementEnabledObject = self.analyticsOptionsDictionary[kFIRIsMeasurementEnabled];
  347. if (measurementEnabledObject && [measurementEnabledObject isKindOfClass:[NSNumber class]]) {
  348. // It doesn't matter what the value is, it's explicitly set.
  349. return YES;
  350. }
  351. // No flags are set to explicitly enable or disable FirebaseAnalytics.
  352. return NO;
  353. }
  354. - (BOOL)isAnalyticsCollectionEnabled {
  355. if (self.isAnalyticsCollectionDeactivated) {
  356. return NO;
  357. }
  358. NSNumber *value = self.analyticsOptionsDictionary[kFIRIsAnalyticsCollectionEnabled];
  359. if (value == nil) {
  360. return self.isMeasurementEnabled; // Fall back to older plist flag.
  361. }
  362. return [value boolValue];
  363. }
  364. - (BOOL)isAnalyticsCollectionDeactivated {
  365. NSNumber *value = self.analyticsOptionsDictionary[kFIRIsAnalyticsCollectionDeactivated];
  366. if (value == nil) {
  367. return NO; // Analytics Collection is not deactivated when the key is not in the dictionary.
  368. }
  369. return [value boolValue];
  370. }
  371. - (BOOL)isAnalyticsEnabled {
  372. return [self.optionsDictionary[kFIRIsAnalyticsEnabled] boolValue];
  373. }
  374. - (BOOL)isSignInEnabled {
  375. return [self.optionsDictionary[kFIRIsSignInEnabled] boolValue];
  376. }
  377. @end