FIROptions.m 14 KB

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