You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

445 lines
15 KiB

6 years ago
  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. @"5" // Major version (one or more digits)
  40. @"00" // Minor version (exactly 2 digits)
  41. @"02" // 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)initialize {
  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)initWithContentsOfFile:(NSString *)plistPath {
  170. self = [super init];
  171. if (self) {
  172. if (plistPath == nil) {
  173. FIRLogError(kFIRLoggerCore, @"I-COR000013", @"The plist file path is nil.");
  174. return nil;
  175. }
  176. _optionsDictionary = [[NSDictionary dictionaryWithContentsOfFile:plistPath] mutableCopy];
  177. if (_optionsDictionary == nil) {
  178. FIRLogError(kFIRLoggerCore, @"I-COR000014",
  179. @"The configuration file at %@ does not exist or "
  180. @"is not a well-formed plist file.",
  181. plistPath);
  182. return nil;
  183. }
  184. // TODO: Do we want to validate the dictionary here? It says we do that already in
  185. // the public header.
  186. }
  187. return self;
  188. }
  189. - (instancetype)initWithGoogleAppID:(NSString *)googleAppID 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)checkEditingLocked {
  204. if (self.isEditingLocked) {
  205. [NSException raise:kFirebaseCoreErrorDomain format:kFIRExceptionBadModification];
  206. }
  207. }
  208. - (void)setAPIKey:(NSString *)APIKey {
  209. [self checkEditingLocked];
  210. _optionsDictionary[kFIRAPIKey] = [APIKey copy];
  211. }
  212. - (NSString *)clientID {
  213. return self.optionsDictionary[kFIRClientID];
  214. }
  215. - (void)setClientID:(NSString *)clientID {
  216. [self checkEditingLocked];
  217. _optionsDictionary[kFIRClientID] = [clientID copy];
  218. }
  219. - (NSString *)trackingID {
  220. return self.optionsDictionary[kFIRTrackingID];
  221. }
  222. - (void)setTrackingID:(NSString *)trackingID {
  223. [self checkEditingLocked];
  224. _optionsDictionary[kFIRTrackingID] = [trackingID copy];
  225. }
  226. - (NSString *)GCMSenderID {
  227. return self.optionsDictionary[kFIRGCMSenderID];
  228. }
  229. - (void)setGCMSenderID:(NSString *)GCMSenderID {
  230. [self checkEditingLocked];
  231. _optionsDictionary[kFIRGCMSenderID] = [GCMSenderID copy];
  232. }
  233. - (NSString *)projectID {
  234. return self.optionsDictionary[kFIRProjectID];
  235. }
  236. - (void)setProjectID:(NSString *)projectID {
  237. [self checkEditingLocked];
  238. _optionsDictionary[kFIRProjectID] = [projectID copy];
  239. }
  240. - (NSString *)androidClientID {
  241. return self.optionsDictionary[kFIRAndroidClientID];
  242. }
  243. - (void)setAndroidClientID:(NSString *)androidClientID {
  244. [self checkEditingLocked];
  245. _optionsDictionary[kFIRAndroidClientID] = [androidClientID copy];
  246. }
  247. - (NSString *)googleAppID {
  248. return self.optionsDictionary[kFIRGoogleAppID];
  249. }
  250. - (void)setGoogleAppID:(NSString *)googleAppID {
  251. [self checkEditingLocked];
  252. _optionsDictionary[kFIRGoogleAppID] = [googleAppID copy];
  253. }
  254. - (NSString *)libraryVersionID {
  255. return kFIRLibraryVersionID;
  256. }
  257. - (void)setLibraryVersionID:(NSString *)libraryVersionID {
  258. _optionsDictionary[kFIRLibraryVersionID] = [libraryVersionID copy];
  259. }
  260. - (NSString *)databaseURL {
  261. return self.optionsDictionary[kFIRDatabaseURL];
  262. }
  263. - (void)setDatabaseURL:(NSString *)databaseURL {
  264. [self checkEditingLocked];
  265. _optionsDictionary[kFIRDatabaseURL] = [databaseURL copy];
  266. }
  267. - (NSString *)storageBucket {
  268. return self.optionsDictionary[kFIRStorageBucket];
  269. }
  270. - (void)setStorageBucket:(NSString *)storageBucket {
  271. [self checkEditingLocked];
  272. _optionsDictionary[kFIRStorageBucket] = [storageBucket copy];
  273. }
  274. - (void)setDeepLinkURLScheme:(NSString *)deepLinkURLScheme {
  275. [self checkEditingLocked];
  276. _deepLinkURLScheme = [deepLinkURLScheme copy];
  277. }
  278. - (NSString *)bundleID {
  279. return self.optionsDictionary[kFIRBundleID];
  280. }
  281. - (void)setBundleID:(NSString *)bundleID {
  282. [self checkEditingLocked];
  283. _optionsDictionary[kFIRBundleID] = [bundleID copy];
  284. }
  285. #pragma mark - Internal instance methods
  286. - (NSDictionary *)analyticsOptionsDictionaryWithInfoDictionary:(NSDictionary *)infoDictionary {
  287. dispatch_once(&_createAnalyticsOptionsDictionaryOnce, ^{
  288. NSMutableDictionary *tempAnalyticsOptions = [[NSMutableDictionary alloc] init];
  289. NSArray *measurementKeys = @[
  290. kFIRIsMeasurementEnabled, kFIRIsAnalyticsCollectionEnabled,
  291. kFIRIsAnalyticsCollectionDeactivated
  292. ];
  293. for (NSString *key in measurementKeys) {
  294. id value = infoDictionary[key] ?: self.optionsDictionary[key] ?: nil;
  295. if (!value) {
  296. continue;
  297. }
  298. tempAnalyticsOptions[key] = value;
  299. }
  300. self->_analyticsOptionsDictionary = tempAnalyticsOptions;
  301. });
  302. return _analyticsOptionsDictionary;
  303. }
  304. - (NSDictionary *)analyticsOptionsDictionary {
  305. return [self analyticsOptionsDictionaryWithInfoDictionary:[NSBundle mainBundle].infoDictionary];
  306. }
  307. /**
  308. * Whether or not Measurement was enabled. Measurement is enabled unless explicitly disabled in
  309. * GoogleService-Info.plist. This uses the old plist flag IS_MEASUREMENT_ENABLED, which should still
  310. * be supported.
  311. */
  312. - (BOOL)isMeasurementEnabled {
  313. if (self.isAnalyticsCollectionDeactivated) {
  314. return NO;
  315. }
  316. NSNumber *value = self.analyticsOptionsDictionary[kFIRIsMeasurementEnabled];
  317. if (value == nil) {
  318. // TODO: This could probably be cleaned up since FIROptions shouldn't know about FIRApp or have
  319. // to check if it's the default app. The FIROptions instance can't be modified after
  320. // `+configure` is called, so it's not a good place to copy it either in case the flag is
  321. // changed at runtime.
  322. // If no values are set for Analytics, fall back to the global collection switch in FIRApp.
  323. // Analytics only supports the default FIRApp, so check that first.
  324. if (![FIRApp isDefaultAppConfigured]) {
  325. return NO;
  326. }
  327. // Fall back to the default app's collection switch when the key is not in the dictionary.
  328. return [FIRApp defaultApp].automaticDataCollectionEnabled;
  329. }
  330. return [value boolValue];
  331. }
  332. - (BOOL)isAnalyticsCollectionExpicitlySet {
  333. // If it's de-activated, it classifies as explicity set. If not, it's not a good enough indication
  334. // that the developer wants FirebaseAnalytics enabled so continue checking.
  335. if (self.isAnalyticsCollectionDeactivated) {
  336. return YES;
  337. }
  338. // Check if the current Analytics flag is set.
  339. id collectionEnabledObject = self.analyticsOptionsDictionary[kFIRIsAnalyticsCollectionEnabled];
  340. if (collectionEnabledObject && [collectionEnabledObject isKindOfClass:[NSNumber class]]) {
  341. // It doesn't matter what the value is, it's explicitly set.
  342. return YES;
  343. }
  344. // Check if the old measurement flag is set.
  345. id measurementEnabledObject = self.analyticsOptionsDictionary[kFIRIsMeasurementEnabled];
  346. if (measurementEnabledObject && [measurementEnabledObject isKindOfClass:[NSNumber class]]) {
  347. // It doesn't matter what the value is, it's explicitly set.
  348. return YES;
  349. }
  350. // No flags are set to explicitly enable or disable FirebaseAnalytics.
  351. return NO;
  352. }
  353. - (BOOL)isAnalyticsCollectionEnabled {
  354. if (self.isAnalyticsCollectionDeactivated) {
  355. return NO;
  356. }
  357. NSNumber *value = self.analyticsOptionsDictionary[kFIRIsAnalyticsCollectionEnabled];
  358. if (value == nil) {
  359. return self.isMeasurementEnabled; // Fall back to older plist flag.
  360. }
  361. return [value boolValue];
  362. }
  363. - (BOOL)isAnalyticsCollectionDeactivated {
  364. NSNumber *value = self.analyticsOptionsDictionary[kFIRIsAnalyticsCollectionDeactivated];
  365. if (value == nil) {
  366. return NO; // Analytics Collection is not deactivated when the key is not in the dictionary.
  367. }
  368. return [value boolValue];
  369. }
  370. - (BOOL)isAnalyticsEnabled {
  371. return [self.optionsDictionary[kFIRIsAnalyticsEnabled] boolValue];
  372. }
  373. - (BOOL)isSignInEnabled {
  374. return [self.optionsDictionary[kFIRIsSignInEnabled] boolValue];
  375. }
  376. @end