FIROptions.m 15 KB

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