No Description

FIROptions.m 15KB

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