FIROptions.m 13 KB

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