FIROptions.m 15 KB

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