FIROptions.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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/FIRBundleUtil.h"
  15. #import "Private/FIRErrors.h"
  16. #import "Private/FIRLogger.h"
  17. #import "Private/FIROptionsInternal.h"
  18. // Keys for the strings in the plist file.
  19. NSString *const kFIRAPIKey = @"API_KEY";
  20. NSString *const kFIRTrackingID = @"TRACKING_ID";
  21. NSString *const kFIRGoogleAppID = @"GOOGLE_APP_ID";
  22. NSString *const kFIRClientID = @"CLIENT_ID";
  23. NSString *const kFIRGCMSenderID = @"GCM_SENDER_ID";
  24. NSString *const kFIRAndroidClientID = @"ANDROID_CLIENT_ID";
  25. NSString *const kFIRDatabaseURL = @"DATABASE_URL";
  26. NSString *const kFIRStorageBucket = @"STORAGE_BUCKET";
  27. // The key to locate the expected bundle identifier in the plist file.
  28. NSString *const kFIRBundleID = @"BUNDLE_ID";
  29. // The key to locate the project identifier in the plist file.
  30. NSString *const kFIRProjectID = @"PROJECT_ID";
  31. NSString *const kFIRIsMeasurementEnabled = @"IS_MEASUREMENT_ENABLED";
  32. NSString *const kFIRIsAnalyticsCollectionEnabled = @"FIREBASE_ANALYTICS_COLLECTION_ENABLED";
  33. NSString *const kFIRIsAnalyticsCollectionDeactivated = @"FIREBASE_ANALYTICS_COLLECTION_DEACTIVATED";
  34. NSString *const kFIRIsAnalyticsEnabled = @"IS_ANALYTICS_ENABLED";
  35. NSString *const kFIRIsSignInEnabled = @"IS_SIGNIN_ENABLED";
  36. // Library version ID.
  37. NSString *const kFIRLibraryVersionID =
  38. @"4" // Major version (one or more digits)
  39. @"00" // Minor version (exactly 2 digits)
  40. @"03" // 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. * Combination of analytics options from both the main plist and the GoogleService-info.plist.
  57. * Values which are present in the main plist override values from the GoogleService-info.plist.
  58. */
  59. @property(nonatomic, readonly) NSDictionary *analyticsOptionsDictionary;
  60. @end
  61. @implementation FIROptions {
  62. /// Backing variable for self.analyticsOptionsDictionary.
  63. NSDictionary *_analyticsOptionsDictionary;
  64. dispatch_once_t _createAnalyticsOptionsDictionaryOnce;
  65. }
  66. static FIROptions *sDefaultOptions = nil;
  67. static NSDictionary *sDefaultOptionsDictionary = nil;
  68. #pragma mark - Public only for internal class methods
  69. + (FIROptions *)defaultOptions {
  70. if (sDefaultOptions != nil) {
  71. return sDefaultOptions;
  72. }
  73. NSDictionary *defaultOptionsDictionary = [self defaultOptionsDictionary];
  74. if (defaultOptionsDictionary == nil) {
  75. return nil;
  76. }
  77. sDefaultOptions =
  78. [[FIROptions alloc] initInternalWithOptionsDictionary:defaultOptionsDictionary];
  79. return sDefaultOptions;
  80. }
  81. #pragma mark - Private class methods
  82. + (NSDictionary *)defaultOptionsDictionary {
  83. if (sDefaultOptionsDictionary != nil) {
  84. return sDefaultOptionsDictionary;
  85. }
  86. NSString *plistFilePath = [FIROptions plistFilePathWithName:kServiceInfoFileName];
  87. if (plistFilePath == nil) {
  88. return nil;
  89. }
  90. sDefaultOptionsDictionary = [NSDictionary dictionaryWithContentsOfFile:plistFilePath];
  91. if (sDefaultOptionsDictionary == nil) {
  92. FIRLogError(kFIRLoggerCore, @"I-COR000011", @"The configuration file is not a dictionary: "
  93. @"'%@.%@'.", kServiceInfoFileName, kServiceInfoFileType);
  94. }
  95. return sDefaultOptionsDictionary;
  96. }
  97. // Returns the path of the plist file with a given file name.
  98. + (NSString *)plistFilePathWithName:(NSString *)fileName {
  99. NSArray *bundles = [FIRBundleUtil relevantBundles];
  100. NSString *plistFilePath =
  101. [FIRBundleUtil optionsDictionaryPathWithResourceName:fileName
  102. andFileType:kServiceInfoFileType
  103. inBundles:bundles];
  104. if (plistFilePath == nil) {
  105. FIRLogError(kFIRLoggerCore, @"I-COR000012", @"Could not locate configuration file: '%@.%@'.",
  106. fileName, kServiceInfoFileType);
  107. }
  108. return plistFilePath;
  109. }
  110. + (void)resetDefaultOptions {
  111. sDefaultOptions = nil;
  112. sDefaultOptionsDictionary = nil;
  113. }
  114. #pragma mark - Private instance methods
  115. - (instancetype)initInternalWithOptionsDictionary:(NSDictionary *)optionsDictionary {
  116. self = [super init];
  117. if (self) {
  118. _optionsDictionary = [optionsDictionary mutableCopy];
  119. _usingOptionsFromDefaultPlist = YES;
  120. }
  121. return self;
  122. }
  123. - (id)copyWithZone:(NSZone *)zone {
  124. FIROptions *newOptions = [[[self class] allocWithZone:zone] init];
  125. if (newOptions) {
  126. newOptions.optionsDictionary = self.optionsDictionary;
  127. newOptions.deepLinkURLScheme = self.deepLinkURLScheme;
  128. newOptions.editingLocked = self.isEditingLocked;
  129. newOptions.usingOptionsFromDefaultPlist = self.usingOptionsFromDefaultPlist;
  130. }
  131. return newOptions;
  132. }
  133. #pragma mark - Public instance methods
  134. - (instancetype)initWithGoogleAppID:(NSString *)googleAppID
  135. bundleID:(NSString *)bundleID
  136. GCMSenderID:(NSString *)GCMSenderID
  137. APIKey:(NSString *)APIKey
  138. clientID:(NSString *)clientID
  139. trackingID:(NSString *)trackingID
  140. androidClientID:(NSString *)androidClientID
  141. databaseURL:(NSString *)databaseURL
  142. storageBucket:(NSString *)storageBucket
  143. deepLinkURLScheme:(NSString *)deepLinkURLScheme {
  144. self = [super init];
  145. if (self) {
  146. if (!googleAppID) {
  147. [NSException raise:kFirebaseCoreErrorDomain format:@"Please specify a valid Google App ID."];
  148. } else if (!GCMSenderID) {
  149. [NSException raise:kFirebaseCoreErrorDomain format:@"Please specify a valid GCM Sender ID."];
  150. }
  151. // `bundleID` is a required property, default to the main `bundleIdentifier` if it's `nil`.
  152. if (!bundleID) {
  153. bundleID = [[NSBundle mainBundle] bundleIdentifier];
  154. }
  155. NSMutableDictionary *mutableOptionsDict = [NSMutableDictionary dictionary];
  156. [mutableOptionsDict setValue:googleAppID forKey:kFIRGoogleAppID];
  157. [mutableOptionsDict setValue:bundleID forKey:kFIRBundleID];
  158. [mutableOptionsDict setValue:GCMSenderID forKey:kFIRGCMSenderID];
  159. [mutableOptionsDict setValue:APIKey forKey:kFIRAPIKey];
  160. [mutableOptionsDict setValue:clientID forKey:kFIRClientID];
  161. [mutableOptionsDict setValue:trackingID forKey:kFIRTrackingID];
  162. [mutableOptionsDict setValue:androidClientID forKey:kFIRAndroidClientID];
  163. [mutableOptionsDict setValue:databaseURL forKey:kFIRDatabaseURL];
  164. [mutableOptionsDict setValue:storageBucket forKey:kFIRStorageBucket];
  165. self.optionsDictionary = mutableOptionsDict;
  166. self.deepLinkURLScheme = deepLinkURLScheme;
  167. }
  168. return self;
  169. }
  170. - (instancetype)initWithContentsOfFile:(NSString *)plistPath {
  171. self = [super init];
  172. if (self) {
  173. if (plistPath == nil) {
  174. FIRLogError(kFIRLoggerCore, @"I-COR000013", @"The plist file path is nil.");
  175. return nil;
  176. }
  177. _optionsDictionary = [[NSDictionary dictionaryWithContentsOfFile:plistPath] mutableCopy];
  178. if (_optionsDictionary == nil) {
  179. FIRLogError(kFIRLoggerCore, @"I-COR000014", @"The configuration file at %@ does not exist or "
  180. @"is not a well-formed plist file.", plistPath);
  181. return nil;
  182. }
  183. // TODO: Do we want to validate the dictionary here? It says we do that already in
  184. // the public header.
  185. }
  186. return self;
  187. }
  188. - (instancetype)initWithGoogleAppID:(NSString *)googleAppID
  189. GCMSenderID:(NSString *)GCMSenderID {
  190. self = [super init];
  191. if (self) {
  192. NSMutableDictionary *mutableOptionsDict = [NSMutableDictionary dictionary];
  193. [mutableOptionsDict setValue:googleAppID forKey:kFIRGoogleAppID];
  194. [mutableOptionsDict setValue:GCMSenderID forKey:kFIRGCMSenderID];
  195. [mutableOptionsDict setValue:[[NSBundle mainBundle] bundleIdentifier] forKey:kFIRBundleID];
  196. self.optionsDictionary = mutableOptionsDict;
  197. }
  198. return self;
  199. }
  200. - (NSString *)APIKey {
  201. return self.optionsDictionary[kFIRAPIKey];
  202. }
  203. - (void)setAPIKey:(NSString *)APIKey {
  204. if (self.isEditingLocked) {
  205. [NSException raise:kFirebaseCoreErrorDomain format:kFIRExceptionBadModification];
  206. }
  207. _optionsDictionary[kFIRAPIKey] = [APIKey copy];
  208. }
  209. - (NSString *)clientID {
  210. return self.optionsDictionary[kFIRClientID];
  211. }
  212. - (void)setClientID:(NSString *)clientID {
  213. if (self.isEditingLocked) {
  214. [NSException raise:kFirebaseCoreErrorDomain format:kFIRExceptionBadModification];
  215. }
  216. _optionsDictionary[kFIRClientID] = [clientID copy];
  217. }
  218. - (NSString *)trackingID {
  219. return self.optionsDictionary[kFIRTrackingID];
  220. }
  221. - (void)setTrackingID:(NSString *)trackingID {
  222. if (self.isEditingLocked) {
  223. [NSException raise:kFirebaseCoreErrorDomain format:kFIRExceptionBadModification];
  224. }
  225. _optionsDictionary[kFIRTrackingID] = [trackingID copy];
  226. }
  227. - (NSString *)GCMSenderID {
  228. return self.optionsDictionary[kFIRGCMSenderID];
  229. }
  230. - (void)setGCMSenderID:(NSString *)GCMSenderID {
  231. if (self.isEditingLocked) {
  232. [NSException raise:kFirebaseCoreErrorDomain format:kFIRExceptionBadModification];
  233. }
  234. _optionsDictionary[kFIRGCMSenderID] = [GCMSenderID copy];
  235. }
  236. - (NSString *)projectID {
  237. return self.optionsDictionary[kFIRProjectID];
  238. }
  239. - (void)setProjectID:(NSString *)projectID {
  240. if (self.isEditingLocked) {
  241. [NSException raise:kFirebaseCoreErrorDomain format:kFIRExceptionBadModification];
  242. }
  243. _optionsDictionary[kFIRProjectID] = [projectID copy];
  244. }
  245. - (NSString *)androidClientID {
  246. return self.optionsDictionary[kFIRAndroidClientID];
  247. }
  248. - (void)setAndroidClientID:(NSString *)androidClientID {
  249. if (self.isEditingLocked) {
  250. [NSException raise:kFirebaseCoreErrorDomain format:kFIRExceptionBadModification];
  251. }
  252. _optionsDictionary[kFIRAndroidClientID] = [androidClientID copy];
  253. }
  254. - (NSString *)googleAppID {
  255. return self.optionsDictionary[kFIRGoogleAppID];
  256. }
  257. - (void)setGoogleAppID:(NSString *)googleAppID {
  258. if (self.isEditingLocked) {
  259. [NSException raise:kFirebaseCoreErrorDomain format:kFIRExceptionBadModification];
  260. }
  261. _optionsDictionary[kFIRGoogleAppID] = [googleAppID copy];
  262. }
  263. - (NSString *)libraryVersionID {
  264. return kFIRLibraryVersionID;
  265. }
  266. - (void)setLibraryVersionID:(NSString *)libraryVersionID {
  267. _optionsDictionary[kFIRLibraryVersionID] = [libraryVersionID copy];
  268. }
  269. - (NSString *)databaseURL {
  270. return self.optionsDictionary[kFIRDatabaseURL];
  271. }
  272. - (void)setDatabaseURL:(NSString *)databaseURL {
  273. if (self.isEditingLocked) {
  274. [NSException raise:kFirebaseCoreErrorDomain format:kFIRExceptionBadModification];
  275. }
  276. _optionsDictionary[kFIRDatabaseURL] = [databaseURL copy];
  277. }
  278. - (NSString *)storageBucket {
  279. return self.optionsDictionary[kFIRStorageBucket];
  280. }
  281. - (void)setStorageBucket:(NSString *)storageBucket {
  282. if (self.isEditingLocked) {
  283. [NSException raise:kFirebaseCoreErrorDomain format:kFIRExceptionBadModification];
  284. }
  285. _optionsDictionary[kFIRStorageBucket] = [storageBucket copy];
  286. }
  287. - (void)setDeepLinkURLScheme:(NSString *)deepLinkURLScheme {
  288. if (self.isEditingLocked) {
  289. [NSException raise:kFirebaseCoreErrorDomain format:kFIRExceptionBadModification];
  290. }
  291. _deepLinkURLScheme = [deepLinkURLScheme copy];
  292. }
  293. - (NSString *)bundleID {
  294. return self.optionsDictionary[kFIRBundleID];
  295. }
  296. - (void)setBundleID:(NSString *)bundleID {
  297. if (self.isEditingLocked) {
  298. [NSException raise:kFirebaseCoreErrorDomain format:kFIRExceptionBadModification];
  299. }
  300. _optionsDictionary[kFIRBundleID] = [bundleID copy];
  301. }
  302. #pragma mark - Internal instance methods
  303. - (NSDictionary *)analyticsOptionsDictionary {
  304. dispatch_once(&_createAnalyticsOptionsDictionaryOnce, ^{
  305. NSMutableDictionary *tempAnalyticsOptions = [[NSMutableDictionary alloc] init];
  306. NSDictionary *mainInfoDictionary = [NSBundle mainBundle].infoDictionary;
  307. NSArray *measurementKeys = @[ kFIRIsMeasurementEnabled,
  308. kFIRIsAnalyticsCollectionEnabled,
  309. kFIRIsAnalyticsCollectionDeactivated ];
  310. for (NSString *key in measurementKeys) {
  311. id value = mainInfoDictionary[key] ?: self.optionsDictionary[key] ?: nil;
  312. if (!value) {
  313. continue;
  314. }
  315. tempAnalyticsOptions[key] = value;
  316. }
  317. _analyticsOptionsDictionary = tempAnalyticsOptions;
  318. });
  319. return _analyticsOptionsDictionary;
  320. }
  321. /**
  322. * Whether or not Measurement was enabled. Measurement is enabled unless explicitly disabled in
  323. * GoogleService-Info.plist. This uses the old plist flag IS_MEASUREMENT_ENABLED, which should still
  324. * be supported.
  325. */
  326. - (BOOL)isMeasurementEnabled {
  327. if (self.isAnalyticsCollectionDeactivated) {
  328. return NO;
  329. }
  330. if (!self.analyticsOptionsDictionary[kFIRIsMeasurementEnabled]) {
  331. return YES; // Enable Measurement by default when the key is not in the dictionary.
  332. }
  333. return [self.analyticsOptionsDictionary[kFIRIsMeasurementEnabled] boolValue];
  334. }
  335. - (BOOL)isAnalyticsCollectionEnabled {
  336. if (self.isAnalyticsCollectionDeactivated) {
  337. return NO;
  338. }
  339. if (!self.analyticsOptionsDictionary[kFIRIsAnalyticsCollectionEnabled]) {
  340. return self.isMeasurementEnabled; // Fall back to older plist flag.
  341. }
  342. return [self.analyticsOptionsDictionary[kFIRIsAnalyticsCollectionEnabled] boolValue];
  343. }
  344. - (BOOL)isAnalyticsCollectionDeactivated {
  345. if (!self.analyticsOptionsDictionary[kFIRIsAnalyticsCollectionDeactivated]) {
  346. return NO; // Analytics Collection is not deactivated when the key is not in the dictionary.
  347. }
  348. return [self.analyticsOptionsDictionary[kFIRIsAnalyticsCollectionDeactivated] boolValue];
  349. }
  350. - (BOOL)isAnalyticsEnabled {
  351. return [self.optionsDictionary[kFIRIsAnalyticsEnabled] boolValue];
  352. }
  353. - (BOOL)isSignInEnabled {
  354. return [self.optionsDictionary[kFIRIsSignInEnabled] boolValue];
  355. }
  356. @end