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.

202 lines
7.3 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
  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 "Firebase/Messaging/FIRMessagingUtilities.h"
  17. #import "Firebase/Messaging/Protos/GtalkCore.pbobjc.h"
  18. #import "Firebase/Messaging/FIRMessagingLogger.h"
  19. #import <GoogleUtilities/GULAppEnvironmentUtil.h>
  20. // Convert the macro to a string
  21. #define STR_EXPAND(x) #x
  22. #define STR(x) STR_EXPAND(x)
  23. static const uint64_t kBytesToMegabytesDivisor = 1024 * 1024LL;
  24. #pragma mark - Protocol Buffers
  25. FIRMessagingProtoTag FIRMessagingGetTagForProto(GPBMessage *proto) {
  26. if ([proto isKindOfClass:[GtalkHeartbeatPing class]]) {
  27. return kFIRMessagingProtoTagHeartbeatPing;
  28. } else if ([proto isKindOfClass:[GtalkHeartbeatAck class]]) {
  29. return kFIRMessagingProtoTagHeartbeatAck;
  30. } else if ([proto isKindOfClass:[GtalkLoginRequest class]]) {
  31. return kFIRMessagingProtoTagLoginRequest;
  32. } else if ([proto isKindOfClass:[GtalkLoginResponse class]]) {
  33. return kFIRMessagingProtoTagLoginResponse;
  34. } else if ([proto isKindOfClass:[GtalkClose class]]) {
  35. return kFIRMessagingProtoTagClose;
  36. } else if ([proto isKindOfClass:[GtalkIqStanza class]]) {
  37. return kFIRMessagingProtoTagIqStanza;
  38. } else if ([proto isKindOfClass:[GtalkDataMessageStanza class]]) {
  39. return kFIRMessagingProtoTagDataMessageStanza;
  40. }
  41. return kFIRMessagingProtoTagInvalid;
  42. }
  43. Class FIRMessagingGetClassForTag(FIRMessagingProtoTag tag) {
  44. switch (tag) {
  45. case kFIRMessagingProtoTagHeartbeatPing:
  46. return GtalkHeartbeatPing.class;
  47. case kFIRMessagingProtoTagHeartbeatAck:
  48. return GtalkHeartbeatAck.class;
  49. case kFIRMessagingProtoTagLoginRequest:
  50. return GtalkLoginRequest.class;
  51. case kFIRMessagingProtoTagLoginResponse:
  52. return GtalkLoginResponse.class;
  53. case kFIRMessagingProtoTagClose:
  54. return GtalkClose.class;
  55. case kFIRMessagingProtoTagIqStanza:
  56. return GtalkIqStanza.class;
  57. case kFIRMessagingProtoTagDataMessageStanza:
  58. return GtalkDataMessageStanza.class;
  59. case kFIRMessagingProtoTagInvalid:
  60. return NSNull.class;
  61. }
  62. return NSNull.class;
  63. }
  64. #pragma mark - MCS
  65. NSString *FIRMessagingGetRmq2Id(GPBMessage *proto) {
  66. if ([proto isKindOfClass:[GtalkIqStanza class]]) {
  67. if (((GtalkIqStanza *)proto).hasPersistentId) {
  68. return ((GtalkIqStanza *)proto).persistentId;
  69. }
  70. } else if ([proto isKindOfClass:[GtalkDataMessageStanza class]]) {
  71. if (((GtalkDataMessageStanza *)proto).hasPersistentId) {
  72. return ((GtalkDataMessageStanza *)proto).persistentId;
  73. }
  74. }
  75. return nil;
  76. }
  77. void FIRMessagingSetRmq2Id(GPBMessage *proto, NSString *pID) {
  78. if ([proto isKindOfClass:[GtalkIqStanza class]]) {
  79. ((GtalkIqStanza *)proto).persistentId = pID;
  80. } else if ([proto isKindOfClass:[GtalkDataMessageStanza class]]) {
  81. ((GtalkDataMessageStanza *)proto).persistentId = pID;
  82. }
  83. }
  84. int FIRMessagingGetLastStreamId(GPBMessage *proto) {
  85. if ([proto isKindOfClass:[GtalkIqStanza class]]) {
  86. if (((GtalkIqStanza *)proto).hasLastStreamIdReceived) {
  87. return ((GtalkIqStanza *)proto).lastStreamIdReceived;
  88. }
  89. } else if ([proto isKindOfClass:[GtalkDataMessageStanza class]]) {
  90. if (((GtalkDataMessageStanza *)proto).hasLastStreamIdReceived) {
  91. return ((GtalkDataMessageStanza *)proto).lastStreamIdReceived;
  92. }
  93. } else if ([proto isKindOfClass:[GtalkHeartbeatPing class]]) {
  94. if (((GtalkHeartbeatPing *)proto).hasLastStreamIdReceived) {
  95. return ((GtalkHeartbeatPing *)proto).lastStreamIdReceived;
  96. }
  97. } else if ([proto isKindOfClass:[GtalkHeartbeatAck class]]) {
  98. if (((GtalkHeartbeatAck *)proto).hasLastStreamIdReceived) {
  99. return ((GtalkHeartbeatAck *)proto).lastStreamIdReceived;
  100. }
  101. }
  102. return -1;
  103. }
  104. void FIRMessagingSetLastStreamId(GPBMessage *proto, int sid) {
  105. if ([proto isKindOfClass:[GtalkIqStanza class]]) {
  106. ((GtalkIqStanza *)proto).lastStreamIdReceived = sid;
  107. } else if ([proto isKindOfClass:[GtalkDataMessageStanza class]]) {
  108. ((GtalkDataMessageStanza *)proto).lastStreamIdReceived = sid;
  109. } else if ([proto isKindOfClass:[GtalkHeartbeatPing class]]) {
  110. ((GtalkHeartbeatPing *)proto).lastStreamIdReceived = sid;
  111. } else if ([proto isKindOfClass:[GtalkHeartbeatAck class]]) {
  112. ((GtalkHeartbeatAck *)proto).lastStreamIdReceived = sid;
  113. }
  114. }
  115. #pragma mark - Time
  116. int64_t FIRMessagingCurrentTimestampInSeconds(void) {
  117. return (int64_t)[[NSDate date] timeIntervalSince1970];
  118. }
  119. int64_t FIRMessagingCurrentTimestampInMilliseconds(void) {
  120. return (int64_t)(FIRMessagingCurrentTimestampInSeconds() * 1000.0);
  121. }
  122. #pragma mark - App Info
  123. NSString *FIRMessagingCurrentAppVersion(void) {
  124. NSString *version = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
  125. if (![version length]) {
  126. FIRMessagingLoggerError(kFIRMessagingMessageCodeUtilities000,
  127. @"Could not find current app version");
  128. return @"";
  129. }
  130. return version;
  131. }
  132. NSString * FIRMessagingBundleIDByRemovingLastPartFrom(NSString *bundleID) {
  133. NSString *bundleIDComponentsSeparator = @".";
  134. NSMutableArray<NSString *> *bundleIDComponents =
  135. [[bundleID componentsSeparatedByString:bundleIDComponentsSeparator] mutableCopy];
  136. [bundleIDComponents removeLastObject];
  137. return [bundleIDComponents componentsJoinedByString:bundleIDComponentsSeparator];
  138. }
  139. NSString *FIRMessagingAppIdentifier(void) {
  140. NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
  141. #if TARGET_OS_WATCH
  142. // The code is running in watchKit extension target but the actually bundleID is in the watchKit
  143. // target. So we need to remove the last part of the bundle ID in watchKit extension to match
  144. // the one in watchKit target.
  145. return FIRMessagingBundleIDByRemovingLastPartFrom(bundleID);
  146. #else
  147. return bundleID;
  148. #endif
  149. }
  150. uint64_t FIRMessagingGetFreeDiskSpaceInMB(void) {
  151. NSError *error;
  152. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  153. NSDictionary *attributesMap =
  154. [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject]
  155. error:&error];
  156. if (attributesMap) {
  157. uint64_t totalSizeInBytes __unused = [attributesMap[NSFileSystemSize] longLongValue];
  158. uint64_t freeSizeInBytes = [attributesMap[NSFileSystemFreeSize] longLongValue];
  159. FIRMessagingLoggerDebug(
  160. kFIRMessagingMessageCodeUtilities001, @"Device has capacity %llu MB with %llu MB free.",
  161. totalSizeInBytes / kBytesToMegabytesDivisor, freeSizeInBytes / kBytesToMegabytesDivisor);
  162. return ((double)freeSizeInBytes) / kBytesToMegabytesDivisor;
  163. } else {
  164. FIRMessagingLoggerError(kFIRMessagingMessageCodeUtilities002,
  165. @"Error in retreiving device's free memory %@", error);
  166. return 0;
  167. }
  168. }
  169. NSSearchPathDirectory FIRMessagingSupportedDirectory(void) {
  170. #if TARGET_OS_TV
  171. return NSCachesDirectory;
  172. #else
  173. return NSApplicationSupportDirectory;
  174. #endif
  175. }