FIRMessagingUtilities.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. // Convert the macro to a string
  20. #define STR_EXPAND(x) #x
  21. #define STR(x) STR_EXPAND(x)
  22. static const uint64_t kBytesToMegabytesDivisor = 1024 * 1024LL;
  23. #pragma mark - Time
  24. int64_t FIRMessagingCurrentTimestampInSeconds(void) {
  25. return (int64_t)[[NSDate date] timeIntervalSince1970];
  26. }
  27. int64_t FIRMessagingCurrentTimestampInMilliseconds(void) {
  28. return (int64_t)(FIRMessagingCurrentTimestampInSeconds() * 1000.0);
  29. }
  30. #pragma mark - App Info
  31. NSString *FIRMessagingCurrentAppVersion(void) {
  32. NSString *version = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
  33. if (![version length]) {
  34. FIRMessagingLoggerError(kFIRMessagingMessageCodeUtilities000,
  35. @"Could not find current app version");
  36. return @"";
  37. }
  38. return version;
  39. }
  40. NSString *FIRMessagingBundleIDByRemovingLastPartFrom(NSString *bundleID) {
  41. NSString *bundleIDComponentsSeparator = @".";
  42. NSMutableArray<NSString *> *bundleIDComponents =
  43. [[bundleID componentsSeparatedByString:bundleIDComponentsSeparator] mutableCopy];
  44. [bundleIDComponents removeLastObject];
  45. return [bundleIDComponents componentsJoinedByString:bundleIDComponentsSeparator];
  46. }
  47. NSString *FIRMessagingAppIdentifier(void) {
  48. NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
  49. #if TARGET_OS_WATCH
  50. // The code is running in watchKit extension target but the actually bundleID is in the watchKit
  51. // target. So we need to remove the last part of the bundle ID in watchKit extension to match
  52. // the one in watchKit target.
  53. return FIRMessagingBundleIDByRemovingLastPartFrom(bundleID);
  54. #else
  55. return bundleID;
  56. #endif
  57. }
  58. uint64_t FIRMessagingGetFreeDiskSpaceInMB(void) {
  59. NSError *error;
  60. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  61. NSDictionary *attributesMap =
  62. [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject]
  63. error:&error];
  64. if (attributesMap) {
  65. uint64_t totalSizeInBytes __unused = [attributesMap[NSFileSystemSize] longLongValue];
  66. uint64_t freeSizeInBytes = [attributesMap[NSFileSystemFreeSize] longLongValue];
  67. FIRMessagingLoggerDebug(
  68. kFIRMessagingMessageCodeUtilities001, @"Device has capacity %llu MB with %llu MB free.",
  69. totalSizeInBytes / kBytesToMegabytesDivisor, freeSizeInBytes / kBytesToMegabytesDivisor);
  70. return ((double)freeSizeInBytes) / kBytesToMegabytesDivisor;
  71. } else {
  72. FIRMessagingLoggerError(kFIRMessagingMessageCodeUtilities002,
  73. @"Error in retreiving device's free memory %@", error);
  74. return 0;
  75. }
  76. }
  77. NSSearchPathDirectory FIRMessagingSupportedDirectory(void) {
  78. #if TARGET_OS_TV
  79. return NSCachesDirectory;
  80. #else
  81. return NSApplicationSupportDirectory;
  82. #endif
  83. }