FIROptions.m 14 KB

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