FIRMessagingUtilities.m 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FirebaseMessaging/Sources/FIRMessagingUtilities.h"
  17. #import "FirebaseMessaging/Sources/FIRMessagingLogger.h"
  18. #import <GoogleUtilities/GULAppEnvironmentUtil.h>
  19. static const uint64_t kBytesToMegabytesDivisor = 1024 * 1024LL;
  20. #pragma mark - Time
  21. int64_t FIRMessagingCurrentTimestampInSeconds(void) {
  22. return (int64_t)[[NSDate date] timeIntervalSince1970];
  23. }
  24. int64_t FIRMessagingCurrentTimestampInMilliseconds(void) {
  25. return (int64_t)(FIRMessagingCurrentTimestampInSeconds() * 1000.0);
  26. }
  27. #pragma mark - App Info
  28. NSString *FIRMessagingCurrentAppVersion(void) {
  29. NSString *version = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
  30. if (![version length]) {
  31. FIRMessagingLoggerError(kFIRMessagingMessageCodeUtilities000,
  32. @"Could not find current app version");
  33. return @"";
  34. }
  35. return version;
  36. }
  37. NSString *FIRMessagingBundleIDByRemovingLastPartFrom(NSString *bundleID) {
  38. NSString *bundleIDComponentsSeparator = @".";
  39. NSMutableArray<NSString *> *bundleIDComponents =
  40. [[bundleID componentsSeparatedByString:bundleIDComponentsSeparator] mutableCopy];
  41. [bundleIDComponents removeLastObject];
  42. return [bundleIDComponents componentsJoinedByString:bundleIDComponentsSeparator];
  43. }
  44. NSString *FIRMessagingAppIdentifier(void) {
  45. NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
  46. #if TARGET_OS_WATCH
  47. // The code is running in watchKit extension target but the actually bundleID is in the watchKit
  48. // target. So we need to remove the last part of the bundle ID in watchKit extension to match
  49. // the one in watchKit target.
  50. return FIRMessagingBundleIDByRemovingLastPartFrom(bundleID);
  51. #else
  52. return bundleID;
  53. #endif
  54. }
  55. uint64_t FIRMessagingGetFreeDiskSpaceInMB(void) {
  56. NSError *error;
  57. NSArray *paths =
  58. NSSearchPathForDirectoriesInDomains(FIRMessagingSupportedDirectory(), NSUserDomainMask, YES);
  59. NSDictionary *attributesMap =
  60. [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject]
  61. error:&error];
  62. if (attributesMap) {
  63. uint64_t totalSizeInBytes __unused = [attributesMap[NSFileSystemSize] longLongValue];
  64. uint64_t freeSizeInBytes = [attributesMap[NSFileSystemFreeSize] longLongValue];
  65. FIRMessagingLoggerDebug(
  66. kFIRMessagingMessageCodeUtilities001, @"Device has capacity %llu MB with %llu MB free.",
  67. totalSizeInBytes / kBytesToMegabytesDivisor, freeSizeInBytes / kBytesToMegabytesDivisor);
  68. return ((double)freeSizeInBytes) / kBytesToMegabytesDivisor;
  69. } else {
  70. FIRMessagingLoggerError(kFIRMessagingMessageCodeUtilities002,
  71. @"Error in retreiving device's free memory %@", error);
  72. return 0;
  73. }
  74. }
  75. NSSearchPathDirectory FIRMessagingSupportedDirectory(void) {
  76. #if TARGET_OS_TV
  77. return NSCachesDirectory;
  78. #else
  79. return NSApplicationSupportDirectory;
  80. #endif
  81. }